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