
wenzelm@42150: (*  Title:      HOL/Probability/Borel_Space.thy
hoelzl@42067:     Author:     Johannes Hölzl, TU München
hoelzl@42067:     Author:     Armin Heller, TU München
hoelzl@42067: *)
hoelzl@38656: 
hoelzl@38656: header {*Borel spaces*}
paulson@33533: 
hoelzl@40859: theory Borel_Space
hoelzl@50387: imports
hoelzl@50387:   Measurable
hoelzl@50387:   "~~/src/HOL/Multivariate_Analysis/Multivariate_Analysis"
paulson@33533: begin
paulson@33533: 
hoelzl@56994: subsection {* Generic Borel spaces *}
paulson@33533: 
hoelzl@47694: definition borel :: "'a::topological_space measure" where
hoelzl@47694:   "borel = sigma UNIV {S. open S}"
paulson@33533: 
hoelzl@47694: abbreviation "borel_measurable M \<equiv> measurable M borel"
paulson@33533: 
paulson@33533: lemma in_borel_measurable:
paulson@33533:    "f \<in> borel_measurable M \<longleftrightarrow>
hoelzl@47694:     (\<forall>S \<in> sigma_sets UNIV {S. open S}. f -` S \<inter> space M \<in> sets M)"
hoelzl@40859:   by (auto simp add: measurable_def borel_def)
paulson@33533: 
hoelzl@40859: lemma in_borel_measurable_borel:
hoelzl@38656:    "f \<in> borel_measurable M \<longleftrightarrow>
hoelzl@40859:     (\<forall>S \<in> sets borel.
hoelzl@38656:       f -` S \<inter> space M \<in> sets M)"
hoelzl@40859:   by (auto simp add: measurable_def borel_def)
paulson@33533: 
hoelzl@40859: lemma space_borel[simp]: "space borel = UNIV"
hoelzl@40859:   unfolding borel_def by auto
hoelzl@38656: 
hoelzl@50002: lemma space_in_borel[measurable]: "UNIV \<in> sets borel"
hoelzl@50002:   unfolding borel_def by auto
hoelzl@50002: 
hoelzl@57235: lemma sets_borel: "sets borel = sigma_sets UNIV {S. open S}"
hoelzl@57235:   unfolding borel_def by (rule sets_measure_of) simp
hoelzl@57235: 
hoelzl@50387: lemma pred_Collect_borel[measurable (raw)]: "Measurable.pred borel P \<Longrightarrow> {x. P x} \<in> sets borel"
hoelzl@50002:   unfolding borel_def pred_def by auto
hoelzl@50002: 
hoelzl@50003: lemma borel_open[measurable (raw generic)]:
hoelzl@40859:   assumes "open A" shows "A \<in> sets borel"
hoelzl@38656: proof -
huffman@44537:   have "A \<in> {S. open S}" unfolding mem_Collect_eq using assms .
hoelzl@47694:   thus ?thesis unfolding borel_def by auto
paulson@33533: qed
paulson@33533: 
hoelzl@50003: lemma borel_closed[measurable (raw generic)]:
hoelzl@40859:   assumes "closed A" shows "A \<in> sets borel"
paulson@33533: proof -
hoelzl@40859:   have "space borel - (- A) \<in> sets borel"
hoelzl@40859:     using assms unfolding closed_def by (blast intro: borel_open)
hoelzl@38656:   thus ?thesis by simp
paulson@33533: qed
paulson@33533: 
hoelzl@50003: lemma borel_singleton[measurable]:
hoelzl@50003:   "A \<in> sets borel \<Longrightarrow> insert x A \<in> sets (borel :: 'a::t1_space measure)"
immler@50244:   unfolding insert_def by (rule sets.Un) auto
hoelzl@50002: 
hoelzl@50003: lemma borel_comp[measurable]: "A \<in> sets borel \<Longrightarrow> - A \<in> sets borel"
hoelzl@50002:   unfolding Compl_eq_Diff_UNIV by simp
hoelzl@41830: 
hoelzl@47694: lemma borel_measurable_vimage:
hoelzl@38656:   fixes f :: "'a \<Rightarrow> 'x::t2_space"
hoelzl@50002:   assumes borel[measurable]: "f \<in> borel_measurable M"
hoelzl@38656:   shows "f -` {x} \<inter> space M \<in> sets M"
hoelzl@50002:   by simp
paulson@33533: 
hoelzl@47694: lemma borel_measurableI:
hoelzl@38656:   fixes f :: "'a \<Rightarrow> 'x\<Colon>topological_space"
hoelzl@38656:   assumes "\<And>S. open S \<Longrightarrow> f -` S \<inter> space M \<in> sets M"
hoelzl@38656:   shows "f \<in> borel_measurable M"
hoelzl@40859:   unfolding borel_def
hoelzl@47694: proof (rule measurable_measure_of, simp_all)
huffman@44537:   fix S :: "'x set" assume "open S" thus "f -` S \<inter> space M \<in> sets M"
huffman@44537:     using assms[of S] by simp
hoelzl@40859: qed
paulson@33533: 
hoelzl@50021: lemma borel_measurable_const:
hoelzl@38656:   "(\<lambda>x. c) \<in> borel_measurable M"
hoelzl@47694:   by auto
paulson@33533: 
hoelzl@50003: lemma borel_measurable_indicator:
hoelzl@38656:   assumes A: "A \<in> sets M"
hoelzl@38656:   shows "indicator A \<in> borel_measurable M"
wenzelm@46905:   unfolding indicator_def [abs_def] using A
hoelzl@47694:   by (auto intro!: measurable_If_set)
paulson@33533: 
hoelzl@50096: lemma borel_measurable_count_space[measurable (raw)]:
hoelzl@50096:   "f \<in> borel_measurable (count_space S)"
hoelzl@50096:   unfolding measurable_def by auto
hoelzl@50096: 
hoelzl@50096: lemma borel_measurable_indicator'[measurable (raw)]:
hoelzl@50096:   assumes [measurable]: "{x\<in>space M. f x \<in> A x} \<in> sets M"
hoelzl@50096:   shows "(\<lambda>x. indicator (A x) (f x)) \<in> borel_measurable M"
hoelzl@50001:   unfolding indicator_def[abs_def]
hoelzl@50001:   by (auto intro!: measurable_If)
hoelzl@50001: 
hoelzl@47694: lemma borel_measurable_indicator_iff:
hoelzl@40859:   "(indicator A :: 'a \<Rightarrow> 'x::{t1_space, zero_neq_one}) \<in> borel_measurable M \<longleftrightarrow> A \<inter> space M \<in> sets M"
hoelzl@40859:     (is "?I \<in> borel_measurable M \<longleftrightarrow> _")
hoelzl@40859: proof
hoelzl@40859:   assume "?I \<in> borel_measurable M"
hoelzl@40859:   then have "?I -` {1} \<inter> space M \<in> sets M"
hoelzl@40859:     unfolding measurable_def by auto
hoelzl@40859:   also have "?I -` {1} \<inter> space M = A \<inter> space M"
wenzelm@46905:     unfolding indicator_def [abs_def] by auto
hoelzl@40859:   finally show "A \<inter> space M \<in> sets M" .
hoelzl@40859: next
hoelzl@40859:   assume "A \<inter> space M \<in> sets M"
hoelzl@40859:   moreover have "?I \<in> borel_measurable M \<longleftrightarrow>
hoelzl@40859:     (indicator (A \<inter> space M) :: 'a \<Rightarrow> 'x) \<in> borel_measurable M"
hoelzl@40859:     by (intro measurable_cong) (auto simp: indicator_def)
hoelzl@40859:   ultimately show "?I \<in> borel_measurable M" by auto
hoelzl@40859: qed
hoelzl@40859: 
hoelzl@47694: lemma borel_measurable_subalgebra:
hoelzl@41545:   assumes "sets N \<subseteq> sets M" "space N = space M" "f \<in> borel_measurable N"
hoelzl@39092:   shows "f \<in> borel_measurable M"
hoelzl@39092:   using assms unfolding measurable_def by auto
hoelzl@39092: 
hoelzl@57137: lemma borel_measurable_restrict_space_iff_ereal:
hoelzl@57137:   fixes f :: "'a \<Rightarrow> ereal"
hoelzl@57137:   assumes \<Omega>[measurable, simp]: "\<Omega> \<inter> space M \<in> sets M"
hoelzl@57137:   shows "f \<in> borel_measurable (restrict_space M \<Omega>) \<longleftrightarrow>
hoelzl@57137:     (\<lambda>x. f x * indicator \<Omega> x) \<in> borel_measurable M"
hoelzl@57138:   by (subst measurable_restrict_space_iff)
hoelzl@57138:      (auto simp: indicator_def if_distrib[where f="\<lambda>x. a * x" for a] cong del: if_cong)
hoelzl@57137: 
hoelzl@57137: lemma borel_measurable_restrict_space_iff:
hoelzl@57137:   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
hoelzl@57137:   assumes \<Omega>[measurable, simp]: "\<Omega> \<inter> space M \<in> sets M"
hoelzl@57137:   shows "f \<in> borel_measurable (restrict_space M \<Omega>) \<longleftrightarrow>
hoelzl@57137:     (\<lambda>x. indicator \<Omega> x *\<^sub>R f x) \<in> borel_measurable M"
hoelzl@57138:   by (subst measurable_restrict_space_iff)
haftmann@57514:      (auto simp: indicator_def if_distrib[where f="\<lambda>x. x *\<^sub>R a" for a] ac_simps cong del: if_cong)
hoelzl@57138: 
hoelzl@57138: lemma cbox_borel[measurable]: "cbox a b \<in> sets borel"
hoelzl@57138:   by (auto intro: borel_closed)
hoelzl@57138: 
hoelzl@57447: lemma box_borel[measurable]: "box a b \<in> sets borel"
hoelzl@57447:   by (auto intro: borel_open)
hoelzl@57447: 
hoelzl@57138: lemma borel_compact: "compact (A::'a::t2_space set) \<Longrightarrow> A \<in> sets borel"
hoelzl@57138:   by (auto intro: borel_closed dest!: compact_imp_closed)
hoelzl@57137: 
hoelzl@57138: lemma borel_measurable_continuous_on_if:
hoelzl@57138:   assumes A: "A \<in> sets borel" and f: "continuous_on A f" and g: "continuous_on (- A) g"
hoelzl@57138:   shows "(\<lambda>x. if x \<in> A then f x else g x) \<in> borel_measurable borel"
hoelzl@57138: proof (rule borel_measurableI)
hoelzl@57138:   fix S :: "'b set" assume "open S"
hoelzl@57138:   have "(\<lambda>x. if x \<in> A then f x else g x) -` S \<inter> space borel = (f -` S \<inter> A) \<union> (g -` S \<inter> -A)"
hoelzl@57138:     by (auto split: split_if_asm)
hoelzl@57138:   moreover obtain A' where "f -` S \<inter> A = A' \<inter> A" "open A'"
hoelzl@57138:     using continuous_on_open_invariant[THEN iffD1, rule_format, OF f `open S`] by metis
hoelzl@57138:   moreover obtain B' where "g -` S \<inter> -A = B' \<inter> -A" "open B'"
hoelzl@57138:     using continuous_on_open_invariant[THEN iffD1, rule_format, OF g `open S`] by metis
hoelzl@57138:   ultimately show "(\<lambda>x. if x \<in> A then f x else g x) -` S \<inter> space borel \<in> sets borel"
hoelzl@57138:     using A by auto
hoelzl@57137: qed
hoelzl@57137: 
hoelzl@57275: lemma borel_measurable_continuous_countable_exceptions:
hoelzl@57275:   fixes f :: "'a::t1_space \<Rightarrow> 'b::topological_space"
hoelzl@57275:   assumes X: "countable X"
hoelzl@57275:   assumes "continuous_on (- X) f"
hoelzl@57275:   shows "f \<in> borel_measurable borel"
hoelzl@57275: proof (rule measurable_discrete_difference[OF _ X])
hoelzl@57275:   have "X \<in> sets borel"
hoelzl@57275:     by (rule sets.countable[OF _ X]) auto
hoelzl@57275:   then show "(\<lambda>x. if x \<in> X then undefined else f x) \<in> borel_measurable borel"
hoelzl@57275:     by (intro borel_measurable_continuous_on_if assms continuous_intros)
hoelzl@57275: qed auto
hoelzl@57275: 
hoelzl@50002: lemma borel_measurable_continuous_on1:
hoelzl@57138:   "continuous_on UNIV f \<Longrightarrow> f \<in> borel_measurable borel"
hoelzl@57138:   using borel_measurable_continuous_on_if[of UNIV f "\<lambda>_. undefined"]
hoelzl@57138:   by (auto intro: continuous_on_const)
hoelzl@57138: 
hoelzl@57138: lemma borel_measurable_continuous_on:
hoelzl@57138:   assumes f: "continuous_on UNIV f" and g: "g \<in> borel_measurable M"
hoelzl@57138:   shows "(\<lambda>x. f (g x)) \<in> borel_measurable M"
hoelzl@57138:   using measurable_comp[OF g borel_measurable_continuous_on1[OF f]] by (simp add: comp_def)
hoelzl@57138: 
hoelzl@57138: lemma borel_measurable_continuous_on_open':
hoelzl@57138:   "continuous_on A f \<Longrightarrow> open A \<Longrightarrow>
hoelzl@57138:     (\<lambda>x. if x \<in> A then f x else c) \<in> borel_measurable borel"
hoelzl@57138:   using borel_measurable_continuous_on_if[of A f "\<lambda>_. c"] by (auto intro: continuous_on_const)
hoelzl@57138: 
hoelzl@57138: lemma borel_measurable_continuous_on_open:
hoelzl@57138:   fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
hoelzl@57138:   assumes cont: "continuous_on A f" "open A"
hoelzl@57138:   assumes g: "g \<in> borel_measurable M"
hoelzl@57138:   shows "(\<lambda>x. if g x \<in> A then f (g x) else c) \<in> borel_measurable M"
hoelzl@57138:   using measurable_comp[OF g borel_measurable_continuous_on_open'[OF cont], of c]
hoelzl@57138:   by (simp add: comp_def)
hoelzl@57138: 
hoelzl@57138: lemma borel_measurable_continuous_on_indicator:
hoelzl@57138:   fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
hoelzl@57138:   assumes A: "A \<in> sets borel" and f: "continuous_on A f"
hoelzl@57138:   shows "(\<lambda>x. indicator A x *\<^sub>R f x) \<in> borel_measurable borel"
hoelzl@57138:   using borel_measurable_continuous_on_if[OF assms, of "\<lambda>_. 0"]
hoelzl@57138:   by (simp add: indicator_def if_distrib[where f="\<lambda>x. x *\<^sub>R a" for a] continuous_on_const
hoelzl@57138:            cong del: if_cong)
hoelzl@50002: 
immler@50245: lemma borel_eq_countable_basis:
immler@50245:   fixes B::"'a::topological_space set set"
immler@50245:   assumes "countable B"
immler@50245:   assumes "topological_basis B"
immler@50245:   shows "borel = sigma UNIV B"
immler@50087:   unfolding borel_def
immler@50087: proof (intro sigma_eqI sigma_sets_eqI, safe)
immler@50245:   interpret countable_basis using assms by unfold_locales
immler@50245:   fix X::"'a set" assume "open X"
immler@50245:   from open_countable_basisE[OF this] guess B' . note B' = this
hoelzl@51683:   then show "X \<in> sigma_sets UNIV B"
hoelzl@51683:     by (blast intro: sigma_sets_UNION `countable B` countable_subset)
immler@50087: next
immler@50245:   fix b assume "b \<in> B"
immler@50245:   hence "open b" by (rule topological_basis_open[OF assms(2)])
immler@50245:   thus "b \<in> sigma_sets UNIV (Collect open)" by auto
immler@50087: qed simp_all
immler@50087: 
hoelzl@50526: lemma borel_measurable_Pair[measurable (raw)]:
hoelzl@50881:   fixes f :: "'a \<Rightarrow> 'b::second_countable_topology" and g :: "'a \<Rightarrow> 'c::second_countable_topology"
hoelzl@50526:   assumes f[measurable]: "f \<in> borel_measurable M"
hoelzl@50526:   assumes g[measurable]: "g \<in> borel_measurable M"
hoelzl@50526:   shows "(\<lambda>x. (f x, g x)) \<in> borel_measurable M"
hoelzl@50526: proof (subst borel_eq_countable_basis)
hoelzl@50526:   let ?B = "SOME B::'b set set. countable B \<and> topological_basis B"
hoelzl@50526:   let ?C = "SOME B::'c set set. countable B \<and> topological_basis B"
hoelzl@50526:   let ?P = "(\<lambda>(b, c). b \<times> c) ` (?B \<times> ?C)"
hoelzl@50526:   show "countable ?P" "topological_basis ?P"
hoelzl@50526:     by (auto intro!: countable_basis topological_basis_prod is_basis)
hoelzl@38656: 
hoelzl@50526:   show "(\<lambda>x. (f x, g x)) \<in> measurable M (sigma UNIV ?P)"
hoelzl@50526:   proof (rule measurable_measure_of)
hoelzl@50526:     fix S assume "S \<in> ?P"
hoelzl@50526:     then obtain b c where "b \<in> ?B" "c \<in> ?C" and S: "S = b \<times> c" by auto
hoelzl@50526:     then have borel: "open b" "open c"
hoelzl@50526:       by (auto intro: is_basis topological_basis_open)
hoelzl@50526:     have "(\<lambda>x. (f x, g x)) -` S \<inter> space M = (f -` b \<inter> space M) \<inter> (g -` c \<inter> space M)"
hoelzl@50526:       unfolding S by auto
hoelzl@50526:     also have "\<dots> \<in> sets M"
hoelzl@50526:       using borel by simp
hoelzl@50526:     finally show "(\<lambda>x. (f x, g x)) -` S \<inter> space M \<in> sets M" .
hoelzl@50526:   qed auto
hoelzl@39087: qed
hoelzl@39087: 
hoelzl@49774: lemma borel_measurable_continuous_Pair:
hoelzl@50881:   fixes f :: "'a \<Rightarrow> 'b::second_countable_topology" and g :: "'a \<Rightarrow> 'c::second_countable_topology"
hoelzl@50003:   assumes [measurable]: "f \<in> borel_measurable M"
hoelzl@50003:   assumes [measurable]: "g \<in> borel_measurable M"
hoelzl@49774:   assumes H: "continuous_on UNIV (\<lambda>x. H (fst x) (snd x))"
hoelzl@49774:   shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
hoelzl@49774: proof -
hoelzl@49774:   have eq: "(\<lambda>x. H (f x) (g x)) = (\<lambda>x. (\<lambda>x. H (fst x) (snd x)) (f x, g x))" by auto
hoelzl@49774:   show ?thesis
hoelzl@49774:     unfolding eq by (rule borel_measurable_continuous_on[OF H]) auto
hoelzl@49774: qed
hoelzl@49774: 
hoelzl@56994: subsection {* Borel spaces on euclidean spaces *}
hoelzl@50526: 
hoelzl@50526: lemma borel_measurable_inner[measurable (raw)]:
hoelzl@50881:   fixes f g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_inner}"
hoelzl@50526:   assumes "f \<in> borel_measurable M"
hoelzl@50526:   assumes "g \<in> borel_measurable M"
hoelzl@50526:   shows "(\<lambda>x. f x \<bullet> g x) \<in> borel_measurable M"
hoelzl@50526:   using assms
hoelzl@56371:   by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
hoelzl@50526: 
hoelzl@50526: lemma [measurable]:
hoelzl@51683:   fixes a b :: "'a\<Colon>linorder_topology"
hoelzl@50526:   shows lessThan_borel: "{..< a} \<in> sets borel"
hoelzl@50526:     and greaterThan_borel: "{a <..} \<in> sets borel"
hoelzl@50526:     and greaterThanLessThan_borel: "{a<..<b} \<in> sets borel"
hoelzl@50526:     and atMost_borel: "{..a} \<in> sets borel"
hoelzl@50526:     and atLeast_borel: "{a..} \<in> sets borel"
hoelzl@50526:     and atLeastAtMost_borel: "{a..b} \<in> sets borel"
hoelzl@50526:     and greaterThanAtMost_borel: "{a<..b} \<in> sets borel"
hoelzl@50526:     and atLeastLessThan_borel: "{a..<b} \<in> sets borel"
hoelzl@50526:   unfolding greaterThanAtMost_def atLeastLessThan_def
hoelzl@51683:   by (blast intro: borel_open borel_closed open_lessThan open_greaterThan open_greaterThanLessThan
hoelzl@51683:                    closed_atMost closed_atLeast closed_atLeastAtMost)+
hoelzl@51683: 
immler@54775: notation
immler@54775:   eucl_less (infix "<e" 50)
immler@54775: 
immler@54775: lemma box_oc: "{x. a <e x \<and> x \<le> b} = {x. a <e x} \<inter> {..b}"
immler@54775:   and box_co: "{x. a \<le> x \<and> x <e b} = {a..} \<inter> {x. x <e b}"
immler@54775:   by auto
immler@54775: 
hoelzl@51683: lemma eucl_ivals[measurable]:
hoelzl@51683:   fixes a b :: "'a\<Colon>ordered_euclidean_space"
immler@54775:   shows "{x. x <e a} \<in> sets borel"
immler@54775:     and "{x. a <e x} \<in> sets borel"
hoelzl@51683:     and "{..a} \<in> sets borel"
hoelzl@51683:     and "{a..} \<in> sets borel"
hoelzl@51683:     and "{a..b} \<in> sets borel"
immler@54775:     and  "{x. a <e x \<and> x \<le> b} \<in> sets borel"
immler@54775:     and "{x. a \<le> x \<and>  x <e b} \<in> sets borel"
immler@54775:   unfolding box_oc box_co
immler@54775:   by (auto intro: borel_open borel_closed)
hoelzl@50526: 
hoelzl@51683: lemma open_Collect_less:
hoelzl@53216:   fixes f g :: "'i::topological_space \<Rightarrow> 'a :: {dense_linorder, linorder_topology}"
hoelzl@51683:   assumes "continuous_on UNIV f"
hoelzl@51683:   assumes "continuous_on UNIV g"
hoelzl@51683:   shows "open {x. f x < g x}"
hoelzl@51683: proof -
hoelzl@51683:   have "open (\<Union>y. {x \<in> UNIV. f x \<in> {..< y}} \<inter> {x \<in> UNIV. g x \<in> {y <..}})" (is "open ?X")
hoelzl@51683:     by (intro open_UN ballI open_Int continuous_open_preimage assms) auto
hoelzl@51683:   also have "?X = {x. f x < g x}"
hoelzl@51683:     by (auto intro: dense)
hoelzl@51683:   finally show ?thesis .
hoelzl@51683: qed
hoelzl@51683: 
hoelzl@51683: lemma closed_Collect_le:
hoelzl@53216:   fixes f g :: "'i::topological_space \<Rightarrow> 'a :: {dense_linorder, linorder_topology}"
hoelzl@51683:   assumes f: "continuous_on UNIV f"
hoelzl@51683:   assumes g: "continuous_on UNIV g"
hoelzl@51683:   shows "closed {x. f x \<le> g x}"
hoelzl@51683:   using open_Collect_less[OF g f] unfolding not_less[symmetric] Collect_neg_eq open_closed .
hoelzl@51683: 
hoelzl@50526: lemma borel_measurable_less[measurable]:
hoelzl@53216:   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, dense_linorder, linorder_topology}"
hoelzl@51683:   assumes "f \<in> borel_measurable M"
hoelzl@51683:   assumes "g \<in> borel_measurable M"
hoelzl@50526:   shows "{w \<in> space M. f w < g w} \<in> sets M"
hoelzl@50526: proof -
hoelzl@51683:   have "{w \<in> space M. f w < g w} = (\<lambda>x. (f x, g x)) -` {x. fst x < snd x} \<inter> space M"
hoelzl@51683:     by auto
hoelzl@51683:   also have "\<dots> \<in> sets M"
hoelzl@51683:     by (intro measurable_sets[OF borel_measurable_Pair borel_open, OF assms open_Collect_less]
hoelzl@56371:               continuous_intros)
hoelzl@51683:   finally show ?thesis .
hoelzl@50526: qed
hoelzl@50526: 
hoelzl@50526: lemma
hoelzl@53216:   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, dense_linorder, linorder_topology}"
hoelzl@50526:   assumes f[measurable]: "f \<in> borel_measurable M"
hoelzl@50526:   assumes g[measurable]: "g \<in> borel_measurable M"
hoelzl@50526:   shows borel_measurable_le[measurable]: "{w \<in> space M. f w \<le> g w} \<in> sets M"
hoelzl@50526:     and borel_measurable_eq[measurable]: "{w \<in> space M. f w = g w} \<in> sets M"
hoelzl@50526:     and borel_measurable_neq: "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
hoelzl@50526:   unfolding eq_iff not_less[symmetric]
hoelzl@50526:   by measurable
hoelzl@50526: 
hoelzl@50526: lemma 
hoelzl@51683:   fixes i :: "'a::{second_countable_topology, real_inner}"
hoelzl@51683:   shows hafspace_less_borel: "{x. a < x \<bullet> i} \<in> sets borel"
hoelzl@51683:     and hafspace_greater_borel: "{x. x \<bullet> i < a} \<in> sets borel"
hoelzl@51683:     and hafspace_less_eq_borel: "{x. a \<le> x \<bullet> i} \<in> sets borel"
hoelzl@51683:     and hafspace_greater_eq_borel: "{x. x \<bullet> i \<le> a} \<in> sets borel"
hoelzl@50526:   by simp_all
hoelzl@50526: 
hoelzl@50526: subsection "Borel space equals sigma algebras over intervals"
hoelzl@50526: 
hoelzl@50526: lemma borel_sigma_sets_subset:
hoelzl@50526:   "A \<subseteq> sets borel \<Longrightarrow> sigma_sets UNIV A \<subseteq> sets borel"
hoelzl@50526:   using sets.sigma_sets_subset[of A borel] by simp
hoelzl@50526: 
hoelzl@50526: lemma borel_eq_sigmaI1:
hoelzl@50526:   fixes F :: "'i \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
hoelzl@50526:   assumes borel_eq: "borel = sigma UNIV X"
hoelzl@50526:   assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV (F ` A))"
hoelzl@50526:   assumes F: "\<And>i. i \<in> A \<Longrightarrow> F i \<in> sets borel"
hoelzl@50526:   shows "borel = sigma UNIV (F ` A)"
hoelzl@50526:   unfolding borel_def
hoelzl@50526: proof (intro sigma_eqI antisym)
hoelzl@50526:   have borel_rev_eq: "sigma_sets UNIV {S::'a set. open S} = sets borel"
hoelzl@50526:     unfolding borel_def by simp
hoelzl@50526:   also have "\<dots> = sigma_sets UNIV X"
hoelzl@50526:     unfolding borel_eq by simp
hoelzl@50526:   also have "\<dots> \<subseteq> sigma_sets UNIV (F`A)"
hoelzl@50526:     using X by (intro sigma_algebra.sigma_sets_subset[OF sigma_algebra_sigma_sets]) auto
hoelzl@50526:   finally show "sigma_sets UNIV {S. open S} \<subseteq> sigma_sets UNIV (F`A)" .
hoelzl@50526:   show "sigma_sets UNIV (F`A) \<subseteq> sigma_sets UNIV {S. open S}"
hoelzl@50526:     unfolding borel_rev_eq using F by (intro borel_sigma_sets_subset) auto
hoelzl@50526: qed auto
hoelzl@50526: 
hoelzl@50526: lemma borel_eq_sigmaI2:
hoelzl@50526:   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set"
hoelzl@50526:     and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
hoelzl@50526:   assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`B)"
hoelzl@50526:   assumes X: "\<And>i j. (i, j) \<in> B \<Longrightarrow> G i j \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
hoelzl@50526:   assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
hoelzl@50526:   shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
hoelzl@50526:   using assms
hoelzl@50526:   by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` B" and F="(\<lambda>(i, j). F i j)"]) auto
hoelzl@50526: 
hoelzl@50526: lemma borel_eq_sigmaI3:
hoelzl@50526:   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
hoelzl@50526:   assumes borel_eq: "borel = sigma UNIV X"
hoelzl@50526:   assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
hoelzl@50526:   assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
hoelzl@50526:   shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
hoelzl@50526:   using assms by (intro borel_eq_sigmaI1[where X=X and F="(\<lambda>(i, j). F i j)"]) auto
hoelzl@50526: 
hoelzl@50526: lemma borel_eq_sigmaI4:
hoelzl@50526:   fixes F :: "'i \<Rightarrow> 'a::topological_space set"
hoelzl@50526:     and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
hoelzl@50526:   assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`A)"
hoelzl@50526:   assumes X: "\<And>i j. (i, j) \<in> A \<Longrightarrow> G i j \<in> sets (sigma UNIV (range F))"
hoelzl@50526:   assumes F: "\<And>i. F i \<in> sets borel"
hoelzl@50526:   shows "borel = sigma UNIV (range F)"
hoelzl@50526:   using assms by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` A" and F=F]) auto
hoelzl@50526: 
hoelzl@50526: lemma borel_eq_sigmaI5:
hoelzl@50526:   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and G :: "'l \<Rightarrow> 'a::topological_space set"
hoelzl@50526:   assumes borel_eq: "borel = sigma UNIV (range G)"
hoelzl@50526:   assumes X: "\<And>i. G i \<in> sets (sigma UNIV (range (\<lambda>(i, j). F i j)))"
hoelzl@50526:   assumes F: "\<And>i j. F i j \<in> sets borel"
hoelzl@50526:   shows "borel = sigma UNIV (range (\<lambda>(i, j). F i j))"
hoelzl@50526:   using assms by (intro borel_eq_sigmaI1[where X="range G" and F="(\<lambda>(i, j). F i j)"]) auto
hoelzl@50526: 
hoelzl@50526: lemma borel_eq_box:
hoelzl@50526:   "borel = sigma UNIV (range (\<lambda> (a, b). box a b :: 'a \<Colon> euclidean_space set))"
hoelzl@50526:     (is "_ = ?SIGMA")
hoelzl@50526: proof (rule borel_eq_sigmaI1[OF borel_def])
hoelzl@50526:   fix M :: "'a set" assume "M \<in> {S. open S}"
hoelzl@50526:   then have "open M" by simp
hoelzl@50526:   show "M \<in> ?SIGMA"
hoelzl@50526:     apply (subst open_UNION_box[OF `open M`])
hoelzl@50526:     apply (safe intro!: sets.countable_UN' countable_PiE countable_Collect)
hoelzl@50526:     apply (auto intro: countable_rat)
hoelzl@50526:     done
hoelzl@50526: qed (auto simp: box_def)
hoelzl@50526: 
hoelzl@50526: lemma halfspace_gt_in_halfspace:
hoelzl@50526:   assumes i: "i \<in> A"
hoelzl@50526:   shows "{x\<Colon>'a. a < x \<bullet> i} \<in> 
hoelzl@50526:     sigma_sets UNIV ((\<lambda> (a, i). {x\<Colon>'a\<Colon>euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> A))"
hoelzl@50526:   (is "?set \<in> ?SIGMA")
hoelzl@50526: proof -
hoelzl@50526:   interpret sigma_algebra UNIV ?SIGMA
hoelzl@50526:     by (intro sigma_algebra_sigma_sets) simp_all
hoelzl@50526:   have *: "?set = (\<Union>n. UNIV - {x\<Colon>'a. x \<bullet> i < a + 1 / real (Suc n)})"
hoelzl@50526:   proof (safe, simp_all add: not_less)
hoelzl@50526:     fix x :: 'a assume "a < x \<bullet> i"
hoelzl@50526:     with reals_Archimedean[of "x \<bullet> i - a"]
hoelzl@50526:     obtain n where "a + 1 / real (Suc n) < x \<bullet> i"
hoelzl@50526:       by (auto simp: inverse_eq_divide field_simps)
hoelzl@50526:     then show "\<exists>n. a + 1 / real (Suc n) \<le> x \<bullet> i"
hoelzl@50526:       by (blast intro: less_imp_le)
hoelzl@50526:   next
hoelzl@50526:     fix x n
hoelzl@50526:     have "a < a + 1 / real (Suc n)" by auto
hoelzl@50526:     also assume "\<dots> \<le> x"
hoelzl@50526:     finally show "a < x" .
hoelzl@50526:   qed
hoelzl@50526:   show "?set \<in> ?SIGMA" unfolding *
hoelzl@50526:     by (auto del: Diff intro!: Diff i)
hoelzl@50526: qed
hoelzl@50526: 
hoelzl@50526: lemma borel_eq_halfspace_less:
hoelzl@50526:   "borel = sigma UNIV ((\<lambda>(a, i). {x::'a::euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> Basis))"
hoelzl@50526:   (is "_ = ?SIGMA")
hoelzl@50526: proof (rule borel_eq_sigmaI2[OF borel_eq_box])
hoelzl@50526:   fix a b :: 'a
hoelzl@50526:   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}"
hoelzl@50526:     by (auto simp: box_def)
hoelzl@50526:   also have "\<dots> \<in> sets ?SIGMA"
hoelzl@50526:     by (intro sets.sets_Collect_conj sets.sets_Collect_finite_All sets.sets_Collect_const)
hoelzl@50526:        (auto intro!: halfspace_gt_in_halfspace countable_PiE countable_rat)
hoelzl@50526:   finally show "box a b \<in> sets ?SIGMA" .
hoelzl@50526: qed auto
hoelzl@50526: 
hoelzl@50526: lemma borel_eq_halfspace_le:
hoelzl@50526:   "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. x \<bullet> i \<le> a}) ` (UNIV \<times> Basis))"
hoelzl@50526:   (is "_ = ?SIGMA")
hoelzl@50526: proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
hoelzl@50526:   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
hoelzl@50526:   then have i: "i \<in> Basis" by auto
hoelzl@50526:   have *: "{x::'a. x\<bullet>i < a} = (\<Union>n. {x. x\<bullet>i \<le> a - 1/real (Suc n)})"
hoelzl@50526:   proof (safe, simp_all)
hoelzl@50526:     fix x::'a assume *: "x\<bullet>i < a"
hoelzl@50526:     with reals_Archimedean[of "a - x\<bullet>i"]
hoelzl@50526:     obtain n where "x \<bullet> i < a - 1 / (real (Suc n))"
hoelzl@50526:       by (auto simp: field_simps inverse_eq_divide)
hoelzl@50526:     then show "\<exists>n. x \<bullet> i \<le> a - 1 / (real (Suc n))"
hoelzl@50526:       by (blast intro: less_imp_le)
hoelzl@50526:   next
hoelzl@50526:     fix x::'a and n
hoelzl@50526:     assume "x\<bullet>i \<le> a - 1 / real (Suc n)"
hoelzl@50526:     also have "\<dots> < a" by auto
hoelzl@50526:     finally show "x\<bullet>i < a" .
hoelzl@50526:   qed
hoelzl@50526:   show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
hoelzl@50526:     by (safe intro!: sets.countable_UN) (auto intro: i)
hoelzl@50526: qed auto
hoelzl@50526: 
hoelzl@50526: lemma borel_eq_halfspace_ge:
hoelzl@50526:   "borel = sigma UNIV ((\<lambda> (a, i). {x\<Colon>'a\<Colon>euclidean_space. a \<le> x \<bullet> i}) ` (UNIV \<times> Basis))"
hoelzl@50526:   (is "_ = ?SIGMA")
hoelzl@50526: proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
hoelzl@50526:   fix a :: real and i :: 'a assume i: "(a, i) \<in> UNIV \<times> Basis"
hoelzl@50526:   have *: "{x::'a. x\<bullet>i < a} = space ?SIGMA - {x::'a. a \<le> x\<bullet>i}" by auto
hoelzl@50526:   show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
hoelzl@50526:     using i by (safe intro!: sets.compl_sets) auto
hoelzl@50526: qed auto
hoelzl@50526: 
hoelzl@50526: lemma borel_eq_halfspace_greater:
hoelzl@50526:   "borel = sigma UNIV ((\<lambda> (a, i). {x\<Colon>'a\<Colon>euclidean_space. a < x \<bullet> i}) ` (UNIV \<times> Basis))"
hoelzl@50526:   (is "_ = ?SIGMA")
hoelzl@50526: proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_le])
hoelzl@50526:   fix a :: real and i :: 'a assume "(a, i) \<in> (UNIV \<times> Basis)"
hoelzl@50526:   then have i: "i \<in> Basis" by auto
hoelzl@50526:   have *: "{x::'a. x\<bullet>i \<le> a} = space ?SIGMA - {x::'a. a < x\<bullet>i}" by auto
hoelzl@50526:   show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
hoelzl@50526:     by (safe intro!: sets.compl_sets) (auto intro: i)
hoelzl@50526: qed auto
hoelzl@50526: 
hoelzl@50526: lemma borel_eq_atMost:
hoelzl@50526:   "borel = sigma UNIV (range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space}))"
hoelzl@50526:   (is "_ = ?SIGMA")
hoelzl@50526: proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
hoelzl@50526:   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
hoelzl@50526:   then have "i \<in> Basis" by auto
hoelzl@50526:   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)})"
hoelzl@50526:   proof (safe, simp_all add: eucl_le[where 'a='a] split: split_if_asm)
hoelzl@50526:     fix x :: 'a
hoelzl@50526:     from real_arch_simple[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"] guess k::nat ..
hoelzl@50526:     then have "\<And>i. i \<in> Basis \<Longrightarrow> x\<bullet>i \<le> real k"
hoelzl@50526:       by (subst (asm) Max_le_iff) auto
hoelzl@50526:     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia \<le> real k"
hoelzl@50526:       by (auto intro!: exI[of _ k])
hoelzl@50526:   qed
hoelzl@50526:   show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
hoelzl@50526:     by (safe intro!: sets.countable_UN) auto
hoelzl@50526: qed auto
hoelzl@50526: 
hoelzl@50526: lemma borel_eq_greaterThan:
immler@54775:   "borel = sigma UNIV (range (\<lambda>a\<Colon>'a\<Colon>ordered_euclidean_space. {x. a <e x}))"
hoelzl@50526:   (is "_ = ?SIGMA")
hoelzl@50526: proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
hoelzl@50526:   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
hoelzl@50526:   then have i: "i \<in> Basis" by auto
hoelzl@50526:   have "{x::'a. x\<bullet>i \<le> a} = UNIV - {x::'a. a < x\<bullet>i}" by auto
hoelzl@50526:   also have *: "{x::'a. a < x\<bullet>i} =
immler@54775:       (\<Union>k::nat. {x. (\<Sum>n\<in>Basis. (if n = i then a else -real k) *\<^sub>R n) <e x})" using i
immler@54775:   proof (safe, simp_all add: eucl_less_def split: split_if_asm)
hoelzl@50526:     fix x :: 'a
hoelzl@50526:     from reals_Archimedean2[of "Max ((\<lambda>i. -x\<bullet>i)`Basis)"]
hoelzl@50526:     guess k::nat .. note k = this
hoelzl@50526:     { fix i :: 'a assume "i \<in> Basis"
hoelzl@50526:       then have "-x\<bullet>i < real k"
hoelzl@50526:         using k by (subst (asm) Max_less_iff) auto
hoelzl@50526:       then have "- real k < x\<bullet>i" by simp }
hoelzl@50526:     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> -real k < x \<bullet> ia"
hoelzl@50526:       by (auto intro!: exI[of _ k])
hoelzl@50526:   qed
hoelzl@50526:   finally show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA"
hoelzl@50526:     apply (simp only:)
hoelzl@50526:     apply (safe intro!: sets.countable_UN sets.Diff)
hoelzl@50526:     apply (auto intro: sigma_sets_top)
hoelzl@50526:     done
hoelzl@50526: qed auto
hoelzl@50526: 
hoelzl@50526: lemma borel_eq_lessThan:
immler@54775:   "borel = sigma UNIV (range (\<lambda>a\<Colon>'a\<Colon>ordered_euclidean_space. {x. x <e a}))"
hoelzl@50526:   (is "_ = ?SIGMA")
hoelzl@50526: proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_ge])
hoelzl@50526:   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
hoelzl@50526:   then have i: "i \<in> Basis" by auto
hoelzl@50526:   have "{x::'a. a \<le> x\<bullet>i} = UNIV - {x::'a. x\<bullet>i < a}" by auto
immler@54775:   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 `i\<in> Basis`
immler@54775:   proof (safe, simp_all add: eucl_less_def split: split_if_asm)
hoelzl@50526:     fix x :: 'a
hoelzl@50526:     from reals_Archimedean2[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"]
hoelzl@50526:     guess k::nat .. note k = this
hoelzl@50526:     { fix i :: 'a assume "i \<in> Basis"
hoelzl@50526:       then have "x\<bullet>i < real k"
hoelzl@50526:         using k by (subst (asm) Max_less_iff) auto
hoelzl@50526:       then have "x\<bullet>i < real k" by simp }
hoelzl@50526:     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia < real k"
hoelzl@50526:       by (auto intro!: exI[of _ k])
hoelzl@50526:   qed
hoelzl@50526:   finally show "{x. a \<le> x\<bullet>i} \<in> ?SIGMA"
hoelzl@50526:     apply (simp only:)
hoelzl@50526:     apply (safe intro!: sets.countable_UN sets.Diff)
immler@54775:     apply (auto intro: sigma_sets_top )
hoelzl@50526:     done
hoelzl@50526: qed auto
hoelzl@50526: 
hoelzl@50526: lemma borel_eq_atLeastAtMost:
hoelzl@50526:   "borel = sigma UNIV (range (\<lambda>(a,b). {a..b} \<Colon>'a\<Colon>ordered_euclidean_space set))"
hoelzl@50526:   (is "_ = ?SIGMA")
hoelzl@50526: proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
hoelzl@50526:   fix a::'a
hoelzl@50526:   have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
hoelzl@50526:   proof (safe, simp_all add: eucl_le[where 'a='a])
hoelzl@50526:     fix x :: 'a
hoelzl@50526:     from real_arch_simple[of "Max ((\<lambda>i. - x\<bullet>i)`Basis)"]
hoelzl@50526:     guess k::nat .. note k = this
hoelzl@50526:     { fix i :: 'a assume "i \<in> Basis"
hoelzl@50526:       with k have "- x\<bullet>i \<le> real k"
hoelzl@50526:         by (subst (asm) Max_le_iff) (auto simp: field_simps)
hoelzl@50526:       then have "- real k \<le> x\<bullet>i" by simp }
hoelzl@50526:     then show "\<exists>n::nat. \<forall>i\<in>Basis. - real n \<le> x \<bullet> i"
hoelzl@50526:       by (auto intro!: exI[of _ k])
hoelzl@50526:   qed
hoelzl@50526:   show "{..a} \<in> ?SIGMA" unfolding *
hoelzl@50526:     by (safe intro!: sets.countable_UN)
hoelzl@50526:        (auto intro!: sigma_sets_top)
hoelzl@50526: qed auto
hoelzl@50526: 
hoelzl@57447: lemma borel_sigma_sets_Ioc: "borel = sigma UNIV (range (\<lambda>(a, b). {a <.. b::real}))"
hoelzl@57447: proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
hoelzl@57447:   fix i :: real
hoelzl@57447:   have "{..i} = (\<Union>j::nat. {-j <.. i})"
hoelzl@57447:     by (auto simp: minus_less_iff reals_Archimedean2)
hoelzl@57447:   also have "\<dots> \<in> sets (sigma UNIV (range (\<lambda>(i, j). {i<..j})))"
hoelzl@57447:     by (intro sets.countable_nat_UN) auto 
hoelzl@57447:   finally show "{..i} \<in> sets (sigma UNIV (range (\<lambda>(i, j). {i<..j})))" .
hoelzl@57447: qed simp
hoelzl@57447: 
immler@54775: lemma eucl_lessThan: "{x::real. x <e a} = lessThan a"
immler@54775:   by (simp add: eucl_less_def lessThan_def)
immler@54775: 
hoelzl@50526: lemma borel_eq_atLeastLessThan:
hoelzl@50526:   "borel = sigma UNIV (range (\<lambda>(a, b). {a ..< b :: real}))" (is "_ = ?SIGMA")
hoelzl@50526: proof (rule borel_eq_sigmaI5[OF borel_eq_lessThan])
hoelzl@50526:   have move_uminus: "\<And>x y::real. -x \<le> y \<longleftrightarrow> -y \<le> x" by auto
hoelzl@50526:   fix x :: real
hoelzl@50526:   have "{..<x} = (\<Union>i::nat. {-real i ..< x})"
hoelzl@50526:     by (auto simp: move_uminus real_arch_simple)
immler@54775:   then show "{y. y <e x} \<in> ?SIGMA"
immler@54775:     by (auto intro: sigma_sets.intros simp: eucl_lessThan)
hoelzl@50526: qed auto
hoelzl@50526: 
hoelzl@50526: lemma borel_eq_closed: "borel = sigma UNIV (Collect closed)"
hoelzl@50526:   unfolding borel_def
hoelzl@50526: proof (intro sigma_eqI sigma_sets_eqI, safe)
hoelzl@50526:   fix x :: "'a set" assume "open x"
hoelzl@50526:   hence "x = UNIV - (UNIV - x)" by auto
hoelzl@50526:   also have "\<dots> \<in> sigma_sets UNIV (Collect closed)"
hoelzl@50526:     by (rule sigma_sets.Compl)
hoelzl@50526:        (auto intro!: sigma_sets.Basic simp: `open x`)
hoelzl@50526:   finally show "x \<in> sigma_sets UNIV (Collect closed)" by simp
hoelzl@50526: next
hoelzl@50526:   fix x :: "'a set" assume "closed x"
hoelzl@50526:   hence "x = UNIV - (UNIV - x)" by auto
hoelzl@50526:   also have "\<dots> \<in> sigma_sets UNIV (Collect open)"
hoelzl@50526:     by (rule sigma_sets.Compl)
hoelzl@50526:        (auto intro!: sigma_sets.Basic simp: `closed x`)
hoelzl@50526:   finally show "x \<in> sigma_sets UNIV (Collect open)" by simp
hoelzl@50526: qed simp_all
hoelzl@50526: 
hoelzl@50526: lemma borel_measurable_halfspacesI:
hoelzl@50526:   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
hoelzl@50526:   assumes F: "borel = sigma UNIV (F ` (UNIV \<times> Basis))"
hoelzl@50526:   and S_eq: "\<And>a i. S a i = f -` F (a,i) \<inter> space M" 
hoelzl@50526:   shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a::real. S a i \<in> sets M)"
hoelzl@50526: proof safe
hoelzl@50526:   fix a :: real and i :: 'b assume i: "i \<in> Basis" and f: "f \<in> borel_measurable M"
hoelzl@50526:   then show "S a i \<in> sets M" unfolding assms
hoelzl@50526:     by (auto intro!: measurable_sets simp: assms(1))
hoelzl@50526: next
hoelzl@50526:   assume a: "\<forall>i\<in>Basis. \<forall>a. S a i \<in> sets M"
hoelzl@50526:   then show "f \<in> borel_measurable M"
hoelzl@50526:     by (auto intro!: measurable_measure_of simp: S_eq F)
hoelzl@50526: qed
hoelzl@50526: 
hoelzl@50526: lemma borel_measurable_iff_halfspace_le:
hoelzl@50526:   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
hoelzl@50526:   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)"
hoelzl@50526:   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_le]) auto
hoelzl@50526: 
hoelzl@50526: lemma borel_measurable_iff_halfspace_less:
hoelzl@50526:   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
hoelzl@50526:   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)"
hoelzl@50526:   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_less]) auto
hoelzl@50526: 
hoelzl@50526: lemma borel_measurable_iff_halfspace_ge:
hoelzl@50526:   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
hoelzl@50526:   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)"
hoelzl@50526:   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_ge]) auto
hoelzl@50526: 
hoelzl@50526: lemma borel_measurable_iff_halfspace_greater:
hoelzl@50526:   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
hoelzl@50526:   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)"
hoelzl@50526:   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_greater]) auto
hoelzl@50526: 
hoelzl@50526: lemma borel_measurable_iff_le:
hoelzl@50526:   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
hoelzl@50526:   using borel_measurable_iff_halfspace_le[where 'c=real] by simp
hoelzl@50526: 
hoelzl@50526: lemma borel_measurable_iff_less:
hoelzl@50526:   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
hoelzl@50526:   using borel_measurable_iff_halfspace_less[where 'c=real] by simp
hoelzl@50526: 
hoelzl@50526: lemma borel_measurable_iff_ge:
hoelzl@50526:   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
hoelzl@50526:   using borel_measurable_iff_halfspace_ge[where 'c=real]
hoelzl@50526:   by simp
hoelzl@50526: 
hoelzl@50526: lemma borel_measurable_iff_greater:
hoelzl@50526:   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
hoelzl@50526:   using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
hoelzl@50526: 
hoelzl@50526: lemma borel_measurable_euclidean_space:
hoelzl@50526:   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
hoelzl@50526:   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M)"
hoelzl@50526: proof safe
hoelzl@50526:   assume f: "\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M"
hoelzl@50526:   then show "f \<in> borel_measurable M"
hoelzl@50526:     by (subst borel_measurable_iff_halfspace_le) auto
hoelzl@50526: qed auto
hoelzl@50526: 
hoelzl@50526: subsection "Borel measurable operators"
hoelzl@50526: 
hoelzl@56993: lemma borel_measurable_norm[measurable]: "norm \<in> borel_measurable borel"
hoelzl@56993:   by (intro borel_measurable_continuous_on1 continuous_intros)
hoelzl@56993: 
hoelzl@57275: lemma borel_measurable_sgn [measurable]: "(sgn::'a::real_normed_vector \<Rightarrow> 'a) \<in> borel_measurable borel"
hoelzl@57275:   by (rule borel_measurable_continuous_countable_exceptions[where X="{0}"])
hoelzl@57275:      (auto intro!: continuous_on_sgn continuous_on_id)
hoelzl@57275: 
hoelzl@50526: lemma borel_measurable_uminus[measurable (raw)]:
hoelzl@51683:   fixes g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
hoelzl@50526:   assumes g: "g \<in> borel_measurable M"
hoelzl@50526:   shows "(\<lambda>x. - g x) \<in> borel_measurable M"
hoelzl@56371:   by (rule borel_measurable_continuous_on[OF _ g]) (intro continuous_intros)
hoelzl@50526: 
hoelzl@50003: lemma borel_measurable_add[measurable (raw)]:
hoelzl@51683:   fixes f g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
hoelzl@49774:   assumes f: "f \<in> borel_measurable M"
hoelzl@49774:   assumes g: "g \<in> borel_measurable M"
hoelzl@49774:   shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
hoelzl@56371:   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
hoelzl@49774: 
hoelzl@50003: lemma borel_measurable_setsum[measurable (raw)]:
hoelzl@51683:   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
hoelzl@49774:   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
hoelzl@49774:   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
hoelzl@49774: proof cases
hoelzl@49774:   assume "finite S"
hoelzl@49774:   thus ?thesis using assms by induct auto
hoelzl@49774: qed simp
hoelzl@49774: 
hoelzl@50003: lemma borel_measurable_diff[measurable (raw)]:
hoelzl@51683:   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
hoelzl@49774:   assumes f: "f \<in> borel_measurable M"
hoelzl@49774:   assumes g: "g \<in> borel_measurable M"
hoelzl@49774:   shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
haftmann@54230:   using borel_measurable_add [of f M "- g"] assms by (simp add: fun_Compl_def)
hoelzl@49774: 
hoelzl@50003: lemma borel_measurable_times[measurable (raw)]:
hoelzl@51683:   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_algebra}"
hoelzl@49774:   assumes f: "f \<in> borel_measurable M"
hoelzl@49774:   assumes g: "g \<in> borel_measurable M"
hoelzl@49774:   shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
hoelzl@56371:   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
hoelzl@51683: 
hoelzl@51683: lemma borel_measurable_setprod[measurable (raw)]:
hoelzl@51683:   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> 'b::{second_countable_topology, real_normed_field}"
hoelzl@51683:   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
hoelzl@51683:   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
hoelzl@51683: proof cases
hoelzl@51683:   assume "finite S"
hoelzl@51683:   thus ?thesis using assms by induct auto
hoelzl@51683: qed simp
hoelzl@49774: 
hoelzl@50003: lemma borel_measurable_dist[measurable (raw)]:
hoelzl@51683:   fixes g f :: "'a \<Rightarrow> 'b::{second_countable_topology, metric_space}"
hoelzl@49774:   assumes f: "f \<in> borel_measurable M"
hoelzl@49774:   assumes g: "g \<in> borel_measurable M"
hoelzl@49774:   shows "(\<lambda>x. dist (f x) (g x)) \<in> borel_measurable M"
hoelzl@56371:   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
hoelzl@49774:   
hoelzl@50002: lemma borel_measurable_scaleR[measurable (raw)]:
hoelzl@51683:   fixes g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
hoelzl@50002:   assumes f: "f \<in> borel_measurable M"
hoelzl@50002:   assumes g: "g \<in> borel_measurable M"
hoelzl@50002:   shows "(\<lambda>x. f x *\<^sub>R g x) \<in> borel_measurable M"
hoelzl@56371:   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
hoelzl@50002: 
hoelzl@47694: lemma affine_borel_measurable_vector:
hoelzl@38656:   fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
hoelzl@38656:   assumes "f \<in> borel_measurable M"
hoelzl@38656:   shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
hoelzl@38656: proof (rule borel_measurableI)
hoelzl@38656:   fix S :: "'x set" assume "open S"
hoelzl@38656:   show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
hoelzl@38656:   proof cases
hoelzl@38656:     assume "b \<noteq> 0"
huffman@44537:     with `open S` have "open ((\<lambda>x. (- a + x) /\<^sub>R b) ` S)" (is "open ?S")
haftmann@54230:       using open_affinity [of S "inverse b" "- a /\<^sub>R b"]
haftmann@54230:       by (auto simp: algebra_simps)
hoelzl@47694:     hence "?S \<in> sets borel" by auto
hoelzl@38656:     moreover
hoelzl@38656:     from `b \<noteq> 0` have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
hoelzl@38656:       apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
hoelzl@40859:     ultimately show ?thesis using assms unfolding in_borel_measurable_borel
hoelzl@38656:       by auto
hoelzl@38656:   qed simp
hoelzl@38656: qed
hoelzl@38656: 
hoelzl@50002: lemma borel_measurable_const_scaleR[measurable (raw)]:
hoelzl@50002:   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. b *\<^sub>R f x ::'a::real_normed_vector) \<in> borel_measurable M"
hoelzl@50002:   using affine_borel_measurable_vector[of f M 0 b] by simp
hoelzl@38656: 
hoelzl@50002: lemma borel_measurable_const_add[measurable (raw)]:
hoelzl@50002:   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. a + f x ::'a::real_normed_vector) \<in> borel_measurable M"
hoelzl@50002:   using affine_borel_measurable_vector[of f M a 1] by simp
hoelzl@50002: 
hoelzl@50003: lemma borel_measurable_inverse[measurable (raw)]:
hoelzl@57275:   fixes f :: "'a \<Rightarrow> 'b::real_normed_div_algebra"
hoelzl@49774:   assumes f: "f \<in> borel_measurable M"
hoelzl@35692:   shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
hoelzl@57275:   apply (rule measurable_compose[OF f])
hoelzl@57275:   apply (rule borel_measurable_continuous_countable_exceptions[of "{0}"])
hoelzl@57275:   apply (auto intro!: continuous_on_inverse continuous_on_id)
hoelzl@57275:   done
hoelzl@35692: 
hoelzl@50003: lemma borel_measurable_divide[measurable (raw)]:
hoelzl@51683:   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow>
hoelzl@57275:     (\<lambda>x. f x / g x::'b::{second_countable_topology, real_normed_div_algebra}) \<in> borel_measurable M"
hoelzl@57275:   by (simp add: divide_inverse)
hoelzl@38656: 
hoelzl@50003: lemma borel_measurable_max[measurable (raw)]:
hoelzl@53216:   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. max (g x) (f x) :: 'b::{second_countable_topology, dense_linorder, linorder_topology}) \<in> borel_measurable M"
hoelzl@50003:   by (simp add: max_def)
hoelzl@38656: 
hoelzl@50003: lemma borel_measurable_min[measurable (raw)]:
hoelzl@53216:   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. min (g x) (f x) :: 'b::{second_countable_topology, dense_linorder, linorder_topology}) \<in> borel_measurable M"
hoelzl@50003:   by (simp add: min_def)
hoelzl@38656: 
hoelzl@57235: lemma borel_measurable_Min[measurable (raw)]:
hoelzl@57235:   "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, dense_linorder, linorder_topology}) \<in> borel_measurable M"
hoelzl@57235: proof (induct I rule: finite_induct)
hoelzl@57235:   case (insert i I) then show ?case
hoelzl@57235:     by (cases "I = {}") auto
hoelzl@57235: qed auto
hoelzl@57235: 
hoelzl@57235: lemma borel_measurable_Max[measurable (raw)]:
hoelzl@57235:   "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, dense_linorder, linorder_topology}) \<in> borel_measurable M"
hoelzl@57235: proof (induct I rule: finite_induct)
hoelzl@57235:   case (insert i I) then show ?case
hoelzl@57235:     by (cases "I = {}") auto
hoelzl@57235: qed auto
hoelzl@57235: 
hoelzl@50003: lemma borel_measurable_abs[measurable (raw)]:
hoelzl@50003:   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
hoelzl@50003:   unfolding abs_real_def by simp
hoelzl@38656: 
hoelzl@50003: lemma borel_measurable_nth[measurable (raw)]:
hoelzl@41026:   "(\<lambda>x::real^'n. x $ i) \<in> borel_measurable borel"
hoelzl@50526:   by (simp add: cart_eq_inner_axis)
hoelzl@41026: 
hoelzl@47694: lemma convex_measurable:
hoelzl@51683:   fixes A :: "'a :: ordered_euclidean_space set"
hoelzl@51683:   assumes X: "X \<in> borel_measurable M" "X ` space M \<subseteq> A" "open A"
hoelzl@51683:   assumes q: "convex_on A q"
hoelzl@49774:   shows "(\<lambda>x. q (X x)) \<in> borel_measurable M"
hoelzl@42990: proof -
hoelzl@51683:   have "(\<lambda>x. if X x \<in> A then q (X x) else 0) \<in> borel_measurable M" (is "?qX")
hoelzl@49774:   proof (rule borel_measurable_continuous_on_open[OF _ _ X(1)])
hoelzl@51683:     show "open A" by fact
hoelzl@51683:     from this q show "continuous_on A q"
hoelzl@42990:       by (rule convex_on_continuous)
hoelzl@41830:   qed
hoelzl@50002:   also have "?qX \<longleftrightarrow> (\<lambda>x. q (X x)) \<in> borel_measurable M"
hoelzl@42990:     using X by (intro measurable_cong) auto
hoelzl@50002:   finally show ?thesis .
hoelzl@41830: qed
hoelzl@41830: 
hoelzl@50003: lemma borel_measurable_ln[measurable (raw)]:
hoelzl@49774:   assumes f: "f \<in> borel_measurable M"
hoelzl@49774:   shows "(\<lambda>x. ln (f x)) \<in> borel_measurable M"
hoelzl@57275:   apply (rule measurable_compose[OF f])
hoelzl@57275:   apply (rule borel_measurable_continuous_countable_exceptions[of "{0}"])
hoelzl@57275:   apply (auto intro!: continuous_on_ln continuous_on_id)
hoelzl@57275:   done
hoelzl@41830: 
hoelzl@50003: lemma borel_measurable_log[measurable (raw)]:
hoelzl@50002:   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. log (g x) (f x)) \<in> borel_measurable M"
hoelzl@49774:   unfolding log_def by auto
hoelzl@41830: 
hoelzl@50419: lemma borel_measurable_exp[measurable]: "exp \<in> borel_measurable borel"
hoelzl@51478:   by (intro borel_measurable_continuous_on1 continuous_at_imp_continuous_on ballI isCont_exp)
hoelzl@50419: 
hoelzl@50002: lemma measurable_real_floor[measurable]:
hoelzl@50002:   "(floor :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
hoelzl@47761: proof -
hoelzl@50002:   have "\<And>a x. \<lfloor>x\<rfloor> = a \<longleftrightarrow> (real a \<le> x \<and> x < real (a + 1))"
hoelzl@50002:     by (auto intro: floor_eq2)
hoelzl@50002:   then show ?thesis
hoelzl@50002:     by (auto simp: vimage_def measurable_count_space_eq2_countable)
hoelzl@47761: qed
hoelzl@47761: 
hoelzl@50002: lemma measurable_real_natfloor[measurable]:
hoelzl@50002:   "(natfloor :: real \<Rightarrow> nat) \<in> measurable borel (count_space UNIV)"
hoelzl@50002:   by (simp add: natfloor_def[abs_def])
hoelzl@50002: 
hoelzl@50002: lemma measurable_real_ceiling[measurable]:
hoelzl@50002:   "(ceiling :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
hoelzl@50002:   unfolding ceiling_def[abs_def] by simp
hoelzl@50002: 
hoelzl@50002: lemma borel_measurable_real_floor: "(\<lambda>x::real. real \<lfloor>x\<rfloor>) \<in> borel_measurable borel"
hoelzl@50002:   by simp
hoelzl@50002: 
hoelzl@50003: lemma borel_measurable_real_natfloor:
hoelzl@50002:   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. real (natfloor (f x))) \<in> borel_measurable M"
hoelzl@50002:   by simp
hoelzl@50002: 
hoelzl@57235: lemma borel_measurable_root [measurable]: "(root n) \<in> borel_measurable borel"
hoelzl@57235:   by (intro borel_measurable_continuous_on1 continuous_intros)
hoelzl@57235: 
hoelzl@57235: lemma borel_measurable_sqrt [measurable]: "sqrt \<in> borel_measurable borel"
hoelzl@57235:   by (intro borel_measurable_continuous_on1 continuous_intros)
hoelzl@57235: 
hoelzl@57235: lemma borel_measurable_power [measurable (raw)]:
hoelzl@57235:    fixes f :: "_ \<Rightarrow> 'b::{power,real_normed_algebra}"
hoelzl@57235:    assumes f: "f \<in> borel_measurable M"
hoelzl@57235:    shows "(\<lambda>x. (f x) ^ n) \<in> borel_measurable M"
hoelzl@57235:    by (intro borel_measurable_continuous_on [OF _ f] continuous_intros)
hoelzl@57235: 
hoelzl@57235: lemma borel_measurable_Re [measurable]: "Re \<in> borel_measurable borel"
hoelzl@57235:   by (intro borel_measurable_continuous_on1 continuous_intros)
hoelzl@57235: 
hoelzl@57235: lemma borel_measurable_Im [measurable]: "Im \<in> borel_measurable borel"
hoelzl@57235:   by (intro borel_measurable_continuous_on1 continuous_intros)
hoelzl@57235: 
hoelzl@57235: lemma borel_measurable_of_real [measurable]: "(of_real :: _ \<Rightarrow> (_::real_normed_algebra)) \<in> borel_measurable borel"
hoelzl@57235:   by (intro borel_measurable_continuous_on1 continuous_intros)
hoelzl@57235: 
hoelzl@57235: lemma borel_measurable_sin [measurable]: "sin \<in> borel_measurable borel"
hoelzl@57235:   by (intro borel_measurable_continuous_on1 continuous_intros)
hoelzl@57235: 
hoelzl@57235: lemma borel_measurable_cos [measurable]: "cos \<in> borel_measurable borel"
hoelzl@57235:   by (intro borel_measurable_continuous_on1 continuous_intros)
hoelzl@57235: 
hoelzl@57235: lemma borel_measurable_arctan [measurable]: "arctan \<in> borel_measurable borel"
hoelzl@57235:   by (intro borel_measurable_continuous_on1 continuous_intros)
hoelzl@57235: 
hoelzl@57259: lemma borel_measurable_complex_iff:
hoelzl@57259:   "f \<in> borel_measurable M \<longleftrightarrow>
hoelzl@57259:     (\<lambda>x. Re (f x)) \<in> borel_measurable M \<and> (\<lambda>x. Im (f x)) \<in> borel_measurable M"
hoelzl@57259:   apply auto
hoelzl@57259:   apply (subst fun_complex_eq)
hoelzl@57259:   apply (intro borel_measurable_add)
hoelzl@57259:   apply auto
hoelzl@57259:   done
hoelzl@57259: 
hoelzl@41981: subsection "Borel space on the extended reals"
hoelzl@41981: 
hoelzl@50003: lemma borel_measurable_ereal[measurable (raw)]:
hoelzl@43920:   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
hoelzl@49774:   using continuous_on_ereal f by (rule borel_measurable_continuous_on)
hoelzl@41981: 
hoelzl@50003: lemma borel_measurable_real_of_ereal[measurable (raw)]:
hoelzl@49774:   fixes f :: "'a \<Rightarrow> ereal" 
hoelzl@49774:   assumes f: "f \<in> borel_measurable M"
hoelzl@49774:   shows "(\<lambda>x. real (f x)) \<in> borel_measurable M"
hoelzl@49774: proof -
hoelzl@49774:   have "(\<lambda>x. if f x \<in> UNIV - { \<infinity>, - \<infinity> } then real (f x) else 0) \<in> borel_measurable M"
hoelzl@49774:     using continuous_on_real
hoelzl@49774:     by (rule borel_measurable_continuous_on_open[OF _ _ f]) auto
hoelzl@49774:   also have "(\<lambda>x. if f x \<in> UNIV - { \<infinity>, - \<infinity> } then real (f x) else 0) = (\<lambda>x. real (f x))"
hoelzl@49774:     by auto
hoelzl@49774:   finally show ?thesis .
hoelzl@49774: qed
hoelzl@49774: 
hoelzl@49774: lemma borel_measurable_ereal_cases:
hoelzl@49774:   fixes f :: "'a \<Rightarrow> ereal" 
hoelzl@49774:   assumes f: "f \<in> borel_measurable M"
hoelzl@49774:   assumes H: "(\<lambda>x. H (ereal (real (f x)))) \<in> borel_measurable M"
hoelzl@49774:   shows "(\<lambda>x. H (f x)) \<in> borel_measurable M"
hoelzl@49774: proof -
hoelzl@50002:   let ?F = "\<lambda>x. if f x = \<infinity> then H \<infinity> else if f x = - \<infinity> then H (-\<infinity>) else H (ereal (real (f x)))"
hoelzl@49774:   { fix x have "H (f x) = ?F x" by (cases "f x") auto }
hoelzl@50002:   with f H show ?thesis by simp
hoelzl@47694: qed
hoelzl@41981: 
hoelzl@49774: lemma
hoelzl@50003:   fixes f :: "'a \<Rightarrow> ereal" assumes f[measurable]: "f \<in> borel_measurable M"
hoelzl@50003:   shows borel_measurable_ereal_abs[measurable(raw)]: "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
hoelzl@50003:     and borel_measurable_ereal_inverse[measurable(raw)]: "(\<lambda>x. inverse (f x) :: ereal) \<in> borel_measurable M"
hoelzl@50003:     and borel_measurable_uminus_ereal[measurable(raw)]: "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
hoelzl@49774:   by (auto simp del: abs_real_of_ereal simp: borel_measurable_ereal_cases[OF f] measurable_If)
hoelzl@49774: 
hoelzl@49774: lemma borel_measurable_uminus_eq_ereal[simp]:
hoelzl@49774:   "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
hoelzl@49774: proof
hoelzl@49774:   assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
hoelzl@49774: qed auto
hoelzl@49774: 
hoelzl@49774: lemma set_Collect_ereal2:
hoelzl@49774:   fixes f g :: "'a \<Rightarrow> ereal" 
hoelzl@49774:   assumes f: "f \<in> borel_measurable M"
hoelzl@49774:   assumes g: "g \<in> borel_measurable M"
hoelzl@49774:   assumes H: "{x \<in> space M. H (ereal (real (f x))) (ereal (real (g x)))} \<in> sets M"
hoelzl@50002:     "{x \<in> space borel. H (-\<infinity>) (ereal x)} \<in> sets borel"
hoelzl@50002:     "{x \<in> space borel. H (\<infinity>) (ereal x)} \<in> sets borel"
hoelzl@50002:     "{x \<in> space borel. H (ereal x) (-\<infinity>)} \<in> sets borel"
hoelzl@50002:     "{x \<in> space borel. H (ereal x) (\<infinity>)} \<in> sets borel"
hoelzl@49774:   shows "{x \<in> space M. H (f x) (g x)} \<in> sets M"
hoelzl@49774: proof -
hoelzl@50002:   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 (g x)))"
hoelzl@50002:   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 (f x))) x"
hoelzl@49774:   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
hoelzl@50002:   note * = this
hoelzl@50002:   from assms show ?thesis
hoelzl@50002:     by (subst *) (simp del: space_borel split del: split_if)
hoelzl@49774: qed
hoelzl@49774: 
hoelzl@47694: lemma borel_measurable_ereal_iff:
hoelzl@43920:   shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
hoelzl@41981: proof
hoelzl@43920:   assume "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
hoelzl@43920:   from borel_measurable_real_of_ereal[OF this]
hoelzl@41981:   show "f \<in> borel_measurable M" by auto
hoelzl@41981: qed auto
hoelzl@41981: 
hoelzl@47694: lemma borel_measurable_ereal_iff_real:
hoelzl@43923:   fixes f :: "'a \<Rightarrow> ereal"
hoelzl@43923:   shows "f \<in> borel_measurable M \<longleftrightarrow>
hoelzl@41981:     ((\<lambda>x. real (f x)) \<in> borel_measurable M \<and> f -` {\<infinity>} \<inter> space M \<in> sets M \<and> f -` {-\<infinity>} \<inter> space M \<in> sets M)"
hoelzl@41981: proof safe
hoelzl@41981:   assume *: "(\<lambda>x. real (f x)) \<in> borel_measurable M" "f -` {\<infinity>} \<inter> space M \<in> sets M" "f -` {-\<infinity>} \<inter> space M \<in> sets M"
hoelzl@41981:   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
hoelzl@41981:   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
wenzelm@46731:   let ?f = "\<lambda>x. if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else ereal (real (f x))"
hoelzl@41981:   have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
hoelzl@43920:   also have "?f = f" by (auto simp: fun_eq_iff ereal_real)
hoelzl@41981:   finally show "f \<in> borel_measurable M" .
hoelzl@50002: qed simp_all
hoelzl@41830: 
hoelzl@47694: lemma borel_measurable_eq_atMost_ereal:
hoelzl@43923:   fixes f :: "'a \<Rightarrow> ereal"
hoelzl@43923:   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
hoelzl@41981: proof (intro iffI allI)
hoelzl@41981:   assume pos[rule_format]: "\<forall>a. f -` {..a} \<inter> space M \<in> sets M"
hoelzl@41981:   show "f \<in> borel_measurable M"
hoelzl@43920:     unfolding borel_measurable_ereal_iff_real borel_measurable_iff_le
hoelzl@41981:   proof (intro conjI allI)
hoelzl@41981:     fix a :: real
hoelzl@43920:     { fix x :: ereal assume *: "\<forall>i::nat. real i < x"
hoelzl@41981:       have "x = \<infinity>"
hoelzl@43920:       proof (rule ereal_top)
huffman@44666:         fix B from reals_Archimedean2[of B] guess n ..
hoelzl@43920:         then have "ereal B < real n" by auto
hoelzl@41981:         with * show "B \<le> x" by (metis less_trans less_imp_le)
hoelzl@41981:       qed }
hoelzl@41981:     then have "f -` {\<infinity>} \<inter> space M = space M - (\<Union>i::nat. f -` {.. real i} \<inter> space M)"
hoelzl@41981:       by (auto simp: not_le)
hoelzl@50002:     then show "f -` {\<infinity>} \<inter> space M \<in> sets M" using pos
hoelzl@50002:       by (auto simp del: UN_simps)
hoelzl@41981:     moreover
hoelzl@43923:     have "{-\<infinity>::ereal} = {..-\<infinity>}" by auto
hoelzl@41981:     then show "f -` {-\<infinity>} \<inter> space M \<in> sets M" using pos by auto
hoelzl@43920:     moreover have "{x\<in>space M. f x \<le> ereal a} \<in> sets M"
hoelzl@43920:       using pos[of "ereal a"] by (simp add: vimage_def Int_def conj_commute)
hoelzl@41981:     moreover have "{w \<in> space M. real (f w) \<le> a} =
hoelzl@43920:       (if a < 0 then {w \<in> space M. f w \<le> ereal a} - f -` {-\<infinity>} \<inter> space M
hoelzl@43920:       else {w \<in> space M. f w \<le> ereal a} \<union> (f -` {\<infinity>} \<inter> space M) \<union> (f -` {-\<infinity>} \<inter> space M))" (is "?l = ?r")
hoelzl@41981:       proof (intro set_eqI) fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases "f x") auto qed
hoelzl@41981:     ultimately show "{w \<in> space M. real (f w) \<le> a} \<in> sets M" by auto
hoelzl@35582:   qed
hoelzl@41981: qed (simp add: measurable_sets)
hoelzl@35582: 
hoelzl@47694: lemma borel_measurable_eq_atLeast_ereal:
hoelzl@43920:   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
hoelzl@41981: proof
hoelzl@41981:   assume pos: "\<forall>a. f -` {a..} \<inter> space M \<in> sets M"
hoelzl@41981:   moreover have "\<And>a. (\<lambda>x. - f x) -` {..a} = f -` {-a ..}"
hoelzl@43920:     by (auto simp: ereal_uminus_le_reorder)
hoelzl@41981:   ultimately have "(\<lambda>x. - f x) \<in> borel_measurable M"
hoelzl@43920:     unfolding borel_measurable_eq_atMost_ereal by auto
hoelzl@41981:   then show "f \<in> borel_measurable M" by simp
hoelzl@41981: qed (simp add: measurable_sets)
hoelzl@35582: 
hoelzl@49774: lemma greater_eq_le_measurable:
hoelzl@49774:   fixes f :: "'a \<Rightarrow> 'c::linorder"
hoelzl@49774:   shows "f -` {..< a} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {a ..} \<inter> space M \<in> sets M"
hoelzl@49774: proof
hoelzl@49774:   assume "f -` {a ..} \<inter> space M \<in> sets M"
hoelzl@49774:   moreover have "f -` {..< a} \<inter> space M = space M - f -` {a ..} \<inter> space M" by auto
hoelzl@49774:   ultimately show "f -` {..< a} \<inter> space M \<in> sets M" by auto
hoelzl@49774: next
hoelzl@49774:   assume "f -` {..< a} \<inter> space M \<in> sets M"
hoelzl@49774:   moreover have "f -` {a ..} \<inter> space M = space M - f -` {..< a} \<inter> space M" by auto
hoelzl@49774:   ultimately show "f -` {a ..} \<inter> space M \<in> sets M" by auto
hoelzl@49774: qed
hoelzl@49774: 
hoelzl@47694: lemma borel_measurable_ereal_iff_less:
hoelzl@43920:   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
hoelzl@43920:   unfolding borel_measurable_eq_atLeast_ereal greater_eq_le_measurable ..
hoelzl@38656: 
hoelzl@49774: lemma less_eq_ge_measurable:
hoelzl@49774:   fixes f :: "'a \<Rightarrow> 'c::linorder"
hoelzl@49774:   shows "f -` {a <..} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {..a} \<inter> space M \<in> sets M"
hoelzl@49774: proof
hoelzl@49774:   assume "f -` {a <..} \<inter> space M \<in> sets M"
hoelzl@49774:   moreover have "f -` {..a} \<inter> space M = space M - f -` {a <..} \<inter> space M" by auto
hoelzl@49774:   ultimately show "f -` {..a} \<inter> space M \<in> sets M" by auto
hoelzl@49774: next
hoelzl@49774:   assume "f -` {..a} \<inter> space M \<in> sets M"
hoelzl@49774:   moreover have "f -` {a <..} \<inter> space M = space M - f -` {..a} \<inter> space M" by auto
hoelzl@49774:   ultimately show "f -` {a <..} \<inter> space M \<in> sets M" by auto
hoelzl@49774: qed
hoelzl@49774: 
hoelzl@47694: lemma borel_measurable_ereal_iff_ge:
hoelzl@43920:   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
hoelzl@43920:   unfolding borel_measurable_eq_atMost_ereal less_eq_ge_measurable ..
hoelzl@38656: 
hoelzl@49774: lemma borel_measurable_ereal2:
hoelzl@49774:   fixes f g :: "'a \<Rightarrow> ereal" 
hoelzl@41981:   assumes f: "f \<in> borel_measurable M"
hoelzl@41981:   assumes g: "g \<in> borel_measurable M"
hoelzl@49774:   assumes H: "(\<lambda>x. H (ereal (real (f x))) (ereal (real (g x)))) \<in> borel_measurable M"
hoelzl@49774:     "(\<lambda>x. H (-\<infinity>) (ereal (real (g x)))) \<in> borel_measurable M"
hoelzl@49774:     "(\<lambda>x. H (\<infinity>) (ereal (real (g x)))) \<in> borel_measurable M"
hoelzl@49774:     "(\<lambda>x. H (ereal (real (f x))) (-\<infinity>)) \<in> borel_measurable M"
hoelzl@49774:     "(\<lambda>x. H (ereal (real (f x))) (\<infinity>)) \<in> borel_measurable M"
hoelzl@49774:   shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
hoelzl@41981: proof -
hoelzl@50002:   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 (g x)))"
hoelzl@50002:   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 (f x))) x"
hoelzl@49774:   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
hoelzl@50002:   note * = this
hoelzl@50002:   from assms show ?thesis unfolding * by simp
hoelzl@41981: qed
hoelzl@41981: 
hoelzl@49774: lemma
hoelzl@49774:   fixes f :: "'a \<Rightarrow> ereal" assumes f: "f \<in> borel_measurable M"
hoelzl@49774:   shows borel_measurable_ereal_eq_const: "{x\<in>space M. f x = c} \<in> sets M"
hoelzl@49774:     and borel_measurable_ereal_neq_const: "{x\<in>space M. f x \<noteq> c} \<in> sets M"
hoelzl@49774:   using f by auto
hoelzl@38656: 
hoelzl@50003: lemma [measurable(raw)]:
hoelzl@43920:   fixes f :: "'a \<Rightarrow> ereal"
hoelzl@50003:   assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
hoelzl@50002:   shows borel_measurable_ereal_add: "(\<lambda>x. f x + g x) \<in> borel_measurable M"
hoelzl@50002:     and borel_measurable_ereal_times: "(\<lambda>x. f x * g x) \<in> borel_measurable M"
hoelzl@50002:     and borel_measurable_ereal_min: "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
hoelzl@50002:     and borel_measurable_ereal_max: "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
hoelzl@50003:   by (simp_all add: borel_measurable_ereal2 min_def max_def)
hoelzl@49774: 
hoelzl@50003: lemma [measurable(raw)]:
hoelzl@49774:   fixes f g :: "'a \<Rightarrow> ereal"
hoelzl@49774:   assumes "f \<in> borel_measurable M"
hoelzl@49774:   assumes "g \<in> borel_measurable M"
hoelzl@50002:   shows borel_measurable_ereal_diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M"
hoelzl@50002:     and borel_measurable_ereal_divide: "(\<lambda>x. f x / g x) \<in> borel_measurable M"
hoelzl@50003:   using assms by (simp_all add: minus_ereal_def divide_ereal_def)
hoelzl@38656: 
hoelzl@50003: lemma borel_measurable_ereal_setsum[measurable (raw)]:
hoelzl@43920:   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
hoelzl@41096:   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
hoelzl@41096:   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
hoelzl@41096: proof cases
hoelzl@41096:   assume "finite S"
hoelzl@41096:   thus ?thesis using assms
hoelzl@41096:     by induct auto
hoelzl@49774: qed simp
hoelzl@38656: 
hoelzl@50003: lemma borel_measurable_ereal_setprod[measurable (raw)]:
hoelzl@43920:   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
hoelzl@38656:   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
hoelzl@41096:   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
hoelzl@38656: proof cases
hoelzl@38656:   assume "finite S"
hoelzl@41096:   thus ?thesis using assms by induct auto
hoelzl@41096: qed simp
hoelzl@38656: 
hoelzl@50003: lemma borel_measurable_SUP[measurable (raw)]:
hoelzl@43920:   fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> ereal"
hoelzl@38656:   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
hoelzl@41097:   shows "(\<lambda>x. SUP i : A. f i x) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
hoelzl@43920:   unfolding borel_measurable_ereal_iff_ge
hoelzl@41981: proof
hoelzl@38656:   fix a
hoelzl@41981:   have "?sup -` {a<..} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
noschinl@46884:     by (auto simp: less_SUP_iff)
hoelzl@41981:   then show "?sup -` {a<..} \<inter> space M \<in> sets M"
hoelzl@38656:     using assms by auto
hoelzl@38656: qed
hoelzl@38656: 
hoelzl@50003: lemma borel_measurable_INF[measurable (raw)]:
hoelzl@43920:   fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> ereal"
hoelzl@38656:   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
hoelzl@41097:   shows "(\<lambda>x. INF i : A. f i x) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
hoelzl@43920:   unfolding borel_measurable_ereal_iff_less
hoelzl@41981: proof
hoelzl@38656:   fix a
hoelzl@41981:   have "?inf -` {..<a} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
noschinl@46884:     by (auto simp: INF_less_iff)
hoelzl@41981:   then show "?inf -` {..<a} \<inter> space M \<in> sets M"
hoelzl@38656:     using assms by auto
hoelzl@38656: qed
hoelzl@38656: 
hoelzl@50003: lemma [measurable (raw)]:
hoelzl@43920:   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
hoelzl@41981:   assumes "\<And>i. f i \<in> borel_measurable M"
hoelzl@50002:   shows borel_measurable_liminf: "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
hoelzl@50002:     and borel_measurable_limsup: "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
haftmann@56212:   unfolding liminf_SUP_INF limsup_INF_SUP using assms by auto
hoelzl@35692: 
hoelzl@50104: lemma sets_Collect_eventually_sequentially[measurable]:
hoelzl@50003:   "(\<And>i. {x\<in>space M. P x i} \<in> sets M) \<Longrightarrow> {x\<in>space M. eventually (P x) sequentially} \<in> sets M"
hoelzl@50003:   unfolding eventually_sequentially by simp
hoelzl@50003: 
hoelzl@50003: lemma sets_Collect_ereal_convergent[measurable]: 
hoelzl@50003:   fixes f :: "nat \<Rightarrow> 'a => ereal"
hoelzl@50003:   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
hoelzl@50003:   shows "{x\<in>space M. convergent (\<lambda>i. f i x)} \<in> sets M"
hoelzl@50003:   unfolding convergent_ereal by auto
hoelzl@50003: 
hoelzl@50003: lemma borel_measurable_extreal_lim[measurable (raw)]:
hoelzl@50003:   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
hoelzl@50003:   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
hoelzl@50003:   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
hoelzl@50003: proof -
hoelzl@50003:   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))"
hoelzl@51351:     by (simp add: lim_def convergent_def convergent_limsup_cl)
hoelzl@50003:   then show ?thesis
hoelzl@50003:     by simp
hoelzl@50003: qed
hoelzl@50003: 
hoelzl@49774: lemma borel_measurable_ereal_LIMSEQ:
hoelzl@49774:   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
hoelzl@49774:   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
hoelzl@49774:   and u: "\<And>i. u i \<in> borel_measurable M"
hoelzl@49774:   shows "u' \<in> borel_measurable M"
hoelzl@47694: proof -
hoelzl@49774:   have "\<And>x. x \<in> space M \<Longrightarrow> u' x = liminf (\<lambda>n. u n x)"
hoelzl@49774:     using u' by (simp add: lim_imp_Liminf[symmetric])
hoelzl@50003:   with u show ?thesis by (simp cong: measurable_cong)
hoelzl@47694: qed
hoelzl@47694: 
hoelzl@50003: lemma borel_measurable_extreal_suminf[measurable (raw)]:
hoelzl@43920:   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
hoelzl@50003:   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
hoelzl@41981:   shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
hoelzl@50003:   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
hoelzl@39092: 
hoelzl@56994: subsection {* LIMSEQ is borel measurable *}
hoelzl@39092: 
hoelzl@47694: lemma borel_measurable_LIMSEQ:
hoelzl@39092:   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
hoelzl@39092:   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
hoelzl@39092:   and u: "\<And>i. u i \<in> borel_measurable M"
hoelzl@39092:   shows "u' \<in> borel_measurable M"
hoelzl@39092: proof -
hoelzl@43920:   have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. ereal (u n x)) = ereal (u' x)"
wenzelm@46731:     using u' by (simp add: lim_imp_Liminf)
hoelzl@43920:   moreover from u have "(\<lambda>x. liminf (\<lambda>n. ereal (u n x))) \<in> borel_measurable M"
hoelzl@39092:     by auto
hoelzl@43920:   ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_ereal_iff)
hoelzl@39092: qed
hoelzl@39092: 
hoelzl@56993: lemma borel_measurable_LIMSEQ_metric:
hoelzl@56993:   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b :: metric_space"
hoelzl@56993:   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
hoelzl@56993:   assumes lim: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. f i x) ----> g x"
hoelzl@56993:   shows "g \<in> borel_measurable M"
hoelzl@56993:   unfolding borel_eq_closed
hoelzl@56993: proof (safe intro!: measurable_measure_of)
hoelzl@56993:   fix A :: "'b set" assume "closed A" 
hoelzl@56993: 
hoelzl@56993:   have [measurable]: "(\<lambda>x. infdist (g x) A) \<in> borel_measurable M"
hoelzl@56993:   proof (rule borel_measurable_LIMSEQ)
hoelzl@56993:     show "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. infdist (f i x) A) ----> infdist (g x) A"
hoelzl@56993:       by (intro tendsto_infdist lim)
hoelzl@56993:     show "\<And>i. (\<lambda>x. infdist (f i x) A) \<in> borel_measurable M"
hoelzl@56993:       by (intro borel_measurable_continuous_on[where f="\<lambda>x. infdist x A"]
hoelzl@56993:         continuous_at_imp_continuous_on ballI continuous_infdist isCont_ident) auto
hoelzl@56993:   qed
hoelzl@56993: 
hoelzl@56993:   show "g -` A \<inter> space M \<in> sets M"
hoelzl@56993:   proof cases
hoelzl@56993:     assume "A \<noteq> {}"
hoelzl@56993:     then have "\<And>x. infdist x A = 0 \<longleftrightarrow> x \<in> A"
hoelzl@56993:       using `closed A` by (simp add: in_closed_iff_infdist_zero)
hoelzl@56993:     then have "g -` A \<inter> space M = {x\<in>space M. infdist (g x) A = 0}"
hoelzl@56993:       by auto
hoelzl@56993:     also have "\<dots> \<in> sets M"
hoelzl@56993:       by measurable
hoelzl@56993:     finally show ?thesis .
hoelzl@56993:   qed simp
hoelzl@56993: qed auto
hoelzl@56993: 
hoelzl@50002: lemma sets_Collect_Cauchy[measurable]: 
hoelzl@57036:   fixes f :: "nat \<Rightarrow> 'a => 'b::{metric_space, second_countable_topology}"
hoelzl@50002:   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
hoelzl@49774:   shows "{x\<in>space M. Cauchy (\<lambda>i. f i x)} \<in> sets M"
hoelzl@57036:   unfolding metric_Cauchy_iff2 using f by auto
hoelzl@49774: 
hoelzl@50002: lemma borel_measurable_lim[measurable (raw)]:
hoelzl@57036:   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{banach, second_countable_topology}"
hoelzl@50002:   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
hoelzl@49774:   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
hoelzl@49774: proof -
hoelzl@50002:   def u' \<equiv> "\<lambda>x. lim (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
hoelzl@50002:   then have *: "\<And>x. lim (\<lambda>i. f i x) = (if Cauchy (\<lambda>i. f i x) then u' x else (THE x. False))"
hoelzl@49774:     by (auto simp: lim_def convergent_eq_cauchy[symmetric])
hoelzl@50002:   have "u' \<in> borel_measurable M"
hoelzl@57036:   proof (rule borel_measurable_LIMSEQ_metric)
hoelzl@50002:     fix x
hoelzl@50002:     have "convergent (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
hoelzl@49774:       by (cases "Cauchy (\<lambda>i. f i x)")
hoelzl@50002:          (auto simp add: convergent_eq_cauchy[symmetric] convergent_def)
hoelzl@50002:     then show "(\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0) ----> u' x"
hoelzl@50002:       unfolding u'_def 
hoelzl@50002:       by (rule convergent_LIMSEQ_iff[THEN iffD1])
hoelzl@50002:   qed measurable
hoelzl@50002:   then show ?thesis
hoelzl@50002:     unfolding * by measurable
hoelzl@49774: qed
hoelzl@49774: 
hoelzl@50002: lemma borel_measurable_suminf[measurable (raw)]:
hoelzl@57036:   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{banach, second_countable_topology}"
hoelzl@50002:   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
hoelzl@49774:   shows "(\<lambda>x. suminf (\<lambda>i. f i x)) \<in> borel_measurable M"
hoelzl@50002:   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
hoelzl@49774: 
hoelzl@57447: (* Proof by Jeremy Avigad and Luke Serafin *)
hoelzl@57447: lemma isCont_borel:
hoelzl@57447:   fixes f :: "'b::metric_space \<Rightarrow> 'a::metric_space"
hoelzl@57447:   shows "{x. isCont f x} \<in> sets borel"
hoelzl@57447: proof -
hoelzl@57447:   let ?I = "\<lambda>j. inverse(real (Suc j))"
hoelzl@57447: 
hoelzl@57447:   { fix x
hoelzl@57447:     have "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)"
hoelzl@57447:       unfolding continuous_at_eps_delta
hoelzl@57447:     proof safe
hoelzl@57447:       fix i assume "\<forall>e>0. \<exists>d>0. \<forall>y. dist y x < d \<longrightarrow> dist (f y) (f x) < e"
hoelzl@57447:       moreover have "0 < ?I i / 2"
hoelzl@57447:         by simp
hoelzl@57447:       ultimately obtain d where d: "0 < d" "\<And>y. dist x y < d \<Longrightarrow> dist (f y) (f x) < ?I i / 2"
hoelzl@57447:         by (metis dist_commute)
hoelzl@57447:       then obtain j where j: "?I j < d"
hoelzl@57447:         by (metis reals_Archimedean)
hoelzl@57447: 
hoelzl@57447:       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"
hoelzl@57447:       proof (safe intro!: exI[where x=j])
hoelzl@57447:         fix y z assume *: "dist x y < ?I j" "dist x z < ?I j"
hoelzl@57447:         have "dist (f y) (f z) \<le> dist (f y) (f x) + dist (f z) (f x)"
hoelzl@57447:           by (rule dist_triangle2)
hoelzl@57447:         also have "\<dots> < ?I i / 2 + ?I i / 2"
hoelzl@57447:           by (intro add_strict_mono d less_trans[OF _ j] *)
hoelzl@57447:         also have "\<dots> \<le> ?I i"
hoelzl@57447:           by (simp add: field_simps real_of_nat_Suc)
hoelzl@57447:         finally show "dist (f y) (f z) \<le> ?I i"
hoelzl@57447:           by simp
hoelzl@57447:       qed
hoelzl@57447:     next
hoelzl@57447:       fix e::real assume "0 < e"
hoelzl@57447:       then obtain n where n: "?I n < e"
hoelzl@57447:         by (metis reals_Archimedean)
hoelzl@57447:       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"
hoelzl@57447:       from this[THEN spec, of "Suc n"]
hoelzl@57447:       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)"
hoelzl@57447:         by auto
hoelzl@57447:       
hoelzl@57447:       show "\<exists>d>0. \<forall>y. dist y x < d \<longrightarrow> dist (f y) (f x) < e"
hoelzl@57447:       proof (safe intro!: exI[of _ "?I j"])
hoelzl@57447:         fix y assume "dist y x < ?I j"
hoelzl@57447:         then have "dist (f y) (f x) \<le> ?I (Suc n)"
hoelzl@57447:           by (intro j) (auto simp: dist_commute)
hoelzl@57447:         also have "?I (Suc n) < ?I n"
hoelzl@57447:           by simp
hoelzl@57447:         also note n
hoelzl@57447:         finally show "dist (f y) (f x) < e" .
hoelzl@57447:       qed simp
hoelzl@57447:     qed }
hoelzl@57447:   note * = this
hoelzl@57447: 
hoelzl@57447:   have **: "\<And>e y. open {x. dist x y < e}"
hoelzl@57447:     using open_ball by (simp_all add: ball_def dist_commute)
hoelzl@57447: 
hoelzl@57447:   have "{x\<in>space borel. isCont f x } \<in> sets borel"
hoelzl@57447:     unfolding *
hoelzl@57447:     apply (intro sets.sets_Collect_countable_All sets.sets_Collect_countable_Ex)
hoelzl@57447:     apply (simp add: Collect_all_eq)
hoelzl@57447:     apply (intro borel_closed closed_INT ballI closed_Collect_imp open_Collect_conj **)
hoelzl@57447:     apply auto
hoelzl@57447:     done
hoelzl@57447:   then show ?thesis
hoelzl@57447:     by simp
hoelzl@57447: qed
hoelzl@57447: 
immler@54775: no_notation
immler@54775:   eucl_less (infix "<e" 50)
immler@54775: 
hoelzl@51683: end




