src/HOL/Analysis/Borel_Space.thy
changeset 63627 6ddb43c6b711
parent 63626 44ce6b524ff3
child 63952 354808e9f44b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Analysis/Borel_Space.thy	Mon Aug 08 14:13:14 2016 +0200
     1.3 @@ -0,0 +1,1915 @@
     1.4 +(*  Title:      HOL/Analysis/Borel_Space.thy
     1.5 +    Author:     Johannes Hölzl, TU München
     1.6 +    Author:     Armin Heller, TU München
     1.7 +*)
     1.8 +
     1.9 +section \<open>Borel spaces\<close>
    1.10 +
    1.11 +theory Borel_Space
    1.12 +imports
    1.13 +  Measurable Derivative Ordered_Euclidean_Space Extended_Real_Limits
    1.14 +begin
    1.15 +
    1.16 +lemma sets_Collect_eventually_sequentially[measurable]:
    1.17 +  "(\<And>i. {x\<in>space M. P x i} \<in> sets M) \<Longrightarrow> {x\<in>space M. eventually (P x) sequentially} \<in> sets M"
    1.18 +  unfolding eventually_sequentially by simp
    1.19 +
    1.20 +lemma topological_basis_trivial: "topological_basis {A. open A}"
    1.21 +  by (auto simp: topological_basis_def)
    1.22 +
    1.23 +lemma open_prod_generated: "open = generate_topology {A \<times> B | A B. open A \<and> open B}"
    1.24 +proof -
    1.25 +  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}))"
    1.26 +    by auto
    1.27 +  then show ?thesis
    1.28 +    by (auto intro: topological_basis_prod topological_basis_trivial topological_basis_imp_subbasis)
    1.29 +qed
    1.30 +
    1.31 +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"
    1.32 +
    1.33 +lemma mono_onI:
    1.34 +  "(\<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"
    1.35 +  unfolding mono_on_def by simp
    1.36 +
    1.37 +lemma mono_onD:
    1.38 +  "\<lbrakk>mono_on f A; r \<in> A; s \<in> A; r \<le> s\<rbrakk> \<Longrightarrow> f r \<le> f s"
    1.39 +  unfolding mono_on_def by simp
    1.40 +
    1.41 +lemma mono_imp_mono_on: "mono f \<Longrightarrow> mono_on f A"
    1.42 +  unfolding mono_def mono_on_def by auto
    1.43 +
    1.44 +lemma mono_on_subset: "mono_on f A \<Longrightarrow> B \<subseteq> A \<Longrightarrow> mono_on f B"
    1.45 +  unfolding mono_on_def by auto
    1.46 +
    1.47 +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"
    1.48 +
    1.49 +lemma strict_mono_onI:
    1.50 +  "(\<And>r s. r \<in> A \<Longrightarrow> s \<in> A \<Longrightarrow> r < s \<Longrightarrow> f r < f s) \<Longrightarrow> strict_mono_on f A"
    1.51 +  unfolding strict_mono_on_def by simp
    1.52 +
    1.53 +lemma strict_mono_onD:
    1.54 +  "\<lbrakk>strict_mono_on f A; r \<in> A; s \<in> A; r < s\<rbrakk> \<Longrightarrow> f r < f s"
    1.55 +  unfolding strict_mono_on_def by simp
    1.56 +
    1.57 +lemma mono_on_greaterD:
    1.58 +  assumes "mono_on g A" "x \<in> A" "y \<in> A" "g x > (g (y::_::linorder) :: _ :: linorder)"
    1.59 +  shows "x > y"
    1.60 +proof (rule ccontr)
    1.61 +  assume "\<not>x > y"
    1.62 +  hence "x \<le> y" by (simp add: not_less)
    1.63 +  from assms(1-3) and this have "g x \<le> g y" by (rule mono_onD)
    1.64 +  with assms(4) show False by simp
    1.65 +qed
    1.66 +
    1.67 +lemma strict_mono_inv:
    1.68 +  fixes f :: "('a::linorder) \<Rightarrow> ('b::linorder)"
    1.69 +  assumes "strict_mono f" and "surj f" and inv: "\<And>x. g (f x) = x"
    1.70 +  shows "strict_mono g"
    1.71 +proof
    1.72 +  fix x y :: 'b assume "x < y"
    1.73 +  from \<open>surj f\<close> obtain x' y' where [simp]: "x = f x'" "y = f y'" by blast
    1.74 +  with \<open>x < y\<close> and \<open>strict_mono f\<close> have "x' < y'" by (simp add: strict_mono_less)
    1.75 +  with inv show "g x < g y" by simp
    1.76 +qed
    1.77 +
    1.78 +lemma strict_mono_on_imp_inj_on:
    1.79 +  assumes "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> (_ :: preorder)) A"
    1.80 +  shows "inj_on f A"
    1.81 +proof (rule inj_onI)
    1.82 +  fix x y assume "x \<in> A" "y \<in> A" "f x = f y"
    1.83 +  thus "x = y"
    1.84 +    by (cases x y rule: linorder_cases)
    1.85 +       (auto dest: strict_mono_onD[OF assms, of x y] strict_mono_onD[OF assms, of y x])
    1.86 +qed
    1.87 +
    1.88 +lemma strict_mono_on_leD:
    1.89 +  assumes "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> _ :: preorder) A" "x \<in> A" "y \<in> A" "x \<le> y"
    1.90 +  shows "f x \<le> f y"
    1.91 +proof (insert le_less_linear[of y x], elim disjE)
    1.92 +  assume "x < y"
    1.93 +  with assms have "f x < f y" by (rule_tac strict_mono_onD[OF assms(1)]) simp_all
    1.94 +  thus ?thesis by (rule less_imp_le)
    1.95 +qed (insert assms, simp)
    1.96 +
    1.97 +lemma strict_mono_on_eqD:
    1.98 +  fixes f :: "(_ :: linorder) \<Rightarrow> (_ :: preorder)"
    1.99 +  assumes "strict_mono_on f A" "f x = f y" "x \<in> A" "y \<in> A"
   1.100 +  shows "y = x"
   1.101 +  using assms by (rule_tac linorder_cases[of x y]) (auto dest: strict_mono_onD)
   1.102 +
   1.103 +lemma mono_on_imp_deriv_nonneg:
   1.104 +  assumes mono: "mono_on f A" and deriv: "(f has_real_derivative D) (at x)"
   1.105 +  assumes "x \<in> interior A"
   1.106 +  shows "D \<ge> 0"
   1.107 +proof (rule tendsto_le_const)
   1.108 +  let ?A' = "(\<lambda>y. y - x) ` interior A"
   1.109 +  from deriv show "((\<lambda>h. (f (x + h) - f x) / h) \<longlongrightarrow> D) (at 0)"
   1.110 +      by (simp add: field_has_derivative_at has_field_derivative_def)
   1.111 +  from mono have mono': "mono_on f (interior A)" by (rule mono_on_subset) (rule interior_subset)
   1.112 +
   1.113 +  show "eventually (\<lambda>h. (f (x + h) - f x) / h \<ge> 0) (at 0)"
   1.114 +  proof (subst eventually_at_topological, intro exI conjI ballI impI)
   1.115 +    have "open (interior A)" by simp
   1.116 +    hence "open (op + (-x) ` interior A)" by (rule open_translation)
   1.117 +    also have "(op + (-x) ` interior A) = ?A'" by auto
   1.118 +    finally show "open ?A'" .
   1.119 +  next
   1.120 +    from \<open>x \<in> interior A\<close> show "0 \<in> ?A'" by auto
   1.121 +  next
   1.122 +    fix h assume "h \<in> ?A'"
   1.123 +    hence "x + h \<in> interior A" by auto
   1.124 +    with mono' and \<open>x \<in> interior A\<close> show "(f (x + h) - f x) / h \<ge> 0"
   1.125 +      by (cases h rule: linorder_cases[of _ 0])
   1.126 +         (simp_all add: divide_nonpos_neg divide_nonneg_pos mono_onD field_simps)
   1.127 +  qed
   1.128 +qed simp
   1.129 +
   1.130 +lemma strict_mono_on_imp_mono_on:
   1.131 +  "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> _ :: preorder) A \<Longrightarrow> mono_on f A"
   1.132 +  by (rule mono_onI, rule strict_mono_on_leD)
   1.133 +
   1.134 +lemma mono_on_ctble_discont:
   1.135 +  fixes f :: "real \<Rightarrow> real"
   1.136 +  fixes A :: "real set"
   1.137 +  assumes "mono_on f A"
   1.138 +  shows "countable {a\<in>A. \<not> continuous (at a within A) f}"
   1.139 +proof -
   1.140 +  have mono: "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
   1.141 +    using \<open>mono_on f A\<close> by (simp add: mono_on_def)
   1.142 +  have "\<forall>a \<in> {a\<in>A. \<not> continuous (at a within A) f}. \<exists>q :: nat \<times> rat.
   1.143 +      (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>
   1.144 +      (fst q = 1 \<and> of_rat (snd q) > f a \<and> (\<forall>x \<in> A. x > a \<longrightarrow> f x > of_rat (snd q)))"
   1.145 +  proof (clarsimp simp del: One_nat_def)
   1.146 +    fix a assume "a \<in> A" assume "\<not> continuous (at a within A) f"
   1.147 +    thus "\<exists>q1 q2.
   1.148 +            q1 = 0 \<and> real_of_rat q2 < f a \<and> (\<forall>x\<in>A. x < a \<longrightarrow> f x < real_of_rat q2) \<or>
   1.149 +            q1 = 1 \<and> f a < real_of_rat q2 \<and> (\<forall>x\<in>A. a < x \<longrightarrow> real_of_rat q2 < f x)"
   1.150 +    proof (auto simp add: continuous_within order_tendsto_iff eventually_at)
   1.151 +      fix l assume "l < f a"
   1.152 +      then obtain q2 where q2: "l < of_rat q2" "of_rat q2 < f a"
   1.153 +        using of_rat_dense by blast
   1.154 +      assume * [rule_format]: "\<forall>d>0. \<exists>x\<in>A. x \<noteq> a \<and> dist x a < d \<and> \<not> l < f x"
   1.155 +      from q2 have "real_of_rat q2 < f a \<and> (\<forall>x\<in>A. x < a \<longrightarrow> f x < real_of_rat q2)"
   1.156 +      proof auto
   1.157 +        fix x assume "x \<in> A" "x < a"
   1.158 +        with q2 *[of "a - x"] show "f x < real_of_rat q2"
   1.159 +          apply (auto simp add: dist_real_def not_less)
   1.160 +          apply (subgoal_tac "f x \<le> f xa")
   1.161 +          by (auto intro: mono)
   1.162 +      qed
   1.163 +      thus ?thesis by auto
   1.164 +    next
   1.165 +      fix u assume "u > f a"
   1.166 +      then obtain q2 where q2: "f a < of_rat q2" "of_rat q2 < u"
   1.167 +        using of_rat_dense by blast
   1.168 +      assume *[rule_format]: "\<forall>d>0. \<exists>x\<in>A. x \<noteq> a \<and> dist x a < d \<and> \<not> u > f x"
   1.169 +      from q2 have "real_of_rat q2 > f a \<and> (\<forall>x\<in>A. x > a \<longrightarrow> f x > real_of_rat q2)"
   1.170 +      proof auto
   1.171 +        fix x assume "x \<in> A" "x > a"
   1.172 +        with q2 *[of "x - a"] show "f x > real_of_rat q2"
   1.173 +          apply (auto simp add: dist_real_def)
   1.174 +          apply (subgoal_tac "f x \<ge> f xa")
   1.175 +          by (auto intro: mono)
   1.176 +      qed
   1.177 +      thus ?thesis by auto
   1.178 +    qed
   1.179 +  qed
   1.180 +  hence "\<exists>g :: real \<Rightarrow> nat \<times> rat . \<forall>a \<in> {a\<in>A. \<not> continuous (at a within A) f}.
   1.181 +      (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)))) |
   1.182 +      (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))))"
   1.183 +    by (rule bchoice)
   1.184 +  then guess g ..
   1.185 +  hence g: "\<And>a x. a \<in> A \<Longrightarrow> \<not> continuous (at a within A) f \<Longrightarrow> x \<in> A \<Longrightarrow>
   1.186 +      (fst (g a) = 0 \<and> of_rat (snd (g a)) < f a \<and> (x < a \<longrightarrow> f x < of_rat (snd (g a)))) |
   1.187 +      (fst (g a) = 1 \<and> of_rat (snd (g a)) > f a \<and> (x > a \<longrightarrow> f x > of_rat (snd (g a))))"
   1.188 +    by auto
   1.189 +  have "inj_on g {a\<in>A. \<not> continuous (at a within A) f}"
   1.190 +  proof (auto simp add: inj_on_def)
   1.191 +    fix w z
   1.192 +    assume 1: "w \<in> A" and 2: "\<not> continuous (at w within A) f" and
   1.193 +           3: "z \<in> A" and 4: "\<not> continuous (at z within A) f" and
   1.194 +           5: "g w = g z"
   1.195 +    from g [OF 1 2 3] g [OF 3 4 1] 5
   1.196 +    show "w = z" by auto
   1.197 +  qed
   1.198 +  thus ?thesis
   1.199 +    by (rule countableI')
   1.200 +qed
   1.201 +
   1.202 +lemma mono_on_ctble_discont_open:
   1.203 +  fixes f :: "real \<Rightarrow> real"
   1.204 +  fixes A :: "real set"
   1.205 +  assumes "open A" "mono_on f A"
   1.206 +  shows "countable {a\<in>A. \<not>isCont f a}"
   1.207 +proof -
   1.208 +  have "{a\<in>A. \<not>isCont f a} = {a\<in>A. \<not>(continuous (at a within A) f)}"
   1.209 +    by (auto simp add: continuous_within_open [OF _ \<open>open A\<close>])
   1.210 +  thus ?thesis
   1.211 +    apply (elim ssubst)
   1.212 +    by (rule mono_on_ctble_discont, rule assms)
   1.213 +qed
   1.214 +
   1.215 +lemma mono_ctble_discont:
   1.216 +  fixes f :: "real \<Rightarrow> real"
   1.217 +  assumes "mono f"
   1.218 +  shows "countable {a. \<not> isCont f a}"
   1.219 +using assms mono_on_ctble_discont [of f UNIV] unfolding mono_on_def mono_def by auto
   1.220 +
   1.221 +lemma has_real_derivative_imp_continuous_on:
   1.222 +  assumes "\<And>x. x \<in> A \<Longrightarrow> (f has_real_derivative f' x) (at x)"
   1.223 +  shows "continuous_on A f"
   1.224 +  apply (intro differentiable_imp_continuous_on, unfold differentiable_on_def)
   1.225 +  apply (intro ballI Deriv.differentiableI)
   1.226 +  apply (rule has_field_derivative_subset[OF assms])
   1.227 +  apply simp_all
   1.228 +  done
   1.229 +
   1.230 +lemma closure_contains_Sup:
   1.231 +  fixes S :: "real set"
   1.232 +  assumes "S \<noteq> {}" "bdd_above S"
   1.233 +  shows "Sup S \<in> closure S"
   1.234 +proof-
   1.235 +  have "Inf (uminus ` S) \<in> closure (uminus ` S)"
   1.236 +      using assms by (intro closure_contains_Inf) auto
   1.237 +  also have "Inf (uminus ` S) = -Sup S" by (simp add: Inf_real_def)
   1.238 +  also have "closure (uminus ` S) = uminus ` closure S"
   1.239 +      by (rule sym, intro closure_injective_linear_image) (auto intro: linearI)
   1.240 +  finally show ?thesis by auto
   1.241 +qed
   1.242 +
   1.243 +lemma closed_contains_Sup:
   1.244 +  fixes S :: "real set"
   1.245 +  shows "S \<noteq> {} \<Longrightarrow> bdd_above S \<Longrightarrow> closed S \<Longrightarrow> Sup S \<in> S"
   1.246 +  by (subst closure_closed[symmetric], assumption, rule closure_contains_Sup)
   1.247 +
   1.248 +lemma deriv_nonneg_imp_mono:
   1.249 +  assumes deriv: "\<And>x. x \<in> {a..b} \<Longrightarrow> (g has_real_derivative g' x) (at x)"
   1.250 +  assumes nonneg: "\<And>x. x \<in> {a..b} \<Longrightarrow> g' x \<ge> 0"
   1.251 +  assumes ab: "a \<le> b"
   1.252 +  shows "g a \<le> g b"
   1.253 +proof (cases "a < b")
   1.254 +  assume "a < b"
   1.255 +  from deriv have "\<forall>x. x \<ge> a \<and> x \<le> b \<longrightarrow> (g has_real_derivative g' x) (at x)" by simp
   1.256 +  from MVT2[OF \<open>a < b\<close> this] and deriv
   1.257 +    obtain \<xi> where \<xi>_ab: "\<xi> > a" "\<xi> < b" and g_ab: "g b - g a = (b - a) * g' \<xi>" by blast
   1.258 +  from \<xi>_ab ab nonneg have "(b - a) * g' \<xi> \<ge> 0" by simp
   1.259 +  with g_ab show ?thesis by simp
   1.260 +qed (insert ab, simp)
   1.261 +
   1.262 +lemma continuous_interval_vimage_Int:
   1.263 +  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"
   1.264 +  assumes "a \<le> b" "(c::real) \<le> d" "{c..d} \<subseteq> {g a..g b}"
   1.265 +  obtains c' d' where "{a..b} \<inter> g -` {c..d} = {c'..d'}" "c' \<le> d'" "g c' = c" "g d' = d"
   1.266 +proof-
   1.267 +  let ?A = "{a..b} \<inter> g -` {c..d}"
   1.268 +  from IVT'[of g a c b, OF _ _ \<open>a \<le> b\<close> assms(1)] assms(4,5)
   1.269 +  obtain c'' where c'': "c'' \<in> ?A" "g c'' = c" by auto
   1.270 +  from IVT'[of g a d b, OF _ _ \<open>a \<le> b\<close> assms(1)] assms(4,5)
   1.271 +  obtain d'' where d'': "d'' \<in> ?A" "g d'' = d" by auto
   1.272 +  hence [simp]: "?A \<noteq> {}" by blast
   1.273 +
   1.274 +  define c' where "c' = Inf ?A"
   1.275 +  define d' where "d' = Sup ?A"
   1.276 +  have "?A \<subseteq> {c'..d'}" unfolding c'_def d'_def
   1.277 +    by (intro subsetI) (auto intro: cInf_lower cSup_upper)
   1.278 +  moreover from assms have "closed ?A"
   1.279 +    using continuous_on_closed_vimage[of "{a..b}" g] by (subst Int_commute) simp
   1.280 +  hence c'd'_in_set: "c' \<in> ?A" "d' \<in> ?A" unfolding c'_def d'_def
   1.281 +    by ((intro closed_contains_Inf closed_contains_Sup, simp_all)[])+
   1.282 +  hence "{c'..d'} \<subseteq> ?A" using assms
   1.283 +    by (intro subsetI)
   1.284 +       (auto intro!: order_trans[of c "g c'" "g x" for x] order_trans[of "g x" "g d'" d for x]
   1.285 +             intro!: mono)
   1.286 +  moreover have "c' \<le> d'" using c'd'_in_set(2) unfolding c'_def by (intro cInf_lower) auto
   1.287 +  moreover have "g c' \<le> c" "g d' \<ge> d"
   1.288 +    apply (insert c'' d'' c'd'_in_set)
   1.289 +    apply (subst c''(2)[symmetric])
   1.290 +    apply (auto simp: c'_def intro!: mono cInf_lower c'') []
   1.291 +    apply (subst d''(2)[symmetric])
   1.292 +    apply (auto simp: d'_def intro!: mono cSup_upper d'') []
   1.293 +    done
   1.294 +  with c'd'_in_set have "g c' = c" "g d' = d" by auto
   1.295 +  ultimately show ?thesis using that by blast
   1.296 +qed
   1.297 +
   1.298 +subsection \<open>Generic Borel spaces\<close>
   1.299 +
   1.300 +definition (in topological_space) borel :: "'a measure" where
   1.301 +  "borel = sigma UNIV {S. open S}"
   1.302 +
   1.303 +abbreviation "borel_measurable M \<equiv> measurable M borel"
   1.304 +
   1.305 +lemma in_borel_measurable:
   1.306 +   "f \<in> borel_measurable M \<longleftrightarrow>
   1.307 +    (\<forall>S \<in> sigma_sets UNIV {S. open S}. f -` S \<inter> space M \<in> sets M)"
   1.308 +  by (auto simp add: measurable_def borel_def)
   1.309 +
   1.310 +lemma in_borel_measurable_borel:
   1.311 +   "f \<in> borel_measurable M \<longleftrightarrow>
   1.312 +    (\<forall>S \<in> sets borel.
   1.313 +      f -` S \<inter> space M \<in> sets M)"
   1.314 +  by (auto simp add: measurable_def borel_def)
   1.315 +
   1.316 +lemma space_borel[simp]: "space borel = UNIV"
   1.317 +  unfolding borel_def by auto
   1.318 +
   1.319 +lemma space_in_borel[measurable]: "UNIV \<in> sets borel"
   1.320 +  unfolding borel_def by auto
   1.321 +
   1.322 +lemma sets_borel: "sets borel = sigma_sets UNIV {S. open S}"
   1.323 +  unfolding borel_def by (rule sets_measure_of) simp
   1.324 +
   1.325 +lemma measurable_sets_borel:
   1.326 +    "\<lbrakk>f \<in> measurable borel M; A \<in> sets M\<rbrakk> \<Longrightarrow> f -` A \<in> sets borel"
   1.327 +  by (drule (1) measurable_sets) simp
   1.328 +
   1.329 +lemma pred_Collect_borel[measurable (raw)]: "Measurable.pred borel P \<Longrightarrow> {x. P x} \<in> sets borel"
   1.330 +  unfolding borel_def pred_def by auto
   1.331 +
   1.332 +lemma borel_open[measurable (raw generic)]:
   1.333 +  assumes "open A" shows "A \<in> sets borel"
   1.334 +proof -
   1.335 +  have "A \<in> {S. open S}" unfolding mem_Collect_eq using assms .
   1.336 +  thus ?thesis unfolding borel_def by auto
   1.337 +qed
   1.338 +
   1.339 +lemma borel_closed[measurable (raw generic)]:
   1.340 +  assumes "closed A" shows "A \<in> sets borel"
   1.341 +proof -
   1.342 +  have "space borel - (- A) \<in> sets borel"
   1.343 +    using assms unfolding closed_def by (blast intro: borel_open)
   1.344 +  thus ?thesis by simp
   1.345 +qed
   1.346 +
   1.347 +lemma borel_singleton[measurable]:
   1.348 +  "A \<in> sets borel \<Longrightarrow> insert x A \<in> sets (borel :: 'a::t1_space measure)"
   1.349 +  unfolding insert_def by (rule sets.Un) auto
   1.350 +
   1.351 +lemma borel_comp[measurable]: "A \<in> sets borel \<Longrightarrow> - A \<in> sets borel"
   1.352 +  unfolding Compl_eq_Diff_UNIV by simp
   1.353 +
   1.354 +lemma borel_measurable_vimage:
   1.355 +  fixes f :: "'a \<Rightarrow> 'x::t2_space"
   1.356 +  assumes borel[measurable]: "f \<in> borel_measurable M"
   1.357 +  shows "f -` {x} \<inter> space M \<in> sets M"
   1.358 +  by simp
   1.359 +
   1.360 +lemma borel_measurableI:
   1.361 +  fixes f :: "'a \<Rightarrow> 'x::topological_space"
   1.362 +  assumes "\<And>S. open S \<Longrightarrow> f -` S \<inter> space M \<in> sets M"
   1.363 +  shows "f \<in> borel_measurable M"
   1.364 +  unfolding borel_def
   1.365 +proof (rule measurable_measure_of, simp_all)
   1.366 +  fix S :: "'x set" assume "open S" thus "f -` S \<inter> space M \<in> sets M"
   1.367 +    using assms[of S] by simp
   1.368 +qed
   1.369 +
   1.370 +lemma borel_measurable_const:
   1.371 +  "(\<lambda>x. c) \<in> borel_measurable M"
   1.372 +  by auto
   1.373 +
   1.374 +lemma borel_measurable_indicator:
   1.375 +  assumes A: "A \<in> sets M"
   1.376 +  shows "indicator A \<in> borel_measurable M"
   1.377 +  unfolding indicator_def [abs_def] using A
   1.378 +  by (auto intro!: measurable_If_set)
   1.379 +
   1.380 +lemma borel_measurable_count_space[measurable (raw)]:
   1.381 +  "f \<in> borel_measurable (count_space S)"
   1.382 +  unfolding measurable_def by auto
   1.383 +
   1.384 +lemma borel_measurable_indicator'[measurable (raw)]:
   1.385 +  assumes [measurable]: "{x\<in>space M. f x \<in> A x} \<in> sets M"
   1.386 +  shows "(\<lambda>x. indicator (A x) (f x)) \<in> borel_measurable M"
   1.387 +  unfolding indicator_def[abs_def]
   1.388 +  by (auto intro!: measurable_If)
   1.389 +
   1.390 +lemma borel_measurable_indicator_iff:
   1.391 +  "(indicator A :: 'a \<Rightarrow> 'x::{t1_space, zero_neq_one}) \<in> borel_measurable M \<longleftrightarrow> A \<inter> space M \<in> sets M"
   1.392 +    (is "?I \<in> borel_measurable M \<longleftrightarrow> _")
   1.393 +proof
   1.394 +  assume "?I \<in> borel_measurable M"
   1.395 +  then have "?I -` {1} \<inter> space M \<in> sets M"
   1.396 +    unfolding measurable_def by auto
   1.397 +  also have "?I -` {1} \<inter> space M = A \<inter> space M"
   1.398 +    unfolding indicator_def [abs_def] by auto
   1.399 +  finally show "A \<inter> space M \<in> sets M" .
   1.400 +next
   1.401 +  assume "A \<inter> space M \<in> sets M"
   1.402 +  moreover have "?I \<in> borel_measurable M \<longleftrightarrow>
   1.403 +    (indicator (A \<inter> space M) :: 'a \<Rightarrow> 'x) \<in> borel_measurable M"
   1.404 +    by (intro measurable_cong) (auto simp: indicator_def)
   1.405 +  ultimately show "?I \<in> borel_measurable M" by auto
   1.406 +qed
   1.407 +
   1.408 +lemma borel_measurable_subalgebra:
   1.409 +  assumes "sets N \<subseteq> sets M" "space N = space M" "f \<in> borel_measurable N"
   1.410 +  shows "f \<in> borel_measurable M"
   1.411 +  using assms unfolding measurable_def by auto
   1.412 +
   1.413 +lemma borel_measurable_restrict_space_iff_ereal:
   1.414 +  fixes f :: "'a \<Rightarrow> ereal"
   1.415 +  assumes \<Omega>[measurable, simp]: "\<Omega> \<inter> space M \<in> sets M"
   1.416 +  shows "f \<in> borel_measurable (restrict_space M \<Omega>) \<longleftrightarrow>
   1.417 +    (\<lambda>x. f x * indicator \<Omega> x) \<in> borel_measurable M"
   1.418 +  by (subst measurable_restrict_space_iff)
   1.419 +     (auto simp: indicator_def if_distrib[where f="\<lambda>x. a * x" for a] cong del: if_weak_cong)
   1.420 +
   1.421 +lemma borel_measurable_restrict_space_iff_ennreal:
   1.422 +  fixes f :: "'a \<Rightarrow> ennreal"
   1.423 +  assumes \<Omega>[measurable, simp]: "\<Omega> \<inter> space M \<in> sets M"
   1.424 +  shows "f \<in> borel_measurable (restrict_space M \<Omega>) \<longleftrightarrow>
   1.425 +    (\<lambda>x. f x * indicator \<Omega> x) \<in> borel_measurable M"
   1.426 +  by (subst measurable_restrict_space_iff)
   1.427 +     (auto simp: indicator_def if_distrib[where f="\<lambda>x. a * x" for a] cong del: if_weak_cong)
   1.428 +
   1.429 +lemma borel_measurable_restrict_space_iff:
   1.430 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   1.431 +  assumes \<Omega>[measurable, simp]: "\<Omega> \<inter> space M \<in> sets M"
   1.432 +  shows "f \<in> borel_measurable (restrict_space M \<Omega>) \<longleftrightarrow>
   1.433 +    (\<lambda>x. indicator \<Omega> x *\<^sub>R f x) \<in> borel_measurable M"
   1.434 +  by (subst measurable_restrict_space_iff)
   1.435 +     (auto simp: indicator_def if_distrib[where f="\<lambda>x. x *\<^sub>R a" for a] ac_simps
   1.436 +       cong del: if_weak_cong)
   1.437 +
   1.438 +lemma cbox_borel[measurable]: "cbox a b \<in> sets borel"
   1.439 +  by (auto intro: borel_closed)
   1.440 +
   1.441 +lemma box_borel[measurable]: "box a b \<in> sets borel"
   1.442 +  by (auto intro: borel_open)
   1.443 +
   1.444 +lemma borel_compact: "compact (A::'a::t2_space set) \<Longrightarrow> A \<in> sets borel"
   1.445 +  by (auto intro: borel_closed dest!: compact_imp_closed)
   1.446 +
   1.447 +lemma borel_sigma_sets_subset:
   1.448 +  "A \<subseteq> sets borel \<Longrightarrow> sigma_sets UNIV A \<subseteq> sets borel"
   1.449 +  using sets.sigma_sets_subset[of A borel] by simp
   1.450 +
   1.451 +lemma borel_eq_sigmaI1:
   1.452 +  fixes F :: "'i \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   1.453 +  assumes borel_eq: "borel = sigma UNIV X"
   1.454 +  assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV (F ` A))"
   1.455 +  assumes F: "\<And>i. i \<in> A \<Longrightarrow> F i \<in> sets borel"
   1.456 +  shows "borel = sigma UNIV (F ` A)"
   1.457 +  unfolding borel_def
   1.458 +proof (intro sigma_eqI antisym)
   1.459 +  have borel_rev_eq: "sigma_sets UNIV {S::'a set. open S} = sets borel"
   1.460 +    unfolding borel_def by simp
   1.461 +  also have "\<dots> = sigma_sets UNIV X"
   1.462 +    unfolding borel_eq by simp
   1.463 +  also have "\<dots> \<subseteq> sigma_sets UNIV (F`A)"
   1.464 +    using X by (intro sigma_algebra.sigma_sets_subset[OF sigma_algebra_sigma_sets]) auto
   1.465 +  finally show "sigma_sets UNIV {S. open S} \<subseteq> sigma_sets UNIV (F`A)" .
   1.466 +  show "sigma_sets UNIV (F`A) \<subseteq> sigma_sets UNIV {S. open S}"
   1.467 +    unfolding borel_rev_eq using F by (intro borel_sigma_sets_subset) auto
   1.468 +qed auto
   1.469 +
   1.470 +lemma borel_eq_sigmaI2:
   1.471 +  fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set"
   1.472 +    and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   1.473 +  assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`B)"
   1.474 +  assumes X: "\<And>i j. (i, j) \<in> B \<Longrightarrow> G i j \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
   1.475 +  assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
   1.476 +  shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
   1.477 +  using assms
   1.478 +  by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` B" and F="(\<lambda>(i, j). F i j)"]) auto
   1.479 +
   1.480 +lemma borel_eq_sigmaI3:
   1.481 +  fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   1.482 +  assumes borel_eq: "borel = sigma UNIV X"
   1.483 +  assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
   1.484 +  assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
   1.485 +  shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
   1.486 +  using assms by (intro borel_eq_sigmaI1[where X=X and F="(\<lambda>(i, j). F i j)"]) auto
   1.487 +
   1.488 +lemma borel_eq_sigmaI4:
   1.489 +  fixes F :: "'i \<Rightarrow> 'a::topological_space set"
   1.490 +    and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   1.491 +  assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`A)"
   1.492 +  assumes X: "\<And>i j. (i, j) \<in> A \<Longrightarrow> G i j \<in> sets (sigma UNIV (range F))"
   1.493 +  assumes F: "\<And>i. F i \<in> sets borel"
   1.494 +  shows "borel = sigma UNIV (range F)"
   1.495 +  using assms by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` A" and F=F]) auto
   1.496 +
   1.497 +lemma borel_eq_sigmaI5:
   1.498 +  fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and G :: "'l \<Rightarrow> 'a::topological_space set"
   1.499 +  assumes borel_eq: "borel = sigma UNIV (range G)"
   1.500 +  assumes X: "\<And>i. G i \<in> sets (sigma UNIV (range (\<lambda>(i, j). F i j)))"
   1.501 +  assumes F: "\<And>i j. F i j \<in> sets borel"
   1.502 +  shows "borel = sigma UNIV (range (\<lambda>(i, j). F i j))"
   1.503 +  using assms by (intro borel_eq_sigmaI1[where X="range G" and F="(\<lambda>(i, j). F i j)"]) auto
   1.504 +
   1.505 +lemma second_countable_borel_measurable:
   1.506 +  fixes X :: "'a::second_countable_topology set set"
   1.507 +  assumes eq: "open = generate_topology X"
   1.508 +  shows "borel = sigma UNIV X"
   1.509 +  unfolding borel_def
   1.510 +proof (intro sigma_eqI sigma_sets_eqI)
   1.511 +  interpret X: sigma_algebra UNIV "sigma_sets UNIV X"
   1.512 +    by (rule sigma_algebra_sigma_sets) simp
   1.513 +
   1.514 +  fix S :: "'a set" assume "S \<in> Collect open"
   1.515 +  then have "generate_topology X S"
   1.516 +    by (auto simp: eq)
   1.517 +  then show "S \<in> sigma_sets UNIV X"
   1.518 +  proof induction
   1.519 +    case (UN K)
   1.520 +    then have K: "\<And>k. k \<in> K \<Longrightarrow> open k"
   1.521 +      unfolding eq by auto
   1.522 +    from ex_countable_basis obtain B :: "'a set set" where
   1.523 +      B:  "\<And>b. b \<in> B \<Longrightarrow> open b" "\<And>X. open X \<Longrightarrow> \<exists>b\<subseteq>B. (\<Union>b) = X" and "countable B"
   1.524 +      by (auto simp: topological_basis_def)
   1.525 +    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"
   1.526 +      by metis
   1.527 +    define U where "U = (\<Union>k\<in>K. m k)"
   1.528 +    with m have "countable U"
   1.529 +      by (intro countable_subset[OF _ \<open>countable B\<close>]) auto
   1.530 +    have "\<Union>U = (\<Union>A\<in>U. A)" by simp
   1.531 +    also have "\<dots> = \<Union>K"
   1.532 +      unfolding U_def UN_simps by (simp add: m)
   1.533 +    finally have "\<Union>U = \<Union>K" .
   1.534 +
   1.535 +    have "\<forall>b\<in>U. \<exists>k\<in>K. b \<subseteq> k"
   1.536 +      using m by (auto simp: U_def)
   1.537 +    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"
   1.538 +      by metis
   1.539 +    then have "(\<Union>b\<in>U. u b) \<subseteq> \<Union>K" "\<Union>U \<subseteq> (\<Union>b\<in>U. u b)"
   1.540 +      by auto
   1.541 +    then have "\<Union>K = (\<Union>b\<in>U. u b)"
   1.542 +      unfolding \<open>\<Union>U = \<Union>K\<close> by auto
   1.543 +    also have "\<dots> \<in> sigma_sets UNIV X"
   1.544 +      using u UN by (intro X.countable_UN' \<open>countable U\<close>) auto
   1.545 +    finally show "\<Union>K \<in> sigma_sets UNIV X" .
   1.546 +  qed auto
   1.547 +qed (auto simp: eq intro: generate_topology.Basis)
   1.548 +
   1.549 +lemma borel_eq_closed: "borel = sigma UNIV (Collect closed)"
   1.550 +  unfolding borel_def
   1.551 +proof (intro sigma_eqI sigma_sets_eqI, safe)
   1.552 +  fix x :: "'a set" assume "open x"
   1.553 +  hence "x = UNIV - (UNIV - x)" by auto
   1.554 +  also have "\<dots> \<in> sigma_sets UNIV (Collect closed)"
   1.555 +    by (force intro: sigma_sets.Compl simp: \<open>open x\<close>)
   1.556 +  finally show "x \<in> sigma_sets UNIV (Collect closed)" by simp
   1.557 +next
   1.558 +  fix x :: "'a set" assume "closed x"
   1.559 +  hence "x = UNIV - (UNIV - x)" by auto
   1.560 +  also have "\<dots> \<in> sigma_sets UNIV (Collect open)"
   1.561 +    by (force intro: sigma_sets.Compl simp: \<open>closed x\<close>)
   1.562 +  finally show "x \<in> sigma_sets UNIV (Collect open)" by simp
   1.563 +qed simp_all
   1.564 +
   1.565 +lemma borel_eq_countable_basis:
   1.566 +  fixes B::"'a::topological_space set set"
   1.567 +  assumes "countable B"
   1.568 +  assumes "topological_basis B"
   1.569 +  shows "borel = sigma UNIV B"
   1.570 +  unfolding borel_def
   1.571 +proof (intro sigma_eqI sigma_sets_eqI, safe)
   1.572 +  interpret countable_basis using assms by unfold_locales
   1.573 +  fix X::"'a set" assume "open X"
   1.574 +  from open_countable_basisE[OF this] guess B' . note B' = this
   1.575 +  then show "X \<in> sigma_sets UNIV B"
   1.576 +    by (blast intro: sigma_sets_UNION \<open>countable B\<close> countable_subset)
   1.577 +next
   1.578 +  fix b assume "b \<in> B"
   1.579 +  hence "open b" by (rule topological_basis_open[OF assms(2)])
   1.580 +  thus "b \<in> sigma_sets UNIV (Collect open)" by auto
   1.581 +qed simp_all
   1.582 +
   1.583 +lemma borel_measurable_continuous_on_restrict:
   1.584 +  fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
   1.585 +  assumes f: "continuous_on A f"
   1.586 +  shows "f \<in> borel_measurable (restrict_space borel A)"
   1.587 +proof (rule borel_measurableI)
   1.588 +  fix S :: "'b set" assume "open S"
   1.589 +  with f obtain T where "f -` S \<inter> A = T \<inter> A" "open T"
   1.590 +    by (metis continuous_on_open_invariant)
   1.591 +  then show "f -` S \<inter> space (restrict_space borel A) \<in> sets (restrict_space borel A)"
   1.592 +    by (force simp add: sets_restrict_space space_restrict_space)
   1.593 +qed
   1.594 +
   1.595 +lemma borel_measurable_continuous_on1: "continuous_on UNIV f \<Longrightarrow> f \<in> borel_measurable borel"
   1.596 +  by (drule borel_measurable_continuous_on_restrict) simp
   1.597 +
   1.598 +lemma borel_measurable_continuous_on_if:
   1.599 +  "A \<in> sets borel \<Longrightarrow> continuous_on A f \<Longrightarrow> continuous_on (- A) g \<Longrightarrow>
   1.600 +    (\<lambda>x. if x \<in> A then f x else g x) \<in> borel_measurable borel"
   1.601 +  by (auto simp add: measurable_If_restrict_space_iff Collect_neg_eq
   1.602 +           intro!: borel_measurable_continuous_on_restrict)
   1.603 +
   1.604 +lemma borel_measurable_continuous_countable_exceptions:
   1.605 +  fixes f :: "'a::t1_space \<Rightarrow> 'b::topological_space"
   1.606 +  assumes X: "countable X"
   1.607 +  assumes "continuous_on (- X) f"
   1.608 +  shows "f \<in> borel_measurable borel"
   1.609 +proof (rule measurable_discrete_difference[OF _ X])
   1.610 +  have "X \<in> sets borel"
   1.611 +    by (rule sets.countable[OF _ X]) auto
   1.612 +  then show "(\<lambda>x. if x \<in> X then undefined else f x) \<in> borel_measurable borel"
   1.613 +    by (intro borel_measurable_continuous_on_if assms continuous_intros)
   1.614 +qed auto
   1.615 +
   1.616 +lemma borel_measurable_continuous_on:
   1.617 +  assumes f: "continuous_on UNIV f" and g: "g \<in> borel_measurable M"
   1.618 +  shows "(\<lambda>x. f (g x)) \<in> borel_measurable M"
   1.619 +  using measurable_comp[OF g borel_measurable_continuous_on1[OF f]] by (simp add: comp_def)
   1.620 +
   1.621 +lemma borel_measurable_continuous_on_indicator:
   1.622 +  fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
   1.623 +  shows "A \<in> sets borel \<Longrightarrow> continuous_on A f \<Longrightarrow> (\<lambda>x. indicator A x *\<^sub>R f x) \<in> borel_measurable borel"
   1.624 +  by (subst borel_measurable_restrict_space_iff[symmetric])
   1.625 +     (auto intro: borel_measurable_continuous_on_restrict)
   1.626 +
   1.627 +lemma borel_measurable_Pair[measurable (raw)]:
   1.628 +  fixes f :: "'a \<Rightarrow> 'b::second_countable_topology" and g :: "'a \<Rightarrow> 'c::second_countable_topology"
   1.629 +  assumes f[measurable]: "f \<in> borel_measurable M"
   1.630 +  assumes g[measurable]: "g \<in> borel_measurable M"
   1.631 +  shows "(\<lambda>x. (f x, g x)) \<in> borel_measurable M"
   1.632 +proof (subst borel_eq_countable_basis)
   1.633 +  let ?B = "SOME B::'b set set. countable B \<and> topological_basis B"
   1.634 +  let ?C = "SOME B::'c set set. countable B \<and> topological_basis B"
   1.635 +  let ?P = "(\<lambda>(b, c). b \<times> c) ` (?B \<times> ?C)"
   1.636 +  show "countable ?P" "topological_basis ?P"
   1.637 +    by (auto intro!: countable_basis topological_basis_prod is_basis)
   1.638 +
   1.639 +  show "(\<lambda>x. (f x, g x)) \<in> measurable M (sigma UNIV ?P)"
   1.640 +  proof (rule measurable_measure_of)
   1.641 +    fix S assume "S \<in> ?P"
   1.642 +    then obtain b c where "b \<in> ?B" "c \<in> ?C" and S: "S = b \<times> c" by auto
   1.643 +    then have borel: "open b" "open c"
   1.644 +      by (auto intro: is_basis topological_basis_open)
   1.645 +    have "(\<lambda>x. (f x, g x)) -` S \<inter> space M = (f -` b \<inter> space M) \<inter> (g -` c \<inter> space M)"
   1.646 +      unfolding S by auto
   1.647 +    also have "\<dots> \<in> sets M"
   1.648 +      using borel by simp
   1.649 +    finally show "(\<lambda>x. (f x, g x)) -` S \<inter> space M \<in> sets M" .
   1.650 +  qed auto
   1.651 +qed
   1.652 +
   1.653 +lemma borel_measurable_continuous_Pair:
   1.654 +  fixes f :: "'a \<Rightarrow> 'b::second_countable_topology" and g :: "'a \<Rightarrow> 'c::second_countable_topology"
   1.655 +  assumes [measurable]: "f \<in> borel_measurable M"
   1.656 +  assumes [measurable]: "g \<in> borel_measurable M"
   1.657 +  assumes H: "continuous_on UNIV (\<lambda>x. H (fst x) (snd x))"
   1.658 +  shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
   1.659 +proof -
   1.660 +  have eq: "(\<lambda>x. H (f x) (g x)) = (\<lambda>x. (\<lambda>x. H (fst x) (snd x)) (f x, g x))" by auto
   1.661 +  show ?thesis
   1.662 +    unfolding eq by (rule borel_measurable_continuous_on[OF H]) auto
   1.663 +qed
   1.664 +
   1.665 +subsection \<open>Borel spaces on order topologies\<close>
   1.666 +
   1.667 +lemma [measurable]:
   1.668 +  fixes a b :: "'a::linorder_topology"
   1.669 +  shows lessThan_borel: "{..< a} \<in> sets borel"
   1.670 +    and greaterThan_borel: "{a <..} \<in> sets borel"
   1.671 +    and greaterThanLessThan_borel: "{a<..<b} \<in> sets borel"
   1.672 +    and atMost_borel: "{..a} \<in> sets borel"
   1.673 +    and atLeast_borel: "{a..} \<in> sets borel"
   1.674 +    and atLeastAtMost_borel: "{a..b} \<in> sets borel"
   1.675 +    and greaterThanAtMost_borel: "{a<..b} \<in> sets borel"
   1.676 +    and atLeastLessThan_borel: "{a..<b} \<in> sets borel"
   1.677 +  unfolding greaterThanAtMost_def atLeastLessThan_def
   1.678 +  by (blast intro: borel_open borel_closed open_lessThan open_greaterThan open_greaterThanLessThan
   1.679 +                   closed_atMost closed_atLeast closed_atLeastAtMost)+
   1.680 +
   1.681 +lemma borel_Iio:
   1.682 +  "borel = sigma UNIV (range lessThan :: 'a::{linorder_topology, second_countable_topology} set set)"
   1.683 +  unfolding second_countable_borel_measurable[OF open_generated_order]
   1.684 +proof (intro sigma_eqI sigma_sets_eqI)
   1.685 +  from countable_dense_setE guess D :: "'a set" . note D = this
   1.686 +
   1.687 +  interpret L: sigma_algebra UNIV "sigma_sets UNIV (range lessThan)"
   1.688 +    by (rule sigma_algebra_sigma_sets) simp
   1.689 +
   1.690 +  fix A :: "'a set" assume "A \<in> range lessThan \<union> range greaterThan"
   1.691 +  then obtain y where "A = {y <..} \<or> A = {..< y}"
   1.692 +    by blast
   1.693 +  then show "A \<in> sigma_sets UNIV (range lessThan)"
   1.694 +  proof
   1.695 +    assume A: "A = {y <..}"
   1.696 +    show ?thesis
   1.697 +    proof cases
   1.698 +      assume "\<forall>x>y. \<exists>d. y < d \<and> d < x"
   1.699 +      with D(2)[of "{y <..< x}" for x] have "\<forall>x>y. \<exists>d\<in>D. y < d \<and> d < x"
   1.700 +        by (auto simp: set_eq_iff)
   1.701 +      then have "A = UNIV - (\<Inter>d\<in>{d\<in>D. y < d}. {..< d})"
   1.702 +        by (auto simp: A) (metis less_asym)
   1.703 +      also have "\<dots> \<in> sigma_sets UNIV (range lessThan)"
   1.704 +        using D(1) by (intro L.Diff L.top L.countable_INT'') auto
   1.705 +      finally show ?thesis .
   1.706 +    next
   1.707 +      assume "\<not> (\<forall>x>y. \<exists>d. y < d \<and> d < x)"
   1.708 +      then obtain x where "y < x"  "\<And>d. y < d \<Longrightarrow> \<not> d < x"
   1.709 +        by auto
   1.710 +      then have "A = UNIV - {..< x}"
   1.711 +        unfolding A by (auto simp: not_less[symmetric])
   1.712 +      also have "\<dots> \<in> sigma_sets UNIV (range lessThan)"
   1.713 +        by auto
   1.714 +      finally show ?thesis .
   1.715 +    qed
   1.716 +  qed auto
   1.717 +qed auto
   1.718 +
   1.719 +lemma borel_Ioi:
   1.720 +  "borel = sigma UNIV (range greaterThan :: 'a::{linorder_topology, second_countable_topology} set set)"
   1.721 +  unfolding second_countable_borel_measurable[OF open_generated_order]
   1.722 +proof (intro sigma_eqI sigma_sets_eqI)
   1.723 +  from countable_dense_setE guess D :: "'a set" . note D = this
   1.724 +
   1.725 +  interpret L: sigma_algebra UNIV "sigma_sets UNIV (range greaterThan)"
   1.726 +    by (rule sigma_algebra_sigma_sets) simp
   1.727 +
   1.728 +  fix A :: "'a set" assume "A \<in> range lessThan \<union> range greaterThan"
   1.729 +  then obtain y where "A = {y <..} \<or> A = {..< y}"
   1.730 +    by blast
   1.731 +  then show "A \<in> sigma_sets UNIV (range greaterThan)"
   1.732 +  proof
   1.733 +    assume A: "A = {..< y}"
   1.734 +    show ?thesis
   1.735 +    proof cases
   1.736 +      assume "\<forall>x<y. \<exists>d. x < d \<and> d < y"
   1.737 +      with D(2)[of "{x <..< y}" for x] have "\<forall>x<y. \<exists>d\<in>D. x < d \<and> d < y"
   1.738 +        by (auto simp: set_eq_iff)
   1.739 +      then have "A = UNIV - (\<Inter>d\<in>{d\<in>D. d < y}. {d <..})"
   1.740 +        by (auto simp: A) (metis less_asym)
   1.741 +      also have "\<dots> \<in> sigma_sets UNIV (range greaterThan)"
   1.742 +        using D(1) by (intro L.Diff L.top L.countable_INT'') auto
   1.743 +      finally show ?thesis .
   1.744 +    next
   1.745 +      assume "\<not> (\<forall>x<y. \<exists>d. x < d \<and> d < y)"
   1.746 +      then obtain x where "x < y"  "\<And>d. y > d \<Longrightarrow> x \<ge> d"
   1.747 +        by (auto simp: not_less[symmetric])
   1.748 +      then have "A = UNIV - {x <..}"
   1.749 +        unfolding A Compl_eq_Diff_UNIV[symmetric] by auto
   1.750 +      also have "\<dots> \<in> sigma_sets UNIV (range greaterThan)"
   1.751 +        by auto
   1.752 +      finally show ?thesis .
   1.753 +    qed
   1.754 +  qed auto
   1.755 +qed auto
   1.756 +
   1.757 +lemma borel_measurableI_less:
   1.758 +  fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   1.759 +  shows "(\<And>y. {x\<in>space M. f x < y} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   1.760 +  unfolding borel_Iio
   1.761 +  by (rule measurable_measure_of) (auto simp: Int_def conj_commute)
   1.762 +
   1.763 +lemma borel_measurableI_greater:
   1.764 +  fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   1.765 +  shows "(\<And>y. {x\<in>space M. y < f x} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   1.766 +  unfolding borel_Ioi
   1.767 +  by (rule measurable_measure_of) (auto simp: Int_def conj_commute)
   1.768 +
   1.769 +lemma borel_measurableI_le:
   1.770 +  fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   1.771 +  shows "(\<And>y. {x\<in>space M. f x \<le> y} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   1.772 +  by (rule borel_measurableI_greater) (auto simp: not_le[symmetric])
   1.773 +
   1.774 +lemma borel_measurableI_ge:
   1.775 +  fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   1.776 +  shows "(\<And>y. {x\<in>space M. y \<le> f x} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   1.777 +  by (rule borel_measurableI_less) (auto simp: not_le[symmetric])
   1.778 +
   1.779 +lemma borel_measurable_less[measurable]:
   1.780 +  fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, linorder_topology}"
   1.781 +  assumes "f \<in> borel_measurable M"
   1.782 +  assumes "g \<in> borel_measurable M"
   1.783 +  shows "{w \<in> space M. f w < g w} \<in> sets M"
   1.784 +proof -
   1.785 +  have "{w \<in> space M. f w < g w} = (\<lambda>x. (f x, g x)) -` {x. fst x < snd x} \<inter> space M"
   1.786 +    by auto
   1.787 +  also have "\<dots> \<in> sets M"
   1.788 +    by (intro measurable_sets[OF borel_measurable_Pair borel_open, OF assms open_Collect_less]
   1.789 +              continuous_intros)
   1.790 +  finally show ?thesis .
   1.791 +qed
   1.792 +
   1.793 +lemma
   1.794 +  fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, linorder_topology}"
   1.795 +  assumes f[measurable]: "f \<in> borel_measurable M"
   1.796 +  assumes g[measurable]: "g \<in> borel_measurable M"
   1.797 +  shows borel_measurable_le[measurable]: "{w \<in> space M. f w \<le> g w} \<in> sets M"
   1.798 +    and borel_measurable_eq[measurable]: "{w \<in> space M. f w = g w} \<in> sets M"
   1.799 +    and borel_measurable_neq: "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
   1.800 +  unfolding eq_iff not_less[symmetric]
   1.801 +  by measurable
   1.802 +
   1.803 +lemma borel_measurable_SUP[measurable (raw)]:
   1.804 +  fixes F :: "_ \<Rightarrow> _ \<Rightarrow> _::{complete_linorder, linorder_topology, second_countable_topology}"
   1.805 +  assumes [simp]: "countable I"
   1.806 +  assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   1.807 +  shows "(\<lambda>x. SUP i:I. F i x) \<in> borel_measurable M"
   1.808 +  by (rule borel_measurableI_greater) (simp add: less_SUP_iff)
   1.809 +
   1.810 +lemma borel_measurable_INF[measurable (raw)]:
   1.811 +  fixes F :: "_ \<Rightarrow> _ \<Rightarrow> _::{complete_linorder, linorder_topology, second_countable_topology}"
   1.812 +  assumes [simp]: "countable I"
   1.813 +  assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   1.814 +  shows "(\<lambda>x. INF i:I. F i x) \<in> borel_measurable M"
   1.815 +  by (rule borel_measurableI_less) (simp add: INF_less_iff)
   1.816 +
   1.817 +lemma borel_measurable_cSUP[measurable (raw)]:
   1.818 +  fixes F :: "_ \<Rightarrow> _ \<Rightarrow> 'a::{conditionally_complete_linorder, linorder_topology, second_countable_topology}"
   1.819 +  assumes [simp]: "countable I"
   1.820 +  assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   1.821 +  assumes bdd: "\<And>x. x \<in> space M \<Longrightarrow> bdd_above ((\<lambda>i. F i x) ` I)"
   1.822 +  shows "(\<lambda>x. SUP i:I. F i x) \<in> borel_measurable M"
   1.823 +proof cases
   1.824 +  assume "I = {}" then show ?thesis
   1.825 +    unfolding \<open>I = {}\<close> image_empty by simp
   1.826 +next
   1.827 +  assume "I \<noteq> {}"
   1.828 +  show ?thesis
   1.829 +  proof (rule borel_measurableI_le)
   1.830 +    fix y
   1.831 +    have "{x \<in> space M. \<forall>i\<in>I. F i x \<le> y} \<in> sets M"
   1.832 +      by measurable
   1.833 +    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}"
   1.834 +      by (simp add: cSUP_le_iff \<open>I \<noteq> {}\<close> bdd cong: conj_cong)
   1.835 +    finally show "{x \<in> space M. (SUP i:I. F i x) \<le>  y} \<in> sets M"  .
   1.836 +  qed
   1.837 +qed
   1.838 +
   1.839 +lemma borel_measurable_cINF[measurable (raw)]:
   1.840 +  fixes F :: "_ \<Rightarrow> _ \<Rightarrow> 'a::{conditionally_complete_linorder, linorder_topology, second_countable_topology}"
   1.841 +  assumes [simp]: "countable I"
   1.842 +  assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   1.843 +  assumes bdd: "\<And>x. x \<in> space M \<Longrightarrow> bdd_below ((\<lambda>i. F i x) ` I)"
   1.844 +  shows "(\<lambda>x. INF i:I. F i x) \<in> borel_measurable M"
   1.845 +proof cases
   1.846 +  assume "I = {}" then show ?thesis
   1.847 +    unfolding \<open>I = {}\<close> image_empty by simp
   1.848 +next
   1.849 +  assume "I \<noteq> {}"
   1.850 +  show ?thesis
   1.851 +  proof (rule borel_measurableI_ge)
   1.852 +    fix y
   1.853 +    have "{x \<in> space M. \<forall>i\<in>I. y \<le> F i x} \<in> sets M"
   1.854 +      by measurable
   1.855 +    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)}"
   1.856 +      by (simp add: le_cINF_iff \<open>I \<noteq> {}\<close> bdd cong: conj_cong)
   1.857 +    finally show "{x \<in> space M. y \<le> (INF i:I. F i x)} \<in> sets M"  .
   1.858 +  qed
   1.859 +qed
   1.860 +
   1.861 +lemma borel_measurable_lfp[consumes 1, case_names continuity step]:
   1.862 +  fixes F :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b::{complete_linorder, linorder_topology, second_countable_topology})"
   1.863 +  assumes "sup_continuous F"
   1.864 +  assumes *: "\<And>f. f \<in> borel_measurable M \<Longrightarrow> F f \<in> borel_measurable M"
   1.865 +  shows "lfp F \<in> borel_measurable M"
   1.866 +proof -
   1.867 +  { fix i have "((F ^^ i) bot) \<in> borel_measurable M"
   1.868 +      by (induct i) (auto intro!: *) }
   1.869 +  then have "(\<lambda>x. SUP i. (F ^^ i) bot x) \<in> borel_measurable M"
   1.870 +    by measurable
   1.871 +  also have "(\<lambda>x. SUP i. (F ^^ i) bot x) = (SUP i. (F ^^ i) bot)"
   1.872 +    by auto
   1.873 +  also have "(SUP i. (F ^^ i) bot) = lfp F"
   1.874 +    by (rule sup_continuous_lfp[symmetric]) fact
   1.875 +  finally show ?thesis .
   1.876 +qed
   1.877 +
   1.878 +lemma borel_measurable_gfp[consumes 1, case_names continuity step]:
   1.879 +  fixes F :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b::{complete_linorder, linorder_topology, second_countable_topology})"
   1.880 +  assumes "inf_continuous F"
   1.881 +  assumes *: "\<And>f. f \<in> borel_measurable M \<Longrightarrow> F f \<in> borel_measurable M"
   1.882 +  shows "gfp F \<in> borel_measurable M"
   1.883 +proof -
   1.884 +  { fix i have "((F ^^ i) top) \<in> borel_measurable M"
   1.885 +      by (induct i) (auto intro!: * simp: bot_fun_def) }
   1.886 +  then have "(\<lambda>x. INF i. (F ^^ i) top x) \<in> borel_measurable M"
   1.887 +    by measurable
   1.888 +  also have "(\<lambda>x. INF i. (F ^^ i) top x) = (INF i. (F ^^ i) top)"
   1.889 +    by auto
   1.890 +  also have "\<dots> = gfp F"
   1.891 +    by (rule inf_continuous_gfp[symmetric]) fact
   1.892 +  finally show ?thesis .
   1.893 +qed
   1.894 +
   1.895 +lemma borel_measurable_max[measurable (raw)]:
   1.896 +  "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"
   1.897 +  by (rule borel_measurableI_less) simp
   1.898 +
   1.899 +lemma borel_measurable_min[measurable (raw)]:
   1.900 +  "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"
   1.901 +  by (rule borel_measurableI_greater) simp
   1.902 +
   1.903 +lemma borel_measurable_Min[measurable (raw)]:
   1.904 +  "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"
   1.905 +proof (induct I rule: finite_induct)
   1.906 +  case (insert i I) then show ?case
   1.907 +    by (cases "I = {}") auto
   1.908 +qed auto
   1.909 +
   1.910 +lemma borel_measurable_Max[measurable (raw)]:
   1.911 +  "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"
   1.912 +proof (induct I rule: finite_induct)
   1.913 +  case (insert i I) then show ?case
   1.914 +    by (cases "I = {}") auto
   1.915 +qed auto
   1.916 +
   1.917 +lemma borel_measurable_sup[measurable (raw)]:
   1.918 +  "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"
   1.919 +  unfolding sup_max by measurable
   1.920 +
   1.921 +lemma borel_measurable_inf[measurable (raw)]:
   1.922 +  "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"
   1.923 +  unfolding inf_min by measurable
   1.924 +
   1.925 +lemma [measurable (raw)]:
   1.926 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   1.927 +  assumes "\<And>i. f i \<in> borel_measurable M"
   1.928 +  shows borel_measurable_liminf: "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
   1.929 +    and borel_measurable_limsup: "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
   1.930 +  unfolding liminf_SUP_INF limsup_INF_SUP using assms by auto
   1.931 +
   1.932 +lemma measurable_convergent[measurable (raw)]:
   1.933 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   1.934 +  assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
   1.935 +  shows "Measurable.pred M (\<lambda>x. convergent (\<lambda>i. f i x))"
   1.936 +  unfolding convergent_ereal by measurable
   1.937 +
   1.938 +lemma sets_Collect_convergent[measurable]:
   1.939 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   1.940 +  assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
   1.941 +  shows "{x\<in>space M. convergent (\<lambda>i. f i x)} \<in> sets M"
   1.942 +  by measurable
   1.943 +
   1.944 +lemma borel_measurable_lim[measurable (raw)]:
   1.945 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   1.946 +  assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
   1.947 +  shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
   1.948 +proof -
   1.949 +  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))"
   1.950 +    by (simp add: lim_def convergent_def convergent_limsup_cl)
   1.951 +  then show ?thesis
   1.952 +    by simp
   1.953 +qed
   1.954 +
   1.955 +lemma borel_measurable_LIMSEQ_order:
   1.956 +  fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   1.957 +  assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) \<longlonglongrightarrow> u' x"
   1.958 +  and u: "\<And>i. u i \<in> borel_measurable M"
   1.959 +  shows "u' \<in> borel_measurable M"
   1.960 +proof -
   1.961 +  have "\<And>x. x \<in> space M \<Longrightarrow> u' x = liminf (\<lambda>n. u n x)"
   1.962 +    using u' by (simp add: lim_imp_Liminf[symmetric])
   1.963 +  with u show ?thesis by (simp cong: measurable_cong)
   1.964 +qed
   1.965 +
   1.966 +subsection \<open>Borel spaces on topological monoids\<close>
   1.967 +
   1.968 +lemma borel_measurable_add[measurable (raw)]:
   1.969 +  fixes f g :: "'a \<Rightarrow> 'b::{second_countable_topology, topological_monoid_add}"
   1.970 +  assumes f: "f \<in> borel_measurable M"
   1.971 +  assumes g: "g \<in> borel_measurable M"
   1.972 +  shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   1.973 +  using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
   1.974 +
   1.975 +lemma borel_measurable_setsum[measurable (raw)]:
   1.976 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> 'b::{second_countable_topology, topological_comm_monoid_add}"
   1.977 +  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   1.978 +  shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   1.979 +proof cases
   1.980 +  assume "finite S"
   1.981 +  thus ?thesis using assms by induct auto
   1.982 +qed simp
   1.983 +
   1.984 +lemma borel_measurable_suminf_order[measurable (raw)]:
   1.985 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology, topological_comm_monoid_add}"
   1.986 +  assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
   1.987 +  shows "(\<lambda>x. suminf (\<lambda>i. f i x)) \<in> borel_measurable M"
   1.988 +  unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
   1.989 +
   1.990 +subsection \<open>Borel spaces on Euclidean spaces\<close>
   1.991 +
   1.992 +lemma borel_measurable_inner[measurable (raw)]:
   1.993 +  fixes f g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_inner}"
   1.994 +  assumes "f \<in> borel_measurable M"
   1.995 +  assumes "g \<in> borel_measurable M"
   1.996 +  shows "(\<lambda>x. f x \<bullet> g x) \<in> borel_measurable M"
   1.997 +  using assms
   1.998 +  by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
   1.999 +
  1.1000 +notation
  1.1001 +  eucl_less (infix "<e" 50)
  1.1002 +
  1.1003 +lemma box_oc: "{x. a <e x \<and> x \<le> b} = {x. a <e x} \<inter> {..b}"
  1.1004 +  and box_co: "{x. a \<le> x \<and> x <e b} = {a..} \<inter> {x. x <e b}"
  1.1005 +  by auto
  1.1006 +
  1.1007 +lemma eucl_ivals[measurable]:
  1.1008 +  fixes a b :: "'a::ordered_euclidean_space"
  1.1009 +  shows "{x. x <e a} \<in> sets borel"
  1.1010 +    and "{x. a <e x} \<in> sets borel"
  1.1011 +    and "{..a} \<in> sets borel"
  1.1012 +    and "{a..} \<in> sets borel"
  1.1013 +    and "{a..b} \<in> sets borel"
  1.1014 +    and  "{x. a <e x \<and> x \<le> b} \<in> sets borel"
  1.1015 +    and "{x. a \<le> x \<and>  x <e b} \<in> sets borel"
  1.1016 +  unfolding box_oc box_co
  1.1017 +  by (auto intro: borel_open borel_closed)
  1.1018 +
  1.1019 +lemma
  1.1020 +  fixes i :: "'a::{second_countable_topology, real_inner}"
  1.1021 +  shows hafspace_less_borel: "{x. a < x \<bullet> i} \<in> sets borel"
  1.1022 +    and hafspace_greater_borel: "{x. x \<bullet> i < a} \<in> sets borel"
  1.1023 +    and hafspace_less_eq_borel: "{x. a \<le> x \<bullet> i} \<in> sets borel"
  1.1024 +    and hafspace_greater_eq_borel: "{x. x \<bullet> i \<le> a} \<in> sets borel"
  1.1025 +  by simp_all
  1.1026 +
  1.1027 +lemma borel_eq_box:
  1.1028 +  "borel = sigma UNIV (range (\<lambda> (a, b). box a b :: 'a :: euclidean_space set))"
  1.1029 +    (is "_ = ?SIGMA")
  1.1030 +proof (rule borel_eq_sigmaI1[OF borel_def])
  1.1031 +  fix M :: "'a set" assume "M \<in> {S. open S}"
  1.1032 +  then have "open M" by simp
  1.1033 +  show "M \<in> ?SIGMA"
  1.1034 +    apply (subst open_UNION_box[OF \<open>open M\<close>])
  1.1035 +    apply (safe intro!: sets.countable_UN' countable_PiE countable_Collect)
  1.1036 +    apply (auto intro: countable_rat)
  1.1037 +    done
  1.1038 +qed (auto simp: box_def)
  1.1039 +
  1.1040 +lemma halfspace_gt_in_halfspace:
  1.1041 +  assumes i: "i \<in> A"
  1.1042 +  shows "{x::'a. a < x \<bullet> i} \<in>
  1.1043 +    sigma_sets UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> A))"
  1.1044 +  (is "?set \<in> ?SIGMA")
  1.1045 +proof -
  1.1046 +  interpret sigma_algebra UNIV ?SIGMA
  1.1047 +    by (intro sigma_algebra_sigma_sets) simp_all
  1.1048 +  have *: "?set = (\<Union>n. UNIV - {x::'a. x \<bullet> i < a + 1 / real (Suc n)})"
  1.1049 +  proof (safe, simp_all add: not_less del: of_nat_Suc)
  1.1050 +    fix x :: 'a assume "a < x \<bullet> i"
  1.1051 +    with reals_Archimedean[of "x \<bullet> i - a"]
  1.1052 +    obtain n where "a + 1 / real (Suc n) < x \<bullet> i"
  1.1053 +      by (auto simp: field_simps)
  1.1054 +    then show "\<exists>n. a + 1 / real (Suc n) \<le> x \<bullet> i"
  1.1055 +      by (blast intro: less_imp_le)
  1.1056 +  next
  1.1057 +    fix x n
  1.1058 +    have "a < a + 1 / real (Suc n)" by auto
  1.1059 +    also assume "\<dots> \<le> x"
  1.1060 +    finally show "a < x" .
  1.1061 +  qed
  1.1062 +  show "?set \<in> ?SIGMA" unfolding *
  1.1063 +    by (auto intro!: Diff sigma_sets_Inter i)
  1.1064 +qed
  1.1065 +
  1.1066 +lemma borel_eq_halfspace_less:
  1.1067 +  "borel = sigma UNIV ((\<lambda>(a, i). {x::'a::euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> Basis))"
  1.1068 +  (is "_ = ?SIGMA")
  1.1069 +proof (rule borel_eq_sigmaI2[OF borel_eq_box])
  1.1070 +  fix a b :: 'a
  1.1071 +  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}"
  1.1072 +    by (auto simp: box_def)
  1.1073 +  also have "\<dots> \<in> sets ?SIGMA"
  1.1074 +    by (intro sets.sets_Collect_conj sets.sets_Collect_finite_All sets.sets_Collect_const)
  1.1075 +       (auto intro!: halfspace_gt_in_halfspace countable_PiE countable_rat)
  1.1076 +  finally show "box a b \<in> sets ?SIGMA" .
  1.1077 +qed auto
  1.1078 +
  1.1079 +lemma borel_eq_halfspace_le:
  1.1080 +  "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. x \<bullet> i \<le> a}) ` (UNIV \<times> Basis))"
  1.1081 +  (is "_ = ?SIGMA")
  1.1082 +proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
  1.1083 +  fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
  1.1084 +  then have i: "i \<in> Basis" by auto
  1.1085 +  have *: "{x::'a. x\<bullet>i < a} = (\<Union>n. {x. x\<bullet>i \<le> a - 1/real (Suc n)})"
  1.1086 +  proof (safe, simp_all del: of_nat_Suc)
  1.1087 +    fix x::'a assume *: "x\<bullet>i < a"
  1.1088 +    with reals_Archimedean[of "a - x\<bullet>i"]
  1.1089 +    obtain n where "x \<bullet> i < a - 1 / (real (Suc n))"
  1.1090 +      by (auto simp: field_simps)
  1.1091 +    then show "\<exists>n. x \<bullet> i \<le> a - 1 / (real (Suc n))"
  1.1092 +      by (blast intro: less_imp_le)
  1.1093 +  next
  1.1094 +    fix x::'a and n
  1.1095 +    assume "x\<bullet>i \<le> a - 1 / real (Suc n)"
  1.1096 +    also have "\<dots> < a" by auto
  1.1097 +    finally show "x\<bullet>i < a" .
  1.1098 +  qed
  1.1099 +  show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
  1.1100 +    by (intro sets.countable_UN) (auto intro: i)
  1.1101 +qed auto
  1.1102 +
  1.1103 +lemma borel_eq_halfspace_ge:
  1.1104 +  "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. a \<le> x \<bullet> i}) ` (UNIV \<times> Basis))"
  1.1105 +  (is "_ = ?SIGMA")
  1.1106 +proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
  1.1107 +  fix a :: real and i :: 'a assume i: "(a, i) \<in> UNIV \<times> Basis"
  1.1108 +  have *: "{x::'a. x\<bullet>i < a} = space ?SIGMA - {x::'a. a \<le> x\<bullet>i}" by auto
  1.1109 +  show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
  1.1110 +    using i by (intro sets.compl_sets) auto
  1.1111 +qed auto
  1.1112 +
  1.1113 +lemma borel_eq_halfspace_greater:
  1.1114 +  "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. a < x \<bullet> i}) ` (UNIV \<times> Basis))"
  1.1115 +  (is "_ = ?SIGMA")
  1.1116 +proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_le])
  1.1117 +  fix a :: real and i :: 'a assume "(a, i) \<in> (UNIV \<times> Basis)"
  1.1118 +  then have i: "i \<in> Basis" by auto
  1.1119 +  have *: "{x::'a. x\<bullet>i \<le> a} = space ?SIGMA - {x::'a. a < x\<bullet>i}" by auto
  1.1120 +  show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
  1.1121 +    by (intro sets.compl_sets) (auto intro: i)
  1.1122 +qed auto
  1.1123 +
  1.1124 +lemma borel_eq_atMost:
  1.1125 +  "borel = sigma UNIV (range (\<lambda>a. {..a::'a::ordered_euclidean_space}))"
  1.1126 +  (is "_ = ?SIGMA")
  1.1127 +proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
  1.1128 +  fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
  1.1129 +  then have "i \<in> Basis" by auto
  1.1130 +  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)})"
  1.1131 +  proof (safe, simp_all add: eucl_le[where 'a='a] split: if_split_asm)
  1.1132 +    fix x :: 'a
  1.1133 +    from real_arch_simple[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"] guess k::nat ..
  1.1134 +    then have "\<And>i. i \<in> Basis \<Longrightarrow> x\<bullet>i \<le> real k"
  1.1135 +      by (subst (asm) Max_le_iff) auto
  1.1136 +    then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia \<le> real k"
  1.1137 +      by (auto intro!: exI[of _ k])
  1.1138 +  qed
  1.1139 +  show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
  1.1140 +    by (intro sets.countable_UN) auto
  1.1141 +qed auto
  1.1142 +
  1.1143 +lemma borel_eq_greaterThan:
  1.1144 +  "borel = sigma UNIV (range (\<lambda>a::'a::ordered_euclidean_space. {x. a <e x}))"
  1.1145 +  (is "_ = ?SIGMA")
  1.1146 +proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
  1.1147 +  fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
  1.1148 +  then have i: "i \<in> Basis" by auto
  1.1149 +  have "{x::'a. x\<bullet>i \<le> a} = UNIV - {x::'a. a < x\<bullet>i}" by auto
  1.1150 +  also have *: "{x::'a. a < x\<bullet>i} =
  1.1151 +      (\<Union>k::nat. {x. (\<Sum>n\<in>Basis. (if n = i then a else -real k) *\<^sub>R n) <e x})" using i
  1.1152 +  proof (safe, simp_all add: eucl_less_def split: if_split_asm)
  1.1153 +    fix x :: 'a
  1.1154 +    from reals_Archimedean2[of "Max ((\<lambda>i. -x\<bullet>i)`Basis)"]
  1.1155 +    guess k::nat .. note k = this
  1.1156 +    { fix i :: 'a assume "i \<in> Basis"
  1.1157 +      then have "-x\<bullet>i < real k"
  1.1158 +        using k by (subst (asm) Max_less_iff) auto
  1.1159 +      then have "- real k < x\<bullet>i" by simp }
  1.1160 +    then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> -real k < x \<bullet> ia"
  1.1161 +      by (auto intro!: exI[of _ k])
  1.1162 +  qed
  1.1163 +  finally show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA"
  1.1164 +    apply (simp only:)
  1.1165 +    apply (intro sets.countable_UN sets.Diff)
  1.1166 +    apply (auto intro: sigma_sets_top)
  1.1167 +    done
  1.1168 +qed auto
  1.1169 +
  1.1170 +lemma borel_eq_lessThan:
  1.1171 +  "borel = sigma UNIV (range (\<lambda>a::'a::ordered_euclidean_space. {x. x <e a}))"
  1.1172 +  (is "_ = ?SIGMA")
  1.1173 +proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_ge])
  1.1174 +  fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
  1.1175 +  then have i: "i \<in> Basis" by auto
  1.1176 +  have "{x::'a. a \<le> x\<bullet>i} = UNIV - {x::'a. x\<bullet>i < a}" by auto
  1.1177 +  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>
  1.1178 +  proof (safe, simp_all add: eucl_less_def split: if_split_asm)
  1.1179 +    fix x :: 'a
  1.1180 +    from reals_Archimedean2[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"]
  1.1181 +    guess k::nat .. note k = this
  1.1182 +    { fix i :: 'a assume "i \<in> Basis"
  1.1183 +      then have "x\<bullet>i < real k"
  1.1184 +        using k by (subst (asm) Max_less_iff) auto
  1.1185 +      then have "x\<bullet>i < real k" by simp }
  1.1186 +    then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia < real k"
  1.1187 +      by (auto intro!: exI[of _ k])
  1.1188 +  qed
  1.1189 +  finally show "{x. a \<le> x\<bullet>i} \<in> ?SIGMA"
  1.1190 +    apply (simp only:)
  1.1191 +    apply (intro sets.countable_UN sets.Diff)
  1.1192 +    apply (auto intro: sigma_sets_top )
  1.1193 +    done
  1.1194 +qed auto
  1.1195 +
  1.1196 +lemma borel_eq_atLeastAtMost:
  1.1197 +  "borel = sigma UNIV (range (\<lambda>(a,b). {a..b} ::'a::ordered_euclidean_space set))"
  1.1198 +  (is "_ = ?SIGMA")
  1.1199 +proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
  1.1200 +  fix a::'a
  1.1201 +  have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
  1.1202 +  proof (safe, simp_all add: eucl_le[where 'a='a])
  1.1203 +    fix x :: 'a
  1.1204 +    from real_arch_simple[of "Max ((\<lambda>i. - x\<bullet>i)`Basis)"]
  1.1205 +    guess k::nat .. note k = this
  1.1206 +    { fix i :: 'a assume "i \<in> Basis"
  1.1207 +      with k have "- x\<bullet>i \<le> real k"
  1.1208 +        by (subst (asm) Max_le_iff) (auto simp: field_simps)
  1.1209 +      then have "- real k \<le> x\<bullet>i" by simp }
  1.1210 +    then show "\<exists>n::nat. \<forall>i\<in>Basis. - real n \<le> x \<bullet> i"
  1.1211 +      by (auto intro!: exI[of _ k])
  1.1212 +  qed
  1.1213 +  show "{..a} \<in> ?SIGMA" unfolding *
  1.1214 +    by (intro sets.countable_UN)
  1.1215 +       (auto intro!: sigma_sets_top)
  1.1216 +qed auto
  1.1217 +
  1.1218 +lemma borel_set_induct[consumes 1, case_names empty interval compl union]:
  1.1219 +  assumes "A \<in> sets borel"
  1.1220 +  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
  1.1221 +          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)"
  1.1222 +  shows "P (A::real set)"
  1.1223 +proof-
  1.1224 +  let ?G = "range (\<lambda>(a,b). {a..b::real})"
  1.1225 +  have "Int_stable ?G" "?G \<subseteq> Pow UNIV" "A \<in> sigma_sets UNIV ?G"
  1.1226 +      using assms(1) by (auto simp add: borel_eq_atLeastAtMost Int_stable_def)
  1.1227 +  thus ?thesis
  1.1228 +  proof (induction rule: sigma_sets_induct_disjoint)
  1.1229 +    case (union f)
  1.1230 +      from union.hyps(2) have "\<And>i. f i \<in> sets borel" by (auto simp: borel_eq_atLeastAtMost)
  1.1231 +      with union show ?case by (auto intro: un)
  1.1232 +  next
  1.1233 +    case (basic A)
  1.1234 +    then obtain a b where "A = {a .. b}" by auto
  1.1235 +    then show ?case
  1.1236 +      by (cases "a \<le> b") (auto intro: int empty)
  1.1237 +  qed (auto intro: empty compl simp: Compl_eq_Diff_UNIV[symmetric] borel_eq_atLeastAtMost)
  1.1238 +qed
  1.1239 +
  1.1240 +lemma borel_sigma_sets_Ioc: "borel = sigma UNIV (range (\<lambda>(a, b). {a <.. b::real}))"
  1.1241 +proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
  1.1242 +  fix i :: real
  1.1243 +  have "{..i} = (\<Union>j::nat. {-j <.. i})"
  1.1244 +    by (auto simp: minus_less_iff reals_Archimedean2)
  1.1245 +  also have "\<dots> \<in> sets (sigma UNIV (range (\<lambda>(i, j). {i<..j})))"
  1.1246 +    by (intro sets.countable_nat_UN) auto
  1.1247 +  finally show "{..i} \<in> sets (sigma UNIV (range (\<lambda>(i, j). {i<..j})))" .
  1.1248 +qed simp
  1.1249 +
  1.1250 +lemma eucl_lessThan: "{x::real. x <e a} = lessThan a"
  1.1251 +  by (simp add: eucl_less_def lessThan_def)
  1.1252 +
  1.1253 +lemma borel_eq_atLeastLessThan:
  1.1254 +  "borel = sigma UNIV (range (\<lambda>(a, b). {a ..< b :: real}))" (is "_ = ?SIGMA")
  1.1255 +proof (rule borel_eq_sigmaI5[OF borel_eq_lessThan])
  1.1256 +  have move_uminus: "\<And>x y::real. -x \<le> y \<longleftrightarrow> -y \<le> x" by auto
  1.1257 +  fix x :: real
  1.1258 +  have "{..<x} = (\<Union>i::nat. {-real i ..< x})"
  1.1259 +    by (auto simp: move_uminus real_arch_simple)
  1.1260 +  then show "{y. y <e x} \<in> ?SIGMA"
  1.1261 +    by (auto intro: sigma_sets.intros(2-) simp: eucl_lessThan)
  1.1262 +qed auto
  1.1263 +
  1.1264 +lemma borel_measurable_halfspacesI:
  1.1265 +  fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1.1266 +  assumes F: "borel = sigma UNIV (F ` (UNIV \<times> Basis))"
  1.1267 +  and S_eq: "\<And>a i. S a i = f -` F (a,i) \<inter> space M"
  1.1268 +  shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a::real. S a i \<in> sets M)"
  1.1269 +proof safe
  1.1270 +  fix a :: real and i :: 'b assume i: "i \<in> Basis" and f: "f \<in> borel_measurable M"
  1.1271 +  then show "S a i \<in> sets M" unfolding assms
  1.1272 +    by (auto intro!: measurable_sets simp: assms(1))
  1.1273 +next
  1.1274 +  assume a: "\<forall>i\<in>Basis. \<forall>a. S a i \<in> sets M"
  1.1275 +  then show "f \<in> borel_measurable M"
  1.1276 +    by (auto intro!: measurable_measure_of simp: S_eq F)
  1.1277 +qed
  1.1278 +
  1.1279 +lemma borel_measurable_iff_halfspace_le:
  1.1280 +  fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1.1281 +  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)"
  1.1282 +  by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_le]) auto
  1.1283 +
  1.1284 +lemma borel_measurable_iff_halfspace_less:
  1.1285 +  fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1.1286 +  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)"
  1.1287 +  by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_less]) auto
  1.1288 +
  1.1289 +lemma borel_measurable_iff_halfspace_ge:
  1.1290 +  fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1.1291 +  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)"
  1.1292 +  by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_ge]) auto
  1.1293 +
  1.1294 +lemma borel_measurable_iff_halfspace_greater:
  1.1295 +  fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1.1296 +  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)"
  1.1297 +  by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_greater]) auto
  1.1298 +
  1.1299 +lemma borel_measurable_iff_le:
  1.1300 +  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
  1.1301 +  using borel_measurable_iff_halfspace_le[where 'c=real] by simp
  1.1302 +
  1.1303 +lemma borel_measurable_iff_less:
  1.1304 +  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
  1.1305 +  using borel_measurable_iff_halfspace_less[where 'c=real] by simp
  1.1306 +
  1.1307 +lemma borel_measurable_iff_ge:
  1.1308 +  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
  1.1309 +  using borel_measurable_iff_halfspace_ge[where 'c=real]
  1.1310 +  by simp
  1.1311 +
  1.1312 +lemma borel_measurable_iff_greater:
  1.1313 +  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
  1.1314 +  using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
  1.1315 +
  1.1316 +lemma borel_measurable_euclidean_space:
  1.1317 +  fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  1.1318 +  shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M)"
  1.1319 +proof safe
  1.1320 +  assume f: "\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M"
  1.1321 +  then show "f \<in> borel_measurable M"
  1.1322 +    by (subst borel_measurable_iff_halfspace_le) auto
  1.1323 +qed auto
  1.1324 +
  1.1325 +subsection "Borel measurable operators"
  1.1326 +
  1.1327 +lemma borel_measurable_norm[measurable]: "norm \<in> borel_measurable borel"
  1.1328 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  1.1329 +
  1.1330 +lemma borel_measurable_sgn [measurable]: "(sgn::'a::real_normed_vector \<Rightarrow> 'a) \<in> borel_measurable borel"
  1.1331 +  by (rule borel_measurable_continuous_countable_exceptions[where X="{0}"])
  1.1332 +     (auto intro!: continuous_on_sgn continuous_on_id)
  1.1333 +
  1.1334 +lemma borel_measurable_uminus[measurable (raw)]:
  1.1335 +  fixes g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
  1.1336 +  assumes g: "g \<in> borel_measurable M"
  1.1337 +  shows "(\<lambda>x. - g x) \<in> borel_measurable M"
  1.1338 +  by (rule borel_measurable_continuous_on[OF _ g]) (intro continuous_intros)
  1.1339 +
  1.1340 +lemma borel_measurable_diff[measurable (raw)]:
  1.1341 +  fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
  1.1342 +  assumes f: "f \<in> borel_measurable M"
  1.1343 +  assumes g: "g \<in> borel_measurable M"
  1.1344 +  shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1.1345 +  using borel_measurable_add [of f M "- g"] assms by (simp add: fun_Compl_def)
  1.1346 +
  1.1347 +lemma borel_measurable_times[measurable (raw)]:
  1.1348 +  fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_algebra}"
  1.1349 +  assumes f: "f \<in> borel_measurable M"
  1.1350 +  assumes g: "g \<in> borel_measurable M"
  1.1351 +  shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1.1352 +  using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
  1.1353 +
  1.1354 +lemma borel_measurable_setprod[measurable (raw)]:
  1.1355 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> 'b::{second_countable_topology, real_normed_field}"
  1.1356 +  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1.1357 +  shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1.1358 +proof cases
  1.1359 +  assume "finite S"
  1.1360 +  thus ?thesis using assms by induct auto
  1.1361 +qed simp
  1.1362 +
  1.1363 +lemma borel_measurable_dist[measurable (raw)]:
  1.1364 +  fixes g f :: "'a \<Rightarrow> 'b::{second_countable_topology, metric_space}"
  1.1365 +  assumes f: "f \<in> borel_measurable M"
  1.1366 +  assumes g: "g \<in> borel_measurable M"
  1.1367 +  shows "(\<lambda>x. dist (f x) (g x)) \<in> borel_measurable M"
  1.1368 +  using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
  1.1369 +
  1.1370 +lemma borel_measurable_scaleR[measurable (raw)]:
  1.1371 +  fixes g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
  1.1372 +  assumes f: "f \<in> borel_measurable M"
  1.1373 +  assumes g: "g \<in> borel_measurable M"
  1.1374 +  shows "(\<lambda>x. f x *\<^sub>R g x) \<in> borel_measurable M"
  1.1375 +  using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
  1.1376 +
  1.1377 +lemma affine_borel_measurable_vector:
  1.1378 +  fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
  1.1379 +  assumes "f \<in> borel_measurable M"
  1.1380 +  shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
  1.1381 +proof (rule borel_measurableI)
  1.1382 +  fix S :: "'x set" assume "open S"
  1.1383 +  show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
  1.1384 +  proof cases
  1.1385 +    assume "b \<noteq> 0"
  1.1386 +    with \<open>open S\<close> have "open ((\<lambda>x. (- a + x) /\<^sub>R b) ` S)" (is "open ?S")
  1.1387 +      using open_affinity [of S "inverse b" "- a /\<^sub>R b"]
  1.1388 +      by (auto simp: algebra_simps)
  1.1389 +    hence "?S \<in> sets borel" by auto
  1.1390 +    moreover
  1.1391 +    from \<open>b \<noteq> 0\<close> have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
  1.1392 +      apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
  1.1393 +    ultimately show ?thesis using assms unfolding in_borel_measurable_borel
  1.1394 +      by auto
  1.1395 +  qed simp
  1.1396 +qed
  1.1397 +
  1.1398 +lemma borel_measurable_const_scaleR[measurable (raw)]:
  1.1399 +  "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. b *\<^sub>R f x ::'a::real_normed_vector) \<in> borel_measurable M"
  1.1400 +  using affine_borel_measurable_vector[of f M 0 b] by simp
  1.1401 +
  1.1402 +lemma borel_measurable_const_add[measurable (raw)]:
  1.1403 +  "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. a + f x ::'a::real_normed_vector) \<in> borel_measurable M"
  1.1404 +  using affine_borel_measurable_vector[of f M a 1] by simp
  1.1405 +
  1.1406 +lemma borel_measurable_inverse[measurable (raw)]:
  1.1407 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_div_algebra"
  1.1408 +  assumes f: "f \<in> borel_measurable M"
  1.1409 +  shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
  1.1410 +  apply (rule measurable_compose[OF f])
  1.1411 +  apply (rule borel_measurable_continuous_countable_exceptions[of "{0}"])
  1.1412 +  apply (auto intro!: continuous_on_inverse continuous_on_id)
  1.1413 +  done
  1.1414 +
  1.1415 +lemma borel_measurable_divide[measurable (raw)]:
  1.1416 +  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow>
  1.1417 +    (\<lambda>x. f x / g x::'b::{second_countable_topology, real_normed_div_algebra}) \<in> borel_measurable M"
  1.1418 +  by (simp add: divide_inverse)
  1.1419 +
  1.1420 +lemma borel_measurable_abs[measurable (raw)]:
  1.1421 +  "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
  1.1422 +  unfolding abs_real_def by simp
  1.1423 +
  1.1424 +lemma borel_measurable_nth[measurable (raw)]:
  1.1425 +  "(\<lambda>x::real^'n. x $ i) \<in> borel_measurable borel"
  1.1426 +  by (simp add: cart_eq_inner_axis)
  1.1427 +
  1.1428 +lemma convex_measurable:
  1.1429 +  fixes A :: "'a :: euclidean_space set"
  1.1430 +  shows "X \<in> borel_measurable M \<Longrightarrow> X ` space M \<subseteq> A \<Longrightarrow> open A \<Longrightarrow> convex_on A q \<Longrightarrow>
  1.1431 +    (\<lambda>x. q (X x)) \<in> borel_measurable M"
  1.1432 +  by (rule measurable_compose[where f=X and N="restrict_space borel A"])
  1.1433 +     (auto intro!: borel_measurable_continuous_on_restrict convex_on_continuous measurable_restrict_space2)
  1.1434 +
  1.1435 +lemma borel_measurable_ln[measurable (raw)]:
  1.1436 +  assumes f: "f \<in> borel_measurable M"
  1.1437 +  shows "(\<lambda>x. ln (f x :: real)) \<in> borel_measurable M"
  1.1438 +  apply (rule measurable_compose[OF f])
  1.1439 +  apply (rule borel_measurable_continuous_countable_exceptions[of "{0}"])
  1.1440 +  apply (auto intro!: continuous_on_ln continuous_on_id)
  1.1441 +  done
  1.1442 +
  1.1443 +lemma borel_measurable_log[measurable (raw)]:
  1.1444 +  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. log (g x) (f x)) \<in> borel_measurable M"
  1.1445 +  unfolding log_def by auto
  1.1446 +
  1.1447 +lemma borel_measurable_exp[measurable]:
  1.1448 +  "(exp::'a::{real_normed_field,banach}\<Rightarrow>'a) \<in> borel_measurable borel"
  1.1449 +  by (intro borel_measurable_continuous_on1 continuous_at_imp_continuous_on ballI isCont_exp)
  1.1450 +
  1.1451 +lemma measurable_real_floor[measurable]:
  1.1452 +  "(floor :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
  1.1453 +proof -
  1.1454 +  have "\<And>a x. \<lfloor>x\<rfloor> = a \<longleftrightarrow> (real_of_int a \<le> x \<and> x < real_of_int (a + 1))"
  1.1455 +    by (auto intro: floor_eq2)
  1.1456 +  then show ?thesis
  1.1457 +    by (auto simp: vimage_def measurable_count_space_eq2_countable)
  1.1458 +qed
  1.1459 +
  1.1460 +lemma measurable_real_ceiling[measurable]:
  1.1461 +  "(ceiling :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
  1.1462 +  unfolding ceiling_def[abs_def] by simp
  1.1463 +
  1.1464 +lemma borel_measurable_real_floor: "(\<lambda>x::real. real_of_int \<lfloor>x\<rfloor>) \<in> borel_measurable borel"
  1.1465 +  by simp
  1.1466 +
  1.1467 +lemma borel_measurable_root [measurable]: "root n \<in> borel_measurable borel"
  1.1468 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  1.1469 +
  1.1470 +lemma borel_measurable_sqrt [measurable]: "sqrt \<in> borel_measurable borel"
  1.1471 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  1.1472 +
  1.1473 +lemma borel_measurable_power [measurable (raw)]:
  1.1474 +  fixes f :: "_ \<Rightarrow> 'b::{power,real_normed_algebra}"
  1.1475 +  assumes f: "f \<in> borel_measurable M"
  1.1476 +  shows "(\<lambda>x. (f x) ^ n) \<in> borel_measurable M"
  1.1477 +  by (intro borel_measurable_continuous_on [OF _ f] continuous_intros)
  1.1478 +
  1.1479 +lemma borel_measurable_Re [measurable]: "Re \<in> borel_measurable borel"
  1.1480 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  1.1481 +
  1.1482 +lemma borel_measurable_Im [measurable]: "Im \<in> borel_measurable borel"
  1.1483 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  1.1484 +
  1.1485 +lemma borel_measurable_of_real [measurable]: "(of_real :: _ \<Rightarrow> (_::real_normed_algebra)) \<in> borel_measurable borel"
  1.1486 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  1.1487 +
  1.1488 +lemma borel_measurable_sin [measurable]: "(sin :: _ \<Rightarrow> (_::{real_normed_field,banach})) \<in> borel_measurable borel"
  1.1489 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  1.1490 +
  1.1491 +lemma borel_measurable_cos [measurable]: "(cos :: _ \<Rightarrow> (_::{real_normed_field,banach})) \<in> borel_measurable borel"
  1.1492 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  1.1493 +
  1.1494 +lemma borel_measurable_arctan [measurable]: "arctan \<in> borel_measurable borel"
  1.1495 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  1.1496 +
  1.1497 +lemma borel_measurable_complex_iff:
  1.1498 +  "f \<in> borel_measurable M \<longleftrightarrow>
  1.1499 +    (\<lambda>x. Re (f x)) \<in> borel_measurable M \<and> (\<lambda>x. Im (f x)) \<in> borel_measurable M"
  1.1500 +  apply auto
  1.1501 +  apply (subst fun_complex_eq)
  1.1502 +  apply (intro borel_measurable_add)
  1.1503 +  apply auto
  1.1504 +  done
  1.1505 +
  1.1506 +subsection "Borel space on the extended reals"
  1.1507 +
  1.1508 +lemma borel_measurable_ereal[measurable (raw)]:
  1.1509 +  assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
  1.1510 +  using continuous_on_ereal f by (rule borel_measurable_continuous_on) (rule continuous_on_id)
  1.1511 +
  1.1512 +lemma borel_measurable_real_of_ereal[measurable (raw)]:
  1.1513 +  fixes f :: "'a \<Rightarrow> ereal"
  1.1514 +  assumes f: "f \<in> borel_measurable M"
  1.1515 +  shows "(\<lambda>x. real_of_ereal (f x)) \<in> borel_measurable M"
  1.1516 +  apply (rule measurable_compose[OF f])
  1.1517 +  apply (rule borel_measurable_continuous_countable_exceptions[of "{\<infinity>, -\<infinity> }"])
  1.1518 +  apply (auto intro: continuous_on_real simp: Compl_eq_Diff_UNIV)
  1.1519 +  done
  1.1520 +
  1.1521 +lemma borel_measurable_ereal_cases:
  1.1522 +  fixes f :: "'a \<Rightarrow> ereal"
  1.1523 +  assumes f: "f \<in> borel_measurable M"
  1.1524 +  assumes H: "(\<lambda>x. H (ereal (real_of_ereal (f x)))) \<in> borel_measurable M"
  1.1525 +  shows "(\<lambda>x. H (f x)) \<in> borel_measurable M"
  1.1526 +proof -
  1.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)))"
  1.1528 +  { fix x have "H (f x) = ?F x" by (cases "f x") auto }
  1.1529 +  with f H show ?thesis by simp
  1.1530 +qed
  1.1531 +
  1.1532 +lemma
  1.1533 +  fixes f :: "'a \<Rightarrow> ereal" assumes f[measurable]: "f \<in> borel_measurable M"
  1.1534 +  shows borel_measurable_ereal_abs[measurable(raw)]: "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
  1.1535 +    and borel_measurable_ereal_inverse[measurable(raw)]: "(\<lambda>x. inverse (f x) :: ereal) \<in> borel_measurable M"
  1.1536 +    and borel_measurable_uminus_ereal[measurable(raw)]: "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
  1.1537 +  by (auto simp del: abs_real_of_ereal simp: borel_measurable_ereal_cases[OF f] measurable_If)
  1.1538 +
  1.1539 +lemma borel_measurable_uminus_eq_ereal[simp]:
  1.1540 +  "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
  1.1541 +proof
  1.1542 +  assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
  1.1543 +qed auto
  1.1544 +
  1.1545 +lemma set_Collect_ereal2:
  1.1546 +  fixes f g :: "'a \<Rightarrow> ereal"
  1.1547 +  assumes f: "f \<in> borel_measurable M"
  1.1548 +  assumes g: "g \<in> borel_measurable M"
  1.1549 +  assumes H: "{x \<in> space M. H (ereal (real_of_ereal (f x))) (ereal (real_of_ereal (g x)))} \<in> sets M"
  1.1550 +    "{x \<in> space borel. H (-\<infinity>) (ereal x)} \<in> sets borel"
  1.1551 +    "{x \<in> space borel. H (\<infinity>) (ereal x)} \<in> sets borel"
  1.1552 +    "{x \<in> space borel. H (ereal x) (-\<infinity>)} \<in> sets borel"
  1.1553 +    "{x \<in> space borel. H (ereal x) (\<infinity>)} \<in> sets borel"
  1.1554 +  shows "{x \<in> space M. H (f x) (g x)} \<in> sets M"
  1.1555 +proof -
  1.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)))"
  1.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"
  1.1558 +  { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
  1.1559 +  note * = this
  1.1560 +  from assms show ?thesis
  1.1561 +    by (subst *) (simp del: space_borel split del: if_split)
  1.1562 +qed
  1.1563 +
  1.1564 +lemma borel_measurable_ereal_iff:
  1.1565 +  shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
  1.1566 +proof
  1.1567 +  assume "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
  1.1568 +  from borel_measurable_real_of_ereal[OF this]
  1.1569 +  show "f \<in> borel_measurable M" by auto
  1.1570 +qed auto
  1.1571 +
  1.1572 +lemma borel_measurable_erealD[measurable_dest]:
  1.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"
  1.1574 +  unfolding borel_measurable_ereal_iff by simp
  1.1575 +
  1.1576 +lemma borel_measurable_ereal_iff_real:
  1.1577 +  fixes f :: "'a \<Rightarrow> ereal"
  1.1578 +  shows "f \<in> borel_measurable M \<longleftrightarrow>
  1.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)"
  1.1580 +proof safe
  1.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"
  1.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
  1.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
  1.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))"
  1.1585 +  have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
  1.1586 +  also have "?f = f" by (auto simp: fun_eq_iff ereal_real)
  1.1587 +  finally show "f \<in> borel_measurable M" .
  1.1588 +qed simp_all
  1.1589 +
  1.1590 +lemma borel_measurable_ereal_iff_Iio:
  1.1591 +  "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
  1.1592 +  by (auto simp: borel_Iio measurable_iff_measure_of)
  1.1593 +
  1.1594 +lemma borel_measurable_ereal_iff_Ioi:
  1.1595 +  "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
  1.1596 +  by (auto simp: borel_Ioi measurable_iff_measure_of)
  1.1597 +
  1.1598 +lemma vimage_sets_compl_iff:
  1.1599 +  "f -` A \<inter> space M \<in> sets M \<longleftrightarrow> f -` (- A) \<inter> space M \<in> sets M"
  1.1600 +proof -
  1.1601 +  { fix A assume "f -` A \<inter> space M \<in> sets M"
  1.1602 +    moreover have "f -` (- A) \<inter> space M = space M - f -` A \<inter> space M" by auto
  1.1603 +    ultimately have "f -` (- A) \<inter> space M \<in> sets M" by auto }
  1.1604 +  from this[of A] this[of "-A"] show ?thesis
  1.1605 +    by (metis double_complement)
  1.1606 +qed
  1.1607 +
  1.1608 +lemma borel_measurable_iff_Iic_ereal:
  1.1609 +  "(f::'a\<Rightarrow>ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
  1.1610 +  unfolding borel_measurable_ereal_iff_Ioi vimage_sets_compl_iff[where A="{a <..}" for a] by simp
  1.1611 +
  1.1612 +lemma borel_measurable_iff_Ici_ereal:
  1.1613 +  "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
  1.1614 +  unfolding borel_measurable_ereal_iff_Iio vimage_sets_compl_iff[where A="{..< a}" for a] by simp
  1.1615 +
  1.1616 +lemma borel_measurable_ereal2:
  1.1617 +  fixes f g :: "'a \<Rightarrow> ereal"
  1.1618 +  assumes f: "f \<in> borel_measurable M"
  1.1619 +  assumes g: "g \<in> borel_measurable M"
  1.1620 +  assumes H: "(\<lambda>x. H (ereal (real_of_ereal (f x))) (ereal (real_of_ereal (g x)))) \<in> borel_measurable M"
  1.1621 +    "(\<lambda>x. H (-\<infinity>) (ereal (real_of_ereal (g x)))) \<in> borel_measurable M"
  1.1622 +    "(\<lambda>x. H (\<infinity>) (ereal (real_of_ereal (g x)))) \<in> borel_measurable M"
  1.1623 +    "(\<lambda>x. H (ereal (real_of_ereal (f x))) (-\<infinity>)) \<in> borel_measurable M"
  1.1624 +    "(\<lambda>x. H (ereal (real_of_ereal (f x))) (\<infinity>)) \<in> borel_measurable M"
  1.1625 +  shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
  1.1626 +proof -
  1.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)))"
  1.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"
  1.1629 +  { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
  1.1630 +  note * = this
  1.1631 +  from assms show ?thesis unfolding * by simp
  1.1632 +qed
  1.1633 +
  1.1634 +lemma [measurable(raw)]:
  1.1635 +  fixes f :: "'a \<Rightarrow> ereal"
  1.1636 +  assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1.1637 +  shows borel_measurable_ereal_add: "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  1.1638 +    and borel_measurable_ereal_times: "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1.1639 +  by (simp_all add: borel_measurable_ereal2)
  1.1640 +
  1.1641 +lemma [measurable(raw)]:
  1.1642 +  fixes f g :: "'a \<Rightarrow> ereal"
  1.1643 +  assumes "f \<in> borel_measurable M"
  1.1644 +  assumes "g \<in> borel_measurable M"
  1.1645 +  shows borel_measurable_ereal_diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1.1646 +    and borel_measurable_ereal_divide: "(\<lambda>x. f x / g x) \<in> borel_measurable M"
  1.1647 +  using assms by (simp_all add: minus_ereal_def divide_ereal_def)
  1.1648 +
  1.1649 +lemma borel_measurable_ereal_setsum[measurable (raw)]:
  1.1650 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1.1651 +  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1.1652 +  shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
  1.1653 +  using assms by (induction S rule: infinite_finite_induct) auto
  1.1654 +
  1.1655 +lemma borel_measurable_ereal_setprod[measurable (raw)]:
  1.1656 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1.1657 +  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1.1658 +  shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1.1659 +  using assms by (induction S rule: infinite_finite_induct) auto
  1.1660 +
  1.1661 +lemma borel_measurable_extreal_suminf[measurable (raw)]:
  1.1662 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1.1663 +  assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1.1664 +  shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
  1.1665 +  unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1.1666 +
  1.1667 +subsection "Borel space on the extended non-negative reals"
  1.1668 +
  1.1669 +text \<open> @{type ennreal} is a topological monoid, so no rules for plus are required, also all order
  1.1670 +  statements are usually done on type classes. \<close>
  1.1671 +
  1.1672 +lemma measurable_enn2ereal[measurable]: "enn2ereal \<in> borel \<rightarrow>\<^sub>M borel"
  1.1673 +  by (intro borel_measurable_continuous_on1 continuous_on_enn2ereal)
  1.1674 +
  1.1675 +lemma measurable_e2ennreal[measurable]: "e2ennreal \<in> borel \<rightarrow>\<^sub>M borel"
  1.1676 +  by (intro borel_measurable_continuous_on1 continuous_on_e2ennreal)
  1.1677 +
  1.1678 +lemma borel_measurable_enn2real[measurable (raw)]:
  1.1679 +  "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. enn2real (f x)) \<in> M \<rightarrow>\<^sub>M borel"
  1.1680 +  unfolding enn2real_def[abs_def] by measurable
  1.1681 +
  1.1682 +definition [simp]: "is_borel f M \<longleftrightarrow> f \<in> borel_measurable M"
  1.1683 +
  1.1684 +lemma is_borel_transfer[transfer_rule]: "rel_fun (rel_fun op = pcr_ennreal) op = is_borel is_borel"
  1.1685 +  unfolding is_borel_def[abs_def]
  1.1686 +proof (safe intro!: rel_funI ext dest!: rel_fun_eq_pcr_ennreal[THEN iffD1])
  1.1687 +  fix f and M :: "'a measure"
  1.1688 +  show "f \<in> borel_measurable M" if f: "enn2ereal \<circ> f \<in> borel_measurable M"
  1.1689 +    using measurable_compose[OF f measurable_e2ennreal] by simp
  1.1690 +qed simp
  1.1691 +
  1.1692 +context
  1.1693 +  includes ennreal.lifting
  1.1694 +begin
  1.1695 +
  1.1696 +lemma measurable_ennreal[measurable]: "ennreal \<in> borel \<rightarrow>\<^sub>M borel"
  1.1697 +  unfolding is_borel_def[symmetric]
  1.1698 +  by transfer simp
  1.1699 +
  1.1700 +lemma borel_measurable_ennreal_iff[simp]:
  1.1701 +  assumes [simp]: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  1.1702 +  shows "(\<lambda>x. ennreal (f x)) \<in> M \<rightarrow>\<^sub>M borel \<longleftrightarrow> f \<in> M \<rightarrow>\<^sub>M borel"
  1.1703 +proof safe
  1.1704 +  assume "(\<lambda>x. ennreal (f x)) \<in> M \<rightarrow>\<^sub>M borel"
  1.1705 +  then have "(\<lambda>x. enn2real (ennreal (f x))) \<in> M \<rightarrow>\<^sub>M borel"
  1.1706 +    by measurable
  1.1707 +  then show "f \<in> M \<rightarrow>\<^sub>M borel"
  1.1708 +    by (rule measurable_cong[THEN iffD1, rotated]) auto
  1.1709 +qed measurable
  1.1710 +
  1.1711 +lemma borel_measurable_times_ennreal[measurable (raw)]:
  1.1712 +  fixes f g :: "'a \<Rightarrow> ennreal"
  1.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"
  1.1714 +  unfolding is_borel_def[symmetric] by transfer simp
  1.1715 +
  1.1716 +lemma borel_measurable_inverse_ennreal[measurable (raw)]:
  1.1717 +  fixes f :: "'a \<Rightarrow> ennreal"
  1.1718 +  shows "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. inverse (f x)) \<in> M \<rightarrow>\<^sub>M borel"
  1.1719 +  unfolding is_borel_def[symmetric] by transfer simp
  1.1720 +
  1.1721 +lemma borel_measurable_divide_ennreal[measurable (raw)]:
  1.1722 +  fixes f :: "'a \<Rightarrow> ennreal"
  1.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"
  1.1724 +  unfolding divide_ennreal_def by simp
  1.1725 +
  1.1726 +lemma borel_measurable_minus_ennreal[measurable (raw)]:
  1.1727 +  fixes f :: "'a \<Rightarrow> ennreal"
  1.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"
  1.1729 +  unfolding is_borel_def[symmetric] by transfer simp
  1.1730 +
  1.1731 +lemma borel_measurable_setprod_ennreal[measurable (raw)]:
  1.1732 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ennreal"
  1.1733 +  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1.1734 +  shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1.1735 +  using assms by (induction S rule: infinite_finite_induct) auto
  1.1736 +
  1.1737 +end
  1.1738 +
  1.1739 +hide_const (open) is_borel
  1.1740 +
  1.1741 +subsection \<open>LIMSEQ is borel measurable\<close>
  1.1742 +
  1.1743 +lemma borel_measurable_LIMSEQ_real:
  1.1744 +  fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1.1745 +  assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) \<longlonglongrightarrow> u' x"
  1.1746 +  and u: "\<And>i. u i \<in> borel_measurable M"
  1.1747 +  shows "u' \<in> borel_measurable M"
  1.1748 +proof -
  1.1749 +  have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. ereal (u n x)) = ereal (u' x)"
  1.1750 +    using u' by (simp add: lim_imp_Liminf)
  1.1751 +  moreover from u have "(\<lambda>x. liminf (\<lambda>n. ereal (u n x))) \<in> borel_measurable M"
  1.1752 +    by auto
  1.1753 +  ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_ereal_iff)
  1.1754 +qed
  1.1755 +
  1.1756 +lemma borel_measurable_LIMSEQ_metric:
  1.1757 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b :: metric_space"
  1.1758 +  assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1.1759 +  assumes lim: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. f i x) \<longlonglongrightarrow> g x"
  1.1760 +  shows "g \<in> borel_measurable M"
  1.1761 +  unfolding borel_eq_closed
  1.1762 +proof (safe intro!: measurable_measure_of)
  1.1763 +  fix A :: "'b set" assume "closed A"
  1.1764 +
  1.1765 +  have [measurable]: "(\<lambda>x. infdist (g x) A) \<in> borel_measurable M"
  1.1766 +  proof (rule borel_measurable_LIMSEQ_real)
  1.1767 +    show "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. infdist (f i x) A) \<longlonglongrightarrow> infdist (g x) A"
  1.1768 +      by (intro tendsto_infdist lim)
  1.1769 +    show "\<And>i. (\<lambda>x. infdist (f i x) A) \<in> borel_measurable M"
  1.1770 +      by (intro borel_measurable_continuous_on[where f="\<lambda>x. infdist x A"]
  1.1771 +        continuous_at_imp_continuous_on ballI continuous_infdist continuous_ident) auto
  1.1772 +  qed
  1.1773 +
  1.1774 +  show "g -` A \<inter> space M \<in> sets M"
  1.1775 +  proof cases
  1.1776 +    assume "A \<noteq> {}"
  1.1777 +    then have "\<And>x. infdist x A = 0 \<longleftrightarrow> x \<in> A"
  1.1778 +      using \<open>closed A\<close> by (simp add: in_closed_iff_infdist_zero)
  1.1779 +    then have "g -` A \<inter> space M = {x\<in>space M. infdist (g x) A = 0}"
  1.1780 +      by auto
  1.1781 +    also have "\<dots> \<in> sets M"
  1.1782 +      by measurable
  1.1783 +    finally show ?thesis .
  1.1784 +  qed simp
  1.1785 +qed auto
  1.1786 +
  1.1787 +lemma sets_Collect_Cauchy[measurable]:
  1.1788 +  fixes f :: "nat \<Rightarrow> 'a => 'b::{metric_space, second_countable_topology}"
  1.1789 +  assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1.1790 +  shows "{x\<in>space M. Cauchy (\<lambda>i. f i x)} \<in> sets M"
  1.1791 +  unfolding metric_Cauchy_iff2 using f by auto
  1.1792 +
  1.1793 +lemma borel_measurable_lim_metric[measurable (raw)]:
  1.1794 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1.1795 +  assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1.1796 +  shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
  1.1797 +proof -
  1.1798 +  define u' where "u' x = lim (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)" for x
  1.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))"
  1.1800 +    by (auto simp: lim_def convergent_eq_cauchy[symmetric])
  1.1801 +  have "u' \<in> borel_measurable M"
  1.1802 +  proof (rule borel_measurable_LIMSEQ_metric)
  1.1803 +    fix x
  1.1804 +    have "convergent (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
  1.1805 +      by (cases "Cauchy (\<lambda>i. f i x)")
  1.1806 +         (auto simp add: convergent_eq_cauchy[symmetric] convergent_def)
  1.1807 +    then show "(\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0) \<longlonglongrightarrow> u' x"
  1.1808 +      unfolding u'_def
  1.1809 +      by (rule convergent_LIMSEQ_iff[THEN iffD1])
  1.1810 +  qed measurable
  1.1811 +  then show ?thesis
  1.1812 +    unfolding * by measurable
  1.1813 +qed
  1.1814 +
  1.1815 +lemma borel_measurable_suminf[measurable (raw)]:
  1.1816 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  1.1817 +  assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1.1818 +  shows "(\<lambda>x. suminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1.1819 +  unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1.1820 +
  1.1821 +lemma Collect_closed_imp_pred_borel: "closed {x. P x} \<Longrightarrow> Measurable.pred borel P"
  1.1822 +  by (simp add: pred_def)
  1.1823 +
  1.1824 +(* Proof by Jeremy Avigad and Luke Serafin *)
  1.1825 +lemma isCont_borel_pred[measurable]:
  1.1826 +  fixes f :: "'b::metric_space \<Rightarrow> 'a::metric_space"
  1.1827 +  shows "Measurable.pred borel (isCont f)"
  1.1828 +proof (subst measurable_cong)
  1.1829 +  let ?I = "\<lambda>j. inverse(real (Suc j))"
  1.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
  1.1831 +    unfolding continuous_at_eps_delta
  1.1832 +  proof safe
  1.1833 +    fix i assume "\<forall>e>0. \<exists>d>0. \<forall>y. dist y x < d \<longrightarrow> dist (f y) (f x) < e"
  1.1834 +    moreover have "0 < ?I i / 2"
  1.1835 +      by simp
  1.1836 +    ultimately obtain d where d: "0 < d" "\<And>y. dist x y < d \<Longrightarrow> dist (f y) (f x) < ?I i / 2"
  1.1837 +      by (metis dist_commute)
  1.1838 +    then obtain j where j: "?I j < d"
  1.1839 +      by (metis reals_Archimedean)
  1.1840 +
  1.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"
  1.1842 +    proof (safe intro!: exI[where x=j])
  1.1843 +      fix y z assume *: "dist x y < ?I j" "dist x z < ?I j"
  1.1844 +      have "dist (f y) (f z) \<le> dist (f y) (f x) + dist (f z) (f x)"
  1.1845 +        by (rule dist_triangle2)
  1.1846 +      also have "\<dots> < ?I i / 2 + ?I i / 2"
  1.1847 +        by (intro add_strict_mono d less_trans[OF _ j] *)
  1.1848 +      also have "\<dots> \<le> ?I i"
  1.1849 +        by (simp add: field_simps of_nat_Suc)
  1.1850 +      finally show "dist (f y) (f z) \<le> ?I i"
  1.1851 +        by simp
  1.1852 +    qed
  1.1853 +  next
  1.1854 +    fix e::real assume "0 < e"
  1.1855 +    then obtain n where n: "?I n < e"
  1.1856 +      by (metis reals_Archimedean)
  1.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"
  1.1858 +    from this[THEN spec, of "Suc n"]
  1.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)"
  1.1860 +      by auto
  1.1861 +
  1.1862 +    show "\<exists>d>0. \<forall>y. dist y x < d \<longrightarrow> dist (f y) (f x) < e"
  1.1863 +    proof (safe intro!: exI[of _ "?I j"])
  1.1864 +      fix y assume "dist y x < ?I j"
  1.1865 +      then have "dist (f y) (f x) \<le> ?I (Suc n)"
  1.1866 +        by (intro j) (auto simp: dist_commute)
  1.1867 +      also have "?I (Suc n) < ?I n"
  1.1868 +        by simp
  1.1869 +      also note n
  1.1870 +      finally show "dist (f y) (f x) < e" .
  1.1871 +    qed simp
  1.1872 +  qed
  1.1873 +qed (intro pred_intros_countable closed_Collect_all closed_Collect_le open_Collect_less
  1.1874 +           Collect_closed_imp_pred_borel closed_Collect_imp open_Collect_conj continuous_intros)
  1.1875 +
  1.1876 +lemma isCont_borel:
  1.1877 +  fixes f :: "'b::metric_space \<Rightarrow> 'a::metric_space"
  1.1878 +  shows "{x. isCont f x} \<in> sets borel"
  1.1879 +  by simp
  1.1880 +
  1.1881 +lemma is_real_interval:
  1.1882 +  assumes S: "is_interval S"
  1.1883 +  shows "\<exists>a b::real. S = {} \<or> S = UNIV \<or> S = {..<b} \<or> S = {..b} \<or> S = {a<..} \<or> S = {a..} \<or>
  1.1884 +    S = {a<..<b} \<or> S = {a<..b} \<or> S = {a..<b} \<or> S = {a..b}"
  1.1885 +  using S unfolding is_interval_1 by (blast intro: interval_cases)
  1.1886 +
  1.1887 +lemma real_interval_borel_measurable:
  1.1888 +  assumes "is_interval (S::real set)"
  1.1889 +  shows "S \<in> sets borel"
  1.1890 +proof -
  1.1891 +  from assms is_real_interval have "\<exists>a b::real. S = {} \<or> S = UNIV \<or> S = {..<b} \<or> S = {..b} \<or>
  1.1892 +    S = {a<..} \<or> S = {a..} \<or> S = {a<..<b} \<or> S = {a<..b} \<or> S = {a..<b} \<or> S = {a..b}" by auto
  1.1893 +  then guess a ..
  1.1894 +  then guess b ..
  1.1895 +  thus ?thesis
  1.1896 +    by auto
  1.1897 +qed
  1.1898 +
  1.1899 +lemma borel_measurable_mono_on_fnc:
  1.1900 +  fixes f :: "real \<Rightarrow> real" and A :: "real set"
  1.1901 +  assumes "mono_on f A"
  1.1902 +  shows "f \<in> borel_measurable (restrict_space borel A)"
  1.1903 +  apply (rule measurable_restrict_countable[OF mono_on_ctble_discont[OF assms]])
  1.1904 +  apply (auto intro!: image_eqI[where x="{x}" for x] simp: sets_restrict_space)
  1.1905 +  apply (auto simp add: sets_restrict_restrict_space continuous_on_eq_continuous_within
  1.1906 +              cong: measurable_cong_sets
  1.1907 +              intro!: borel_measurable_continuous_on_restrict intro: continuous_within_subset)
  1.1908 +  done
  1.1909 +
  1.1910 +lemma borel_measurable_mono:
  1.1911 +  fixes f :: "real \<Rightarrow> real"
  1.1912 +  shows "mono f \<Longrightarrow> f \<in> borel_measurable borel"
  1.1913 +  using borel_measurable_mono_on_fnc[of f UNIV] by (simp add: mono_def mono_on_def)
  1.1914 +
  1.1915 +no_notation
  1.1916 +  eucl_less (infix "<e" 50)
  1.1917 +
  1.1918 +end