src/HOL/Analysis/Borel_Space.thy
author wenzelm
Tue Jan 17 13:59:10 2017 +0100 (2017-01-17)
changeset 64911 f0e07600de47
parent 64320 ba194424b895
child 66164 2d79288b042c
permissions -rw-r--r--
isabelle update_cartouches -c -t;
     1 (*  Title:      HOL/Analysis/Borel_Space.thy
     2     Author:     Johannes Hölzl, TU München
     3     Author:     Armin Heller, TU München
     4 *)
     5 
     6 section \<open>Borel spaces\<close>
     7 
     8 theory Borel_Space
     9 imports
    10   Measurable Derivative Ordered_Euclidean_Space Extended_Real_Limits
    11 begin
    12 
    13 lemma sets_Collect_eventually_sequentially[measurable]:
    14   "(\<And>i. {x\<in>space M. P x i} \<in> sets M) \<Longrightarrow> {x\<in>space M. eventually (P x) sequentially} \<in> sets M"
    15   unfolding eventually_sequentially by simp
    16 
    17 lemma topological_basis_trivial: "topological_basis {A. open A}"
    18   by (auto simp: topological_basis_def)
    19 
    20 lemma open_prod_generated: "open = generate_topology {A \<times> B | A B. open A \<and> open B}"
    21 proof -
    22   have "{A \<times> B :: ('a \<times> 'b) set | A B. open A \<and> open B} = ((\<lambda>(a, b). a \<times> b) ` ({A. open A} \<times> {A. open A}))"
    23     by auto
    24   then show ?thesis
    25     by (auto intro: topological_basis_prod topological_basis_trivial topological_basis_imp_subbasis)
    26 qed
    27 
    28 definition "mono_on f A \<equiv> \<forall>r s. r \<in> A \<and> s \<in> A \<and> r \<le> s \<longrightarrow> f r \<le> f s"
    29 
    30 lemma mono_onI:
    31   "(\<And>r s. r \<in> A \<Longrightarrow> s \<in> A \<Longrightarrow> r \<le> s \<Longrightarrow> f r \<le> f s) \<Longrightarrow> mono_on f A"
    32   unfolding mono_on_def by simp
    33 
    34 lemma mono_onD:
    35   "\<lbrakk>mono_on f A; r \<in> A; s \<in> A; r \<le> s\<rbrakk> \<Longrightarrow> f r \<le> f s"
    36   unfolding mono_on_def by simp
    37 
    38 lemma mono_imp_mono_on: "mono f \<Longrightarrow> mono_on f A"
    39   unfolding mono_def mono_on_def by auto
    40 
    41 lemma mono_on_subset: "mono_on f A \<Longrightarrow> B \<subseteq> A \<Longrightarrow> mono_on f B"
    42   unfolding mono_on_def by auto
    43 
    44 definition "strict_mono_on f A \<equiv> \<forall>r s. r \<in> A \<and> s \<in> A \<and> r < s \<longrightarrow> f r < f s"
    45 
    46 lemma strict_mono_onI:
    47   "(\<And>r s. r \<in> A \<Longrightarrow> s \<in> A \<Longrightarrow> r < s \<Longrightarrow> f r < f s) \<Longrightarrow> strict_mono_on f A"
    48   unfolding strict_mono_on_def by simp
    49 
    50 lemma strict_mono_onD:
    51   "\<lbrakk>strict_mono_on f A; r \<in> A; s \<in> A; r < s\<rbrakk> \<Longrightarrow> f r < f s"
    52   unfolding strict_mono_on_def by simp
    53 
    54 lemma mono_on_greaterD:
    55   assumes "mono_on g A" "x \<in> A" "y \<in> A" "g x > (g (y::_::linorder) :: _ :: linorder)"
    56   shows "x > y"
    57 proof (rule ccontr)
    58   assume "\<not>x > y"
    59   hence "x \<le> y" by (simp add: not_less)
    60   from assms(1-3) and this have "g x \<le> g y" by (rule mono_onD)
    61   with assms(4) show False by simp
    62 qed
    63 
    64 lemma strict_mono_inv:
    65   fixes f :: "('a::linorder) \<Rightarrow> ('b::linorder)"
    66   assumes "strict_mono f" and "surj f" and inv: "\<And>x. g (f x) = x"
    67   shows "strict_mono g"
    68 proof
    69   fix x y :: 'b assume "x < y"
    70   from \<open>surj f\<close> obtain x' y' where [simp]: "x = f x'" "y = f y'" by blast
    71   with \<open>x < y\<close> and \<open>strict_mono f\<close> have "x' < y'" by (simp add: strict_mono_less)
    72   with inv show "g x < g y" by simp
    73 qed
    74 
    75 lemma strict_mono_on_imp_inj_on:
    76   assumes "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> (_ :: preorder)) A"
    77   shows "inj_on f A"
    78 proof (rule inj_onI)
    79   fix x y assume "x \<in> A" "y \<in> A" "f x = f y"
    80   thus "x = y"
    81     by (cases x y rule: linorder_cases)
    82        (auto dest: strict_mono_onD[OF assms, of x y] strict_mono_onD[OF assms, of y x])
    83 qed
    84 
    85 lemma strict_mono_on_leD:
    86   assumes "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> _ :: preorder) A" "x \<in> A" "y \<in> A" "x \<le> y"
    87   shows "f x \<le> f y"
    88 proof (insert le_less_linear[of y x], elim disjE)
    89   assume "x < y"
    90   with assms have "f x < f y" by (rule_tac strict_mono_onD[OF assms(1)]) simp_all
    91   thus ?thesis by (rule less_imp_le)
    92 qed (insert assms, simp)
    93 
    94 lemma strict_mono_on_eqD:
    95   fixes f :: "(_ :: linorder) \<Rightarrow> (_ :: preorder)"
    96   assumes "strict_mono_on f A" "f x = f y" "x \<in> A" "y \<in> A"
    97   shows "y = x"
    98   using assms by (rule_tac linorder_cases[of x y]) (auto dest: strict_mono_onD)
    99 
   100 lemma mono_on_imp_deriv_nonneg:
   101   assumes mono: "mono_on f A" and deriv: "(f has_real_derivative D) (at x)"
   102   assumes "x \<in> interior A"
   103   shows "D \<ge> 0"
   104 proof (rule tendsto_lowerbound)
   105   let ?A' = "(\<lambda>y. y - x) ` interior A"
   106   from deriv show "((\<lambda>h. (f (x + h) - f x) / h) \<longlongrightarrow> D) (at 0)"
   107       by (simp add: field_has_derivative_at has_field_derivative_def)
   108   from mono have mono': "mono_on f (interior A)" by (rule mono_on_subset) (rule interior_subset)
   109 
   110   show "eventually (\<lambda>h. (f (x + h) - f x) / h \<ge> 0) (at 0)"
   111   proof (subst eventually_at_topological, intro exI conjI ballI impI)
   112     have "open (interior A)" by simp
   113     hence "open (op + (-x) ` interior A)" by (rule open_translation)
   114     also have "(op + (-x) ` interior A) = ?A'" by auto
   115     finally show "open ?A'" .
   116   next
   117     from \<open>x \<in> interior A\<close> show "0 \<in> ?A'" by auto
   118   next
   119     fix h assume "h \<in> ?A'"
   120     hence "x + h \<in> interior A" by auto
   121     with mono' and \<open>x \<in> interior A\<close> show "(f (x + h) - f x) / h \<ge> 0"
   122       by (cases h rule: linorder_cases[of _ 0])
   123          (simp_all add: divide_nonpos_neg divide_nonneg_pos mono_onD field_simps)
   124   qed
   125 qed simp
   126 
   127 lemma strict_mono_on_imp_mono_on:
   128   "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> _ :: preorder) A \<Longrightarrow> mono_on f A"
   129   by (rule mono_onI, rule strict_mono_on_leD)
   130 
   131 lemma mono_on_ctble_discont:
   132   fixes f :: "real \<Rightarrow> real"
   133   fixes A :: "real set"
   134   assumes "mono_on f A"
   135   shows "countable {a\<in>A. \<not> continuous (at a within A) f}"
   136 proof -
   137   have mono: "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
   138     using \<open>mono_on f A\<close> by (simp add: mono_on_def)
   139   have "\<forall>a \<in> {a\<in>A. \<not> continuous (at a within A) f}. \<exists>q :: nat \<times> rat.
   140       (fst q = 0 \<and> of_rat (snd q) < f a \<and> (\<forall>x \<in> A. x < a \<longrightarrow> f x < of_rat (snd q))) \<or>
   141       (fst q = 1 \<and> of_rat (snd q) > f a \<and> (\<forall>x \<in> A. x > a \<longrightarrow> f x > of_rat (snd q)))"
   142   proof (clarsimp simp del: One_nat_def)
   143     fix a assume "a \<in> A" assume "\<not> continuous (at a within A) f"
   144     thus "\<exists>q1 q2.
   145             q1 = 0 \<and> real_of_rat q2 < f a \<and> (\<forall>x\<in>A. x < a \<longrightarrow> f x < real_of_rat q2) \<or>
   146             q1 = 1 \<and> f a < real_of_rat q2 \<and> (\<forall>x\<in>A. a < x \<longrightarrow> real_of_rat q2 < f x)"
   147     proof (auto simp add: continuous_within order_tendsto_iff eventually_at)
   148       fix l assume "l < f a"
   149       then obtain q2 where q2: "l < of_rat q2" "of_rat q2 < f a"
   150         using of_rat_dense by blast
   151       assume * [rule_format]: "\<forall>d>0. \<exists>x\<in>A. x \<noteq> a \<and> dist x a < d \<and> \<not> l < f x"
   152       from q2 have "real_of_rat q2 < f a \<and> (\<forall>x\<in>A. x < a \<longrightarrow> f x < real_of_rat q2)"
   153       proof auto
   154         fix x assume "x \<in> A" "x < a"
   155         with q2 *[of "a - x"] show "f x < real_of_rat q2"
   156           apply (auto simp add: dist_real_def not_less)
   157           apply (subgoal_tac "f x \<le> f xa")
   158           by (auto intro: mono)
   159       qed
   160       thus ?thesis by auto
   161     next
   162       fix u assume "u > f a"
   163       then obtain q2 where q2: "f a < of_rat q2" "of_rat q2 < u"
   164         using of_rat_dense by blast
   165       assume *[rule_format]: "\<forall>d>0. \<exists>x\<in>A. x \<noteq> a \<and> dist x a < d \<and> \<not> u > f x"
   166       from q2 have "real_of_rat q2 > f a \<and> (\<forall>x\<in>A. x > a \<longrightarrow> f x > real_of_rat q2)"
   167       proof auto
   168         fix x assume "x \<in> A" "x > a"
   169         with q2 *[of "x - a"] show "f x > real_of_rat q2"
   170           apply (auto simp add: dist_real_def)
   171           apply (subgoal_tac "f x \<ge> f xa")
   172           by (auto intro: mono)
   173       qed
   174       thus ?thesis by auto
   175     qed
   176   qed
   177   hence "\<exists>g :: real \<Rightarrow> nat \<times> rat . \<forall>a \<in> {a\<in>A. \<not> continuous (at a within A) f}.
   178       (fst (g a) = 0 \<and> of_rat (snd (g a)) < f a \<and> (\<forall>x \<in> A. x < a \<longrightarrow> f x < of_rat (snd (g a)))) |
   179       (fst (g a) = 1 \<and> of_rat (snd (g a)) > f a \<and> (\<forall>x \<in> A. x > a \<longrightarrow> f x > of_rat (snd (g a))))"
   180     by (rule bchoice)
   181   then guess g ..
   182   hence g: "\<And>a x. a \<in> A \<Longrightarrow> \<not> continuous (at a within A) f \<Longrightarrow> x \<in> A \<Longrightarrow>
   183       (fst (g a) = 0 \<and> of_rat (snd (g a)) < f a \<and> (x < a \<longrightarrow> f x < of_rat (snd (g a)))) |
   184       (fst (g a) = 1 \<and> of_rat (snd (g a)) > f a \<and> (x > a \<longrightarrow> f x > of_rat (snd (g a))))"
   185     by auto
   186   have "inj_on g {a\<in>A. \<not> continuous (at a within A) f}"
   187   proof (auto simp add: inj_on_def)
   188     fix w z
   189     assume 1: "w \<in> A" and 2: "\<not> continuous (at w within A) f" and
   190            3: "z \<in> A" and 4: "\<not> continuous (at z within A) f" and
   191            5: "g w = g z"
   192     from g [OF 1 2 3] g [OF 3 4 1] 5
   193     show "w = z" by auto
   194   qed
   195   thus ?thesis
   196     by (rule countableI')
   197 qed
   198 
   199 lemma mono_on_ctble_discont_open:
   200   fixes f :: "real \<Rightarrow> real"
   201   fixes A :: "real set"
   202   assumes "open A" "mono_on f A"
   203   shows "countable {a\<in>A. \<not>isCont f a}"
   204 proof -
   205   have "{a\<in>A. \<not>isCont f a} = {a\<in>A. \<not>(continuous (at a within A) f)}"
   206     by (auto simp add: continuous_within_open [OF _ \<open>open A\<close>])
   207   thus ?thesis
   208     apply (elim ssubst)
   209     by (rule mono_on_ctble_discont, rule assms)
   210 qed
   211 
   212 lemma mono_ctble_discont:
   213   fixes f :: "real \<Rightarrow> real"
   214   assumes "mono f"
   215   shows "countable {a. \<not> isCont f a}"
   216 using assms mono_on_ctble_discont [of f UNIV] unfolding mono_on_def mono_def by auto
   217 
   218 lemma has_real_derivative_imp_continuous_on:
   219   assumes "\<And>x. x \<in> A \<Longrightarrow> (f has_real_derivative f' x) (at x)"
   220   shows "continuous_on A f"
   221   apply (intro differentiable_imp_continuous_on, unfold differentiable_on_def)
   222   apply (intro ballI Deriv.differentiableI)
   223   apply (rule has_field_derivative_subset[OF assms])
   224   apply simp_all
   225   done
   226 
   227 lemma closure_contains_Sup:
   228   fixes S :: "real set"
   229   assumes "S \<noteq> {}" "bdd_above S"
   230   shows "Sup S \<in> closure S"
   231 proof-
   232   have "Inf (uminus ` S) \<in> closure (uminus ` S)"
   233       using assms by (intro closure_contains_Inf) auto
   234   also have "Inf (uminus ` S) = -Sup S" by (simp add: Inf_real_def)
   235   also have "closure (uminus ` S) = uminus ` closure S"
   236       by (rule sym, intro closure_injective_linear_image) (auto intro: linearI)
   237   finally show ?thesis by auto
   238 qed
   239 
   240 lemma closed_contains_Sup:
   241   fixes S :: "real set"
   242   shows "S \<noteq> {} \<Longrightarrow> bdd_above S \<Longrightarrow> closed S \<Longrightarrow> Sup S \<in> S"
   243   by (subst closure_closed[symmetric], assumption, rule closure_contains_Sup)
   244 
   245 lemma deriv_nonneg_imp_mono:
   246   assumes deriv: "\<And>x. x \<in> {a..b} \<Longrightarrow> (g has_real_derivative g' x) (at x)"
   247   assumes nonneg: "\<And>x. x \<in> {a..b} \<Longrightarrow> g' x \<ge> 0"
   248   assumes ab: "a \<le> b"
   249   shows "g a \<le> g b"
   250 proof (cases "a < b")
   251   assume "a < b"
   252   from deriv have "\<forall>x. x \<ge> a \<and> x \<le> b \<longrightarrow> (g has_real_derivative g' x) (at x)" by simp
   253   from MVT2[OF \<open>a < b\<close> this] and deriv
   254     obtain \<xi> where \<xi>_ab: "\<xi> > a" "\<xi> < b" and g_ab: "g b - g a = (b - a) * g' \<xi>" by blast
   255   from \<xi>_ab ab nonneg have "(b - a) * g' \<xi> \<ge> 0" by simp
   256   with g_ab show ?thesis by simp
   257 qed (insert ab, simp)
   258 
   259 lemma continuous_interval_vimage_Int:
   260   assumes "continuous_on {a::real..b} g" and mono: "\<And>x y. a \<le> x \<Longrightarrow> x \<le> y \<Longrightarrow> y \<le> b \<Longrightarrow> g x \<le> g y"
   261   assumes "a \<le> b" "(c::real) \<le> d" "{c..d} \<subseteq> {g a..g b}"
   262   obtains c' d' where "{a..b} \<inter> g -` {c..d} = {c'..d'}" "c' \<le> d'" "g c' = c" "g d' = d"
   263 proof-
   264   let ?A = "{a..b} \<inter> g -` {c..d}"
   265   from IVT'[of g a c b, OF _ _ \<open>a \<le> b\<close> assms(1)] assms(4,5)
   266   obtain c'' where c'': "c'' \<in> ?A" "g c'' = c" by auto
   267   from IVT'[of g a d b, OF _ _ \<open>a \<le> b\<close> assms(1)] assms(4,5)
   268   obtain d'' where d'': "d'' \<in> ?A" "g d'' = d" by auto
   269   hence [simp]: "?A \<noteq> {}" by blast
   270 
   271   define c' where "c' = Inf ?A"
   272   define d' where "d' = Sup ?A"
   273   have "?A \<subseteq> {c'..d'}" unfolding c'_def d'_def
   274     by (intro subsetI) (auto intro: cInf_lower cSup_upper)
   275   moreover from assms have "closed ?A"
   276     using continuous_on_closed_vimage[of "{a..b}" g] by (subst Int_commute) simp
   277   hence c'd'_in_set: "c' \<in> ?A" "d' \<in> ?A" unfolding c'_def d'_def
   278     by ((intro closed_contains_Inf closed_contains_Sup, simp_all)[])+
   279   hence "{c'..d'} \<subseteq> ?A" using assms
   280     by (intro subsetI)
   281        (auto intro!: order_trans[of c "g c'" "g x" for x] order_trans[of "g x" "g d'" d for x]
   282              intro!: mono)
   283   moreover have "c' \<le> d'" using c'd'_in_set(2) unfolding c'_def by (intro cInf_lower) auto
   284   moreover have "g c' \<le> c" "g d' \<ge> d"
   285     apply (insert c'' d'' c'd'_in_set)
   286     apply (subst c''(2)[symmetric])
   287     apply (auto simp: c'_def intro!: mono cInf_lower c'') []
   288     apply (subst d''(2)[symmetric])
   289     apply (auto simp: d'_def intro!: mono cSup_upper d'') []
   290     done
   291   with c'd'_in_set have "g c' = c" "g d' = d" by auto
   292   ultimately show ?thesis using that by blast
   293 qed
   294 
   295 subsection \<open>Generic Borel spaces\<close>
   296 
   297 definition (in topological_space) borel :: "'a measure" where
   298   "borel = sigma UNIV {S. open S}"
   299 
   300 abbreviation "borel_measurable M \<equiv> measurable M borel"
   301 
   302 lemma in_borel_measurable:
   303    "f \<in> borel_measurable M \<longleftrightarrow>
   304     (\<forall>S \<in> sigma_sets UNIV {S. open S}. f -` S \<inter> space M \<in> sets M)"
   305   by (auto simp add: measurable_def borel_def)
   306 
   307 lemma in_borel_measurable_borel:
   308    "f \<in> borel_measurable M \<longleftrightarrow>
   309     (\<forall>S \<in> sets borel.
   310       f -` S \<inter> space M \<in> sets M)"
   311   by (auto simp add: measurable_def borel_def)
   312 
   313 lemma space_borel[simp]: "space borel = UNIV"
   314   unfolding borel_def by auto
   315 
   316 lemma space_in_borel[measurable]: "UNIV \<in> sets borel"
   317   unfolding borel_def by auto
   318 
   319 lemma sets_borel: "sets borel = sigma_sets UNIV {S. open S}"
   320   unfolding borel_def by (rule sets_measure_of) simp
   321 
   322 lemma measurable_sets_borel:
   323     "\<lbrakk>f \<in> measurable borel M; A \<in> sets M\<rbrakk> \<Longrightarrow> f -` A \<in> sets borel"
   324   by (drule (1) measurable_sets) simp
   325 
   326 lemma pred_Collect_borel[measurable (raw)]: "Measurable.pred borel P \<Longrightarrow> {x. P x} \<in> sets borel"
   327   unfolding borel_def pred_def by auto
   328 
   329 lemma borel_open[measurable (raw generic)]:
   330   assumes "open A" shows "A \<in> sets borel"
   331 proof -
   332   have "A \<in> {S. open S}" unfolding mem_Collect_eq using assms .
   333   thus ?thesis unfolding borel_def by auto
   334 qed
   335 
   336 lemma borel_closed[measurable (raw generic)]:
   337   assumes "closed A" shows "A \<in> sets borel"
   338 proof -
   339   have "space borel - (- A) \<in> sets borel"
   340     using assms unfolding closed_def by (blast intro: borel_open)
   341   thus ?thesis by simp
   342 qed
   343 
   344 lemma borel_singleton[measurable]:
   345   "A \<in> sets borel \<Longrightarrow> insert x A \<in> sets (borel :: 'a::t1_space measure)"
   346   unfolding insert_def by (rule sets.Un) auto
   347 
   348 lemma sets_borel_eq_count_space: "sets (borel :: 'a::{countable, t2_space} measure) = count_space UNIV"
   349 proof -
   350   have "(\<Union>a\<in>A. {a}) \<in> sets borel" for A :: "'a set"
   351     by (intro sets.countable_UN') auto
   352   then show ?thesis
   353     by auto
   354 qed
   355 
   356 lemma borel_comp[measurable]: "A \<in> sets borel \<Longrightarrow> - A \<in> sets borel"
   357   unfolding Compl_eq_Diff_UNIV by simp
   358 
   359 lemma borel_measurable_vimage:
   360   fixes f :: "'a \<Rightarrow> 'x::t2_space"
   361   assumes borel[measurable]: "f \<in> borel_measurable M"
   362   shows "f -` {x} \<inter> space M \<in> sets M"
   363   by simp
   364 
   365 lemma borel_measurableI:
   366   fixes f :: "'a \<Rightarrow> 'x::topological_space"
   367   assumes "\<And>S. open S \<Longrightarrow> f -` S \<inter> space M \<in> sets M"
   368   shows "f \<in> borel_measurable M"
   369   unfolding borel_def
   370 proof (rule measurable_measure_of, simp_all)
   371   fix S :: "'x set" assume "open S" thus "f -` S \<inter> space M \<in> sets M"
   372     using assms[of S] by simp
   373 qed
   374 
   375 lemma borel_measurable_const:
   376   "(\<lambda>x. c) \<in> borel_measurable M"
   377   by auto
   378 
   379 lemma borel_measurable_indicator:
   380   assumes A: "A \<in> sets M"
   381   shows "indicator A \<in> borel_measurable M"
   382   unfolding indicator_def [abs_def] using A
   383   by (auto intro!: measurable_If_set)
   384 
   385 lemma borel_measurable_count_space[measurable (raw)]:
   386   "f \<in> borel_measurable (count_space S)"
   387   unfolding measurable_def by auto
   388 
   389 lemma borel_measurable_indicator'[measurable (raw)]:
   390   assumes [measurable]: "{x\<in>space M. f x \<in> A x} \<in> sets M"
   391   shows "(\<lambda>x. indicator (A x) (f x)) \<in> borel_measurable M"
   392   unfolding indicator_def[abs_def]
   393   by (auto intro!: measurable_If)
   394 
   395 lemma borel_measurable_indicator_iff:
   396   "(indicator A :: 'a \<Rightarrow> 'x::{t1_space, zero_neq_one}) \<in> borel_measurable M \<longleftrightarrow> A \<inter> space M \<in> sets M"
   397     (is "?I \<in> borel_measurable M \<longleftrightarrow> _")
   398 proof
   399   assume "?I \<in> borel_measurable M"
   400   then have "?I -` {1} \<inter> space M \<in> sets M"
   401     unfolding measurable_def by auto
   402   also have "?I -` {1} \<inter> space M = A \<inter> space M"
   403     unfolding indicator_def [abs_def] by auto
   404   finally show "A \<inter> space M \<in> sets M" .
   405 next
   406   assume "A \<inter> space M \<in> sets M"
   407   moreover have "?I \<in> borel_measurable M \<longleftrightarrow>
   408     (indicator (A \<inter> space M) :: 'a \<Rightarrow> 'x) \<in> borel_measurable M"
   409     by (intro measurable_cong) (auto simp: indicator_def)
   410   ultimately show "?I \<in> borel_measurable M" by auto
   411 qed
   412 
   413 lemma borel_measurable_subalgebra:
   414   assumes "sets N \<subseteq> sets M" "space N = space M" "f \<in> borel_measurable N"
   415   shows "f \<in> borel_measurable M"
   416   using assms unfolding measurable_def by auto
   417 
   418 lemma borel_measurable_restrict_space_iff_ereal:
   419   fixes f :: "'a \<Rightarrow> ereal"
   420   assumes \<Omega>[measurable, simp]: "\<Omega> \<inter> space M \<in> sets M"
   421   shows "f \<in> borel_measurable (restrict_space M \<Omega>) \<longleftrightarrow>
   422     (\<lambda>x. f x * indicator \<Omega> x) \<in> borel_measurable M"
   423   by (subst measurable_restrict_space_iff)
   424      (auto simp: indicator_def if_distrib[where f="\<lambda>x. a * x" for a] cong del: if_weak_cong)
   425 
   426 lemma borel_measurable_restrict_space_iff_ennreal:
   427   fixes f :: "'a \<Rightarrow> ennreal"
   428   assumes \<Omega>[measurable, simp]: "\<Omega> \<inter> space M \<in> sets M"
   429   shows "f \<in> borel_measurable (restrict_space M \<Omega>) \<longleftrightarrow>
   430     (\<lambda>x. f x * indicator \<Omega> x) \<in> borel_measurable M"
   431   by (subst measurable_restrict_space_iff)
   432      (auto simp: indicator_def if_distrib[where f="\<lambda>x. a * x" for a] cong del: if_weak_cong)
   433 
   434 lemma borel_measurable_restrict_space_iff:
   435   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   436   assumes \<Omega>[measurable, simp]: "\<Omega> \<inter> space M \<in> sets M"
   437   shows "f \<in> borel_measurable (restrict_space M \<Omega>) \<longleftrightarrow>
   438     (\<lambda>x. indicator \<Omega> x *\<^sub>R f x) \<in> borel_measurable M"
   439   by (subst measurable_restrict_space_iff)
   440      (auto simp: indicator_def if_distrib[where f="\<lambda>x. x *\<^sub>R a" for a] ac_simps
   441        cong del: if_weak_cong)
   442 
   443 lemma cbox_borel[measurable]: "cbox a b \<in> sets borel"
   444   by (auto intro: borel_closed)
   445 
   446 lemma box_borel[measurable]: "box a b \<in> sets borel"
   447   by (auto intro: borel_open)
   448 
   449 lemma borel_compact: "compact (A::'a::t2_space set) \<Longrightarrow> A \<in> sets borel"
   450   by (auto intro: borel_closed dest!: compact_imp_closed)
   451 
   452 lemma borel_sigma_sets_subset:
   453   "A \<subseteq> sets borel \<Longrightarrow> sigma_sets UNIV A \<subseteq> sets borel"
   454   using sets.sigma_sets_subset[of A borel] by simp
   455 
   456 lemma borel_eq_sigmaI1:
   457   fixes F :: "'i \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   458   assumes borel_eq: "borel = sigma UNIV X"
   459   assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV (F ` A))"
   460   assumes F: "\<And>i. i \<in> A \<Longrightarrow> F i \<in> sets borel"
   461   shows "borel = sigma UNIV (F ` A)"
   462   unfolding borel_def
   463 proof (intro sigma_eqI antisym)
   464   have borel_rev_eq: "sigma_sets UNIV {S::'a set. open S} = sets borel"
   465     unfolding borel_def by simp
   466   also have "\<dots> = sigma_sets UNIV X"
   467     unfolding borel_eq by simp
   468   also have "\<dots> \<subseteq> sigma_sets UNIV (F`A)"
   469     using X by (intro sigma_algebra.sigma_sets_subset[OF sigma_algebra_sigma_sets]) auto
   470   finally show "sigma_sets UNIV {S. open S} \<subseteq> sigma_sets UNIV (F`A)" .
   471   show "sigma_sets UNIV (F`A) \<subseteq> sigma_sets UNIV {S. open S}"
   472     unfolding borel_rev_eq using F by (intro borel_sigma_sets_subset) auto
   473 qed auto
   474 
   475 lemma borel_eq_sigmaI2:
   476   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set"
   477     and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   478   assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`B)"
   479   assumes X: "\<And>i j. (i, j) \<in> B \<Longrightarrow> G i j \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
   480   assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
   481   shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
   482   using assms
   483   by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` B" and F="(\<lambda>(i, j). F i j)"]) auto
   484 
   485 lemma borel_eq_sigmaI3:
   486   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   487   assumes borel_eq: "borel = sigma UNIV X"
   488   assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
   489   assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
   490   shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
   491   using assms by (intro borel_eq_sigmaI1[where X=X and F="(\<lambda>(i, j). F i j)"]) auto
   492 
   493 lemma borel_eq_sigmaI4:
   494   fixes F :: "'i \<Rightarrow> 'a::topological_space set"
   495     and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   496   assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`A)"
   497   assumes X: "\<And>i j. (i, j) \<in> A \<Longrightarrow> G i j \<in> sets (sigma UNIV (range F))"
   498   assumes F: "\<And>i. F i \<in> sets borel"
   499   shows "borel = sigma UNIV (range F)"
   500   using assms by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` A" and F=F]) auto
   501 
   502 lemma borel_eq_sigmaI5:
   503   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and G :: "'l \<Rightarrow> 'a::topological_space set"
   504   assumes borel_eq: "borel = sigma UNIV (range G)"
   505   assumes X: "\<And>i. G i \<in> sets (sigma UNIV (range (\<lambda>(i, j). F i j)))"
   506   assumes F: "\<And>i j. F i j \<in> sets borel"
   507   shows "borel = sigma UNIV (range (\<lambda>(i, j). F i j))"
   508   using assms by (intro borel_eq_sigmaI1[where X="range G" and F="(\<lambda>(i, j). F i j)"]) auto
   509 
   510 lemma second_countable_borel_measurable:
   511   fixes X :: "'a::second_countable_topology set set"
   512   assumes eq: "open = generate_topology X"
   513   shows "borel = sigma UNIV X"
   514   unfolding borel_def
   515 proof (intro sigma_eqI sigma_sets_eqI)
   516   interpret X: sigma_algebra UNIV "sigma_sets UNIV X"
   517     by (rule sigma_algebra_sigma_sets) simp
   518 
   519   fix S :: "'a set" assume "S \<in> Collect open"
   520   then have "generate_topology X S"
   521     by (auto simp: eq)
   522   then show "S \<in> sigma_sets UNIV X"
   523   proof induction
   524     case (UN K)
   525     then have K: "\<And>k. k \<in> K \<Longrightarrow> open k"
   526       unfolding eq by auto
   527     from ex_countable_basis obtain B :: "'a set set" where
   528       B:  "\<And>b. b \<in> B \<Longrightarrow> open b" "\<And>X. open X \<Longrightarrow> \<exists>b\<subseteq>B. (\<Union>b) = X" and "countable B"
   529       by (auto simp: topological_basis_def)
   530     from B(2)[OF K] obtain m where m: "\<And>k. k \<in> K \<Longrightarrow> m k \<subseteq> B" "\<And>k. k \<in> K \<Longrightarrow> (\<Union>m k) = k"
   531       by metis
   532     define U where "U = (\<Union>k\<in>K. m k)"
   533     with m have "countable U"
   534       by (intro countable_subset[OF _ \<open>countable B\<close>]) auto
   535     have "\<Union>U = (\<Union>A\<in>U. A)" by simp
   536     also have "\<dots> = \<Union>K"
   537       unfolding U_def UN_simps by (simp add: m)
   538     finally have "\<Union>U = \<Union>K" .
   539 
   540     have "\<forall>b\<in>U. \<exists>k\<in>K. b \<subseteq> k"
   541       using m by (auto simp: U_def)
   542     then obtain u where u: "\<And>b. b \<in> U \<Longrightarrow> u b \<in> K" and "\<And>b. b \<in> U \<Longrightarrow> b \<subseteq> u b"
   543       by metis
   544     then have "(\<Union>b\<in>U. u b) \<subseteq> \<Union>K" "\<Union>U \<subseteq> (\<Union>b\<in>U. u b)"
   545       by auto
   546     then have "\<Union>K = (\<Union>b\<in>U. u b)"
   547       unfolding \<open>\<Union>U = \<Union>K\<close> by auto
   548     also have "\<dots> \<in> sigma_sets UNIV X"
   549       using u UN by (intro X.countable_UN' \<open>countable U\<close>) auto
   550     finally show "\<Union>K \<in> sigma_sets UNIV X" .
   551   qed auto
   552 qed (auto simp: eq intro: generate_topology.Basis)
   553 
   554 lemma borel_eq_closed: "borel = sigma UNIV (Collect closed)"
   555   unfolding borel_def
   556 proof (intro sigma_eqI sigma_sets_eqI, safe)
   557   fix x :: "'a set" assume "open x"
   558   hence "x = UNIV - (UNIV - x)" by auto
   559   also have "\<dots> \<in> sigma_sets UNIV (Collect closed)"
   560     by (force intro: sigma_sets.Compl simp: \<open>open x\<close>)
   561   finally show "x \<in> sigma_sets UNIV (Collect closed)" by simp
   562 next
   563   fix x :: "'a set" assume "closed x"
   564   hence "x = UNIV - (UNIV - x)" by auto
   565   also have "\<dots> \<in> sigma_sets UNIV (Collect open)"
   566     by (force intro: sigma_sets.Compl simp: \<open>closed x\<close>)
   567   finally show "x \<in> sigma_sets UNIV (Collect open)" by simp
   568 qed simp_all
   569 
   570 lemma borel_eq_countable_basis:
   571   fixes B::"'a::topological_space set set"
   572   assumes "countable B"
   573   assumes "topological_basis B"
   574   shows "borel = sigma UNIV B"
   575   unfolding borel_def
   576 proof (intro sigma_eqI sigma_sets_eqI, safe)
   577   interpret countable_basis using assms by unfold_locales
   578   fix X::"'a set" assume "open X"
   579   from open_countable_basisE[OF this] guess B' . note B' = this
   580   then show "X \<in> sigma_sets UNIV B"
   581     by (blast intro: sigma_sets_UNION \<open>countable B\<close> countable_subset)
   582 next
   583   fix b assume "b \<in> B"
   584   hence "open b" by (rule topological_basis_open[OF assms(2)])
   585   thus "b \<in> sigma_sets UNIV (Collect open)" by auto
   586 qed simp_all
   587 
   588 lemma borel_measurable_continuous_on_restrict:
   589   fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
   590   assumes f: "continuous_on A f"
   591   shows "f \<in> borel_measurable (restrict_space borel A)"
   592 proof (rule borel_measurableI)
   593   fix S :: "'b set" assume "open S"
   594   with f obtain T where "f -` S \<inter> A = T \<inter> A" "open T"
   595     by (metis continuous_on_open_invariant)
   596   then show "f -` S \<inter> space (restrict_space borel A) \<in> sets (restrict_space borel A)"
   597     by (force simp add: sets_restrict_space space_restrict_space)
   598 qed
   599 
   600 lemma borel_measurable_continuous_on1: "continuous_on UNIV f \<Longrightarrow> f \<in> borel_measurable borel"
   601   by (drule borel_measurable_continuous_on_restrict) simp
   602 
   603 lemma borel_measurable_continuous_on_if:
   604   "A \<in> sets borel \<Longrightarrow> continuous_on A f \<Longrightarrow> continuous_on (- A) g \<Longrightarrow>
   605     (\<lambda>x. if x \<in> A then f x else g x) \<in> borel_measurable borel"
   606   by (auto simp add: measurable_If_restrict_space_iff Collect_neg_eq
   607            intro!: borel_measurable_continuous_on_restrict)
   608 
   609 lemma borel_measurable_continuous_countable_exceptions:
   610   fixes f :: "'a::t1_space \<Rightarrow> 'b::topological_space"
   611   assumes X: "countable X"
   612   assumes "continuous_on (- X) f"
   613   shows "f \<in> borel_measurable borel"
   614 proof (rule measurable_discrete_difference[OF _ X])
   615   have "X \<in> sets borel"
   616     by (rule sets.countable[OF _ X]) auto
   617   then show "(\<lambda>x. if x \<in> X then undefined else f x) \<in> borel_measurable borel"
   618     by (intro borel_measurable_continuous_on_if assms continuous_intros)
   619 qed auto
   620 
   621 lemma borel_measurable_continuous_on:
   622   assumes f: "continuous_on UNIV f" and g: "g \<in> borel_measurable M"
   623   shows "(\<lambda>x. f (g x)) \<in> borel_measurable M"
   624   using measurable_comp[OF g borel_measurable_continuous_on1[OF f]] by (simp add: comp_def)
   625 
   626 lemma borel_measurable_continuous_on_indicator:
   627   fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
   628   shows "A \<in> sets borel \<Longrightarrow> continuous_on A f \<Longrightarrow> (\<lambda>x. indicator A x *\<^sub>R f x) \<in> borel_measurable borel"
   629   by (subst borel_measurable_restrict_space_iff[symmetric])
   630      (auto intro: borel_measurable_continuous_on_restrict)
   631 
   632 lemma borel_measurable_Pair[measurable (raw)]:
   633   fixes f :: "'a \<Rightarrow> 'b::second_countable_topology" and g :: "'a \<Rightarrow> 'c::second_countable_topology"
   634   assumes f[measurable]: "f \<in> borel_measurable M"
   635   assumes g[measurable]: "g \<in> borel_measurable M"
   636   shows "(\<lambda>x. (f x, g x)) \<in> borel_measurable M"
   637 proof (subst borel_eq_countable_basis)
   638   let ?B = "SOME B::'b set set. countable B \<and> topological_basis B"
   639   let ?C = "SOME B::'c set set. countable B \<and> topological_basis B"
   640   let ?P = "(\<lambda>(b, c). b \<times> c) ` (?B \<times> ?C)"
   641   show "countable ?P" "topological_basis ?P"
   642     by (auto intro!: countable_basis topological_basis_prod is_basis)
   643 
   644   show "(\<lambda>x. (f x, g x)) \<in> measurable M (sigma UNIV ?P)"
   645   proof (rule measurable_measure_of)
   646     fix S assume "S \<in> ?P"
   647     then obtain b c where "b \<in> ?B" "c \<in> ?C" and S: "S = b \<times> c" by auto
   648     then have borel: "open b" "open c"
   649       by (auto intro: is_basis topological_basis_open)
   650     have "(\<lambda>x. (f x, g x)) -` S \<inter> space M = (f -` b \<inter> space M) \<inter> (g -` c \<inter> space M)"
   651       unfolding S by auto
   652     also have "\<dots> \<in> sets M"
   653       using borel by simp
   654     finally show "(\<lambda>x. (f x, g x)) -` S \<inter> space M \<in> sets M" .
   655   qed auto
   656 qed
   657 
   658 lemma borel_measurable_continuous_Pair:
   659   fixes f :: "'a \<Rightarrow> 'b::second_countable_topology" and g :: "'a \<Rightarrow> 'c::second_countable_topology"
   660   assumes [measurable]: "f \<in> borel_measurable M"
   661   assumes [measurable]: "g \<in> borel_measurable M"
   662   assumes H: "continuous_on UNIV (\<lambda>x. H (fst x) (snd x))"
   663   shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
   664 proof -
   665   have eq: "(\<lambda>x. H (f x) (g x)) = (\<lambda>x. (\<lambda>x. H (fst x) (snd x)) (f x, g x))" by auto
   666   show ?thesis
   667     unfolding eq by (rule borel_measurable_continuous_on[OF H]) auto
   668 qed
   669 
   670 subsection \<open>Borel spaces on order topologies\<close>
   671 
   672 lemma [measurable]:
   673   fixes a b :: "'a::linorder_topology"
   674   shows lessThan_borel: "{..< a} \<in> sets borel"
   675     and greaterThan_borel: "{a <..} \<in> sets borel"
   676     and greaterThanLessThan_borel: "{a<..<b} \<in> sets borel"
   677     and atMost_borel: "{..a} \<in> sets borel"
   678     and atLeast_borel: "{a..} \<in> sets borel"
   679     and atLeastAtMost_borel: "{a..b} \<in> sets borel"
   680     and greaterThanAtMost_borel: "{a<..b} \<in> sets borel"
   681     and atLeastLessThan_borel: "{a..<b} \<in> sets borel"
   682   unfolding greaterThanAtMost_def atLeastLessThan_def
   683   by (blast intro: borel_open borel_closed open_lessThan open_greaterThan open_greaterThanLessThan
   684                    closed_atMost closed_atLeast closed_atLeastAtMost)+
   685 
   686 lemma borel_Iio:
   687   "borel = sigma UNIV (range lessThan :: 'a::{linorder_topology, second_countable_topology} set set)"
   688   unfolding second_countable_borel_measurable[OF open_generated_order]
   689 proof (intro sigma_eqI sigma_sets_eqI)
   690   from countable_dense_setE guess D :: "'a set" . note D = this
   691 
   692   interpret L: sigma_algebra UNIV "sigma_sets UNIV (range lessThan)"
   693     by (rule sigma_algebra_sigma_sets) simp
   694 
   695   fix A :: "'a set" assume "A \<in> range lessThan \<union> range greaterThan"
   696   then obtain y where "A = {y <..} \<or> A = {..< y}"
   697     by blast
   698   then show "A \<in> sigma_sets UNIV (range lessThan)"
   699   proof
   700     assume A: "A = {y <..}"
   701     show ?thesis
   702     proof cases
   703       assume "\<forall>x>y. \<exists>d. y < d \<and> d < x"
   704       with D(2)[of "{y <..< x}" for x] have "\<forall>x>y. \<exists>d\<in>D. y < d \<and> d < x"
   705         by (auto simp: set_eq_iff)
   706       then have "A = UNIV - (\<Inter>d\<in>{d\<in>D. y < d}. {..< d})"
   707         by (auto simp: A) (metis less_asym)
   708       also have "\<dots> \<in> sigma_sets UNIV (range lessThan)"
   709         using D(1) by (intro L.Diff L.top L.countable_INT'') auto
   710       finally show ?thesis .
   711     next
   712       assume "\<not> (\<forall>x>y. \<exists>d. y < d \<and> d < x)"
   713       then obtain x where "y < x"  "\<And>d. y < d \<Longrightarrow> \<not> d < x"
   714         by auto
   715       then have "A = UNIV - {..< x}"
   716         unfolding A by (auto simp: not_less[symmetric])
   717       also have "\<dots> \<in> sigma_sets UNIV (range lessThan)"
   718         by auto
   719       finally show ?thesis .
   720     qed
   721   qed auto
   722 qed auto
   723 
   724 lemma borel_Ioi:
   725   "borel = sigma UNIV (range greaterThan :: 'a::{linorder_topology, second_countable_topology} set set)"
   726   unfolding second_countable_borel_measurable[OF open_generated_order]
   727 proof (intro sigma_eqI sigma_sets_eqI)
   728   from countable_dense_setE guess D :: "'a set" . note D = this
   729 
   730   interpret L: sigma_algebra UNIV "sigma_sets UNIV (range greaterThan)"
   731     by (rule sigma_algebra_sigma_sets) simp
   732 
   733   fix A :: "'a set" assume "A \<in> range lessThan \<union> range greaterThan"
   734   then obtain y where "A = {y <..} \<or> A = {..< y}"
   735     by blast
   736   then show "A \<in> sigma_sets UNIV (range greaterThan)"
   737   proof
   738     assume A: "A = {..< y}"
   739     show ?thesis
   740     proof cases
   741       assume "\<forall>x<y. \<exists>d. x < d \<and> d < y"
   742       with D(2)[of "{x <..< y}" for x] have "\<forall>x<y. \<exists>d\<in>D. x < d \<and> d < y"
   743         by (auto simp: set_eq_iff)
   744       then have "A = UNIV - (\<Inter>d\<in>{d\<in>D. d < y}. {d <..})"
   745         by (auto simp: A) (metis less_asym)
   746       also have "\<dots> \<in> sigma_sets UNIV (range greaterThan)"
   747         using D(1) by (intro L.Diff L.top L.countable_INT'') auto
   748       finally show ?thesis .
   749     next
   750       assume "\<not> (\<forall>x<y. \<exists>d. x < d \<and> d < y)"
   751       then obtain x where "x < y"  "\<And>d. y > d \<Longrightarrow> x \<ge> d"
   752         by (auto simp: not_less[symmetric])
   753       then have "A = UNIV - {x <..}"
   754         unfolding A Compl_eq_Diff_UNIV[symmetric] by auto
   755       also have "\<dots> \<in> sigma_sets UNIV (range greaterThan)"
   756         by auto
   757       finally show ?thesis .
   758     qed
   759   qed auto
   760 qed auto
   761 
   762 lemma borel_measurableI_less:
   763   fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   764   shows "(\<And>y. {x\<in>space M. f x < y} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   765   unfolding borel_Iio
   766   by (rule measurable_measure_of) (auto simp: Int_def conj_commute)
   767 
   768 lemma borel_measurableI_greater:
   769   fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   770   shows "(\<And>y. {x\<in>space M. y < f x} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   771   unfolding borel_Ioi
   772   by (rule measurable_measure_of) (auto simp: Int_def conj_commute)
   773 
   774 lemma borel_measurableI_le:
   775   fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   776   shows "(\<And>y. {x\<in>space M. f x \<le> y} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   777   by (rule borel_measurableI_greater) (auto simp: not_le[symmetric])
   778 
   779 lemma borel_measurableI_ge:
   780   fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   781   shows "(\<And>y. {x\<in>space M. y \<le> f x} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   782   by (rule borel_measurableI_less) (auto simp: not_le[symmetric])
   783 
   784 lemma borel_measurable_less[measurable]:
   785   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, linorder_topology}"
   786   assumes "f \<in> borel_measurable M"
   787   assumes "g \<in> borel_measurable M"
   788   shows "{w \<in> space M. f w < g w} \<in> sets M"
   789 proof -
   790   have "{w \<in> space M. f w < g w} = (\<lambda>x. (f x, g x)) -` {x. fst x < snd x} \<inter> space M"
   791     by auto
   792   also have "\<dots> \<in> sets M"
   793     by (intro measurable_sets[OF borel_measurable_Pair borel_open, OF assms open_Collect_less]
   794               continuous_intros)
   795   finally show ?thesis .
   796 qed
   797 
   798 lemma
   799   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, linorder_topology}"
   800   assumes f[measurable]: "f \<in> borel_measurable M"
   801   assumes g[measurable]: "g \<in> borel_measurable M"
   802   shows borel_measurable_le[measurable]: "{w \<in> space M. f w \<le> g w} \<in> sets M"
   803     and borel_measurable_eq[measurable]: "{w \<in> space M. f w = g w} \<in> sets M"
   804     and borel_measurable_neq: "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
   805   unfolding eq_iff not_less[symmetric]
   806   by measurable
   807 
   808 lemma borel_measurable_SUP[measurable (raw)]:
   809   fixes F :: "_ \<Rightarrow> _ \<Rightarrow> _::{complete_linorder, linorder_topology, second_countable_topology}"
   810   assumes [simp]: "countable I"
   811   assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   812   shows "(\<lambda>x. SUP i:I. F i x) \<in> borel_measurable M"
   813   by (rule borel_measurableI_greater) (simp add: less_SUP_iff)
   814 
   815 lemma borel_measurable_INF[measurable (raw)]:
   816   fixes F :: "_ \<Rightarrow> _ \<Rightarrow> _::{complete_linorder, linorder_topology, second_countable_topology}"
   817   assumes [simp]: "countable I"
   818   assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   819   shows "(\<lambda>x. INF i:I. F i x) \<in> borel_measurable M"
   820   by (rule borel_measurableI_less) (simp add: INF_less_iff)
   821 
   822 lemma borel_measurable_cSUP[measurable (raw)]:
   823   fixes F :: "_ \<Rightarrow> _ \<Rightarrow> 'a::{conditionally_complete_linorder, linorder_topology, second_countable_topology}"
   824   assumes [simp]: "countable I"
   825   assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   826   assumes bdd: "\<And>x. x \<in> space M \<Longrightarrow> bdd_above ((\<lambda>i. F i x) ` I)"
   827   shows "(\<lambda>x. SUP i:I. F i x) \<in> borel_measurable M"
   828 proof cases
   829   assume "I = {}" then show ?thesis
   830     unfolding \<open>I = {}\<close> image_empty by simp
   831 next
   832   assume "I \<noteq> {}"
   833   show ?thesis
   834   proof (rule borel_measurableI_le)
   835     fix y
   836     have "{x \<in> space M. \<forall>i\<in>I. F i x \<le> y} \<in> sets M"
   837       by measurable
   838     also have "{x \<in> space M. \<forall>i\<in>I. F i x \<le> y} = {x \<in> space M. (SUP i:I. F i x) \<le> y}"
   839       by (simp add: cSUP_le_iff \<open>I \<noteq> {}\<close> bdd cong: conj_cong)
   840     finally show "{x \<in> space M. (SUP i:I. F i x) \<le>  y} \<in> sets M"  .
   841   qed
   842 qed
   843 
   844 lemma borel_measurable_cINF[measurable (raw)]:
   845   fixes F :: "_ \<Rightarrow> _ \<Rightarrow> 'a::{conditionally_complete_linorder, linorder_topology, second_countable_topology}"
   846   assumes [simp]: "countable I"
   847   assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   848   assumes bdd: "\<And>x. x \<in> space M \<Longrightarrow> bdd_below ((\<lambda>i. F i x) ` I)"
   849   shows "(\<lambda>x. INF i:I. F i x) \<in> borel_measurable M"
   850 proof cases
   851   assume "I = {}" then show ?thesis
   852     unfolding \<open>I = {}\<close> image_empty by simp
   853 next
   854   assume "I \<noteq> {}"
   855   show ?thesis
   856   proof (rule borel_measurableI_ge)
   857     fix y
   858     have "{x \<in> space M. \<forall>i\<in>I. y \<le> F i x} \<in> sets M"
   859       by measurable
   860     also have "{x \<in> space M. \<forall>i\<in>I. y \<le> F i x} = {x \<in> space M. y \<le> (INF i:I. F i x)}"
   861       by (simp add: le_cINF_iff \<open>I \<noteq> {}\<close> bdd cong: conj_cong)
   862     finally show "{x \<in> space M. y \<le> (INF i:I. F i x)} \<in> sets M"  .
   863   qed
   864 qed
   865 
   866 lemma borel_measurable_lfp[consumes 1, case_names continuity step]:
   867   fixes F :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b::{complete_linorder, linorder_topology, second_countable_topology})"
   868   assumes "sup_continuous F"
   869   assumes *: "\<And>f. f \<in> borel_measurable M \<Longrightarrow> F f \<in> borel_measurable M"
   870   shows "lfp F \<in> borel_measurable M"
   871 proof -
   872   { fix i have "((F ^^ i) bot) \<in> borel_measurable M"
   873       by (induct i) (auto intro!: *) }
   874   then have "(\<lambda>x. SUP i. (F ^^ i) bot x) \<in> borel_measurable M"
   875     by measurable
   876   also have "(\<lambda>x. SUP i. (F ^^ i) bot x) = (SUP i. (F ^^ i) bot)"
   877     by auto
   878   also have "(SUP i. (F ^^ i) bot) = lfp F"
   879     by (rule sup_continuous_lfp[symmetric]) fact
   880   finally show ?thesis .
   881 qed
   882 
   883 lemma borel_measurable_gfp[consumes 1, case_names continuity step]:
   884   fixes F :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b::{complete_linorder, linorder_topology, second_countable_topology})"
   885   assumes "inf_continuous F"
   886   assumes *: "\<And>f. f \<in> borel_measurable M \<Longrightarrow> F f \<in> borel_measurable M"
   887   shows "gfp F \<in> borel_measurable M"
   888 proof -
   889   { fix i have "((F ^^ i) top) \<in> borel_measurable M"
   890       by (induct i) (auto intro!: * simp: bot_fun_def) }
   891   then have "(\<lambda>x. INF i. (F ^^ i) top x) \<in> borel_measurable M"
   892     by measurable
   893   also have "(\<lambda>x. INF i. (F ^^ i) top x) = (INF i. (F ^^ i) top)"
   894     by auto
   895   also have "\<dots> = gfp F"
   896     by (rule inf_continuous_gfp[symmetric]) fact
   897   finally show ?thesis .
   898 qed
   899 
   900 lemma borel_measurable_max[measurable (raw)]:
   901   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. max (g x) (f x) :: 'b::{second_countable_topology, linorder_topology}) \<in> borel_measurable M"
   902   by (rule borel_measurableI_less) simp
   903 
   904 lemma borel_measurable_min[measurable (raw)]:
   905   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. min (g x) (f x) :: 'b::{second_countable_topology, linorder_topology}) \<in> borel_measurable M"
   906   by (rule borel_measurableI_greater) simp
   907 
   908 lemma borel_measurable_Min[measurable (raw)]:
   909   "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable M) \<Longrightarrow> (\<lambda>x. Min ((\<lambda>i. f i x)`I) :: 'b::{second_countable_topology, linorder_topology}) \<in> borel_measurable M"
   910 proof (induct I rule: finite_induct)
   911   case (insert i I) then show ?case
   912     by (cases "I = {}") auto
   913 qed auto
   914 
   915 lemma borel_measurable_Max[measurable (raw)]:
   916   "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable M) \<Longrightarrow> (\<lambda>x. Max ((\<lambda>i. f i x)`I) :: 'b::{second_countable_topology, linorder_topology}) \<in> borel_measurable M"
   917 proof (induct I rule: finite_induct)
   918   case (insert i I) then show ?case
   919     by (cases "I = {}") auto
   920 qed auto
   921 
   922 lemma borel_measurable_sup[measurable (raw)]:
   923   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. sup (g x) (f x) :: 'b::{lattice, second_countable_topology, linorder_topology}) \<in> borel_measurable M"
   924   unfolding sup_max by measurable
   925 
   926 lemma borel_measurable_inf[measurable (raw)]:
   927   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. inf (g x) (f x) :: 'b::{lattice, second_countable_topology, linorder_topology}) \<in> borel_measurable M"
   928   unfolding inf_min by measurable
   929 
   930 lemma [measurable (raw)]:
   931   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   932   assumes "\<And>i. f i \<in> borel_measurable M"
   933   shows borel_measurable_liminf: "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
   934     and borel_measurable_limsup: "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
   935   unfolding liminf_SUP_INF limsup_INF_SUP using assms by auto
   936 
   937 lemma measurable_convergent[measurable (raw)]:
   938   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   939   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
   940   shows "Measurable.pred M (\<lambda>x. convergent (\<lambda>i. f i x))"
   941   unfolding convergent_ereal by measurable
   942 
   943 lemma sets_Collect_convergent[measurable]:
   944   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   945   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
   946   shows "{x\<in>space M. convergent (\<lambda>i. f i x)} \<in> sets M"
   947   by measurable
   948 
   949 lemma borel_measurable_lim[measurable (raw)]:
   950   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   951   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
   952   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
   953 proof -
   954   have "\<And>x. lim (\<lambda>i. f i x) = (if convergent (\<lambda>i. f i x) then limsup (\<lambda>i. f i x) else (THE i. False))"
   955     by (simp add: lim_def convergent_def convergent_limsup_cl)
   956   then show ?thesis
   957     by simp
   958 qed
   959 
   960 lemma borel_measurable_LIMSEQ_order:
   961   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   962   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) \<longlonglongrightarrow> u' x"
   963   and u: "\<And>i. u i \<in> borel_measurable M"
   964   shows "u' \<in> borel_measurable M"
   965 proof -
   966   have "\<And>x. x \<in> space M \<Longrightarrow> u' x = liminf (\<lambda>n. u n x)"
   967     using u' by (simp add: lim_imp_Liminf[symmetric])
   968   with u show ?thesis by (simp cong: measurable_cong)
   969 qed
   970 
   971 subsection \<open>Borel spaces on topological monoids\<close>
   972 
   973 lemma borel_measurable_add[measurable (raw)]:
   974   fixes f g :: "'a \<Rightarrow> 'b::{second_countable_topology, topological_monoid_add}"
   975   assumes f: "f \<in> borel_measurable M"
   976   assumes g: "g \<in> borel_measurable M"
   977   shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   978   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
   979 
   980 lemma borel_measurable_sum[measurable (raw)]:
   981   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> 'b::{second_countable_topology, topological_comm_monoid_add}"
   982   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   983   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   984 proof cases
   985   assume "finite S"
   986   thus ?thesis using assms by induct auto
   987 qed simp
   988 
   989 lemma borel_measurable_suminf_order[measurable (raw)]:
   990   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology, topological_comm_monoid_add}"
   991   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
   992   shows "(\<lambda>x. suminf (\<lambda>i. f i x)) \<in> borel_measurable M"
   993   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
   994 
   995 subsection \<open>Borel spaces on Euclidean spaces\<close>
   996 
   997 lemma borel_measurable_inner[measurable (raw)]:
   998   fixes f g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_inner}"
   999   assumes "f \<in> borel_measurable M"
  1000   assumes "g \<in> borel_measurable M"
  1001   shows "(\<lambda>x. f x \<bullet> g x) \<in> borel_measurable M"
  1002   using assms
  1003   by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
  1004 
  1005 notation
  1006   eucl_less (infix "<e" 50)
  1007 
  1008 lemma box_oc: "{x. a <e x \<and> x \<le> b} = {x. a <e x} \<inter> {..b}"
  1009   and box_co: "{x. a \<le> x \<and> x <e b} = {a..} \<inter> {x. x <e b}"
  1010   by auto
  1011 
  1012 lemma eucl_ivals[measurable]:
  1013   fixes a b :: "'a::ordered_euclidean_space"
  1014   shows "{x. x <e a} \<in> sets borel"
  1015     and "{x. a <e x} \<in> sets borel"
  1016     and "{..a} \<in> sets borel"
  1017     and "{a..} \<in> sets borel"
  1018     and "{a..b} \<in> sets borel"
  1019     and  "{x. a <e x \<and> x \<le> b} \<in> sets borel"
  1020     and "{x. a \<le> x \<and>  x <e b} \<in> sets borel"
  1021   unfolding box_oc box_co
  1022   by (auto intro: borel_open borel_closed)
  1023 
  1024 lemma
  1025   fixes i :: "'a::{second_countable_topology, real_inner}"
  1026   shows hafspace_less_borel: "{x. a < x \<bullet> i} \<in> sets borel"
  1027     and hafspace_greater_borel: "{x. x \<bullet> i < a} \<in> sets borel"
  1028     and hafspace_less_eq_borel: "{x. a \<le> x \<bullet> i} \<in> sets borel"
  1029     and hafspace_greater_eq_borel: "{x. x \<bullet> i \<le> a} \<in> sets borel"
  1030   by simp_all
  1031 
  1032 lemma borel_eq_box:
  1033   "borel = sigma UNIV (range (\<lambda> (a, b). box a b :: 'a :: euclidean_space set))"
  1034     (is "_ = ?SIGMA")
  1035 proof (rule borel_eq_sigmaI1[OF borel_def])
  1036   fix M :: "'a set" assume "M \<in> {S. open S}"
  1037   then have "open M" by simp
  1038   show "M \<in> ?SIGMA"
  1039     apply (subst open_UNION_box[OF \<open>open M\<close>])
  1040     apply (safe intro!: sets.countable_UN' countable_PiE countable_Collect)
  1041     apply (auto intro: countable_rat)
  1042     done
  1043 qed (auto simp: box_def)
  1044 
  1045 lemma halfspace_gt_in_halfspace:
  1046   assumes i: "i \<in> A"
  1047   shows "{x::'a. a < x \<bullet> i} \<in>
  1048     sigma_sets UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> A))"
  1049   (is "?set \<in> ?SIGMA")
  1050 proof -
  1051   interpret sigma_algebra UNIV ?SIGMA
  1052     by (intro sigma_algebra_sigma_sets) simp_all
  1053   have *: "?set = (\<Union>n. UNIV - {x::'a. x \<bullet> i < a + 1 / real (Suc n)})"
  1054   proof (safe, simp_all add: not_less del: of_nat_Suc)
  1055     fix x :: 'a assume "a < x \<bullet> i"
  1056     with reals_Archimedean[of "x \<bullet> i - a"]
  1057     obtain n where "a + 1 / real (Suc n) < x \<bullet> i"
  1058       by (auto simp: field_simps)
  1059     then show "\<exists>n. a + 1 / real (Suc n) \<le> x \<bullet> i"
  1060       by (blast intro: less_imp_le)
  1061   next
  1062     fix x n
  1063     have "a < a + 1 / real (Suc n)" by auto
  1064     also assume "\<dots> \<le> x"
  1065     finally show "a < x" .
  1066   qed
  1067   show "?set \<in> ?SIGMA" unfolding *
  1068     by (auto intro!: Diff sigma_sets_Inter i)
  1069 qed
  1070 
  1071 lemma borel_eq_halfspace_less:
  1072   "borel = sigma UNIV ((\<lambda>(a, i). {x::'a::euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> Basis))"
  1073   (is "_ = ?SIGMA")
  1074 proof (rule borel_eq_sigmaI2[OF borel_eq_box])
  1075   fix a b :: 'a
  1076   have "box a b = {x\<in>space ?SIGMA. \<forall>i\<in>Basis. a \<bullet> i < x \<bullet> i \<and> x \<bullet> i < b \<bullet> i}"
  1077     by (auto simp: box_def)
  1078   also have "\<dots> \<in> sets ?SIGMA"
  1079     by (intro sets.sets_Collect_conj sets.sets_Collect_finite_All sets.sets_Collect_const)
  1080        (auto intro!: halfspace_gt_in_halfspace countable_PiE countable_rat)
  1081   finally show "box a b \<in> sets ?SIGMA" .
  1082 qed auto
  1083 
  1084 lemma borel_eq_halfspace_le:
  1085   "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. x \<bullet> i \<le> a}) ` (UNIV \<times> Basis))"
  1086   (is "_ = ?SIGMA")
  1087 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
  1088   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
  1089   then have i: "i \<in> Basis" by auto
  1090   have *: "{x::'a. x\<bullet>i < a} = (\<Union>n. {x. x\<bullet>i \<le> a - 1/real (Suc n)})"
  1091   proof (safe, simp_all del: of_nat_Suc)
  1092     fix x::'a assume *: "x\<bullet>i < a"
  1093     with reals_Archimedean[of "a - x\<bullet>i"]
  1094     obtain n where "x \<bullet> i < a - 1 / (real (Suc n))"
  1095       by (auto simp: field_simps)
  1096     then show "\<exists>n. x \<bullet> i \<le> a - 1 / (real (Suc n))"
  1097       by (blast intro: less_imp_le)
  1098   next
  1099     fix x::'a and n
  1100     assume "x\<bullet>i \<le> a - 1 / real (Suc n)"
  1101     also have "\<dots> < a" by auto
  1102     finally show "x\<bullet>i < a" .
  1103   qed
  1104   show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
  1105     by (intro sets.countable_UN) (auto intro: i)
  1106 qed auto
  1107 
  1108 lemma borel_eq_halfspace_ge:
  1109   "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. a \<le> x \<bullet> i}) ` (UNIV \<times> Basis))"
  1110   (is "_ = ?SIGMA")
  1111 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
  1112   fix a :: real and i :: 'a assume i: "(a, i) \<in> UNIV \<times> Basis"
  1113   have *: "{x::'a. x\<bullet>i < a} = space ?SIGMA - {x::'a. a \<le> x\<bullet>i}" by auto
  1114   show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
  1115     using i by (intro sets.compl_sets) auto
  1116 qed auto
  1117 
  1118 lemma borel_eq_halfspace_greater:
  1119   "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. a < x \<bullet> i}) ` (UNIV \<times> Basis))"
  1120   (is "_ = ?SIGMA")
  1121 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_le])
  1122   fix a :: real and i :: 'a assume "(a, i) \<in> (UNIV \<times> Basis)"
  1123   then have i: "i \<in> Basis" by auto
  1124   have *: "{x::'a. x\<bullet>i \<le> a} = space ?SIGMA - {x::'a. a < x\<bullet>i}" by auto
  1125   show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
  1126     by (intro sets.compl_sets) (auto intro: i)
  1127 qed auto
  1128 
  1129 lemma borel_eq_atMost:
  1130   "borel = sigma UNIV (range (\<lambda>a. {..a::'a::ordered_euclidean_space}))"
  1131   (is "_ = ?SIGMA")
  1132 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
  1133   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
  1134   then have "i \<in> Basis" by auto
  1135   then have *: "{x::'a. x\<bullet>i \<le> a} = (\<Union>k::nat. {.. (\<Sum>n\<in>Basis. (if n = i then a else real k)*\<^sub>R n)})"
  1136   proof (safe, simp_all add: eucl_le[where 'a='a] split: if_split_asm)
  1137     fix x :: 'a
  1138     from real_arch_simple[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"] guess k::nat ..
  1139     then have "\<And>i. i \<in> Basis \<Longrightarrow> x\<bullet>i \<le> real k"
  1140       by (subst (asm) Max_le_iff) auto
  1141     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia \<le> real k"
  1142       by (auto intro!: exI[of _ k])
  1143   qed
  1144   show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
  1145     by (intro sets.countable_UN) auto
  1146 qed auto
  1147 
  1148 lemma borel_eq_greaterThan:
  1149   "borel = sigma UNIV (range (\<lambda>a::'a::ordered_euclidean_space. {x. a <e x}))"
  1150   (is "_ = ?SIGMA")
  1151 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
  1152   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
  1153   then have i: "i \<in> Basis" by auto
  1154   have "{x::'a. x\<bullet>i \<le> a} = UNIV - {x::'a. a < x\<bullet>i}" by auto
  1155   also have *: "{x::'a. a < x\<bullet>i} =
  1156       (\<Union>k::nat. {x. (\<Sum>n\<in>Basis. (if n = i then a else -real k) *\<^sub>R n) <e x})" using i
  1157   proof (safe, simp_all add: eucl_less_def split: if_split_asm)
  1158     fix x :: 'a
  1159     from reals_Archimedean2[of "Max ((\<lambda>i. -x\<bullet>i)`Basis)"]
  1160     guess k::nat .. note k = this
  1161     { fix i :: 'a assume "i \<in> Basis"
  1162       then have "-x\<bullet>i < real k"
  1163         using k by (subst (asm) Max_less_iff) auto
  1164       then have "- real k < x\<bullet>i" by simp }
  1165     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> -real k < x \<bullet> ia"
  1166       by (auto intro!: exI[of _ k])
  1167   qed
  1168   finally show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA"
  1169     apply (simp only:)
  1170     apply (intro sets.countable_UN sets.Diff)
  1171     apply (auto intro: sigma_sets_top)
  1172     done
  1173 qed auto
  1174 
  1175 lemma borel_eq_lessThan:
  1176   "borel = sigma UNIV (range (\<lambda>a::'a::ordered_euclidean_space. {x. x <e a}))"
  1177   (is "_ = ?SIGMA")
  1178 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_ge])
  1179   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
  1180   then have i: "i \<in> Basis" by auto
  1181   have "{x::'a. a \<le> x\<bullet>i} = UNIV - {x::'a. x\<bullet>i < a}" by auto
  1182   also have *: "{x::'a. x\<bullet>i < a} = (\<Union>k::nat. {x. x <e (\<Sum>n\<in>Basis. (if n = i then a else real k) *\<^sub>R n)})" using \<open>i\<in> Basis\<close>
  1183   proof (safe, simp_all add: eucl_less_def split: if_split_asm)
  1184     fix x :: 'a
  1185     from reals_Archimedean2[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"]
  1186     guess k::nat .. note k = this
  1187     { fix i :: 'a assume "i \<in> Basis"
  1188       then have "x\<bullet>i < real k"
  1189         using k by (subst (asm) Max_less_iff) auto
  1190       then have "x\<bullet>i < real k" by simp }
  1191     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia < real k"
  1192       by (auto intro!: exI[of _ k])
  1193   qed
  1194   finally show "{x. a \<le> x\<bullet>i} \<in> ?SIGMA"
  1195     apply (simp only:)
  1196     apply (intro sets.countable_UN sets.Diff)
  1197     apply (auto intro: sigma_sets_top )
  1198     done
  1199 qed auto
  1200 
  1201 lemma borel_eq_atLeastAtMost:
  1202   "borel = sigma UNIV (range (\<lambda>(a,b). {a..b} ::'a::ordered_euclidean_space set))"
  1203   (is "_ = ?SIGMA")
  1204 proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
  1205   fix a::'a
  1206   have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
  1207   proof (safe, simp_all add: eucl_le[where 'a='a])
  1208     fix x :: 'a
  1209     from real_arch_simple[of "Max ((\<lambda>i. - x\<bullet>i)`Basis)"]
  1210     guess k::nat .. note k = this
  1211     { fix i :: 'a assume "i \<in> Basis"
  1212       with k have "- x\<bullet>i \<le> real k"
  1213         by (subst (asm) Max_le_iff) (auto simp: field_simps)
  1214       then have "- real k \<le> x\<bullet>i" by simp }
  1215     then show "\<exists>n::nat. \<forall>i\<in>Basis. - real n \<le> x \<bullet> i"
  1216       by (auto intro!: exI[of _ k])
  1217   qed
  1218   show "{..a} \<in> ?SIGMA" unfolding *
  1219     by (intro sets.countable_UN)
  1220        (auto intro!: sigma_sets_top)
  1221 qed auto
  1222 
  1223 lemma borel_set_induct[consumes 1, case_names empty interval compl union]:
  1224   assumes "A \<in> sets borel"
  1225   assumes empty: "P {}" and int: "\<And>a b. a \<le> b \<Longrightarrow> P {a..b}" and compl: "\<And>A. A \<in> sets borel \<Longrightarrow> P A \<Longrightarrow> P (-A)" and
  1226           un: "\<And>f. disjoint_family f \<Longrightarrow> (\<And>i. f i \<in> sets borel) \<Longrightarrow>  (\<And>i. P (f i)) \<Longrightarrow> P (\<Union>i::nat. f i)"
  1227   shows "P (A::real set)"
  1228 proof-
  1229   let ?G = "range (\<lambda>(a,b). {a..b::real})"
  1230   have "Int_stable ?G" "?G \<subseteq> Pow UNIV" "A \<in> sigma_sets UNIV ?G"
  1231       using assms(1) by (auto simp add: borel_eq_atLeastAtMost Int_stable_def)
  1232   thus ?thesis
  1233   proof (induction rule: sigma_sets_induct_disjoint)
  1234     case (union f)
  1235       from union.hyps(2) have "\<And>i. f i \<in> sets borel" by (auto simp: borel_eq_atLeastAtMost)
  1236       with union show ?case by (auto intro: un)
  1237   next
  1238     case (basic A)
  1239     then obtain a b where "A = {a .. b}" by auto
  1240     then show ?case
  1241       by (cases "a \<le> b") (auto intro: int empty)
  1242   qed (auto intro: empty compl simp: Compl_eq_Diff_UNIV[symmetric] borel_eq_atLeastAtMost)
  1243 qed
  1244 
  1245 lemma borel_sigma_sets_Ioc: "borel = sigma UNIV (range (\<lambda>(a, b). {a <.. b::real}))"
  1246 proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
  1247   fix i :: real
  1248   have "{..i} = (\<Union>j::nat. {-j <.. i})"
  1249     by (auto simp: minus_less_iff reals_Archimedean2)
  1250   also have "\<dots> \<in> sets (sigma UNIV (range (\<lambda>(i, j). {i<..j})))"
  1251     by (intro sets.countable_nat_UN) auto
  1252   finally show "{..i} \<in> sets (sigma UNIV (range (\<lambda>(i, j). {i<..j})))" .
  1253 qed simp
  1254 
  1255 lemma eucl_lessThan: "{x::real. x <e a} = lessThan a"
  1256   by (simp add: eucl_less_def lessThan_def)
  1257 
  1258 lemma borel_eq_atLeastLessThan:
  1259   "borel = sigma UNIV (range (\<lambda>(a, b). {a ..< b :: real}))" (is "_ = ?SIGMA")
  1260 proof (rule borel_eq_sigmaI5[OF borel_eq_lessThan])
  1261   have move_uminus: "\<And>x y::real. -x \<le> y \<longleftrightarrow> -y \<le> x" by auto
  1262   fix x :: real
  1263   have "{..<x} = (\<Union>i::nat. {-real i ..< x})"
  1264     by (auto simp: move_uminus real_arch_simple)
  1265   then show "{y. y <e x} \<in> ?SIGMA"
  1266     by (auto intro: sigma_sets.intros(2-) simp: eucl_lessThan)
  1267 qed auto
  1268 
  1269 lemma borel_measurable_halfspacesI:
  1270   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1271   assumes F: "borel = sigma UNIV (F ` (UNIV \<times> Basis))"
  1272   and S_eq: "\<And>a i. S a i = f -` F (a,i) \<inter> space M"
  1273   shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a::real. S a i \<in> sets M)"
  1274 proof safe
  1275   fix a :: real and i :: 'b assume i: "i \<in> Basis" and f: "f \<in> borel_measurable M"
  1276   then show "S a i \<in> sets M" unfolding assms
  1277     by (auto intro!: measurable_sets simp: assms(1))
  1278 next
  1279   assume a: "\<forall>i\<in>Basis. \<forall>a. S a i \<in> sets M"
  1280   then show "f \<in> borel_measurable M"
  1281     by (auto intro!: measurable_measure_of simp: S_eq F)
  1282 qed
  1283 
  1284 lemma borel_measurable_iff_halfspace_le:
  1285   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1286   shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a. {w \<in> space M. f w \<bullet> i \<le> a} \<in> sets M)"
  1287   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_le]) auto
  1288 
  1289 lemma borel_measurable_iff_halfspace_less:
  1290   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1291   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. \<forall>a. {w \<in> space M. f w \<bullet> i < a} \<in> sets M)"
  1292   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_less]) auto
  1293 
  1294 lemma borel_measurable_iff_halfspace_ge:
  1295   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1296   shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a. {w \<in> space M. a \<le> f w \<bullet> i} \<in> sets M)"
  1297   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_ge]) auto
  1298 
  1299 lemma borel_measurable_iff_halfspace_greater:
  1300   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1301   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. \<forall>a. {w \<in> space M. a < f w \<bullet> i} \<in> sets M)"
  1302   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_greater]) auto
  1303 
  1304 lemma borel_measurable_iff_le:
  1305   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
  1306   using borel_measurable_iff_halfspace_le[where 'c=real] by simp
  1307 
  1308 lemma borel_measurable_iff_less:
  1309   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
  1310   using borel_measurable_iff_halfspace_less[where 'c=real] by simp
  1311 
  1312 lemma borel_measurable_iff_ge:
  1313   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
  1314   using borel_measurable_iff_halfspace_ge[where 'c=real]
  1315   by simp
  1316 
  1317 lemma borel_measurable_iff_greater:
  1318   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
  1319   using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
  1320 
  1321 lemma borel_measurable_euclidean_space:
  1322   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1323   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M)"
  1324 proof safe
  1325   assume f: "\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M"
  1326   then show "f \<in> borel_measurable M"
  1327     by (subst borel_measurable_iff_halfspace_le) auto
  1328 qed auto
  1329 
  1330 subsection "Borel measurable operators"
  1331 
  1332 lemma borel_measurable_norm[measurable]: "norm \<in> borel_measurable borel"
  1333   by (intro borel_measurable_continuous_on1 continuous_intros)
  1334 
  1335 lemma borel_measurable_sgn [measurable]: "(sgn::'a::real_normed_vector \<Rightarrow> 'a) \<in> borel_measurable borel"
  1336   by (rule borel_measurable_continuous_countable_exceptions[where X="{0}"])
  1337      (auto intro!: continuous_on_sgn continuous_on_id)
  1338 
  1339 lemma borel_measurable_uminus[measurable (raw)]:
  1340   fixes g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
  1341   assumes g: "g \<in> borel_measurable M"
  1342   shows "(\<lambda>x. - g x) \<in> borel_measurable M"
  1343   by (rule borel_measurable_continuous_on[OF _ g]) (intro continuous_intros)
  1344 
  1345 lemma borel_measurable_diff[measurable (raw)]:
  1346   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
  1347   assumes f: "f \<in> borel_measurable M"
  1348   assumes g: "g \<in> borel_measurable M"
  1349   shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1350   using borel_measurable_add [of f M "- g"] assms by (simp add: fun_Compl_def)
  1351 
  1352 lemma borel_measurable_times[measurable (raw)]:
  1353   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_algebra}"
  1354   assumes f: "f \<in> borel_measurable M"
  1355   assumes g: "g \<in> borel_measurable M"
  1356   shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1357   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
  1358 
  1359 lemma borel_measurable_prod[measurable (raw)]:
  1360   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> 'b::{second_countable_topology, real_normed_field}"
  1361   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1362   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1363 proof cases
  1364   assume "finite S"
  1365   thus ?thesis using assms by induct auto
  1366 qed simp
  1367 
  1368 lemma borel_measurable_dist[measurable (raw)]:
  1369   fixes g f :: "'a \<Rightarrow> 'b::{second_countable_topology, metric_space}"
  1370   assumes f: "f \<in> borel_measurable M"
  1371   assumes g: "g \<in> borel_measurable M"
  1372   shows "(\<lambda>x. dist (f x) (g x)) \<in> borel_measurable M"
  1373   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
  1374 
  1375 lemma borel_measurable_scaleR[measurable (raw)]:
  1376   fixes g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
  1377   assumes f: "f \<in> borel_measurable M"
  1378   assumes g: "g \<in> borel_measurable M"
  1379   shows "(\<lambda>x. f x *\<^sub>R g x) \<in> borel_measurable M"
  1380   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
  1381 
  1382 lemma affine_borel_measurable_vector:
  1383   fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
  1384   assumes "f \<in> borel_measurable M"
  1385   shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
  1386 proof (rule borel_measurableI)
  1387   fix S :: "'x set" assume "open S"
  1388   show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
  1389   proof cases
  1390     assume "b \<noteq> 0"
  1391     with \<open>open S\<close> have "open ((\<lambda>x. (- a + x) /\<^sub>R b) ` S)" (is "open ?S")
  1392       using open_affinity [of S "inverse b" "- a /\<^sub>R b"]
  1393       by (auto simp: algebra_simps)
  1394     hence "?S \<in> sets borel" by auto
  1395     moreover
  1396     from \<open>b \<noteq> 0\<close> have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
  1397       apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
  1398     ultimately show ?thesis using assms unfolding in_borel_measurable_borel
  1399       by auto
  1400   qed simp
  1401 qed
  1402 
  1403 lemma borel_measurable_const_scaleR[measurable (raw)]:
  1404   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. b *\<^sub>R f x ::'a::real_normed_vector) \<in> borel_measurable M"
  1405   using affine_borel_measurable_vector[of f M 0 b] by simp
  1406 
  1407 lemma borel_measurable_const_add[measurable (raw)]:
  1408   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. a + f x ::'a::real_normed_vector) \<in> borel_measurable M"
  1409   using affine_borel_measurable_vector[of f M a 1] by simp
  1410 
  1411 lemma borel_measurable_inverse[measurable (raw)]:
  1412   fixes f :: "'a \<Rightarrow> 'b::real_normed_div_algebra"
  1413   assumes f: "f \<in> borel_measurable M"
  1414   shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
  1415   apply (rule measurable_compose[OF f])
  1416   apply (rule borel_measurable_continuous_countable_exceptions[of "{0}"])
  1417   apply (auto intro!: continuous_on_inverse continuous_on_id)
  1418   done
  1419 
  1420 lemma borel_measurable_divide[measurable (raw)]:
  1421   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow>
  1422     (\<lambda>x. f x / g x::'b::{second_countable_topology, real_normed_div_algebra}) \<in> borel_measurable M"
  1423   by (simp add: divide_inverse)
  1424 
  1425 lemma borel_measurable_abs[measurable (raw)]:
  1426   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
  1427   unfolding abs_real_def by simp
  1428 
  1429 lemma borel_measurable_nth[measurable (raw)]:
  1430   "(\<lambda>x::real^'n. x $ i) \<in> borel_measurable borel"
  1431   by (simp add: cart_eq_inner_axis)
  1432 
  1433 lemma convex_measurable:
  1434   fixes A :: "'a :: euclidean_space set"
  1435   shows "X \<in> borel_measurable M \<Longrightarrow> X ` space M \<subseteq> A \<Longrightarrow> open A \<Longrightarrow> convex_on A q \<Longrightarrow>
  1436     (\<lambda>x. q (X x)) \<in> borel_measurable M"
  1437   by (rule measurable_compose[where f=X and N="restrict_space borel A"])
  1438      (auto intro!: borel_measurable_continuous_on_restrict convex_on_continuous measurable_restrict_space2)
  1439 
  1440 lemma borel_measurable_ln[measurable (raw)]:
  1441   assumes f: "f \<in> borel_measurable M"
  1442   shows "(\<lambda>x. ln (f x :: real)) \<in> borel_measurable M"
  1443   apply (rule measurable_compose[OF f])
  1444   apply (rule borel_measurable_continuous_countable_exceptions[of "{0}"])
  1445   apply (auto intro!: continuous_on_ln continuous_on_id)
  1446   done
  1447 
  1448 lemma borel_measurable_log[measurable (raw)]:
  1449   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. log (g x) (f x)) \<in> borel_measurable M"
  1450   unfolding log_def by auto
  1451 
  1452 lemma borel_measurable_exp[measurable]:
  1453   "(exp::'a::{real_normed_field,banach}\<Rightarrow>'a) \<in> borel_measurable borel"
  1454   by (intro borel_measurable_continuous_on1 continuous_at_imp_continuous_on ballI isCont_exp)
  1455 
  1456 lemma measurable_real_floor[measurable]:
  1457   "(floor :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
  1458 proof -
  1459   have "\<And>a x. \<lfloor>x\<rfloor> = a \<longleftrightarrow> (real_of_int a \<le> x \<and> x < real_of_int (a + 1))"
  1460     by (auto intro: floor_eq2)
  1461   then show ?thesis
  1462     by (auto simp: vimage_def measurable_count_space_eq2_countable)
  1463 qed
  1464 
  1465 lemma measurable_real_ceiling[measurable]:
  1466   "(ceiling :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
  1467   unfolding ceiling_def[abs_def] by simp
  1468 
  1469 lemma borel_measurable_real_floor: "(\<lambda>x::real. real_of_int \<lfloor>x\<rfloor>) \<in> borel_measurable borel"
  1470   by simp
  1471 
  1472 lemma borel_measurable_root [measurable]: "root n \<in> borel_measurable borel"
  1473   by (intro borel_measurable_continuous_on1 continuous_intros)
  1474 
  1475 lemma borel_measurable_sqrt [measurable]: "sqrt \<in> borel_measurable borel"
  1476   by (intro borel_measurable_continuous_on1 continuous_intros)
  1477 
  1478 lemma borel_measurable_power [measurable (raw)]:
  1479   fixes f :: "_ \<Rightarrow> 'b::{power,real_normed_algebra}"
  1480   assumes f: "f \<in> borel_measurable M"
  1481   shows "(\<lambda>x. (f x) ^ n) \<in> borel_measurable M"
  1482   by (intro borel_measurable_continuous_on [OF _ f] continuous_intros)
  1483 
  1484 lemma borel_measurable_Re [measurable]: "Re \<in> borel_measurable borel"
  1485   by (intro borel_measurable_continuous_on1 continuous_intros)
  1486 
  1487 lemma borel_measurable_Im [measurable]: "Im \<in> borel_measurable borel"
  1488   by (intro borel_measurable_continuous_on1 continuous_intros)
  1489 
  1490 lemma borel_measurable_of_real [measurable]: "(of_real :: _ \<Rightarrow> (_::real_normed_algebra)) \<in> borel_measurable borel"
  1491   by (intro borel_measurable_continuous_on1 continuous_intros)
  1492 
  1493 lemma borel_measurable_sin [measurable]: "(sin :: _ \<Rightarrow> (_::{real_normed_field,banach})) \<in> borel_measurable borel"
  1494   by (intro borel_measurable_continuous_on1 continuous_intros)
  1495 
  1496 lemma borel_measurable_cos [measurable]: "(cos :: _ \<Rightarrow> (_::{real_normed_field,banach})) \<in> borel_measurable borel"
  1497   by (intro borel_measurable_continuous_on1 continuous_intros)
  1498 
  1499 lemma borel_measurable_arctan [measurable]: "arctan \<in> borel_measurable borel"
  1500   by (intro borel_measurable_continuous_on1 continuous_intros)
  1501 
  1502 lemma borel_measurable_complex_iff:
  1503   "f \<in> borel_measurable M \<longleftrightarrow>
  1504     (\<lambda>x. Re (f x)) \<in> borel_measurable M \<and> (\<lambda>x. Im (f x)) \<in> borel_measurable M"
  1505   apply auto
  1506   apply (subst fun_complex_eq)
  1507   apply (intro borel_measurable_add)
  1508   apply auto
  1509   done
  1510 
  1511 lemma measurable_of_bool[measurable]: "of_bool \<in> count_space UNIV \<rightarrow>\<^sub>M borel"
  1512   by simp
  1513 
  1514 subsection "Borel space on the extended reals"
  1515 
  1516 lemma borel_measurable_ereal[measurable (raw)]:
  1517   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
  1518   using continuous_on_ereal f by (rule borel_measurable_continuous_on) (rule continuous_on_id)
  1519 
  1520 lemma borel_measurable_real_of_ereal[measurable (raw)]:
  1521   fixes f :: "'a \<Rightarrow> ereal"
  1522   assumes f: "f \<in> borel_measurable M"
  1523   shows "(\<lambda>x. real_of_ereal (f x)) \<in> borel_measurable M"
  1524   apply (rule measurable_compose[OF f])
  1525   apply (rule borel_measurable_continuous_countable_exceptions[of "{\<infinity>, -\<infinity> }"])
  1526   apply (auto intro: continuous_on_real simp: Compl_eq_Diff_UNIV)
  1527   done
  1528 
  1529 lemma borel_measurable_ereal_cases:
  1530   fixes f :: "'a \<Rightarrow> ereal"
  1531   assumes f: "f \<in> borel_measurable M"
  1532   assumes H: "(\<lambda>x. H (ereal (real_of_ereal (f x)))) \<in> borel_measurable M"
  1533   shows "(\<lambda>x. H (f x)) \<in> borel_measurable M"
  1534 proof -
  1535   let ?F = "\<lambda>x. if f x = \<infinity> then H \<infinity> else if f x = - \<infinity> then H (-\<infinity>) else H (ereal (real_of_ereal (f x)))"
  1536   { fix x have "H (f x) = ?F x" by (cases "f x") auto }
  1537   with f H show ?thesis by simp
  1538 qed
  1539 
  1540 lemma
  1541   fixes f :: "'a \<Rightarrow> ereal" assumes f[measurable]: "f \<in> borel_measurable M"
  1542   shows borel_measurable_ereal_abs[measurable(raw)]: "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
  1543     and borel_measurable_ereal_inverse[measurable(raw)]: "(\<lambda>x. inverse (f x) :: ereal) \<in> borel_measurable M"
  1544     and borel_measurable_uminus_ereal[measurable(raw)]: "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
  1545   by (auto simp del: abs_real_of_ereal simp: borel_measurable_ereal_cases[OF f] measurable_If)
  1546 
  1547 lemma borel_measurable_uminus_eq_ereal[simp]:
  1548   "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
  1549 proof
  1550   assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
  1551 qed auto
  1552 
  1553 lemma set_Collect_ereal2:
  1554   fixes f g :: "'a \<Rightarrow> ereal"
  1555   assumes f: "f \<in> borel_measurable M"
  1556   assumes g: "g \<in> borel_measurable M"
  1557   assumes H: "{x \<in> space M. H (ereal (real_of_ereal (f x))) (ereal (real_of_ereal (g x)))} \<in> sets M"
  1558     "{x \<in> space borel. H (-\<infinity>) (ereal x)} \<in> sets borel"
  1559     "{x \<in> space borel. H (\<infinity>) (ereal x)} \<in> sets borel"
  1560     "{x \<in> space borel. H (ereal x) (-\<infinity>)} \<in> sets borel"
  1561     "{x \<in> space borel. H (ereal x) (\<infinity>)} \<in> sets borel"
  1562   shows "{x \<in> space M. H (f x) (g x)} \<in> sets M"
  1563 proof -
  1564   let ?G = "\<lambda>y x. if g x = \<infinity> then H y \<infinity> else if g x = -\<infinity> then H y (-\<infinity>) else H y (ereal (real_of_ereal (g x)))"
  1565   let ?F = "\<lambda>x. if f x = \<infinity> then ?G \<infinity> x else if f x = -\<infinity> then ?G (-\<infinity>) x else ?G (ereal (real_of_ereal (f x))) x"
  1566   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
  1567   note * = this
  1568   from assms show ?thesis
  1569     by (subst *) (simp del: space_borel split del: if_split)
  1570 qed
  1571 
  1572 lemma borel_measurable_ereal_iff:
  1573   shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
  1574 proof
  1575   assume "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
  1576   from borel_measurable_real_of_ereal[OF this]
  1577   show "f \<in> borel_measurable M" by auto
  1578 qed auto
  1579 
  1580 lemma borel_measurable_erealD[measurable_dest]:
  1581   "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<Longrightarrow> g \<in> measurable N M \<Longrightarrow> (\<lambda>x. f (g x)) \<in> borel_measurable N"
  1582   unfolding borel_measurable_ereal_iff by simp
  1583 
  1584 lemma borel_measurable_ereal_iff_real:
  1585   fixes f :: "'a \<Rightarrow> ereal"
  1586   shows "f \<in> borel_measurable M \<longleftrightarrow>
  1587     ((\<lambda>x. real_of_ereal (f x)) \<in> borel_measurable M \<and> f -` {\<infinity>} \<inter> space M \<in> sets M \<and> f -` {-\<infinity>} \<inter> space M \<in> sets M)"
  1588 proof safe
  1589   assume *: "(\<lambda>x. real_of_ereal (f x)) \<in> borel_measurable M" "f -` {\<infinity>} \<inter> space M \<in> sets M" "f -` {-\<infinity>} \<inter> space M \<in> sets M"
  1590   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
  1591   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
  1592   let ?f = "\<lambda>x. if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else ereal (real_of_ereal (f x))"
  1593   have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
  1594   also have "?f = f" by (auto simp: fun_eq_iff ereal_real)
  1595   finally show "f \<in> borel_measurable M" .
  1596 qed simp_all
  1597 
  1598 lemma borel_measurable_ereal_iff_Iio:
  1599   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
  1600   by (auto simp: borel_Iio measurable_iff_measure_of)
  1601 
  1602 lemma borel_measurable_ereal_iff_Ioi:
  1603   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
  1604   by (auto simp: borel_Ioi measurable_iff_measure_of)
  1605 
  1606 lemma vimage_sets_compl_iff:
  1607   "f -` A \<inter> space M \<in> sets M \<longleftrightarrow> f -` (- A) \<inter> space M \<in> sets M"
  1608 proof -
  1609   { fix A assume "f -` A \<inter> space M \<in> sets M"
  1610     moreover have "f -` (- A) \<inter> space M = space M - f -` A \<inter> space M" by auto
  1611     ultimately have "f -` (- A) \<inter> space M \<in> sets M" by auto }
  1612   from this[of A] this[of "-A"] show ?thesis
  1613     by (metis double_complement)
  1614 qed
  1615 
  1616 lemma borel_measurable_iff_Iic_ereal:
  1617   "(f::'a\<Rightarrow>ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
  1618   unfolding borel_measurable_ereal_iff_Ioi vimage_sets_compl_iff[where A="{a <..}" for a] by simp
  1619 
  1620 lemma borel_measurable_iff_Ici_ereal:
  1621   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
  1622   unfolding borel_measurable_ereal_iff_Iio vimage_sets_compl_iff[where A="{..< a}" for a] by simp
  1623 
  1624 lemma borel_measurable_ereal2:
  1625   fixes f g :: "'a \<Rightarrow> ereal"
  1626   assumes f: "f \<in> borel_measurable M"
  1627   assumes g: "g \<in> borel_measurable M"
  1628   assumes H: "(\<lambda>x. H (ereal (real_of_ereal (f x))) (ereal (real_of_ereal (g x)))) \<in> borel_measurable M"
  1629     "(\<lambda>x. H (-\<infinity>) (ereal (real_of_ereal (g x)))) \<in> borel_measurable M"
  1630     "(\<lambda>x. H (\<infinity>) (ereal (real_of_ereal (g x)))) \<in> borel_measurable M"
  1631     "(\<lambda>x. H (ereal (real_of_ereal (f x))) (-\<infinity>)) \<in> borel_measurable M"
  1632     "(\<lambda>x. H (ereal (real_of_ereal (f x))) (\<infinity>)) \<in> borel_measurable M"
  1633   shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
  1634 proof -
  1635   let ?G = "\<lambda>y x. if g x = \<infinity> then H y \<infinity> else if g x = - \<infinity> then H y (-\<infinity>) else H y (ereal (real_of_ereal (g x)))"
  1636   let ?F = "\<lambda>x. if f x = \<infinity> then ?G \<infinity> x else if f x = - \<infinity> then ?G (-\<infinity>) x else ?G (ereal (real_of_ereal (f x))) x"
  1637   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
  1638   note * = this
  1639   from assms show ?thesis unfolding * by simp
  1640 qed
  1641 
  1642 lemma [measurable(raw)]:
  1643   fixes f :: "'a \<Rightarrow> ereal"
  1644   assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1645   shows borel_measurable_ereal_add: "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  1646     and borel_measurable_ereal_times: "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1647   by (simp_all add: borel_measurable_ereal2)
  1648 
  1649 lemma [measurable(raw)]:
  1650   fixes f g :: "'a \<Rightarrow> ereal"
  1651   assumes "f \<in> borel_measurable M"
  1652   assumes "g \<in> borel_measurable M"
  1653   shows borel_measurable_ereal_diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1654     and borel_measurable_ereal_divide: "(\<lambda>x. f x / g x) \<in> borel_measurable M"
  1655   using assms by (simp_all add: minus_ereal_def divide_ereal_def)
  1656 
  1657 lemma borel_measurable_ereal_sum[measurable (raw)]:
  1658   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1659   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1660   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
  1661   using assms by (induction S rule: infinite_finite_induct) auto
  1662 
  1663 lemma borel_measurable_ereal_prod[measurable (raw)]:
  1664   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1665   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1666   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1667   using assms by (induction S rule: infinite_finite_induct) auto
  1668 
  1669 lemma borel_measurable_extreal_suminf[measurable (raw)]:
  1670   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1671   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1672   shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
  1673   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1674 
  1675 subsection "Borel space on the extended non-negative reals"
  1676 
  1677 text \<open> @{type ennreal} is a topological monoid, so no rules for plus are required, also all order
  1678   statements are usually done on type classes. \<close>
  1679 
  1680 lemma measurable_enn2ereal[measurable]: "enn2ereal \<in> borel \<rightarrow>\<^sub>M borel"
  1681   by (intro borel_measurable_continuous_on1 continuous_on_enn2ereal)
  1682 
  1683 lemma measurable_e2ennreal[measurable]: "e2ennreal \<in> borel \<rightarrow>\<^sub>M borel"
  1684   by (intro borel_measurable_continuous_on1 continuous_on_e2ennreal)
  1685 
  1686 lemma borel_measurable_enn2real[measurable (raw)]:
  1687   "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. enn2real (f x)) \<in> M \<rightarrow>\<^sub>M borel"
  1688   unfolding enn2real_def[abs_def] by measurable
  1689 
  1690 definition [simp]: "is_borel f M \<longleftrightarrow> f \<in> borel_measurable M"
  1691 
  1692 lemma is_borel_transfer[transfer_rule]: "rel_fun (rel_fun op = pcr_ennreal) op = is_borel is_borel"
  1693   unfolding is_borel_def[abs_def]
  1694 proof (safe intro!: rel_funI ext dest!: rel_fun_eq_pcr_ennreal[THEN iffD1])
  1695   fix f and M :: "'a measure"
  1696   show "f \<in> borel_measurable M" if f: "enn2ereal \<circ> f \<in> borel_measurable M"
  1697     using measurable_compose[OF f measurable_e2ennreal] by simp
  1698 qed simp
  1699 
  1700 context
  1701   includes ennreal.lifting
  1702 begin
  1703 
  1704 lemma measurable_ennreal[measurable]: "ennreal \<in> borel \<rightarrow>\<^sub>M borel"
  1705   unfolding is_borel_def[symmetric]
  1706   by transfer simp
  1707 
  1708 lemma borel_measurable_ennreal_iff[simp]:
  1709   assumes [simp]: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  1710   shows "(\<lambda>x. ennreal (f x)) \<in> M \<rightarrow>\<^sub>M borel \<longleftrightarrow> f \<in> M \<rightarrow>\<^sub>M borel"
  1711 proof safe
  1712   assume "(\<lambda>x. ennreal (f x)) \<in> M \<rightarrow>\<^sub>M borel"
  1713   then have "(\<lambda>x. enn2real (ennreal (f x))) \<in> M \<rightarrow>\<^sub>M borel"
  1714     by measurable
  1715   then show "f \<in> M \<rightarrow>\<^sub>M borel"
  1716     by (rule measurable_cong[THEN iffD1, rotated]) auto
  1717 qed measurable
  1718 
  1719 lemma borel_measurable_times_ennreal[measurable (raw)]:
  1720   fixes f g :: "'a \<Rightarrow> ennreal"
  1721   shows "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> g \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. f x * g x) \<in> M \<rightarrow>\<^sub>M borel"
  1722   unfolding is_borel_def[symmetric] by transfer simp
  1723 
  1724 lemma borel_measurable_inverse_ennreal[measurable (raw)]:
  1725   fixes f :: "'a \<Rightarrow> ennreal"
  1726   shows "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. inverse (f x)) \<in> M \<rightarrow>\<^sub>M borel"
  1727   unfolding is_borel_def[symmetric] by transfer simp
  1728 
  1729 lemma borel_measurable_divide_ennreal[measurable (raw)]:
  1730   fixes f :: "'a \<Rightarrow> ennreal"
  1731   shows "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> g \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. f x / g x) \<in> M \<rightarrow>\<^sub>M borel"
  1732   unfolding divide_ennreal_def by simp
  1733 
  1734 lemma borel_measurable_minus_ennreal[measurable (raw)]:
  1735   fixes f :: "'a \<Rightarrow> ennreal"
  1736   shows "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> g \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. f x - g x) \<in> M \<rightarrow>\<^sub>M borel"
  1737   unfolding is_borel_def[symmetric] by transfer simp
  1738 
  1739 lemma borel_measurable_prod_ennreal[measurable (raw)]:
  1740   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ennreal"
  1741   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1742   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1743   using assms by (induction S rule: infinite_finite_induct) auto
  1744 
  1745 end
  1746 
  1747 hide_const (open) is_borel
  1748 
  1749 subsection \<open>LIMSEQ is borel measurable\<close>
  1750 
  1751 lemma borel_measurable_LIMSEQ_real:
  1752   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1753   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) \<longlonglongrightarrow> u' x"
  1754   and u: "\<And>i. u i \<in> borel_measurable M"
  1755   shows "u' \<in> borel_measurable M"
  1756 proof -
  1757   have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. ereal (u n x)) = ereal (u' x)"
  1758     using u' by (simp add: lim_imp_Liminf)
  1759   moreover from u have "(\<lambda>x. liminf (\<lambda>n. ereal (u n x))) \<in> borel_measurable M"
  1760     by auto
  1761   ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_ereal_iff)
  1762 qed
  1763 
  1764 lemma borel_measurable_LIMSEQ_metric:
  1765   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b :: metric_space"
  1766   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1767   assumes lim: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. f i x) \<longlonglongrightarrow> g x"
  1768   shows "g \<in> borel_measurable M"
  1769   unfolding borel_eq_closed
  1770 proof (safe intro!: measurable_measure_of)
  1771   fix A :: "'b set" assume "closed A"
  1772 
  1773   have [measurable]: "(\<lambda>x. infdist (g x) A) \<in> borel_measurable M"
  1774   proof (rule borel_measurable_LIMSEQ_real)
  1775     show "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. infdist (f i x) A) \<longlonglongrightarrow> infdist (g x) A"
  1776       by (intro tendsto_infdist lim)
  1777     show "\<And>i. (\<lambda>x. infdist (f i x) A) \<in> borel_measurable M"
  1778       by (intro borel_measurable_continuous_on[where f="\<lambda>x. infdist x A"]
  1779         continuous_at_imp_continuous_on ballI continuous_infdist continuous_ident) auto
  1780   qed
  1781 
  1782   show "g -` A \<inter> space M \<in> sets M"
  1783   proof cases
  1784     assume "A \<noteq> {}"
  1785     then have "\<And>x. infdist x A = 0 \<longleftrightarrow> x \<in> A"
  1786       using \<open>closed A\<close> by (simp add: in_closed_iff_infdist_zero)
  1787     then have "g -` A \<inter> space M = {x\<in>space M. infdist (g x) A = 0}"
  1788       by auto
  1789     also have "\<dots> \<in> sets M"
  1790       by measurable
  1791     finally show ?thesis .
  1792   qed simp
  1793 qed auto
  1794 
  1795 lemma sets_Collect_Cauchy[measurable]:
  1796   fixes f :: "nat \<Rightarrow> 'a => 'b::{metric_space, second_countable_topology}"
  1797   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1798   shows "{x\<in>space M. Cauchy (\<lambda>i. f i x)} \<in> sets M"
  1799   unfolding metric_Cauchy_iff2 using f by auto
  1800 
  1801 lemma borel_measurable_lim_metric[measurable (raw)]:
  1802   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1803   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1804   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
  1805 proof -
  1806   define u' where "u' x = lim (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)" for x
  1807   then have *: "\<And>x. lim (\<lambda>i. f i x) = (if Cauchy (\<lambda>i. f i x) then u' x else (THE x. False))"
  1808     by (auto simp: lim_def convergent_eq_Cauchy[symmetric])
  1809   have "u' \<in> borel_measurable M"
  1810   proof (rule borel_measurable_LIMSEQ_metric)
  1811     fix x
  1812     have "convergent (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
  1813       by (cases "Cauchy (\<lambda>i. f i x)")
  1814          (auto simp add: convergent_eq_Cauchy[symmetric] convergent_def)
  1815     then show "(\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0) \<longlonglongrightarrow> u' x"
  1816       unfolding u'_def
  1817       by (rule convergent_LIMSEQ_iff[THEN iffD1])
  1818   qed measurable
  1819   then show ?thesis
  1820     unfolding * by measurable
  1821 qed
  1822 
  1823 lemma borel_measurable_suminf[measurable (raw)]:
  1824   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1825   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1826   shows "(\<lambda>x. suminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1827   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1828 
  1829 lemma Collect_closed_imp_pred_borel: "closed {x. P x} \<Longrightarrow> Measurable.pred borel P"
  1830   by (simp add: pred_def)
  1831 
  1832 (* Proof by Jeremy Avigad and Luke Serafin *)
  1833 lemma isCont_borel_pred[measurable]:
  1834   fixes f :: "'b::metric_space \<Rightarrow> 'a::metric_space"
  1835   shows "Measurable.pred borel (isCont f)"
  1836 proof (subst measurable_cong)
  1837   let ?I = "\<lambda>j. inverse(real (Suc j))"
  1838   show "isCont f x = (\<forall>i. \<exists>j. \<forall>y z. dist x y < ?I j \<and> dist x z < ?I j \<longrightarrow> dist (f y) (f z) \<le> ?I i)" for x
  1839     unfolding continuous_at_eps_delta
  1840   proof safe
  1841     fix i assume "\<forall>e>0. \<exists>d>0. \<forall>y. dist y x < d \<longrightarrow> dist (f y) (f x) < e"
  1842     moreover have "0 < ?I i / 2"
  1843       by simp
  1844     ultimately obtain d where d: "0 < d" "\<And>y. dist x y < d \<Longrightarrow> dist (f y) (f x) < ?I i / 2"
  1845       by (metis dist_commute)
  1846     then obtain j where j: "?I j < d"
  1847       by (metis reals_Archimedean)
  1848 
  1849     show "\<exists>j. \<forall>y z. dist x y < ?I j \<and> dist x z < ?I j \<longrightarrow> dist (f y) (f z) \<le> ?I i"
  1850     proof (safe intro!: exI[where x=j])
  1851       fix y z assume *: "dist x y < ?I j" "dist x z < ?I j"
  1852       have "dist (f y) (f z) \<le> dist (f y) (f x) + dist (f z) (f x)"
  1853         by (rule dist_triangle2)
  1854       also have "\<dots> < ?I i / 2 + ?I i / 2"
  1855         by (intro add_strict_mono d less_trans[OF _ j] *)
  1856       also have "\<dots> \<le> ?I i"
  1857         by (simp add: field_simps of_nat_Suc)
  1858       finally show "dist (f y) (f z) \<le> ?I i"
  1859         by simp
  1860     qed
  1861   next
  1862     fix e::real assume "0 < e"
  1863     then obtain n where n: "?I n < e"
  1864       by (metis reals_Archimedean)
  1865     assume "\<forall>i. \<exists>j. \<forall>y z. dist x y < ?I j \<and> dist x z < ?I j \<longrightarrow> dist (f y) (f z) \<le> ?I i"
  1866     from this[THEN spec, of "Suc n"]
  1867     obtain j where j: "\<And>y z. dist x y < ?I j \<Longrightarrow> dist x z < ?I j \<Longrightarrow> dist (f y) (f z) \<le> ?I (Suc n)"
  1868       by auto
  1869 
  1870     show "\<exists>d>0. \<forall>y. dist y x < d \<longrightarrow> dist (f y) (f x) < e"
  1871     proof (safe intro!: exI[of _ "?I j"])
  1872       fix y assume "dist y x < ?I j"
  1873       then have "dist (f y) (f x) \<le> ?I (Suc n)"
  1874         by (intro j) (auto simp: dist_commute)
  1875       also have "?I (Suc n) < ?I n"
  1876         by simp
  1877       also note n
  1878       finally show "dist (f y) (f x) < e" .
  1879     qed simp
  1880   qed
  1881 qed (intro pred_intros_countable closed_Collect_all closed_Collect_le open_Collect_less
  1882            Collect_closed_imp_pred_borel closed_Collect_imp open_Collect_conj continuous_intros)
  1883 
  1884 lemma isCont_borel:
  1885   fixes f :: "'b::metric_space \<Rightarrow> 'a::metric_space"
  1886   shows "{x. isCont f x} \<in> sets borel"
  1887   by simp
  1888 
  1889 lemma is_real_interval:
  1890   assumes S: "is_interval S"
  1891   shows "\<exists>a b::real. S = {} \<or> S = UNIV \<or> S = {..<b} \<or> S = {..b} \<or> S = {a<..} \<or> S = {a..} \<or>
  1892     S = {a<..<b} \<or> S = {a<..b} \<or> S = {a..<b} \<or> S = {a..b}"
  1893   using S unfolding is_interval_1 by (blast intro: interval_cases)
  1894 
  1895 lemma real_interval_borel_measurable:
  1896   assumes "is_interval (S::real set)"
  1897   shows "S \<in> sets borel"
  1898 proof -
  1899   from assms is_real_interval have "\<exists>a b::real. S = {} \<or> S = UNIV \<or> S = {..<b} \<or> S = {..b} \<or>
  1900     S = {a<..} \<or> S = {a..} \<or> S = {a<..<b} \<or> S = {a<..b} \<or> S = {a..<b} \<or> S = {a..b}" by auto
  1901   then guess a ..
  1902   then guess b ..
  1903   thus ?thesis
  1904     by auto
  1905 qed
  1906 
  1907 text \<open>The next lemmas hold in any second countable linorder (including ennreal or ereal for instance),
  1908 but in the current state they are restricted to reals.\<close>
  1909 
  1910 lemma borel_measurable_mono_on_fnc:
  1911   fixes f :: "real \<Rightarrow> real" and A :: "real set"
  1912   assumes "mono_on f A"
  1913   shows "f \<in> borel_measurable (restrict_space borel A)"
  1914   apply (rule measurable_restrict_countable[OF mono_on_ctble_discont[OF assms]])
  1915   apply (auto intro!: image_eqI[where x="{x}" for x] simp: sets_restrict_space)
  1916   apply (auto simp add: sets_restrict_restrict_space continuous_on_eq_continuous_within
  1917               cong: measurable_cong_sets
  1918               intro!: borel_measurable_continuous_on_restrict intro: continuous_within_subset)
  1919   done
  1920 
  1921 lemma borel_measurable_piecewise_mono:
  1922   fixes f::"real \<Rightarrow> real" and C::"real set set"
  1923   assumes "countable C" "\<And>c. c \<in> C \<Longrightarrow> c \<in> sets borel" "\<And>c. c \<in> C \<Longrightarrow> mono_on f c" "(\<Union>C) = UNIV"
  1924   shows "f \<in> borel_measurable borel"
  1925 by (rule measurable_piecewise_restrict[of C], auto intro: borel_measurable_mono_on_fnc simp: assms)
  1926 
  1927 lemma borel_measurable_mono:
  1928   fixes f :: "real \<Rightarrow> real"
  1929   shows "mono f \<Longrightarrow> f \<in> borel_measurable borel"
  1930   using borel_measurable_mono_on_fnc[of f UNIV] by (simp add: mono_def mono_on_def)
  1931 
  1932 lemma measurable_bdd_below_real[measurable (raw)]:
  1933   fixes F :: "'a \<Rightarrow> 'i \<Rightarrow> real"
  1934   assumes [simp]: "countable I" and [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> M \<rightarrow>\<^sub>M borel"
  1935   shows "Measurable.pred M (\<lambda>x. bdd_below ((\<lambda>i. F i x)`I))"
  1936 proof (subst measurable_cong)
  1937   show "bdd_below ((\<lambda>i. F i x)`I) \<longleftrightarrow> (\<exists>q\<in>\<int>. \<forall>i\<in>I. q \<le> F i x)" for x
  1938     by (auto simp: bdd_below_def intro!: bexI[of _ "of_int (floor _)"] intro: order_trans of_int_floor_le)
  1939   show "Measurable.pred M (\<lambda>w. \<exists>q\<in>\<int>. \<forall>i\<in>I. q \<le> F i w)"
  1940     using countable_int by measurable
  1941 qed
  1942 
  1943 lemma borel_measurable_cINF_real[measurable (raw)]:
  1944   fixes F :: "_ \<Rightarrow> _ \<Rightarrow> real"
  1945   assumes [simp]: "countable I"
  1946   assumes F[measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
  1947   shows "(\<lambda>x. INF i:I. F i x) \<in> borel_measurable M"
  1948 proof (rule measurable_piecewise_restrict)
  1949   let ?\<Omega> = "{x\<in>space M. bdd_below ((\<lambda>i. F i x)`I)}"
  1950   show "countable {?\<Omega>, - ?\<Omega>}" "space M \<subseteq> \<Union>{?\<Omega>, - ?\<Omega>}" "\<And>X. X \<in> {?\<Omega>, - ?\<Omega>} \<Longrightarrow> X \<inter> space M \<in> sets M"
  1951     by auto
  1952   fix X assume "X \<in> {?\<Omega>, - ?\<Omega>}" then show "(\<lambda>x. INF i:I. F i x) \<in> borel_measurable (restrict_space M X)"
  1953   proof safe
  1954     show "(\<lambda>x. INF i:I. F i x) \<in> borel_measurable (restrict_space M ?\<Omega>)"
  1955       by (intro borel_measurable_cINF measurable_restrict_space1 F)
  1956          (auto simp: space_restrict_space)
  1957     show "(\<lambda>x. INF i:I. F i x) \<in> borel_measurable (restrict_space M (-?\<Omega>))"
  1958     proof (subst measurable_cong)
  1959       fix x assume "x \<in> space (restrict_space M (-?\<Omega>))"
  1960       then have "\<not> (\<forall>i\<in>I. - F i x \<le> y)" for y
  1961         by (auto simp: space_restrict_space bdd_above_def bdd_above_uminus[symmetric])
  1962       then show "(INF i:I. F i x) = - (THE x. False)"
  1963         by (auto simp: space_restrict_space Inf_real_def Sup_real_def Least_def simp del: Set.ball_simps(10))
  1964     qed simp
  1965   qed
  1966 qed
  1967 
  1968 lemma borel_Ici: "borel = sigma UNIV (range (\<lambda>x::real. {x ..}))"
  1969 proof (safe intro!: borel_eq_sigmaI1[OF borel_Iio])
  1970   fix x :: real
  1971   have eq: "{..<x} = space (sigma UNIV (range atLeast)) - {x ..}"
  1972     by auto
  1973   show "{..<x} \<in> sets (sigma UNIV (range atLeast))"
  1974     unfolding eq by (intro sets.compl_sets) auto
  1975 qed auto
  1976 
  1977 lemma borel_measurable_pred_less[measurable (raw)]:
  1978   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, linorder_topology}"
  1979   shows "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> Measurable.pred M (\<lambda>w. f w < g w)"
  1980   unfolding Measurable.pred_def by (rule borel_measurable_less)
  1981 
  1982 no_notation
  1983   eucl_less (infix "<e" 50)
  1984 
  1985 lemma borel_measurable_Max2[measurable (raw)]:
  1986   fixes f::"_ \<Rightarrow> _ \<Rightarrow> 'a::{second_countable_topology, dense_linorder, linorder_topology}"
  1987   assumes "finite I"
  1988     and [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1989   shows "(\<lambda>x. Max{f i x |i. i \<in> I}) \<in> borel_measurable M"
  1990 by (simp add: borel_measurable_Max[OF assms(1), where ?f=f and ?M=M] Setcompr_eq_image)
  1991 
  1992 lemma measurable_compose_n [measurable (raw)]:
  1993   assumes "T \<in> measurable M M"
  1994   shows "(T^^n) \<in> measurable M M"
  1995 by (induction n, auto simp add: measurable_compose[OF _ assms])
  1996 
  1997 lemma measurable_real_imp_nat:
  1998   fixes f::"'a \<Rightarrow> nat"
  1999   assumes [measurable]: "(\<lambda>x. real(f x)) \<in> borel_measurable M"
  2000   shows "f \<in> measurable M (count_space UNIV)"
  2001 proof -
  2002   let ?g = "(\<lambda>x. real(f x))"
  2003   have "\<And>(n::nat). ?g-`({real n}) \<inter> space M = f-`{n} \<inter> space M" by auto
  2004   moreover have "\<And>(n::nat). ?g-`({real n}) \<inter> space M \<in> sets M" using assms by measurable
  2005   ultimately have "\<And>(n::nat). f-`{n} \<inter> space M \<in> sets M" by simp
  2006   then show ?thesis using measurable_count_space_eq2_countable by blast
  2007 qed
  2008 
  2009 lemma measurable_equality_set [measurable]:
  2010   fixes f g::"_\<Rightarrow> 'a::{second_countable_topology, t2_space}"
  2011   assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  2012   shows "{x \<in> space M. f x = g x} \<in> sets M"
  2013 
  2014 proof -
  2015   define A where "A = {x \<in> space M. f x = g x}"
  2016   define B where "B = {y. \<exists>x::'a. y = (x,x)}"
  2017   have "A = (\<lambda>x. (f x, g x))-`B \<inter> space M" unfolding A_def B_def by auto
  2018   moreover have "(\<lambda>x. (f x, g x)) \<in> borel_measurable M" by simp
  2019   moreover have "B \<in> sets borel" unfolding B_def by (simp add: closed_diagonal)
  2020   ultimately have "A \<in> sets M" by simp
  2021   then show ?thesis unfolding A_def by simp
  2022 qed
  2023 
  2024 lemma measurable_inequality_set [measurable]:
  2025   fixes f g::"_ \<Rightarrow> 'a::{second_countable_topology, linorder_topology}"
  2026   assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  2027   shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
  2028         "{x \<in> space M. f x < g x} \<in> sets M"
  2029         "{x \<in> space M. f x \<ge> g x} \<in> sets M"
  2030         "{x \<in> space M. f x > g x} \<in> sets M"
  2031 proof -
  2032   define F where "F = (\<lambda>x. (f x, g x))"
  2033   have * [measurable]: "F \<in> borel_measurable M" unfolding F_def by simp
  2034 
  2035   have "{x \<in> space M. f x \<le> g x} = F-`{(x, y) | x y. x \<le> y} \<inter> space M" unfolding F_def by auto
  2036   moreover have "{(x, y) | x y. x \<le> (y::'a)} \<in> sets borel" using closed_subdiagonal borel_closed by blast
  2037   ultimately show "{x \<in> space M. f x \<le> g x} \<in> sets M" using * by (metis (mono_tags, lifting) measurable_sets)
  2038 
  2039   have "{x \<in> space M. f x < g x} = F-`{(x, y) | x y. x < y} \<inter> space M" unfolding F_def by auto
  2040   moreover have "{(x, y) | x y. x < (y::'a)} \<in> sets borel" using open_subdiagonal borel_open by blast
  2041   ultimately show "{x \<in> space M. f x < g x} \<in> sets M" using * by (metis (mono_tags, lifting) measurable_sets)
  2042 
  2043   have "{x \<in> space M. f x \<ge> g x} = F-`{(x, y) | x y. x \<ge> y} \<inter> space M" unfolding F_def by auto
  2044   moreover have "{(x, y) | x y. x \<ge> (y::'a)} \<in> sets borel" using closed_superdiagonal borel_closed by blast
  2045   ultimately show "{x \<in> space M. f x \<ge> g x} \<in> sets M" using * by (metis (mono_tags, lifting) measurable_sets)
  2046 
  2047   have "{x \<in> space M. f x > g x} = F-`{(x, y) | x y. x > y} \<inter> space M" unfolding F_def by auto
  2048   moreover have "{(x, y) | x y. x > (y::'a)} \<in> sets borel" using open_superdiagonal borel_open by blast
  2049   ultimately show "{x \<in> space M. f x > g x} \<in> sets M" using * by (metis (mono_tags, lifting) measurable_sets)
  2050 qed
  2051 
  2052 lemma measurable_limit [measurable]:
  2053   fixes f::"nat \<Rightarrow> 'a \<Rightarrow> 'b::first_countable_topology"
  2054   assumes [measurable]: "\<And>n::nat. f n \<in> borel_measurable M"
  2055   shows "Measurable.pred M (\<lambda>x. (\<lambda>n. f n x) \<longlonglongrightarrow> c)"
  2056 proof -
  2057   obtain A :: "nat \<Rightarrow> 'b set" where A:
  2058     "\<And>i. open (A i)"
  2059     "\<And>i. c \<in> A i"
  2060     "\<And>S. open S \<Longrightarrow> c \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  2061   by (rule countable_basis_at_decseq) blast
  2062 
  2063   have [measurable]: "\<And>N i. (f N)-`(A i) \<inter> space M \<in> sets M" using A(1) by auto
  2064   then have mes: "(\<Inter>i. \<Union>n. \<Inter>N\<in>{n..}. (f N)-`(A i) \<inter> space M) \<in> sets M" by blast
  2065 
  2066   have "(u \<longlonglongrightarrow> c) \<longleftrightarrow> (\<forall>i. eventually (\<lambda>n. u n \<in> A i) sequentially)" for u::"nat \<Rightarrow> 'b"
  2067   proof
  2068     assume "u \<longlonglongrightarrow> c"
  2069     then have "eventually (\<lambda>n. u n \<in> A i) sequentially" for i using A(1)[of i] A(2)[of i]
  2070       by (simp add: topological_tendstoD)
  2071     then show "(\<forall>i. eventually (\<lambda>n. u n \<in> A i) sequentially)" by auto
  2072   next
  2073     assume H: "(\<forall>i. eventually (\<lambda>n. u n \<in> A i) sequentially)"
  2074     show "(u \<longlonglongrightarrow> c)"
  2075     proof (rule topological_tendstoI)
  2076       fix S assume "open S" "c \<in> S"
  2077       with A(3)[OF this] obtain i where "A i \<subseteq> S"
  2078         using eventually_False_sequentially eventually_mono by blast
  2079       moreover have "eventually (\<lambda>n. u n \<in> A i) sequentially" using H by simp
  2080       ultimately show "\<forall>\<^sub>F n in sequentially. u n \<in> S"
  2081         by (simp add: eventually_mono subset_eq)
  2082     qed
  2083   qed
  2084   then have "{x. (\<lambda>n. f n x) \<longlonglongrightarrow> c} = (\<Inter>i. \<Union>n. \<Inter>N\<in>{n..}. (f N)-`(A i))"
  2085     by (auto simp add: atLeast_def eventually_at_top_linorder)
  2086   then have "{x \<in> space M. (\<lambda>n. f n x) \<longlonglongrightarrow> c} = (\<Inter>i. \<Union>n. \<Inter>N\<in>{n..}. (f N)-`(A i) \<inter> space M)"
  2087     by auto
  2088   then have "{x \<in> space M. (\<lambda>n. f n x) \<longlonglongrightarrow> c} \<in> sets M" using mes by simp
  2089   then show ?thesis by auto
  2090 qed
  2091 
  2092 lemma measurable_limit2 [measurable]:
  2093   fixes u::"nat \<Rightarrow> 'a \<Rightarrow> real"
  2094   assumes [measurable]: "\<And>n. u n \<in> borel_measurable M" "v \<in> borel_measurable M"
  2095   shows "Measurable.pred M (\<lambda>x. (\<lambda>n. u n x) \<longlonglongrightarrow> v x)"
  2096 proof -
  2097   define w where "w = (\<lambda>n x. u n x - v x)"
  2098   have [measurable]: "w n \<in> borel_measurable M" for n unfolding w_def by auto
  2099   have "((\<lambda>n. u n x) \<longlonglongrightarrow> v x) \<longleftrightarrow> ((\<lambda>n. w n x) \<longlonglongrightarrow> 0)" for x
  2100     unfolding w_def using Lim_null by auto
  2101   then show ?thesis using measurable_limit by auto
  2102 qed
  2103 
  2104 lemma measurable_P_restriction [measurable (raw)]:
  2105   assumes [measurable]: "Measurable.pred M P" "A \<in> sets M"
  2106   shows "{x \<in> A. P x} \<in> sets M"
  2107 proof -
  2108   have "A \<subseteq> space M" using sets.sets_into_space[OF assms(2)].
  2109   then have "{x \<in> A. P x} = A \<inter> {x \<in> space M. P x}" by blast
  2110   then show ?thesis by auto
  2111 qed
  2112 
  2113 lemma measurable_sum_nat [measurable (raw)]:
  2114   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> nat"
  2115   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> measurable M (count_space UNIV)"
  2116   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> measurable M (count_space UNIV)"
  2117 proof cases
  2118   assume "finite S"
  2119   then show ?thesis using assms by induct auto
  2120 qed simp
  2121 
  2122 
  2123 lemma measurable_abs_powr [measurable]:
  2124   fixes p::real
  2125   assumes [measurable]: "f \<in> borel_measurable M"
  2126   shows "(\<lambda>x. \<bar>f x\<bar> powr p) \<in> borel_measurable M"
  2127 unfolding powr_def by auto
  2128 
  2129 text \<open>The next one is a variation around \verb+measurable_restrict_space+.\<close>
  2130 
  2131 lemma measurable_restrict_space3:
  2132   assumes "f \<in> measurable M N" and
  2133           "f \<in> A \<rightarrow> B"
  2134   shows "f \<in> measurable (restrict_space M A) (restrict_space N B)"
  2135 proof -
  2136   have "f \<in> measurable (restrict_space M A) N" using assms(1) measurable_restrict_space1 by auto
  2137   then show ?thesis by (metis Int_iff funcsetI funcset_mem
  2138       measurable_restrict_space2[of f, of "restrict_space M A", of B, of N] assms(2) space_restrict_space)
  2139 qed
  2140 
  2141 text \<open>The next one is a variation around \verb+measurable_piecewise_restrict+.\<close>
  2142 
  2143 lemma measurable_piecewise_restrict2:
  2144   assumes [measurable]: "\<And>n. A n \<in> sets M"
  2145       and "space M = (\<Union>(n::nat). A n)"
  2146           "\<And>n. \<exists>h \<in> measurable M N. (\<forall>x \<in> A n. f x = h x)"
  2147   shows "f \<in> measurable M N"
  2148 proof (rule measurableI)
  2149   fix B assume [measurable]: "B \<in> sets N"
  2150   {
  2151     fix n::nat
  2152     obtain h where [measurable]: "h \<in> measurable M N" and "\<forall>x \<in> A n. f x = h x" using assms(3) by blast
  2153     then have *: "f-`B \<inter> A n = h-`B \<inter> A n" by auto
  2154     have "h-`B \<inter> A n = h-`B \<inter> space M \<inter> A n" using assms(2) sets.sets_into_space by auto
  2155     then have "h-`B \<inter> A n \<in> sets M" by simp
  2156     then have "f-`B \<inter> A n \<in> sets M" using * by simp
  2157   }
  2158   then have "(\<Union>n. f-`B \<inter> A n) \<in> sets M" by measurable
  2159   moreover have "f-`B \<inter> space M = (\<Union>n. f-`B \<inter> A n)" using assms(2) by blast
  2160   ultimately show "f-`B \<inter> space M \<in> sets M" by simp
  2161 next
  2162   fix x assume "x \<in> space M"
  2163   then obtain n where "x \<in> A n" using assms(2) by blast
  2164   obtain h where [measurable]: "h \<in> measurable M N" and "\<forall>x \<in> A n. f x = h x" using assms(3) by blast
  2165   then have "f x = h x" using \<open>x \<in> A n\<close> by blast
  2166   moreover have "h x \<in> space N" by (metis measurable_space \<open>x \<in> space M\<close> \<open>h \<in> measurable M N\<close>)
  2167   ultimately show "f x \<in> space N" by simp
  2168 qed
  2169 
  2170 end