author paulson Tue Apr 17 22:35:48 2018 +0100 (18 months ago) changeset 67998 73a5a33486ee parent 67997 ae76012879c6 child 67999 1b05f74f2e5f
Change of variables proof
```     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Analysis/Change_Of_Vars.thy	Tue Apr 17 22:35:48 2018 +0100
1.3 @@ -0,0 +1,3974 @@
1.4 +theory Change_Of_Vars
1.5 +  imports  "HOL-Analysis.Vitali_Covering_Theorem" "HOL-Analysis.Determinants"
1.6 +
1.7 +begin
1.8 +
1.9 +subsection\<open>Induction on matrix row operations\<close>
1.10 +
1.11 +lemma induct_matrix_row_operations:
1.12 +  fixes P :: "(real^'n, 'n::finite) vec \<Rightarrow> bool"
1.13 +  assumes zero_row: "\<And>A i. row i A = 0 \<Longrightarrow> P A"
1.14 +    and diagonal: "\<And>A. (\<And>i j. i \<noteq> j \<Longrightarrow> A\$i\$j = 0) \<Longrightarrow> P A"
1.15 +    and swap_cols: "\<And>A m n. \<lbrakk>P A; m \<noteq> n\<rbrakk> \<Longrightarrow> P(\<chi> i j. A \$ i \$ Fun.swap m n id j)"
1.16 +    and row_op: "\<And>A m n c. \<lbrakk>P A; m \<noteq> n\<rbrakk>
1.17 +                   \<Longrightarrow> P(\<chi> i. if i = m then row m A + c *\<^sub>R row n A else row i A)"
1.18 +  shows "P A"
1.19 +proof -
1.20 +  have "P A" if "(\<And>i j. \<lbrakk>j \<in> -K;  i \<noteq> j\<rbrakk> \<Longrightarrow> A\$i\$j = 0)" for A K
1.21 +  proof -
1.22 +    have "finite K"
1.23 +      by simp
1.24 +    then show ?thesis using that
1.25 +    proof (induction arbitrary: A rule: finite_induct)
1.26 +      case empty
1.27 +      with diagonal show ?case
1.28 +        by simp
1.29 +    next
1.30 +      case (insert k K)
1.31 +      note insertK = insert
1.32 +      have "P A" if kk: "A\$k\$k \<noteq> 0"
1.33 +        and 0: "\<And>i j. \<lbrakk>j \<in> - insert k K; i \<noteq> j\<rbrakk> \<Longrightarrow> A\$i\$j = 0"
1.34 +               "\<And>i. \<lbrakk>i \<in> -L; i \<noteq> k\<rbrakk> \<Longrightarrow> A\$i\$k = 0" for A L
1.35 +      proof -
1.36 +        have "finite L"
1.37 +          by simp
1.38 +        then show ?thesis using 0 kk
1.39 +        proof (induction arbitrary: A rule: finite_induct)
1.40 +          case (empty B)
1.41 +          show ?case
1.42 +          proof (rule insertK)
1.43 +            fix i j
1.44 +            assume "i \<in> - K" "j \<noteq> i"
1.45 +            show "B \$ j \$ i = 0"
1.46 +              using \<open>j \<noteq> i\<close> \<open>i \<in> - K\<close> empty
1.47 +              by (metis ComplD ComplI Compl_eq_Diff_UNIV Diff_empty UNIV_I insert_iff)
1.48 +          qed
1.49 +        next
1.50 +          case (insert l L B)
1.51 +          show ?case
1.52 +          proof (cases "k = l")
1.53 +            case True
1.54 +            with insert show ?thesis
1.55 +              by auto
1.56 +          next
1.57 +            case False
1.58 +            let ?C = "\<chi> i. if i = l then row l B - (B \$ l \$ k / B \$ k \$ k) *\<^sub>R row k B else row i B"
1.59 +            have 1: "\<lbrakk>j \<in> - insert k K; i \<noteq> j\<rbrakk> \<Longrightarrow> ?C \$ i \$ j = 0" for j i
1.60 +              by (auto simp: insert.prems(1) row_def)
1.61 +            have 2: "?C \$ i \$ k = 0"
1.62 +              if "i \<in> - L" "i \<noteq> k" for i
1.63 +            proof (cases "i=l")
1.64 +              case True
1.65 +              with that insert.prems show ?thesis
1.66 +                by (simp add: row_def)
1.67 +            next
1.68 +              case False
1.69 +              with that show ?thesis
1.70 +                by (simp add: insert.prems(2) row_def)
1.71 +            qed
1.72 +            have 3: "?C \$ k \$ k \<noteq> 0"
1.73 +              by (auto simp: insert.prems row_def \<open>k \<noteq> l\<close>)
1.74 +            have PC: "P ?C"
1.75 +              using insert.IH [OF 1 2 3] by auto
1.76 +            have eqB: "(\<chi> i. if i = l then row l ?C + (B \$ l \$ k / B \$ k \$ k) *\<^sub>R row k ?C else row i ?C) = B"
1.77 +              using \<open>k \<noteq> l\<close> by (simp add: vec_eq_iff row_def)
1.78 +            show ?thesis
1.79 +              using row_op [OF PC, of l k, where c = "B\$l\$k / B\$k\$k"] eqB \<open>k \<noteq> l\<close>
1.80 +              by (simp add: cong: if_cong)
1.81 +          qed
1.82 +        qed
1.83 +      qed
1.84 +      then have nonzero_hyp: "P A"
1.85 +        if kk: "A\$k\$k \<noteq> 0" and zeroes: "\<And>i j. j \<in> - insert k K \<and> i\<noteq>j \<Longrightarrow> A\$i\$j = 0" for A
1.86 +        by (auto simp: intro!: kk zeroes)
1.87 +      show ?case
1.88 +      proof (cases "row k A = 0")
1.89 +        case True
1.90 +        with zero_row show ?thesis by auto
1.91 +      next
1.92 +        case False
1.93 +        then obtain l where l: "A\$k\$l \<noteq> 0"
1.94 +          by (auto simp: row_def zero_vec_def vec_eq_iff)
1.95 +        show ?thesis
1.96 +        proof (cases "k = l")
1.97 +          case True
1.98 +          with l nonzero_hyp insert.prems show ?thesis
1.99 +            by blast
1.100 +        next
1.101 +          case False
1.102 +          have *: "A \$ i \$ Fun.swap k l id j = 0" if "j \<noteq> k" "j \<notin> K" "i \<noteq> j" for i j
1.103 +            using False l insert.prems that
1.104 +            by (auto simp: swap_def insert split: if_split_asm)
1.105 +          have "P (\<chi> i j. (\<chi> i j. A \$ i \$ Fun.swap k l id j) \$ i \$ Fun.swap k l id j)"
1.106 +            by (rule swap_cols [OF nonzero_hyp False]) (auto simp: l *)
1.107 +          moreover
1.108 +          have "(\<chi> i j. (\<chi> i j. A \$ i \$ Fun.swap k l id j) \$ i \$ Fun.swap k l id j) = A"
1.109 +            by (metis (no_types, lifting) id_apply o_apply swap_id_idempotent vec_lambda_unique vec_lambda_unique)
1.110 +          ultimately show ?thesis
1.111 +            by simp
1.112 +        qed
1.113 +      qed
1.114 +    qed
1.115 +  qed
1.116 +  then show ?thesis
1.117 +    by blast
1.118 +qed
1.119 +
1.120 +lemma induct_matrix_elementary:
1.121 +  fixes P :: "(real^'n, 'n::finite) vec \<Rightarrow> bool"
1.122 +  assumes mult: "\<And>A B. \<lbrakk>P A; P B\<rbrakk> \<Longrightarrow> P(A ** B)"
1.123 +    and zero_row: "\<And>A i. row i A = 0 \<Longrightarrow> P A"
1.124 +    and diagonal: "\<And>A. (\<And>i j. i \<noteq> j \<Longrightarrow> A\$i\$j = 0) \<Longrightarrow> P A"
1.125 +    and swap1: "\<And>m n. m \<noteq> n \<Longrightarrow> P(\<chi> i j. mat 1 \$ i \$ Fun.swap m n id j)"
1.126 +    and idplus: "\<And>m n c. m \<noteq> n \<Longrightarrow> P(\<chi> i j. if i = m \<and> j = n then c else of_bool (i = j))"
1.127 +  shows "P A"
1.128 +proof -
1.129 +  have swap: "P (\<chi> i j. A \$ i \$ Fun.swap m n id j)"  (is "P ?C")
1.130 +    if "P A" "m \<noteq> n" for A m n
1.131 +  proof -
1.132 +    have "A ** (\<chi> i j. mat 1 \$ i \$ Fun.swap m n id j) = ?C"
1.133 +      by (simp add: matrix_matrix_mult_def mat_def vec_eq_iff if_distrib sum.delta_remove)
1.134 +    then show ?thesis
1.135 +      using mult swap1 that by metis
1.136 +  qed
1.137 +  have row: "P (\<chi> i. if i = m then row m A + c *\<^sub>R row n A else row i A)"  (is "P ?C")
1.138 +    if "P A" "m \<noteq> n" for A m n c
1.139 +  proof -
1.140 +    let ?B = "\<chi> i j. if i = m \<and> j = n then c else of_bool (i = j)"
1.141 +    have "?B ** A = ?C"
1.142 +      using \<open>m \<noteq> n\<close> unfolding matrix_matrix_mult_def row_def of_bool_def
1.143 +      by (auto simp: vec_eq_iff if_distrib [of "\<lambda>x. x * y" for y] sum.remove cong: if_cong)
1.144 +    then show ?thesis
1.145 +      by (rule subst) (auto simp: that mult idplus)
1.146 +  qed
1.147 +  show ?thesis
1.148 +    by (rule induct_matrix_row_operations [OF zero_row diagonal swap row])
1.149 +qed
1.150 +
1.151 +lemma induct_matrix_elementary_alt:
1.152 +  fixes P :: "(real^'n, 'n::finite) vec \<Rightarrow> bool"
1.153 +  assumes mult: "\<And>A B. \<lbrakk>P A; P B\<rbrakk> \<Longrightarrow> P(A ** B)"
1.154 +    and zero_row: "\<And>A i. row i A = 0 \<Longrightarrow> P A"
1.155 +    and diagonal: "\<And>A. (\<And>i j. i \<noteq> j \<Longrightarrow> A\$i\$j = 0) \<Longrightarrow> P A"
1.156 +    and swap1: "\<And>m n. m \<noteq> n \<Longrightarrow> P(\<chi> i j. mat 1 \$ i \$ Fun.swap m n id j)"
1.157 +    and idplus: "\<And>m n. m \<noteq> n \<Longrightarrow> P(\<chi> i j. of_bool (i = m \<and> j = n \<or> i = j))"
1.158 +  shows "P A"
1.159 +proof -
1.160 +  have *: "P (\<chi> i j. if i = m \<and> j = n then c else of_bool (i = j))"
1.161 +    if "m \<noteq> n" for m n c
1.162 +  proof (cases "c = 0")
1.163 +    case True
1.164 +    with diagonal show ?thesis by auto
1.165 +  next
1.166 +    case False
1.167 +    then have eq: "(\<chi> i j. if i = m \<and> j = n then c else of_bool (i = j)) =
1.168 +                      (\<chi> i j. if i = j then (if j = n then inverse c else 1) else 0) **
1.169 +                      (\<chi> i j. of_bool (i = m \<and> j = n \<or> i = j)) **
1.170 +                      (\<chi> i j. if i = j then if j = n then c else 1 else 0)"
1.171 +      using \<open>m \<noteq> n\<close>
1.172 +      apply (simp add: matrix_matrix_mult_def vec_eq_iff of_bool_def if_distrib [of "\<lambda>x. y * x" for y] cong: if_cong)
1.173 +      apply (simp add: if_if_eq_conj sum.neutral conj_commute cong: conj_cong)
1.174 +      done
1.175 +    show ?thesis
1.176 +      apply (subst eq)
1.177 +      apply (intro mult idplus that)
1.178 +       apply (auto intro: diagonal)
1.179 +      done
1.180 +  qed
1.181 +  show ?thesis
1.182 +    by (rule induct_matrix_elementary) (auto intro: assms *)
1.183 +qed
1.184 +
1.185 +lemma induct_linear_elementary:
1.186 +  fixes f :: "real^'n \<Rightarrow> real^'n"
1.187 +  assumes "linear f"
1.188 +    and comp: "\<And>f g. \<lbrakk>linear f; linear g; P f; P g\<rbrakk> \<Longrightarrow> P(f \<circ> g)"
1.189 +    and zeroes: "\<And>f i. \<lbrakk>linear f; \<And>x. (f x) \$ i = 0\<rbrakk> \<Longrightarrow> P f"
1.190 +    and const: "\<And>c. P(\<lambda>x. \<chi> i. c i * x\$i)"
1.191 +    and swap: "\<And>m n::'n. m \<noteq> n \<Longrightarrow> P(\<lambda>x. \<chi> i. x \$ Fun.swap m n id i)"
1.192 +    and idplus: "\<And>m n::'n. m \<noteq> n \<Longrightarrow> P(\<lambda>x. \<chi> i. if i = m then x\$m + x\$n else x\$i)"
1.193 +  shows "P f"
1.194 +proof -
1.195 +  have "P (( *v) A)" for A
1.196 +  proof (rule induct_matrix_elementary_alt)
1.197 +    fix A B
1.198 +    assume "P (( *v) A)" and "P (( *v) B)"
1.199 +    then show "P (( *v) (A ** B))"
1.200 +      by (metis (no_types, lifting) comp linear_compose matrix_compose matrix_eq matrix_vector_mul matrix_vector_mul_linear)
1.201 +  next
1.202 +    fix A :: "((real, 'n) vec, 'n) vec" and i
1.203 +    assume "row i A = 0"
1.204 +    then show "P (( *v) A)"
1.205 +      by (metis inner_zero_left matrix_vector_mul_component matrix_vector_mul_linear row_def vec_eq_iff vec_lambda_beta zeroes)
1.206 +  next
1.207 +    fix A :: "((real, 'n) vec, 'n) vec"
1.208 +    assume 0: "\<And>i j. i \<noteq> j \<Longrightarrow> A \$ i \$ j = 0"
1.209 +    have "A \$ i \$ i * x \$ i = (\<Sum>j\<in>UNIV. A \$ i \$ j * x \$ j)" for x :: "(real, 'n) vec" and i :: "'n"
1.210 +      by (simp add: 0 comm_monoid_add_class.sum.remove [where x=i])
1.211 +    then have "(\<lambda>x. \<chi> i. A \$ i \$ i * x \$ i) = (( *v) A)"
1.212 +      by (auto simp: 0 matrix_vector_mult_def)
1.213 +    then show "P (( *v) A)"
1.214 +      using const [of "\<lambda>i. A \$ i \$ i"] by simp
1.215 +  next
1.216 +    fix m n :: "'n"
1.217 +    assume "m \<noteq> n"
1.218 +    have eq: "(\<Sum>j\<in>UNIV. if i = Fun.swap m n id j then x \$ j else 0) =
1.219 +              (\<Sum>j\<in>UNIV. if j = Fun.swap m n id i then x \$ j else 0)"
1.220 +      for i and x :: "(real, 'n) vec"
1.221 +      unfolding swap_def by (rule sum.cong) auto
1.222 +    have "(\<lambda>x::real^'n. \<chi> i. x \$ Fun.swap m n id i) = (( *v) (\<chi> i j. if i = Fun.swap m n id j then 1 else 0))"
1.223 +      by (auto simp: mat_def matrix_vector_mult_def eq if_distrib [of "\<lambda>x. x * y" for y] cong: if_cong)
1.224 +    with swap [OF \<open>m \<noteq> n\<close>] show "P (( *v) (\<chi> i j. mat 1 \$ i \$ Fun.swap m n id j))"
1.225 +      by (simp add: mat_def matrix_vector_mult_def)
1.226 +  next
1.227 +    fix m n :: "'n"
1.228 +    assume "m \<noteq> n"
1.229 +    then have "x \$ m + x \$ n = (\<Sum>j\<in>UNIV. of_bool (j = n \<or> m = j) * x \$ j)" for x :: "(real, 'n) vec"
1.230 +      by (auto simp: of_bool_def if_distrib [of "\<lambda>x. x * y" for y] sum.remove cong: if_cong)
1.231 +    then have "(\<lambda>x::real^'n. \<chi> i. if i = m then x \$ m + x \$ n else x \$ i) =
1.232 +               (( *v) (\<chi> i j. of_bool (i = m \<and> j = n \<or> i = j)))"
1.233 +      unfolding matrix_vector_mult_def of_bool_def
1.234 +      by (auto simp: vec_eq_iff if_distrib [of "\<lambda>x. x * y" for y] cong: if_cong)
1.235 +    then show "P (( *v) (\<chi> i j. of_bool (i = m \<and> j = n \<or> i = j)))"
1.236 +      using idplus [OF \<open>m \<noteq> n\<close>] by simp
1.237 +  qed
1.238 +  then show ?thesis
1.239 +    by (metis \<open>linear f\<close> matrix_vector_mul)
1.240 +qed
1.241 +
1.242 +
1.243 +proposition
1.244 +  fixes a :: "real^'n"
1.245 +  assumes "m \<noteq> n" and ab_ne: "cbox a b \<noteq> {}" and an: "0 \<le> a\$n"
1.246 +  shows measurable_shear_interval: "(\<lambda>x. \<chi> i. if i = m then x\$m + x\$n else x\$i) ` (cbox a b) \<in> lmeasurable"
1.247 +       (is  "?f ` _ \<in> _")
1.248 +   and measure_shear_interval: "measure lebesgue ((\<lambda>x. \<chi> i. if i = m then x\$m + x\$n else x\$i) ` cbox a b)
1.249 +               = measure lebesgue (cbox a b)" (is "?Q")
1.250 +proof -
1.251 +  have lin: "linear ?f"
1.252 +    by (force simp: plus_vec_def scaleR_vec_def algebra_simps intro: linearI)
1.253 +  show fab: "?f ` cbox a b \<in> lmeasurable"
1.254 +    by (simp add: lin measurable_linear_image_interval)
1.255 +  let ?c = "\<chi> i. if i = m then b\$m + b\$n else b\$i"
1.256 +  let ?mn = "axis m 1 - axis n (1::real)"
1.257 +  have eq1: "measure lebesgue (cbox a ?c)
1.258 +            = measure lebesgue (?f ` cbox a b)
1.259 +            + measure lebesgue (cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a\$m})
1.260 +            + measure lebesgue (cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m})"
1.261 +  proof (rule measure_Un3_negligible)
1.262 +    show "cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a\$m} \<in> lmeasurable" "cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m} \<in> lmeasurable"
1.263 +      by (auto simp: convex_Int convex_halfspace_le convex_halfspace_ge bounded_Int measurable_convex)
1.264 +    have "negligible {x. ?mn \<bullet> x = a\$m}"
1.265 +      by (metis \<open>m \<noteq> n\<close> axis_index_axis eq_iff_diff_eq_0 negligible_hyperplane)
1.266 +    moreover have "?f ` cbox a b \<inter> (cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m}) \<subseteq> {x. ?mn \<bullet> x = a\$m}"
1.267 +      using \<open>m \<noteq> n\<close> antisym_conv by (fastforce simp: algebra_simps mem_box_cart inner_axis')
1.268 +    ultimately show "negligible ((?f ` cbox a b) \<inter> (cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m}))"
1.269 +      by (rule negligible_subset)
1.270 +    have "negligible {x. ?mn \<bullet> x = b\$m}"
1.271 +      by (metis \<open>m \<noteq> n\<close> axis_index_axis eq_iff_diff_eq_0 negligible_hyperplane)
1.272 +    moreover have "(?f ` cbox a b) \<inter> (cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m}) \<subseteq> {x. ?mn \<bullet> x = b\$m}"
1.273 +      using \<open>m \<noteq> n\<close> antisym_conv by (fastforce simp: algebra_simps mem_box_cart inner_axis')
1.274 +    ultimately show "negligible (?f ` cbox a b \<inter> (cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m}))"
1.275 +      by (rule negligible_subset)
1.276 +    have "negligible {x. ?mn \<bullet> x = b\$m}"
1.277 +      by (metis \<open>m \<noteq> n\<close> axis_index_axis eq_iff_diff_eq_0 negligible_hyperplane)
1.278 +    moreover have "(cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m} \<inter> (cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m})) \<subseteq> {x. ?mn \<bullet> x = b\$m}"
1.279 +      using \<open>m \<noteq> n\<close> ab_ne
1.280 +      apply (auto simp: algebra_simps mem_box_cart inner_axis')
1.281 +      apply (drule_tac x=m in spec)+
1.282 +      apply simp
1.283 +      done
1.284 +    ultimately show "negligible (cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m} \<inter> (cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m}))"
1.285 +      by (rule negligible_subset)
1.286 +    show "?f ` cbox a b \<union> cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m} \<union> cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m} = cbox a ?c" (is "?lhs = _")
1.287 +    proof
1.288 +      show "?lhs \<subseteq> cbox a ?c"
1.289 +        by (auto simp: mem_box_cart add_mono) (meson add_increasing2 an order_trans)
1.290 +      show "cbox a ?c \<subseteq> ?lhs"
1.291 +        apply (auto simp: algebra_simps image_iff inner_axis' lambda_add_Galois [OF \<open>m \<noteq> n\<close>])
1.292 +        apply (auto simp: mem_box_cart split: if_split_asm)
1.293 +        done
1.294 +    qed
1.295 +  qed (fact fab)
1.296 +  let ?d = "\<chi> i. if i = m then a \$ m - b \$ m else 0"
1.297 +  have eq2: "measure lebesgue (cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m}) + measure lebesgue (cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m})
1.298 +           = measure lebesgue (cbox a (\<chi> i. if i = m then a \$ m + b \$ n else b \$ i))"
1.299 +  proof (rule measure_translate_add[of "cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a\$m}" "cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m}"
1.300 +     "(\<chi> i. if i = m then a\$m - b\$m else 0)" "cbox a (\<chi> i. if i = m then a\$m + b\$n else b\$i)"])
1.301 +    show "(cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a\$m}) \<in> lmeasurable"
1.302 +      "cbox a ?c \<inter> {x. ?mn \<bullet> x \<ge> b\$m} \<in> lmeasurable"
1.303 +      by (auto simp: convex_Int convex_halfspace_le convex_halfspace_ge bounded_Int measurable_convex)
1.304 +    have "\<And>x. \<lbrakk>x \$ n + a \$ m \<le> x \$ m\<rbrakk>
1.305 +         \<Longrightarrow> x \<in> (+) (\<chi> i. if i = m then a \$ m - b \$ m else 0) ` {x. x \$ n + b \$ m \<le> x \$ m}"
1.306 +      using \<open>m \<noteq> n\<close>
1.307 +      by (rule_tac x="x - (\<chi> i. if i = m then a\$m - b\$m else 0)" in image_eqI)
1.308 +         (simp_all add: mem_box_cart)
1.309 +    then have imeq: "(+) ?d ` {x. b \$ m \<le> ?mn \<bullet> x} = {x. a \$ m \<le> ?mn \<bullet> x}"
1.310 +      using \<open>m \<noteq> n\<close> by (auto simp: mem_box_cart inner_axis' algebra_simps)
1.311 +    have "\<And>x. \<lbrakk>0 \<le> a \$ n; x \$ n + a \$ m \<le> x \$ m;
1.312 +                \<forall>i. i \<noteq> m \<longrightarrow> a \$ i \<le> x \$ i \<and> x \$ i \<le> b \$ i\<rbrakk>
1.313 +         \<Longrightarrow> a \$ m \<le> x \$ m"
1.314 +      using \<open>m \<noteq> n\<close>  by force
1.315 +    then have "(+) ?d ` (cbox a ?c \<inter> {x. b \$ m \<le> ?mn \<bullet> x})
1.316 +            = cbox a (\<chi> i. if i = m then a \$ m + b \$ n else b \$ i) \<inter> {x. a \$ m \<le> ?mn \<bullet> x}"
1.317 +      using an ab_ne
1.318 +      apply (simp add: cbox_translation [symmetric] translation_Int interval_ne_empty_cart imeq)
1.319 +      apply (auto simp: mem_box_cart inner_axis' algebra_simps if_distrib all_if_distrib)
1.320 +      by (metis (full_types) add_mono mult_2_right)
1.321 +    then show "cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m} \<union>
1.322 +          (+) ?d ` (cbox a ?c \<inter> {x. b \$ m \<le> ?mn \<bullet> x}) =
1.323 +          cbox a (\<chi> i. if i = m then a \$ m + b \$ n else b \$ i)"  (is "?lhs = ?rhs")
1.324 +      using an \<open>m \<noteq> n\<close>
1.325 +      apply (auto simp: mem_box_cart inner_axis' algebra_simps if_distrib all_if_distrib, force)
1.326 +        apply (drule_tac x=n in spec)+
1.327 +      by (meson ab_ne add_mono_thms_linordered_semiring(3) dual_order.trans interval_ne_empty_cart(1))
1.328 +    have "negligible{x. ?mn \<bullet> x = a\$m}"
1.329 +      by (metis \<open>m \<noteq> n\<close> axis_index_axis eq_iff_diff_eq_0 negligible_hyperplane)
1.330 +    moreover have "(cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m} \<inter>
1.331 +                                 (+) ?d ` (cbox a ?c \<inter> {x. b \$ m \<le> ?mn \<bullet> x})) \<subseteq> {x. ?mn \<bullet> x = a\$m}"
1.332 +      using \<open>m \<noteq> n\<close> antisym_conv by (fastforce simp: algebra_simps mem_box_cart inner_axis')
1.333 +    ultimately show "negligible (cbox a ?c \<inter> {x. ?mn \<bullet> x \<le> a \$ m} \<inter>
1.334 +                                 (+) ?d ` (cbox a ?c \<inter> {x. b \$ m \<le> ?mn \<bullet> x}))"
1.335 +      by (rule negligible_subset)
1.336 +  qed
1.337 +  have ac_ne: "cbox a ?c \<noteq> {}"
1.338 +    using ab_ne an
1.339 +    by (clarsimp simp: interval_eq_empty_cart) (meson add_less_same_cancel1 le_less_linear less_le_trans)
1.340 +  have ax_ne: "cbox a (\<chi> i. if i = m then a \$ m + b \$ n else b \$ i) \<noteq> {}"
1.341 +    using ab_ne an
1.342 +    by (clarsimp simp: interval_eq_empty_cart) (meson add_less_same_cancel1 le_less_linear less_le_trans)
1.343 +  have eq3: "measure lebesgue (cbox a ?c) = measure lebesgue (cbox a (\<chi> i. if i = m then a\$m + b\$n else b\$i)) + measure lebesgue (cbox a b)"
1.344 +    by (simp add: content_cbox_if_cart ab_ne ac_ne ax_ne algebra_simps prod.delta_remove
1.345 +             if_distrib [of "\<lambda>u. u - z" for z] prod.remove)
1.346 +  show ?Q
1.347 +    using eq1 eq2 eq3
1.348 +    by (simp add: algebra_simps)
1.349 +qed
1.350 +
1.351 +
1.352 +
1.353 +proposition
1.354 +  fixes S :: "(real^'n) set"
1.355 +  assumes "S \<in> lmeasurable"
1.356 +  shows measurable_stretch: "((\<lambda>x. \<chi> k. m k * x\$k) ` S) \<in> lmeasurable" (is  "?f ` S \<in> _")
1.357 +    and measure_stretch: "measure lebesgue ((\<lambda>x. \<chi> k. m k * x\$k) ` S) = \<bar>prod m UNIV\<bar> * measure lebesgue S"
1.358 +    (is "?MEQ")
1.359 +proof -
1.360 +  have "(?f ` S) \<in> lmeasurable \<and> ?MEQ"
1.361 +  proof (cases "\<forall>k. m k \<noteq> 0")
1.362 +    case True
1.363 +    have m0: "0 < \<bar>prod m UNIV\<bar>"
1.364 +      using True by simp
1.365 +    have "(indicat_real (?f ` S) has_integral \<bar>prod m UNIV\<bar> * measure lebesgue S) UNIV"
1.366 +    proof (clarsimp simp add: has_integral_alt [where i=UNIV])
1.367 +      fix e :: "real"
1.368 +      assume "e > 0"
1.369 +      have "(indicat_real S has_integral (measure lebesgue S)) UNIV"
1.370 +        using assms lmeasurable_iff_has_integral by blast
1.371 +      then obtain B where "B>0"
1.372 +        and B: "\<And>a b. ball 0 B \<subseteq> cbox a b \<Longrightarrow>
1.373 +                        \<exists>z. (indicat_real S has_integral z) (cbox a b) \<and>
1.374 +                            \<bar>z - measure lebesgue S\<bar> < e / \<bar>prod m UNIV\<bar>"
1.375 +        by (simp add: has_integral_alt [where i=UNIV]) (metis (full_types) divide_pos_pos m0  m0 \<open>e > 0\<close>)
1.376 +      show "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> cbox a b \<longrightarrow>
1.377 +                  (\<exists>z. (indicat_real (?f ` S) has_integral z) (cbox a b) \<and>
1.378 +                       \<bar>z - \<bar>prod m UNIV\<bar> * measure lebesgue S\<bar> < e)"
1.379 +      proof (intro exI conjI allI)
1.380 +        let ?C = "Max (range (\<lambda>k. \<bar>m k\<bar>)) * B"
1.381 +        show "?C > 0"
1.382 +          using True \<open>B > 0\<close> by (simp add: Max_gr_iff)
1.383 +        show "ball 0 ?C \<subseteq> cbox u v \<longrightarrow>
1.384 +                  (\<exists>z. (indicat_real (?f ` S) has_integral z) (cbox u v) \<and>
1.385 +                       \<bar>z - \<bar>prod m UNIV\<bar> * measure lebesgue S\<bar> < e)" for u v
1.386 +        proof
1.387 +          assume uv: "ball 0 ?C \<subseteq> cbox u v"
1.388 +          with \<open>?C > 0\<close> have cbox_ne: "cbox u v \<noteq> {}"
1.389 +            using centre_in_ball by blast
1.390 +          let ?\<alpha> = "\<lambda>k. u\$k / m k"
1.391 +          let ?\<beta> = "\<lambda>k. v\$k / m k"
1.392 +          have invm0: "\<And>k. inverse (m k) \<noteq> 0"
1.393 +            using True by auto
1.394 +          have "ball 0 B \<subseteq> (\<lambda>x. \<chi> k. x \$ k / m k) ` ball 0 ?C"
1.395 +          proof clarsimp
1.396 +            fix x :: "(real, 'n) vec"
1.397 +            assume x: "norm x < B"
1.398 +            have [simp]: "\<bar>Max (range (\<lambda>k. \<bar>m k\<bar>))\<bar> = Max (range (\<lambda>k. \<bar>m k\<bar>))"
1.399 +              by (meson Max_ge abs_ge_zero abs_of_nonneg finite finite_imageI order_trans rangeI)
1.400 +            have "norm (\<chi> k. m k * x \$ k) \<le> norm (Max (range (\<lambda>k. \<bar>m k\<bar>)) *\<^sub>R x)"
1.401 +              by (rule norm_le_componentwise_cart) (auto simp: abs_mult intro: mult_right_mono)
1.402 +            also have "\<dots> < ?C"
1.403 +              using x by simp (metis \<open>B > 0\<close> \<open>?C > 0\<close> mult.commute real_mult_less_iff1 zero_less_mult_pos)
1.404 +            finally have "norm (\<chi> k. m k * x \$ k) < ?C" .
1.405 +            then show "x \<in> (\<lambda>x. \<chi> k. x \$ k / m k) ` ball 0 ?C"
1.406 +              using stretch_Galois [of "inverse \<circ> m"] True by (auto simp: image_iff field_simps)
1.407 +          qed
1.408 +          then have Bsub: "ball 0 B \<subseteq> cbox (\<chi> k. min (?\<alpha> k) (?\<beta> k)) (\<chi> k. max (?\<alpha> k) (?\<beta> k))"
1.409 +            using cbox_ne uv image_stretch_interval_cart [of "inverse \<circ> m" u v, symmetric]
1.410 +            by (force simp: field_simps)
1.411 +          obtain z where zint: "(indicat_real S has_integral z) (cbox (\<chi> k. min (?\<alpha> k) (?\<beta> k)) (\<chi> k. max (?\<alpha> k) (?\<beta> k)))"
1.412 +                   and zless: "\<bar>z - measure lebesgue S\<bar> < e / \<bar>prod m UNIV\<bar>"
1.413 +            using B [OF Bsub] by blast
1.414 +          have ind: "indicat_real (?f ` S) = (\<lambda>x. indicator S (\<chi> k. x\$k / m k))"
1.415 +            using True stretch_Galois [of m] by (force simp: indicator_def)
1.416 +          show "\<exists>z. (indicat_real (?f ` S) has_integral z) (cbox u v) \<and>
1.417 +                       \<bar>z - \<bar>prod m UNIV\<bar> * measure lebesgue S\<bar> < e"
1.418 +          proof (simp add: ind, intro conjI exI)
1.419 +            have "((\<lambda>x. indicat_real S (\<chi> k. x \$ k/ m k)) has_integral z *\<^sub>R \<bar>prod m UNIV\<bar>)
1.420 +                ((\<lambda>x. \<chi> k. x \$ k * m k) ` cbox (\<chi> k. min (?\<alpha> k) (?\<beta> k)) (\<chi> k. max (?\<alpha> k) (?\<beta> k)))"
1.421 +              using True has_integral_stretch_cart [OF zint, of "inverse \<circ> m"]
1.422 +              by (simp add: field_simps prod_dividef)
1.423 +            moreover have "((\<lambda>x. \<chi> k. x \$ k * m k) ` cbox (\<chi> k. min (?\<alpha> k) (?\<beta> k)) (\<chi> k. max (?\<alpha> k) (?\<beta> k))) = cbox u v"
1.424 +              using True image_stretch_interval_cart [of "inverse \<circ> m" u v, symmetric]
1.425 +                image_stretch_interval_cart [of "\<lambda>k. 1" u v, symmetric] \<open>cbox u v \<noteq> {}\<close>
1.426 +              by (simp add: field_simps image_comp o_def)
1.427 +            ultimately show "((\<lambda>x. indicat_real S (\<chi> k. x \$ k/ m k)) has_integral z *\<^sub>R \<bar>prod m UNIV\<bar>) (cbox u v)"
1.428 +              by simp
1.429 +            have "\<bar>z *\<^sub>R \<bar>prod m UNIV\<bar> - \<bar>prod m UNIV\<bar> * measure lebesgue S\<bar>
1.430 +                 = \<bar>prod m UNIV\<bar> * \<bar>z - measure lebesgue S\<bar>"
1.431 +              by (metis (no_types, hide_lams) abs_abs abs_scaleR mult.commute real_scaleR_def right_diff_distrib')
1.432 +            also have "\<dots> < e"
1.433 +              using zless True by (simp add: field_simps)
1.434 +            finally show "\<bar>z *\<^sub>R \<bar>prod m UNIV\<bar> - \<bar>prod m UNIV\<bar> * measure lebesgue S\<bar> < e" .
1.435 +          qed
1.436 +        qed
1.437 +      qed
1.438 +    qed
1.439 +    then show ?thesis
1.440 +      by (auto simp: has_integral_integrable integral_unique lmeasure_integral_UNIV measurable_integrable)
1.441 +  next
1.442 +    case False
1.443 +    then obtain k where "m k = 0" and prm: "prod m UNIV = 0"
1.444 +      by auto
1.445 +    have nfS: "negligible (?f ` S)"
1.446 +      by (rule negligible_subset [OF negligible_standard_hyperplane_cart]) (use \<open>m k = 0\<close> in auto)
1.447 +    then have "(?f ` S) \<in> lmeasurable"
1.448 +      by (simp add: negligible_iff_measure)
1.449 +    with nfS show ?thesis
1.450 +      by (simp add: prm negligible_iff_measure0)
1.451 +  qed
1.452 +  then show "(?f ` S) \<in> lmeasurable" ?MEQ
1.453 +    by metis+
1.454 +qed
1.455 +
1.456 +
1.457 +
1.458 +
1.459 +
1.460 +proposition
1.461 + fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
1.462 +  assumes "linear f" "S \<in> lmeasurable"
1.463 +  shows measurable_linear_image: "(f ` S) \<in> lmeasurable"
1.464 +    and measure_linear_image: "measure lebesgue (f ` S) = \<bar>det (matrix f)\<bar> * measure lebesgue S" (is "?Q f S")
1.465 +proof -
1.466 +  have "\<forall>S \<in> lmeasurable. (f ` S) \<in> lmeasurable \<and> ?Q f S"
1.467 +  proof (rule induct_linear_elementary [OF \<open>linear f\<close>]; intro ballI)
1.468 +    fix f g and S :: "(real,'n) vec set"
1.469 +    assume "linear f" and "linear g"
1.470 +      and f [rule_format]: "\<forall>S \<in> lmeasurable. f ` S \<in> lmeasurable \<and> ?Q f S"
1.471 +      and g [rule_format]: "\<forall>S \<in> lmeasurable. g ` S \<in> lmeasurable \<and> ?Q g S"
1.472 +      and S: "S \<in> lmeasurable"
1.473 +    then have gS: "g ` S \<in> lmeasurable"
1.474 +      by blast
1.475 +    show "(f \<circ> g) ` S \<in> lmeasurable \<and> ?Q (f \<circ> g) S"
1.476 +      using f [OF gS] g [OF S] matrix_compose [OF \<open>linear g\<close> \<open>linear f\<close>]
1.477 +      by (simp add: o_def image_comp abs_mult det_mul)
1.478 +  next
1.479 +    fix f :: "(real, 'n) vec \<Rightarrow> (real, 'n) vec" and i and S :: "(real, 'n) vec set"
1.480 +    assume "linear f" and 0: "\<And>x. f x \$ i = 0" and "S \<in> lmeasurable"
1.481 +    then have "\<not> inj f"
1.482 +      by (metis (full_types) linear_injective_imp_surjective one_neq_zero surjE vec_component)
1.483 +    have detf: "det (matrix f) = 0"
1.484 +      by (metis "0" \<open>linear f\<close> invertible_det_nz invertible_right_inverse matrix_right_invertible_surjective matrix_vector_mul surjE vec_component)
1.485 +    show "f ` S \<in> lmeasurable \<and> ?Q f S"
1.486 +    proof
1.487 +      show "f ` S \<in> lmeasurable"
1.488 +        using lmeasurable_iff_indicator_has_integral \<open>linear f\<close> \<open>\<not> inj f\<close> negligible_UNIV negligible_linear_singular_image by blast
1.489 +      have "measure lebesgue (f ` S) = 0"
1.490 +        by (meson \<open>\<not> inj f\<close> \<open>linear f\<close> negligible_imp_measure0 negligible_linear_singular_image)
1.491 +      also have "\<dots> = \<bar>det (matrix f)\<bar> * measure lebesgue S"
1.492 +        by (simp add: detf)
1.493 +      finally show "?Q f S" .
1.494 +    qed
1.495 +  next
1.496 +    fix c and S :: "(real, 'n) vec set"
1.497 +    assume "S \<in> lmeasurable"
1.498 +    show "(\<lambda>a. \<chi> i. c i * a \$ i) ` S \<in> lmeasurable \<and> ?Q (\<lambda>a. \<chi> i. c i * a \$ i) S"
1.499 +    proof
1.500 +      show "(\<lambda>a. \<chi> i. c i * a \$ i) ` S \<in> lmeasurable"
1.501 +        by (simp add: \<open>S \<in> lmeasurable\<close> measurable_stretch)
1.502 +      show "?Q (\<lambda>a. \<chi> i. c i * a \$ i) S"
1.503 +        by (simp add: measure_stretch [OF \<open>S \<in> lmeasurable\<close>, of c] axis_def matrix_def det_diagonal)
1.504 +    qed
1.505 +  next
1.506 +    fix m :: "'n" and n :: "'n" and S :: "(real, 'n) vec set"
1.507 +    assume "m \<noteq> n" and "S \<in> lmeasurable"
1.508 +    let ?h = "\<lambda>v::(real, 'n) vec. \<chi> i. v \$ Fun.swap m n id i"
1.509 +    have lin: "linear ?h"
1.510 +      by (simp add: plus_vec_def scaleR_vec_def linearI)
1.511 +    have meq: "measure lebesgue ((\<lambda>v::(real, 'n) vec. \<chi> i. v \$ Fun.swap m n id i) ` cbox a b)
1.512 +             = measure lebesgue (cbox a b)" for a b
1.513 +    proof (cases "cbox a b = {}")
1.514 +      case True then show ?thesis
1.515 +        by simp
1.516 +    next
1.517 +      case False
1.518 +      then have him: "?h ` (cbox a b) \<noteq> {}"
1.519 +        by blast
1.520 +      have eq: "?h ` (cbox a b) = cbox (?h a) (?h b)"
1.521 +        by (auto simp: image_iff lambda_swap_Galois mem_box_cart) (metis swap_id_eq)+
1.522 +      show ?thesis
1.523 +        using him prod.permute [OF permutes_swap_id, where S=UNIV and g="\<lambda>i. (b - a)\$i", symmetric]
1.524 +        by (simp add: eq content_cbox_cart False)
1.525 +    qed
1.526 +    have "(\<chi> i j. if Fun.swap m n id i = j then 1 else 0) = (\<chi> i j. if j = Fun.swap m n id i then 1 else (0::real))"
1.527 +      by (auto intro!: Cart_lambda_cong)
1.528 +    then have "matrix ?h = transpose(\<chi> i j. mat 1 \$ i \$ Fun.swap m n id j)"
1.529 +      by (auto simp: matrix_eq transpose_def axis_def mat_def matrix_def)
1.530 +    then have 1: "\<bar>det (matrix ?h)\<bar> = 1"
1.531 +      by (simp add: det_permute_columns permutes_swap_id sign_swap_id abs_mult)
1.532 +    show "?h ` S \<in> lmeasurable \<and> ?Q ?h S"
1.533 +    proof
1.534 +      show "?h ` S \<in> lmeasurable" "?Q ?h S"
1.535 +        using measure_linear_sufficient [OF lin \<open>S \<in> lmeasurable\<close>] meq 1 by force+
1.536 +    qed
1.537 +  next
1.538 +    fix m :: "'n" and n :: "'n" and S :: "(real, 'n) vec set"
1.539 +    assume "m \<noteq> n" and "S \<in> lmeasurable"
1.540 +    let ?h = "\<lambda>v::(real, 'n) vec. \<chi> i. if i = m then v \$ m + v \$ n else v \$ i"
1.541 +    have lin: "linear ?h"
1.542 +      by (auto simp: algebra_simps plus_vec_def scaleR_vec_def vec_eq_iff intro: linearI)
1.543 +    consider "m < n" | " n < m"
1.544 +      using \<open>m \<noteq> n\<close> less_linear by blast
1.545 +    then have 1: "det(matrix ?h) = 1"
1.546 +    proof cases
1.547 +      assume "m < n"
1.548 +      have *: "matrix ?h \$ i \$ j = (0::real)" if "j < i" for i j :: 'n
1.549 +      proof -
1.550 +        have "axis j 1 = (\<chi> n. if n = j then 1 else (0::real))"
1.551 +          using axis_def by blast
1.552 +        then have "(\<chi> p q. if p = m then axis q 1 \$ m + axis q 1 \$ n else axis q 1 \$ p) \$ i \$ j = (0::real)"
1.553 +          using \<open>j < i\<close> axis_def \<open>m < n\<close> by auto
1.554 +        with \<open>m < n\<close> show ?thesis
1.555 +          by (auto simp: matrix_def axis_def cong: if_cong)
1.556 +      qed
1.557 +      show ?thesis
1.558 +        using \<open>m \<noteq> n\<close> by (subst det_upperdiagonal [OF *]) (auto simp: matrix_def axis_def cong: if_cong)
1.559 +    next
1.560 +      assume "n < m"
1.561 +      have *: "matrix ?h \$ i \$ j = (0::real)" if "j > i" for i j :: 'n
1.562 +      proof -
1.563 +        have "axis j 1 = (\<chi> n. if n = j then 1 else (0::real))"
1.564 +          using axis_def by blast
1.565 +        then have "(\<chi> p q. if p = m then axis q 1 \$ m + axis q 1 \$ n else axis q 1 \$ p) \$ i \$ j = (0::real)"
1.566 +          using \<open>j > i\<close> axis_def \<open>m > n\<close> by auto
1.567 +        with \<open>m > n\<close> show ?thesis
1.568 +          by (auto simp: matrix_def axis_def cong: if_cong)
1.569 +      qed
1.570 +      show ?thesis
1.571 +        using \<open>m \<noteq> n\<close>
1.572 +        by (subst det_lowerdiagonal [OF *]) (auto simp: matrix_def axis_def cong: if_cong)
1.573 +    qed
1.574 +    have meq: "measure lebesgue (?h ` (cbox a b)) = measure lebesgue (cbox a b)" for a b
1.575 +    proof (cases "cbox a b = {}")
1.576 +      case True then show ?thesis by simp
1.577 +    next
1.578 +      case False
1.579 +      then have ne: "(+) (\<chi> i. if i = n then - a \$ n else 0) ` cbox a b \<noteq> {}"
1.580 +        by auto
1.581 +      let ?v = "\<chi> i. if i = n then - a \$ n else 0"
1.582 +      have "?h ` cbox a b
1.583 +            = (+) (\<chi> i. if i = m \<or> i = n then a \$ n else 0) ` ?h ` (+) ?v ` (cbox a b)"
1.584 +        using \<open>m \<noteq> n\<close> unfolding image_comp o_def by (force simp: vec_eq_iff)
1.585 +      then have "measure lebesgue (?h ` (cbox a b))
1.586 +               = measure lebesgue ((\<lambda>v. \<chi> i. if i = m then v \$ m + v \$ n else v \$ i) `
1.587 +                                   (+) ?v ` cbox a b)"
1.588 +        by (rule ssubst) (rule measure_translation)
1.589 +      also have "\<dots> = measure lebesgue ((\<lambda>v. \<chi> i. if i = m then v \$ m + v \$ n else v \$ i) ` cbox (?v +a) (?v + b))"
1.590 +        by (metis (no_types, lifting) cbox_translation)
1.591 +      also have "\<dots> = measure lebesgue ((+) (\<chi> i. if i = n then - a \$ n else 0) ` cbox a b)"
1.592 +        apply (subst measure_shear_interval)
1.593 +        using \<open>m \<noteq> n\<close> ne apply auto
1.594 +        apply (simp add: cbox_translation)
1.595 +        by (metis cbox_borel cbox_translation measure_completion sets_lborel)
1.596 +      also have "\<dots> = measure lebesgue (cbox a b)"
1.597 +        by (rule measure_translation)
1.598 +        finally show ?thesis .
1.599 +      qed
1.600 +    show "?h ` S \<in> lmeasurable \<and> ?Q ?h S"
1.601 +      using measure_linear_sufficient [OF lin \<open>S \<in> lmeasurable\<close>] meq 1 by force
1.602 +  qed
1.603 +  with assms show "(f ` S) \<in> lmeasurable" "?Q f S"
1.604 +    by metis+
1.605 +qed
1.606 +
1.607 +
1.608 +lemma
1.609 + fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
1.610 +  assumes f: "orthogonal_transformation f" and S: "S \<in> lmeasurable"
1.611 +  shows measurable_orthogonal_image: "f ` S \<in> lmeasurable"
1.612 +    and measure_orthogonal_image: "measure lebesgue (f ` S) = measure lebesgue S"
1.613 +proof -
1.614 +  have "linear f"
1.615 +    by (simp add: f orthogonal_transformation_linear)
1.616 +  then show "f ` S \<in> lmeasurable"
1.617 +    by (metis S measurable_linear_image)
1.618 +  show "measure lebesgue (f ` S) = measure lebesgue S"
1.619 +    by (simp add: measure_linear_image \<open>linear f\<close> S f)
1.620 +qed
1.621 +
1.622 +lemma sets_lebesgue_inner_closed:
1.623 +  assumes "S \<in> sets lebesgue" "e > 0"
1.624 +  obtains T where "closed T" "T \<subseteq> S" "S-T \<in> lmeasurable" "measure lebesgue (S - T) < e"
1.625 +proof -
1.626 +  have "-S \<in> sets lebesgue"
1.627 +    using assms by (simp add: Compl_in_sets_lebesgue)
1.628 +  then obtain T where "open T" "-S \<subseteq> T"
1.629 +          and T: "(T - -S) \<in> lmeasurable" "measure lebesgue (T - -S) < e"
1.630 +    using lmeasurable_outer_open assms  by blast
1.631 +  show thesis
1.632 +  proof
1.633 +    show "closed (-T)"
1.634 +      using \<open>open T\<close> by blast
1.635 +    show "-T \<subseteq> S"
1.636 +      using \<open>- S \<subseteq> T\<close> by auto
1.637 +    show "S - ( -T) \<in> lmeasurable" "measure lebesgue (S - (- T)) < e"
1.638 +      using T by (auto simp: Int_commute)
1.639 +  qed
1.640 +qed
1.641 +
1.642 +subsection\<open>@{text F_sigma} and @{text G_delta} sets.\<close>
1.643 +
1.644 +(*https://en.wikipedia.org/wiki/F\<sigma>_set*)
1.645 +inductive fsigma :: "'a::topological_space set \<Rightarrow> bool" where
1.646 +  "(\<And>n::nat. closed (F n)) \<Longrightarrow> fsigma (UNION UNIV F)"
1.647 +
1.648 +inductive gdelta :: "'a::topological_space set \<Rightarrow> bool" where
1.649 +  "(\<And>n::nat. open (F n)) \<Longrightarrow> gdelta (INTER UNIV F)"
1.650 +
1.651 +
1.652 +lemma fsigma_Union_compact:
1.653 +  fixes S :: "'a::{real_normed_vector,heine_borel} set"
1.654 +  shows "fsigma S \<longleftrightarrow> (\<exists>F::nat \<Rightarrow> 'a set. range F \<subseteq> Collect compact \<and> S = UNION UNIV F)"
1.655 +proof safe
1.656 +  assume "fsigma S"
1.657 +  then obtain F :: "nat \<Rightarrow> 'a set" where F: "range F \<subseteq> Collect closed" "S = UNION UNIV F"
1.658 +    by (meson fsigma.cases image_subsetI mem_Collect_eq)
1.659 +  then have "\<exists>D::nat \<Rightarrow> 'a set. range D \<subseteq> Collect compact \<and> UNION UNIV D = F i" for i
1.660 +    using closed_Union_compact_subsets [of "F i"]
1.661 +    by (metis image_subsetI mem_Collect_eq range_subsetD)
1.662 +  then obtain D :: "nat \<Rightarrow> nat \<Rightarrow> 'a set"
1.663 +    where D: "\<And>i. range (D i) \<subseteq> Collect compact \<and> UNION UNIV (D i) = F i"
1.664 +    by metis
1.665 +  let ?DD = "\<lambda>n. (\<lambda>(i,j). D i j) (prod_decode n)"
1.666 +  show "\<exists>F::nat \<Rightarrow> 'a set. range F \<subseteq> Collect compact \<and> S = UNION UNIV F"
1.667 +  proof (intro exI conjI)
1.668 +    show "range ?DD \<subseteq> Collect compact"
1.669 +      using D by clarsimp (metis mem_Collect_eq rangeI split_conv subsetCE surj_pair)
1.670 +    show "S = UNION UNIV ?DD"
1.671 +    proof
1.672 +      show "S \<subseteq> UNION UNIV ?DD"
1.673 +        using D F
1.674 +        by clarsimp (metis UN_iff old.prod.case prod_decode_inverse prod_encode_eq)
1.675 +      show "UNION UNIV ?DD \<subseteq> S"
1.676 +        using D F  by fastforce
1.677 +    qed
1.678 +  qed
1.679 +next
1.680 +  fix F :: "nat \<Rightarrow> 'a set"
1.681 +  assume "range F \<subseteq> Collect compact" and "S = UNION UNIV F"
1.682 +  then show "fsigma (UNION UNIV F)"
1.683 +    by (simp add: compact_imp_closed fsigma.intros image_subset_iff)
1.684 +qed
1.685 +
1.686 +lemma gdelta_imp_fsigma: "gdelta S \<Longrightarrow> fsigma (- S)"
1.687 +proof (induction rule: gdelta.induct)
1.688 +  case (1 F)
1.689 +  have "- INTER UNIV F = (\<Union>i. -(F i))"
1.690 +    by auto
1.691 +  then show ?case
1.692 +    by (simp add: fsigma.intros closed_Compl 1)
1.693 +qed
1.694 +
1.695 +lemma fsigma_imp_gdelta: "fsigma S \<Longrightarrow> gdelta (- S)"
1.696 +proof (induction rule: fsigma.induct)
1.697 +  case (1 F)
1.698 +  have "- UNION UNIV F = (\<Inter>i. -(F i))"
1.699 +    by auto
1.700 +  then show ?case
1.701 +    by (simp add: 1 gdelta.intros open_closed)
1.702 +qed
1.703 +
1.704 +
1.705 +
1.706 +lemma gdelta_complement: "gdelta(- S) \<longleftrightarrow> fsigma S"
1.707 +  using fsigma_imp_gdelta gdelta_imp_fsigma by force
1.708 +
1.709 +
1.710 +text\<open>A Lebesgue set is almost an @{text F_sigma} or @{text G_delta}.\<close>
1.711 +lemma lebesgue_set_almost_fsigma:
1.712 +  assumes "S \<in> sets lebesgue"
1.713 +  obtains C T where "fsigma C" "negligible T" "C \<union> T = S" "disjnt C T"
1.714 +proof -
1.715 +  { fix n::nat
1.716 +    have "\<exists>T. closed T \<and> T \<subseteq> S \<and> S - T \<in> lmeasurable \<and> measure lebesgue (S-T) < 1 / Suc n"
1.717 +      using sets_lebesgue_inner_closed [OF assms]
1.718 +      by (metis divide_pos_pos less_numeral_extra(1) of_nat_0_less_iff zero_less_Suc)
1.719 +  }
1.720 +  then obtain F where F: "\<And>n::nat. closed (F n) \<and> F n \<subseteq> S \<and> S - F n \<in> lmeasurable \<and> measure lebesgue (S - F n) < 1 / Suc n"
1.721 +    by metis
1.722 +  let ?C = "UNION UNIV F"
1.723 +  show thesis
1.724 +  proof
1.725 +    show "fsigma ?C"
1.726 +      using F by (simp add: fsigma.intros)
1.727 +    show "negligible (S - ?C)"
1.728 +    proof (clarsimp simp add: negligible_outer_le)
1.729 +      fix e :: "real"
1.730 +      assume "0 < e"
1.731 +      then obtain n where n: "1 / Suc n < e"
1.732 +        using nat_approx_posE by metis
1.733 +      show "\<exists>T. S - (\<Union>x. F x) \<subseteq> T \<and> T \<in> lmeasurable \<and> measure lebesgue T \<le> e"
1.734 +      proof (intro exI conjI)
1.735 +        show "measure lebesgue (S - F n) \<le> e"
1.736 +          by (meson F n less_trans not_le order.asym)
1.737 +      qed (use F in auto)
1.738 +    qed
1.739 +    show "?C \<union> (S - ?C) = S"
1.740 +      using F by blast
1.741 +    show "disjnt ?C (S - ?C)"
1.742 +      by (auto simp: disjnt_def)
1.743 +  qed
1.744 +qed
1.745 +
1.746 +lemma lebesgue_set_almost_gdelta:
1.747 +  assumes "S \<in> sets lebesgue"
1.748 +  obtains C T where "gdelta C" "negligible T" "S \<union> T = C" "disjnt S T"
1.749 +proof -
1.750 +  have "-S \<in> sets lebesgue"
1.751 +    using assms Compl_in_sets_lebesgue by blast
1.752 +  then obtain C T where C: "fsigma C" "negligible T" "C \<union> T = -S" "disjnt C T"
1.753 +    using lebesgue_set_almost_fsigma by metis
1.754 +  show thesis
1.755 +  proof
1.756 +    show "gdelta (-C)"
1.757 +      by (simp add: \<open>fsigma C\<close> fsigma_imp_gdelta)
1.758 +    show "S \<union> T = -C" "disjnt S T"
1.759 +      using C by (auto simp: disjnt_def)
1.760 +  qed (use C in auto)
1.761 +qed
1.762 +
1.763 +
1.764 +proposition measure_semicontinuous_with_hausdist_explicit:
1.765 +  assumes "bounded S" and neg: "negligible(frontier S)" and "e > 0"
1.766 +  obtains d where "d > 0"
1.767 +                  "\<And>T. \<lbrakk>T \<in> lmeasurable; \<And>y. y \<in> T \<Longrightarrow> \<exists>x. x \<in> S \<and> dist x y < d\<rbrakk>
1.768 +                        \<Longrightarrow> measure lebesgue T < measure lebesgue S + e"
1.769 +proof (cases "S = {}")
1.770 +  case True
1.771 +  with that \<open>e > 0\<close> show ?thesis by force
1.772 +next
1.773 +  case False
1.774 +  then have frS: "frontier S \<noteq> {}"
1.775 +    using \<open>bounded S\<close> frontier_eq_empty not_bounded_UNIV by blast
1.776 +  have "S \<in> lmeasurable"
1.777 +    by (simp add: \<open>bounded S\<close> measurable_Jordan neg)
1.778 +  have null: "(frontier S) \<in> null_sets lebesgue"
1.779 +    by (metis neg negligible_iff_null_sets)
1.780 +  have "frontier S \<in> lmeasurable" and mS0: "measure lebesgue (frontier S) = 0"
1.781 +    using neg negligible_imp_measurable negligible_iff_measure by blast+
1.782 +  with \<open>e > 0\<close> lmeasurable_outer_open
1.783 +  obtain U where "open U"
1.784 +    and U: "frontier S \<subseteq> U" "U - frontier S \<in> lmeasurable" "measure lebesgue (U - frontier S) < e"
1.785 +    by (metis fmeasurableD)
1.786 +  with null have "U \<in> lmeasurable"
1.787 +    by (metis borel_open measurable_Diff_null_set sets_completionI_sets sets_lborel)
1.788 +  have "measure lebesgue (U - frontier S) = measure lebesgue U"
1.789 +    using mS0 by (simp add: \<open>U \<in> lmeasurable\<close> fmeasurableD measure_Diff_null_set null)
1.790 +  with U have mU: "measure lebesgue U < e"
1.791 +    by simp
1.792 +  show ?thesis
1.793 +  proof
1.794 +    have "U \<noteq> UNIV"
1.795 +      using \<open>U \<in> lmeasurable\<close> by auto
1.796 +    then have "- U \<noteq> {}"
1.797 +      by blast
1.798 +    with \<open>open U\<close> \<open>frontier S \<subseteq> U\<close> show "setdist (frontier S) (- U) > 0"
1.799 +      by (auto simp: \<open>bounded S\<close> open_closed compact_frontier_bounded setdist_gt_0_compact_closed frS)
1.800 +    fix T
1.801 +    assume "T \<in> lmeasurable"
1.802 +      and T: "\<And>t. t \<in> T \<Longrightarrow> \<exists>y. y \<in> S \<and> dist y t < setdist (frontier S) (- U)"
1.803 +    then have "measure lebesgue T - measure lebesgue S \<le> measure lebesgue (T - S)"
1.804 +      by (simp add: \<open>S \<in> lmeasurable\<close> measure_diff_le_measure_setdiff)
1.805 +    also have "\<dots>  \<le> measure lebesgue U"
1.806 +    proof -
1.807 +      have "T - S \<subseteq> U"
1.808 +      proof clarify
1.809 +        fix x
1.810 +        assume "x \<in> T" and "x \<notin> S"
1.811 +        then obtain y where "y \<in> S" and y: "dist y x < setdist (frontier S) (- U)"
1.812 +          using T by blast
1.813 +        have "closed_segment x y \<inter> frontier S \<noteq> {}"
1.814 +          using connected_Int_frontier \<open>x \<notin> S\<close> \<open>y \<in> S\<close> by blast
1.815 +        then obtain z where z: "z \<in> closed_segment x y" "z \<in> frontier S"
1.816 +          by auto
1.817 +        with y have "dist z x < setdist(frontier S) (- U)"
1.818 +          by (auto simp: dist_commute dest!: dist_in_closed_segment)
1.819 +        with z have False if "x \<in> -U"
1.820 +          using setdist_le_dist [OF \<open>z \<in> frontier S\<close> that] by auto
1.821 +        then show "x \<in> U"
1.822 +          by blast
1.823 +      qed
1.824 +      then show ?thesis
1.825 +        by (simp add: \<open>S \<in> lmeasurable\<close> \<open>T \<in> lmeasurable\<close> \<open>U \<in> lmeasurable\<close> fmeasurableD measure_mono_fmeasurable sets.Diff)
1.826 +    qed
1.827 +    finally have "measure lebesgue T - measure lebesgue S \<le> measure lebesgue U" .
1.828 +    with mU show "measure lebesgue T < measure lebesgue S + e"
1.829 +      by linarith
1.830 +  qed
1.831 +qed
1.832 +
1.833 +proposition lebesgue_regular_inner:
1.834 + assumes "S \<in> sets lebesgue"
1.835 + obtains K C where "negligible K" "\<And>n::nat. compact(C n)" "S = (\<Union>n. C n) \<union> K"
1.836 +proof -
1.837 +  have "\<exists>T. closed T \<and> T \<subseteq> S \<and> (S - T) \<in> lmeasurable \<and> measure lebesgue (S - T) < (1/2)^n" for n
1.838 +    using sets_lebesgue_inner_closed assms
1.839 +    by (metis sets_lebesgue_inner_closed zero_less_divide_1_iff zero_less_numeral zero_less_power)
1.840 +  then obtain C where clo: "\<And>n. closed (C n)" and subS: "\<And>n. C n \<subseteq> S"
1.841 +    and mea: "\<And>n. (S - C n) \<in> lmeasurable"
1.842 +    and less: "\<And>n. measure lebesgue (S - C n) < (1/2)^n"
1.843 +    by metis
1.844 +  have "\<exists>F. (\<forall>n::nat. compact(F n)) \<and> (\<Union>n. F n) = C m" for m::nat
1.845 +    by (metis clo closed_Union_compact_subsets)
1.846 +  then obtain D :: "[nat,nat] \<Rightarrow> 'a set" where D: "\<And>m n. compact(D m n)" "\<And>m. (\<Union>n. D m n) = C m"
1.847 +    by metis
1.848 +  let ?C = "from_nat_into (\<Union>m. range (D m))"
1.849 +  have "countable (\<Union>m. range (D m))"
1.850 +    by blast
1.851 +  have "range (from_nat_into (\<Union>m. range (D m))) = (\<Union>m. range (D m))"
1.852 +    using range_from_nat_into by simp
1.853 +  then have CD: "\<exists>m n. ?C k = D m n"  for k
1.854 +    by (metis (mono_tags, lifting) UN_iff rangeE range_eqI)
1.855 +  show thesis
1.856 +  proof
1.857 +    show "negligible (S - (\<Union>n. C n))"
1.858 +    proof (clarsimp simp: negligible_outer_le)
1.859 +      fix e :: "real"
1.860 +      assume "e > 0"
1.861 +      then obtain n where n: "(1/2)^n < e"
1.862 +        using real_arch_pow_inv [of e "1/2"] by auto
1.863 +      show "\<exists>T. S - (\<Union>n. C n) \<subseteq> T \<and> T \<in> lmeasurable \<and> measure lebesgue T \<le> e"
1.864 +      proof (intro exI conjI)
1.865 +        show "S - (\<Union>n. C n) \<subseteq> S - C n"
1.866 +          by blast
1.867 +        show "S - C n \<in> lmeasurable"
1.868 +          by (simp add: mea)
1.869 +        show "measure lebesgue (S - C n) \<le> e"
1.870 +          using less [of n] n by simp
1.871 +      qed
1.872 +    qed
1.873 +    show "compact (?C n)" for n
1.874 +      using CD D by metis
1.875 +    show "S = (\<Union>n. ?C n) \<union> (S - (\<Union>n. C n))" (is "_ = ?rhs")
1.876 +    proof
1.877 +      show "S \<subseteq> ?rhs"
1.878 +        using D by fastforce
1.879 +      show "?rhs \<subseteq> S"
1.880 +        using subS D CD by auto (metis Sup_upper range_eqI subsetCE)
1.881 +    qed
1.882 +  qed
1.883 +qed
1.884 +
1.885 +
1.886 +lemma sets_lebesgue_continuous_image:
1.887 +  assumes T: "T \<in> sets lebesgue" and contf: "continuous_on S f"
1.888 +    and negim: "\<And>T. \<lbrakk>negligible T; T \<subseteq> S\<rbrakk> \<Longrightarrow> negligible(f ` T)" and "T \<subseteq> S"
1.889 + shows "f ` T \<in> sets lebesgue"
1.890 +proof -
1.891 +  obtain K C where "negligible K" and com: "\<And>n::nat. compact(C n)" and Teq: "T = (\<Union>n. C n) \<union> K"
1.892 +    using lebesgue_regular_inner [OF T] by metis
1.893 +  then have comf: "\<And>n::nat. compact(f ` C n)"
1.894 +    by (metis Un_subset_iff Union_upper \<open>T \<subseteq> S\<close> compact_continuous_image contf continuous_on_subset rangeI)
1.895 +  have "((\<Union>n. f ` C n) \<union> f ` K) \<in> sets lebesgue"
1.896 +  proof (rule sets.Un)
1.897 +    have "K \<subseteq> S"
1.898 +      using Teq \<open>T \<subseteq> S\<close> by blast
1.899 +    show "(\<Union>n. f ` C n) \<in> sets lebesgue"
1.900 +    proof (rule sets.countable_Union)
1.901 +      show "range (\<lambda>n. f ` C n) \<subseteq> sets lebesgue"
1.902 +        using borel_compact comf by (auto simp: borel_compact)
1.903 +    qed auto
1.904 +    show "f ` K \<in> sets lebesgue"
1.905 +      by (simp add: \<open>K \<subseteq> S\<close> \<open>negligible K\<close> negim negligible_imp_sets)
1.906 +  qed
1.907 +  then show ?thesis
1.908 +    by (simp add: Teq image_Un image_Union)
1.909 +qed
1.910 +
1.911 +lemma differentiable_image_in_sets_lebesgue:
1.912 +  fixes f :: "'m::euclidean_space \<Rightarrow> 'n::euclidean_space"
1.913 +  assumes S: "S \<in> sets lebesgue" and dim: "DIM('m) \<le> DIM('n)" and f: "f differentiable_on S"
1.914 +  shows "f`S \<in> sets lebesgue"
1.915 +proof (rule sets_lebesgue_continuous_image [OF S])
1.916 +  show "continuous_on S f"
1.917 +    by (meson differentiable_imp_continuous_on f)
1.918 +  show "\<And>T. \<lbrakk>negligible T; T \<subseteq> S\<rbrakk> \<Longrightarrow> negligible (f ` T)"
1.919 +    using differentiable_on_subset f
1.920 +    by (auto simp: intro!: negligible_differentiable_image_negligible [OF dim])
1.921 +qed auto
1.922 +
1.923 +lemma sets_lebesgue_on_continuous_image:
1.924 +  assumes S: "S \<in> sets lebesgue" and X: "X \<in> sets (lebesgue_on S)" and contf: "continuous_on S f"
1.925 +    and negim: "\<And>T. \<lbrakk>negligible T; T \<subseteq> S\<rbrakk> \<Longrightarrow> negligible(f ` T)"
1.926 +  shows "f ` X \<in> sets (lebesgue_on (f ` S))"
1.927 +proof -
1.928 +  have "X \<subseteq> S"
1.929 +    by (metis S X sets.Int_space_eq2 sets_restrict_space_iff)
1.930 +  moreover have "f ` S \<in> sets lebesgue"
1.931 +    using S contf negim sets_lebesgue_continuous_image by blast
1.932 +  moreover have "f ` X \<in> sets lebesgue"
1.933 +    by (metis S X contf negim sets_lebesgue_continuous_image sets_restrict_space_iff space_restrict_space space_restrict_space2)
1.934 +  ultimately show ?thesis
1.935 +    by (auto simp: sets_restrict_space_iff)
1.936 +qed
1.937 +
1.938 +lemma differentiable_image_in_sets_lebesgue_on:
1.939 +  fixes f :: "'m::euclidean_space \<Rightarrow> 'n::euclidean_space"
1.940 +  assumes S: "S \<in> sets lebesgue" and X: "X \<in> sets (lebesgue_on S)" and dim: "DIM('m) \<le> DIM('n)"
1.941 +       and f: "f differentiable_on S"
1.942 +     shows "f ` X \<in> sets (lebesgue_on (f`S))"
1.943 +proof (rule sets_lebesgue_on_continuous_image [OF S X])
1.944 +  show "continuous_on S f"
1.945 +    by (meson differentiable_imp_continuous_on f)
1.946 +  show "\<And>T. \<lbrakk>negligible T; T \<subseteq> S\<rbrakk> \<Longrightarrow> negligible (f ` T)"
1.947 +    using differentiable_on_subset f
1.948 +    by (auto simp: intro!: negligible_differentiable_image_negligible [OF dim])
1.949 +qed
1.950 +
1.951 +
1.952 +proposition
1.953 + fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
1.954 +  assumes S: "S \<in> lmeasurable"
1.955 +  and deriv: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
1.956 +  and int: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on S"
1.957 +  and bounded: "\<And>x. x \<in> S \<Longrightarrow> \<bar>det (matrix (f' x))\<bar> \<le> B"
1.958 +  shows measurable_bounded_differentiable_image:
1.959 +       "f ` S \<in> lmeasurable"
1.960 +    and measure_bounded_differentiable_image:
1.961 +       "measure lebesgue (f ` S) \<le> B * measure lebesgue S" (is "?M")
1.962 +proof -
1.963 +  have "f ` S \<in> lmeasurable \<and> measure lebesgue (f ` S) \<le> B * measure lebesgue S"
1.964 +  proof (cases "B < 0")
1.965 +    case True
1.966 +    then have "S = {}"
1.967 +      by (meson abs_ge_zero bounded empty_iff equalityI less_le_trans linorder_not_less subsetI)
1.968 +    then show ?thesis
1.969 +      by auto
1.970 +  next
1.971 +    case False
1.972 +    then have "B \<ge> 0"
1.973 +      by arith
1.974 +    let ?\<mu> = "measure lebesgue"
1.975 +    have f_diff: "f differentiable_on S"
1.976 +      using deriv by (auto simp: differentiable_on_def differentiable_def)
1.977 +    have eps: "f ` S \<in> lmeasurable" "?\<mu> (f ` S) \<le> (B+e) * ?\<mu> S" (is "?ME")
1.978 +              if "e > 0" for e
1.979 +    proof -
1.980 +      have eps_d: "f ` S \<in> lmeasurable"  "?\<mu> (f ` S) \<le> (B+e) * (?\<mu> S + d)" (is "?MD")
1.981 +                  if "d > 0" for d
1.982 +      proof -
1.983 +        obtain T where "open T" "S \<subseteq> T" and TS: "(T-S) \<in> lmeasurable" and "?\<mu> (T-S) < d"
1.984 +          using S \<open>d > 0\<close> lmeasurable_outer_open by blast
1.985 +        with S have "T \<in> lmeasurable" and Tless: "?\<mu> T < ?\<mu> S + d"
1.986 +          by (auto simp: measurable_measure_Diff dest!: fmeasurable_Diff_D)
1.987 +        have "\<exists>r. 0 < r \<and> r < d \<and> ball x r \<subseteq> T \<and> f ` (S \<inter> ball x r) \<in> lmeasurable \<and>
1.988 +                  ?\<mu> (f ` (S \<inter> ball x r)) \<le> (B + e) * ?\<mu> (ball x r)"
1.989 +          if "x \<in> S" "d > 0" for x d
1.990 +        proof -
1.991 +          have lin: "linear (f' x)"
1.992 +            and lim0: "((\<lambda>y. (f y - (f x + f' x (y - x))) /\<^sub>R norm(y - x)) \<longlongrightarrow> 0) (at x within S)"
1.993 +            using deriv \<open>x \<in> S\<close> by (auto simp: has_derivative_within bounded_linear.linear field_simps)
1.994 +          have bo: "bounded (f' x ` ball 0 1)"
1.995 +            by (simp add: bounded_linear_image linear_linear lin)
1.996 +          have neg: "negligible (frontier (f' x ` ball 0 1))"
1.997 +            using deriv has_derivative_linear \<open>x \<in> S\<close>
1.998 +            by (auto intro!: negligible_convex_frontier [OF convex_linear_image])
1.999 +          have 0: "0 < e * unit_ball_vol (real CARD('n))"
1.1000 +            using  \<open>e > 0\<close> by simp
1.1001 +          obtain k where "k > 0" and k:
1.1002 +                  "\<And>U. \<lbrakk>U \<in> lmeasurable; \<And>y. y \<in> U \<Longrightarrow> \<exists>z. z \<in> f' x ` ball 0 1 \<and> dist z y < k\<rbrakk>
1.1003 +                        \<Longrightarrow> ?\<mu> U < ?\<mu> (f' x ` ball 0 1) + e * unit_ball_vol (CARD('n))"
1.1004 +            using measure_semicontinuous_with_hausdist_explicit [OF bo neg 0] by blast
1.1005 +          obtain l where "l > 0" and l: "ball x l \<subseteq> T"
1.1006 +            using \<open>x \<in> S\<close> \<open>open T\<close> \<open>S \<subseteq> T\<close> openE by blast
1.1007 +          obtain \<zeta> where "0 < \<zeta>"
1.1008 +            and \<zeta>: "\<And>y. \<lbrakk>y \<in> S; y \<noteq> x; dist y x < \<zeta>\<rbrakk>
1.1009 +                        \<Longrightarrow> norm (f y - (f x + f' x (y - x))) / norm (y - x) < k"
1.1010 +            using lim0 \<open>k > 0\<close> by (force simp: Lim_within field_simps)
1.1011 +          define r where "r \<equiv> min (min l (\<zeta>/2)) (min 1 (d/2))"
1.1012 +          show ?thesis
1.1013 +          proof (intro exI conjI)
1.1014 +            show "r > 0" "r < d"
1.1015 +              using \<open>l > 0\<close> \<open>\<zeta> > 0\<close> \<open>d > 0\<close> by (auto simp: r_def)
1.1016 +            have "r \<le> l"
1.1017 +              by (auto simp: r_def)
1.1018 +            with l show "ball x r \<subseteq> T"
1.1019 +              by auto
1.1020 +            have ex_lessK: "\<exists>x' \<in> ball 0 1. dist (f' x x') ((f y - f x) /\<^sub>R r) < k"
1.1021 +              if "y \<in> S" and "dist x y < r" for y
1.1022 +            proof (cases "y = x")
1.1023 +              case True
1.1024 +              with lin linear_0 \<open>k > 0\<close> that show ?thesis
1.1025 +                by (rule_tac x=0 in bexI) (auto simp: linear_0)
1.1026 +            next
1.1027 +              case False
1.1028 +              then show ?thesis
1.1029 +              proof (rule_tac x="(y - x) /\<^sub>R r" in bexI)
1.1030 +                have "f' x ((y - x) /\<^sub>R r) = f' x (y - x) /\<^sub>R r"
1.1031 +                  by (simp add: lin linear_cmul)
1.1032 +                then have "dist (f' x ((y - x) /\<^sub>R r)) ((f y - f x) /\<^sub>R r) = norm (f' x (y - x) /\<^sub>R r - (f y - f x) /\<^sub>R r)"
1.1033 +                  by (simp add: dist_norm)
1.1034 +                also have "\<dots> = norm (f' x (y - x) - (f y - f x)) / r"
1.1035 +                  using \<open>r > 0\<close> by (simp add: real_vector.scale_right_diff_distrib [symmetric] divide_simps)
1.1036 +                also have "\<dots> \<le> norm (f y - (f x + f' x (y - x))) / norm (y - x)"
1.1037 +                  using that \<open>r > 0\<close> False by (simp add: algebra_simps divide_simps dist_norm norm_minus_commute mult_right_mono)
1.1038 +                also have "\<dots> < k"
1.1039 +                  using that \<open>0 < \<zeta>\<close> by (simp add: dist_commute r_def  \<zeta> [OF \<open>y \<in> S\<close> False])
1.1040 +                finally show "dist (f' x ((y - x) /\<^sub>R r)) ((f y - f x) /\<^sub>R r) < k" .
1.1041 +                show "(y - x) /\<^sub>R r \<in> ball 0 1"
1.1042 +                  using that \<open>r > 0\<close> by (simp add: dist_norm divide_simps norm_minus_commute)
1.1043 +              qed
1.1044 +            qed
1.1045 +            let ?rfs = "(\<lambda>x. x /\<^sub>R r) ` (+) (- f x) ` f ` (S \<inter> ball x r)"
1.1046 +            have rfs_mble: "?rfs \<in> lmeasurable"
1.1047 +            proof (rule bounded_set_imp_lmeasurable)
1.1048 +              have "f differentiable_on S \<inter> ball x r"
1.1049 +                using f_diff by (auto simp: fmeasurableD differentiable_on_subset)
1.1050 +              with S show "?rfs \<in> sets lebesgue"
1.1051 +                by (auto simp: sets.Int intro!: lebesgue_sets_translation differentiable_image_in_sets_lebesgue)
1.1052 +              let ?B = "(\<lambda>(x, y). x + y) ` (f' x ` ball 0 1 \<times> ball 0 k)"
1.1053 +              have "bounded ?B"
1.1054 +                by (simp add: bounded_plus [OF bo])
1.1055 +              moreover have "?rfs \<subseteq> ?B"
1.1056 +                apply (auto simp: dist_norm image_iff dest!: ex_lessK)
1.1057 +                by (metis (no_types, hide_lams) add.commute diff_add_cancel dist_0_norm dist_commute dist_norm mem_ball)
1.1058 +              ultimately show "bounded (?rfs)"
1.1059 +                by (rule bounded_subset)
1.1060 +            qed
1.1061 +            then have "(\<lambda>x. r *\<^sub>R x) ` ?rfs \<in> lmeasurable"
1.1062 +              by (simp add: measurable_linear_image)
1.1063 +            with \<open>r > 0\<close> have "(+) (- f x) ` f ` (S \<inter> ball x r) \<in> lmeasurable"
1.1064 +              by (simp add: image_comp o_def)
1.1065 +            then have "(+) (f x) ` (+) (- f x) ` f ` (S \<inter> ball x r) \<in> lmeasurable"
1.1066 +              using  measurable_translation by blast
1.1067 +            then show fsb: "f ` (S \<inter> ball x r) \<in> lmeasurable"
1.1068 +              by (simp add: image_comp o_def)
1.1069 +            have "?\<mu> (f ` (S \<inter> ball x r)) = ?\<mu> (?rfs) * r ^ CARD('n)"
1.1070 +              using \<open>r > 0\<close> by (simp add: measure_translation measure_linear_image measurable_translation fsb field_simps)
1.1071 +            also have "\<dots> \<le> (\<bar>det (matrix (f' x))\<bar> * unit_ball_vol (CARD('n)) + e * unit_ball_vol (CARD('n))) * r ^ CARD('n)"
1.1072 +            proof -
1.1073 +              have "?\<mu> (?rfs) < ?\<mu> (f' x ` ball 0 1) + e * unit_ball_vol (CARD('n))"
1.1074 +                using rfs_mble by (force intro: k dest!: ex_lessK)
1.1075 +              then have "?\<mu> (?rfs) < \<bar>det (matrix (f' x))\<bar> * unit_ball_vol (CARD('n)) + e * unit_ball_vol (CARD('n))"
1.1076 +                by (simp add: lin measure_linear_image [of "f' x"] content_ball)
1.1077 +              with \<open>r > 0\<close> show ?thesis
1.1078 +                by auto
1.1079 +            qed
1.1080 +            also have "\<dots> \<le> (B + e) * ?\<mu> (ball x r)"
1.1081 +              using bounded [OF \<open>x \<in> S\<close>] \<open>r > 0\<close> by (simp add: content_ball algebra_simps)
1.1082 +            finally show "?\<mu> (f ` (S \<inter> ball x r)) \<le> (B + e) * ?\<mu> (ball x r)" .
1.1083 +          qed
1.1084 +        qed
1.1085 +        then obtain r where
1.1086 +          r0d: "\<And>x d. \<lbrakk>x \<in> S; d > 0\<rbrakk> \<Longrightarrow> 0 < r x d \<and> r x d < d"
1.1087 +          and rT: "\<And>x d. \<lbrakk>x \<in> S; d > 0\<rbrakk> \<Longrightarrow> ball x (r x d) \<subseteq> T"
1.1088 +          and r: "\<And>x d. \<lbrakk>x \<in> S; d > 0\<rbrakk> \<Longrightarrow>
1.1089 +                  (f ` (S \<inter> ball x (r x d))) \<in> lmeasurable \<and>
1.1090 +                  ?\<mu> (f ` (S \<inter> ball x (r x d))) \<le> (B + e) * ?\<mu> (ball x (r x d))"
1.1091 +          by metis
1.1092 +        obtain C where "countable C" and Csub: "C \<subseteq> {(x,r x t) |x t. x \<in> S \<and> 0 < t}"
1.1093 +          and pwC: "pairwise (\<lambda>i j. disjnt (ball (fst i) (snd i)) (ball (fst j) (snd j))) C"
1.1094 +          and negC: "negligible(S - (\<Union>i \<in> C. ball (fst i) (snd i)))"
1.1095 +          apply (rule Vitali_covering_theorem_balls [of S "{(x,r x t) |x t. x \<in> S \<and> 0 < t}" fst snd])
1.1096 +           apply auto
1.1097 +          by (metis dist_eq_0_iff r0d)
1.1098 +        let ?UB = "(\<Union>(x,s) \<in> C. ball x s)"
1.1099 +        have eq: "f ` (S \<inter> ?UB) = (\<Union>(x,s) \<in> C. f ` (S \<inter> ball x s))"
1.1100 +          by auto
1.1101 +        have mle: "?\<mu> (\<Union>(x,s) \<in> K. f ` (S \<inter> ball x s)) \<le> (B + e) * (?\<mu> S + d)"  (is "?l \<le> ?r")
1.1102 +          if "K \<subseteq> C" and "finite K" for K
1.1103 +        proof -
1.1104 +          have gt0: "b > 0" if "(a, b) \<in> K" for a b
1.1105 +            using Csub that \<open>K \<subseteq> C\<close> r0d by auto
1.1106 +          have inj: "inj_on (\<lambda>(x, y). ball x y) K"
1.1107 +            by (force simp: inj_on_def ball_eq_ball_iff dest: gt0)
1.1108 +          have disjnt: "disjoint ((\<lambda>(x, y). ball x y) ` K)"
1.1109 +            using pwC that
1.1110 +            apply (clarsimp simp: pairwise_def case_prod_unfold ball_eq_ball_iff)
1.1111 +            by (metis subsetD fst_conv snd_conv)
1.1112 +          have "?l \<le> (\<Sum>i\<in>K. ?\<mu> (case i of (x, s) \<Rightarrow> f ` (S \<inter> ball x s)))"
1.1113 +          proof (rule measure_UNION_le [OF \<open>finite K\<close>], clarify)
1.1114 +            fix x r
1.1115 +            assume "(x,r) \<in> K"
1.1116 +            then have "x \<in> S"
1.1117 +              using Csub \<open>K \<subseteq> C\<close> by auto
1.1118 +            show "f ` (S \<inter> ball x r) \<in> sets lebesgue"
1.1119 +              by (meson Int_lower1 S differentiable_on_subset f_diff fmeasurableD lmeasurable_ball order_refl sets.Int differentiable_image_in_sets_lebesgue)
1.1120 +          qed
1.1121 +          also have "\<dots> \<le> (\<Sum>(x,s) \<in> K. (B + e) * ?\<mu> (ball x s))"
1.1122 +            apply (rule sum_mono)
1.1123 +            using Csub r \<open>K \<subseteq> C\<close> by auto
1.1124 +          also have "\<dots> = (B + e) * (\<Sum>(x,s) \<in> K. ?\<mu> (ball x s))"
1.1125 +            by (simp add: prod.case_distrib sum_distrib_left)
1.1126 +          also have "\<dots> = (B + e) * sum ?\<mu> ((\<lambda>(x, y). ball x y) ` K)"
1.1127 +            using \<open>B \<ge> 0\<close> \<open>e > 0\<close> by (simp add: inj sum.reindex prod.case_distrib)
1.1128 +          also have "\<dots> = (B + e) * ?\<mu> (\<Union>(x,s) \<in> K. ball x s)"
1.1129 +            using \<open>B \<ge> 0\<close> \<open>e > 0\<close> that
1.1130 +            by (subst measure_Union') (auto simp: disjnt measure_Union')
1.1131 +          also have "\<dots> \<le> (B + e) * ?\<mu> T"
1.1132 +            using \<open>B \<ge> 0\<close> \<open>e > 0\<close> that apply simp
1.1133 +            apply (rule measure_mono_fmeasurable [OF _ _ \<open>T \<in> lmeasurable\<close>])
1.1134 +            using Csub rT by force+
1.1135 +          also have "\<dots> \<le> (B + e) * (?\<mu> S + d)"
1.1136 +            using \<open>B \<ge> 0\<close> \<open>e > 0\<close> Tless by simp
1.1137 +          finally show ?thesis .
1.1138 +        qed
1.1139 +        have fSUB_mble: "(f ` (S \<inter> ?UB)) \<in> lmeasurable"
1.1140 +          unfolding eq using Csub r False \<open>e > 0\<close> that
1.1141 +          by (auto simp: intro!: fmeasurable_UN_bound [OF \<open>countable C\<close> _ mle])
1.1142 +        have fSUB_meas: "?\<mu> (f ` (S \<inter> ?UB)) \<le> (B + e) * (?\<mu> S + d)"  (is "?MUB")
1.1143 +          unfolding eq using Csub r False \<open>e > 0\<close> that
1.1144 +          by (auto simp: intro!: measure_UN_bound [OF \<open>countable C\<close> _ mle])
1.1145 +        have neg: "negligible ((f ` (S \<inter> ?UB) - f ` S) \<union> (f ` S - f ` (S \<inter> ?UB)))"
1.1146 +        proof (rule negligible_subset [OF negligible_differentiable_image_negligible [OF order_refl negC, where f=f]])
1.1147 +          show "f differentiable_on S - (\<Union>i\<in>C. ball (fst i) (snd i))"
1.1148 +            by (meson DiffE differentiable_on_subset subsetI f_diff)
1.1149 +        qed force
1.1150 +        show "f ` S \<in> lmeasurable"
1.1151 +          by (rule lmeasurable_negligible_symdiff [OF fSUB_mble neg])
1.1152 +        show ?MD
1.1153 +          using fSUB_meas measure_negligible_symdiff [OF fSUB_mble neg] by simp
1.1154 +      qed
1.1155 +      show "f ` S \<in> lmeasurable"
1.1156 +        using eps_d [of 1] by simp
1.1157 +      show ?ME
1.1158 +      proof (rule field_le_epsilon)
1.1159 +        fix \<delta> :: real
1.1160 +        assume "0 < \<delta>"
1.1161 +        then show "?\<mu> (f ` S) \<le> (B + e) * ?\<mu> S + \<delta>"
1.1162 +          using eps_d [of "\<delta> / (B+e)"] \<open>e > 0\<close> \<open>B \<ge> 0\<close> by (auto simp: divide_simps mult_ac)
1.1163 +      qed
1.1164 +    qed
1.1165 +    show ?thesis
1.1166 +    proof (cases "?\<mu> S = 0")
1.1167 +      case True
1.1168 +      with eps have "?\<mu> (f ` S) = 0"
1.1169 +        by (metis mult_zero_right not_le zero_less_measure_iff)
1.1170 +      then show ?thesis
1.1171 +        using eps [of 1] by (simp add: True)
1.1172 +    next
1.1173 +      case False
1.1174 +      have "?\<mu> (f ` S) \<le> B * ?\<mu> S"
1.1175 +      proof (rule field_le_epsilon)
1.1176 +        fix e :: real
1.1177 +        assume "e > 0"
1.1178 +        then show "?\<mu> (f ` S) \<le> B * ?\<mu> S + e"
1.1179 +          using eps [of "e / ?\<mu> S"] False by (auto simp: algebra_simps zero_less_measure_iff)
1.1180 +      qed
1.1181 +      with eps [of 1] show ?thesis by auto
1.1182 +    qed
1.1183 +  qed
1.1184 +  then show "f ` S \<in> lmeasurable" ?M by blast+
1.1185 +qed
1.1186 +
1.1187 +lemma
1.1188 + fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
1.1189 +  assumes S: "S \<in> lmeasurable"
1.1190 +    and deriv: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
1.1191 +    and int: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on S"
1.1192 +  shows m_diff_image_weak: "f ` S \<in> lmeasurable \<and> measure lebesgue (f ` S) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
1.1193 +proof -
1.1194 +  let ?\<mu> = "measure lebesgue"
1.1195 +  have aint_S: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on S"
1.1196 +    using int unfolding absolutely_integrable_on_def by auto
1.1197 +  define m where "m \<equiv> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
1.1198 +  have *: "f ` S \<in> lmeasurable" "?\<mu> (f ` S) \<le> m + e * ?\<mu> S"
1.1199 +    if "e > 0" for e
1.1200 +  proof -
1.1201 +    define T where "T \<equiv> \<lambda>n. {x \<in> S. n * e \<le> \<bar>det (matrix (f' x))\<bar> \<and>
1.1202 +                                     \<bar>det (matrix (f' x))\<bar> < (Suc n) * e}"
1.1203 +    have meas_t: "T n \<in> lmeasurable" for n
1.1204 +    proof -
1.1205 +      have *: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) \<in> borel_measurable (lebesgue_on S)"
1.1206 +        using aint_S by (simp add: S borel_measurable_restrict_space_iff fmeasurableD set_integrable_def)
1.1207 +      have [intro]: "x \<in> sets (lebesgue_on S) \<Longrightarrow> x \<in> sets lebesgue" for x
1.1208 +        using S sets_restrict_space_subset by blast
1.1209 +      have "{x \<in> S. real n * e \<le> \<bar>det (matrix (f' x))\<bar>} \<in> sets lebesgue"
1.1210 +        using * by (auto simp: borel_measurable_iff_halfspace_ge space_restrict_space)
1.1211 +      then have 1: "{x \<in> S. real n * e \<le> \<bar>det (matrix (f' x))\<bar>} \<in> lmeasurable"
1.1212 +        using S by (simp add: fmeasurableI2)
1.1213 +      have "{x \<in> S. \<bar>det (matrix (f' x))\<bar> < (1 + real n) * e} \<in> sets lebesgue"
1.1214 +        using * by (auto simp: borel_measurable_iff_halfspace_less space_restrict_space)
1.1215 +      then have 2: "{x \<in> S. \<bar>det (matrix (f' x))\<bar> < (1 + real n) * e} \<in> lmeasurable"
1.1216 +        using S by (simp add: fmeasurableI2)
1.1217 +      show ?thesis
1.1218 +        using fmeasurable.Int [OF 1 2] by (simp add: T_def Int_def cong: conj_cong)
1.1219 +    qed
1.1220 +    have aint_T: "\<And>k. (\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on T k"
1.1221 +      using set_integrable_subset [OF aint_S] meas_t T_def by blast
1.1222 +    have Seq: "S = (\<Union>n. T n)"
1.1223 +      apply (auto simp: T_def)
1.1224 +      apply (rule_tac x="nat(floor(abs(det(matrix(f' x))) / e))" in exI)
1.1225 +      using that apply auto
1.1226 +      using of_int_floor_le pos_le_divide_eq apply blast
1.1227 +      by (metis add.commute pos_divide_less_eq real_of_int_floor_add_one_gt)
1.1228 +    have meas_ft: "f ` T n \<in> lmeasurable" for n
1.1229 +    proof (rule measurable_bounded_differentiable_image)
1.1230 +      show "T n \<in> lmeasurable"
1.1231 +        by (simp add: meas_t)
1.1232 +    next
1.1233 +      fix x :: "(real,'n) vec"
1.1234 +      assume "x \<in> T n"
1.1235 +      show "(f has_derivative f' x) (at x within T n)"
1.1236 +        by (metis (no_types, lifting) \<open>x \<in> T n\<close> deriv has_derivative_within_subset mem_Collect_eq subsetI T_def)
1.1237 +      show "\<bar>det (matrix (f' x))\<bar> \<le> (Suc n) * e"
1.1238 +        using \<open>x \<in> T n\<close> T_def by auto
1.1239 +    next
1.1240 +      show "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on T n"
1.1241 +        using aint_T absolutely_integrable_on_def by blast
1.1242 +    qed
1.1243 +    have disT: "disjoint (range T)"
1.1244 +      unfolding disjoint_def
1.1245 +    proof clarsimp
1.1246 +      show "T m \<inter> T n = {}" if "T m \<noteq> T n" for m n
1.1247 +        using that
1.1248 +      proof (induction m n rule: linorder_less_wlog)
1.1249 +        case (less m n)
1.1250 +        with \<open>e > 0\<close> show ?case
1.1251 +          unfolding T_def
1.1252 +          proof (clarsimp simp add: Collect_conj_eq [symmetric])
1.1253 +            fix x
1.1254 +            assume "e > 0"  "m < n"  "n * e \<le> \<bar>det (matrix (f' x))\<bar>"  "\<bar>det (matrix (f' x))\<bar> < (1 + real m) * e"
1.1255 +            then have "n < 1 + real m"
1.1256 +              by (metis (no_types, hide_lams) less_le_trans mult.commute not_le real_mult_le_cancel_iff2)
1.1257 +            then show "False"
1.1258 +              using less.hyps by linarith
1.1259 +          qed
1.1260 +      qed auto
1.1261 +    qed
1.1262 +    have injT: "inj_on T ({n. T n \<noteq> {}})"
1.1263 +      unfolding inj_on_def
1.1264 +    proof clarsimp
1.1265 +      show "m = n" if "T m = T n" "T n \<noteq> {}" for m n
1.1266 +        using that
1.1267 +      proof (induction m n rule: linorder_less_wlog)
1.1268 +        case (less m n)
1.1269 +        have False if "T n \<subseteq> T m" "x \<in> T n" for x
1.1270 +          using \<open>e > 0\<close> \<open>m < n\<close> that
1.1271 +          apply (auto simp: T_def  mult.commute intro: less_le_trans dest!: subsetD)
1.1272 +          by (metis add.commute less_le_trans nat_less_real_le not_le real_mult_le_cancel_iff2)
1.1273 +        then show ?case
1.1274 +          using less.prems by blast
1.1275 +      qed auto
1.1276 +    qed
1.1277 +    have sum_eq_Tim: "(\<Sum>k\<le>n. f (T k)) = sum f (T ` {..n})" if "f {} = 0" for f :: "_ \<Rightarrow> real" and n
1.1278 +    proof (subst sum.reindex_nontrivial)
1.1279 +      fix i j  assume "i \<in> {..n}" "j \<in> {..n}" "i \<noteq> j" "T i = T j"
1.1280 +      with that  injT [unfolded inj_on_def] show "f (T i) = 0"
1.1281 +        by simp metis
1.1282 +    qed (use atMost_atLeast0 in auto)
1.1283 +    let ?B = "m + e * ?\<mu> S"
1.1284 +    have "(\<Sum>k\<le>n. ?\<mu> (f ` T k)) \<le> ?B" for n
1.1285 +    proof -
1.1286 +      have "(\<Sum>k\<le>n. ?\<mu> (f ` T k)) \<le> (\<Sum>k\<le>n. ((k+1) * e) * ?\<mu>(T k))"
1.1287 +      proof (rule sum_mono [OF measure_bounded_differentiable_image])
1.1288 +        show "(f has_derivative f' x) (at x within T k)" if "x \<in> T k" for k x
1.1289 +          using that unfolding T_def by (blast intro: deriv has_derivative_within_subset)
1.1290 +        show "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on T k" for k
1.1291 +          using absolutely_integrable_on_def aint_T by blast
1.1292 +        show "\<bar>det (matrix (f' x))\<bar> \<le> real (k + 1) * e" if "x \<in> T k" for k x
1.1293 +          using T_def that by auto
1.1294 +      qed (use meas_t in auto)
1.1295 +      also have "\<dots> \<le> (\<Sum>k\<le>n. (k * e) * ?\<mu>(T k)) + (\<Sum>k\<le>n. e * ?\<mu>(T k))"
1.1296 +        by (simp add: algebra_simps sum.distrib)
1.1297 +      also have "\<dots> \<le> ?B"
1.1298 +      proof (rule add_mono)
1.1299 +        have "(\<Sum>k\<le>n. real k * e * ?\<mu> (T k)) = (\<Sum>k\<le>n. integral (T k) (\<lambda>x. k * e))"
1.1300 +          by (simp add: lmeasure_integral [OF meas_t]
1.1301 +                        integral_mult_right [symmetric] integral_mult_left [symmetric]
1.1302 +                   del: integral_mult_right integral_mult_left)
1.1303 +        also have "\<dots> \<le> (\<Sum>k\<le>n. integral (T k) (\<lambda>x.  (abs (det (matrix (f' x))))))"
1.1304 +        proof (rule sum_mono)
1.1305 +          fix k
1.1306 +          assume "k \<in> {..n}"
1.1307 +          show "integral (T k) (\<lambda>x. k * e) \<le> integral (T k) (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
1.1308 +          proof (rule integral_le [OF integrable_on_const [OF meas_t]])
1.1309 +            show "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on T k"
1.1310 +              using absolutely_integrable_on_def aint_T by blast
1.1311 +          next
1.1312 +            fix x assume "x \<in> T k"
1.1313 +            show "k * e \<le> \<bar>det (matrix (f' x))\<bar>"
1.1314 +              using \<open>x \<in> T k\<close> T_def by blast
1.1315 +          qed
1.1316 +        qed
1.1317 +        also have "\<dots> = sum (\<lambda>T. integral T (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)) (T ` {..n})"
1.1318 +          by (auto intro: sum_eq_Tim)
1.1319 +        also have "\<dots> = integral (\<Union>k\<le>n. T k) (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
1.1320 +        proof (rule integral_unique [OF has_integral_Union, symmetric])
1.1321 +          fix S  assume "S \<in> T ` {..n}"
1.1322 +          then show "((\<lambda>x. \<bar>det (matrix (f' x))\<bar>) has_integral integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)) S"
1.1323 +          using absolutely_integrable_on_def aint_T by blast
1.1324 +        next
1.1325 +          show "pairwise (\<lambda>S S'. negligible (S \<inter> S')) (T ` {..n})"
1.1326 +            using disT unfolding disjnt_iff by (auto simp: pairwise_def intro!: empty_imp_negligible)
1.1327 +        qed auto
1.1328 +        also have "\<dots> \<le> m"
1.1329 +          unfolding m_def
1.1330 +        proof (rule integral_subset_le)
1.1331 +          have "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on (\<Union>k\<le>n. T k)"
1.1332 +            apply (rule set_integrable_subset [OF aint_S])
1.1333 +             apply (intro measurable meas_t fmeasurableD)
1.1334 +            apply (force simp: Seq)
1.1335 +            done
1.1336 +          then show "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on (\<Union>k\<le>n. T k)"
1.1337 +            using absolutely_integrable_on_def by blast
1.1338 +        qed (use Seq int in auto)
1.1339 +        finally show "(\<Sum>k\<le>n. real k * e * ?\<mu> (T k)) \<le> m" .
1.1340 +      next
1.1341 +        have "(\<Sum>k\<le>n. ?\<mu> (T k)) = sum ?\<mu> (T ` {..n})"
1.1342 +          by (auto intro: sum_eq_Tim)
1.1343 +        also have "\<dots> = ?\<mu> (\<Union>k\<le>n. T k)"
1.1344 +          using S disT by (auto simp: pairwise_def meas_t intro: measure_Union' [symmetric])
1.1345 +        also have "\<dots> \<le> ?\<mu> S"
1.1346 +          using S by (auto simp: Seq intro: meas_t fmeasurableD measure_mono_fmeasurable)
1.1347 +        finally have "(\<Sum>k\<le>n. ?\<mu> (T k)) \<le> ?\<mu> S" .
1.1348 +        then show "(\<Sum>k\<le>n. e * ?\<mu> (T k)) \<le> e * ?\<mu> S"
1.1349 +          by (metis less_eq_real_def ordered_comm_semiring_class.comm_mult_left_mono sum_distrib_left that)
1.1350 +      qed
1.1351 +      finally show "(\<Sum>k\<le>n. ?\<mu> (f ` T k)) \<le> ?B" .
1.1352 +    qed
1.1353 +    moreover have "measure lebesgue (\<Union>k\<le>n. f ` T k) \<le> (\<Sum>k\<le>n. ?\<mu> (f ` T k))" for n
1.1354 +      by (simp add: fmeasurableD meas_ft measure_UNION_le)
1.1355 +    ultimately have B_ge_m: "?\<mu> (\<Union>k\<le>n. (f ` T k)) \<le> ?B" for n
1.1356 +      by (meson order_trans)
1.1357 +    have "(\<Union>n. f ` T n) \<in> lmeasurable"
1.1358 +      by (rule fmeasurable_countable_Union [OF meas_ft B_ge_m])
1.1359 +    moreover have "?\<mu> (\<Union>n. f ` T n) \<le> m + e * ?\<mu> S"
1.1360 +      by (rule measure_countable_Union_le [OF meas_ft B_ge_m])
1.1361 +    ultimately show "f ` S \<in> lmeasurable" "?\<mu> (f ` S) \<le> m + e * ?\<mu> S"
1.1362 +      by (auto simp: Seq image_Union)
1.1363 +  qed
1.1364 +  show ?thesis
1.1365 +  proof
1.1366 +    show "f ` S \<in> lmeasurable"
1.1367 +      using * linordered_field_no_ub by blast
1.1368 +    let ?x = "m - ?\<mu> (f ` S)"
1.1369 +    have False if "?\<mu> (f ` S) > integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
1.1370 +    proof -
1.1371 +      have ml: "m < ?\<mu> (f ` S)"
1.1372 +        using m_def that by blast
1.1373 +      then have "?\<mu> S \<noteq> 0"
1.1374 +        using "*"(2) bgauge_existence_lemma by fastforce
1.1375 +      with ml have 0: "0 < - (m - ?\<mu> (f ` S))/2 / ?\<mu> S"
1.1376 +        using that zero_less_measure_iff by force
1.1377 +      then show ?thesis
1.1378 +        using * [OF 0] that by (auto simp: divide_simps m_def split: if_split_asm)
1.1379 +    qed
1.1380 +    then show "?\<mu> (f ` S) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
1.1381 +      by fastforce
1.1382 +  qed
1.1383 +qed
1.1384 +
1.1385 +
1.1386 +theorem
1.1387 + fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
1.1388 +  assumes S: "S \<in> sets lebesgue"
1.1389 +    and deriv: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
1.1390 +    and int: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on S"
1.1391 +  shows measurable_differentiable_image: "f ` S \<in> lmeasurable"
1.1392 +    and measure_differentiable_image:
1.1393 +       "measure lebesgue (f ` S) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)" (is "?M")
1.1394 +proof -
1.1395 +  let ?I = "\<lambda>n::nat. cbox (vec (-n)) (vec n) \<inter> S"
1.1396 +  let ?\<mu> = "measure lebesgue"
1.1397 +  have "x \<in> cbox (vec (- real (nat \<lceil>norm x\<rceil>))) (vec (real (nat \<lceil>norm x\<rceil>)))" for x :: "real^'n::_"
1.1398 +    apply (auto simp: mem_box_cart)
1.1399 +    apply (metis abs_le_iff component_le_norm_cart minus_le_iff of_nat_ceiling order.trans)
1.1400 +    by (meson abs_le_D1 norm_bound_component_le_cart real_nat_ceiling_ge)
1.1401 +  then have Seq: "S = (\<Union>n. ?I n)"
1.1402 +    by auto
1.1403 +  have fIn: "f ` ?I n \<in> lmeasurable"
1.1404 +       and mfIn: "?\<mu> (f ` ?I n) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)" (is ?MN) for n
1.1405 +  proof -
1.1406 +    have In: "?I n \<in> lmeasurable"
1.1407 +      by (simp add: S bounded_Int bounded_set_imp_lmeasurable sets.Int)
1.1408 +    moreover have "\<And>x. x \<in> ?I n \<Longrightarrow> (f has_derivative f' x) (at x within ?I n)"
1.1409 +      by (meson Int_iff deriv has_derivative_within_subset subsetI)
1.1410 +    moreover have int_In: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on ?I n"
1.1411 +    proof -
1.1412 +      have "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on S"
1.1413 +        using int absolutely_integrable_integrable_bound by force
1.1414 +      then have "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on ?I n"
1.1415 +        by (metis (no_types) Int_lower1 In fmeasurableD inf_commute set_integrable_subset)
1.1416 +      then show ?thesis
1.1417 +        using absolutely_integrable_on_def by blast
1.1418 +    qed
1.1419 +    ultimately have "f ` ?I n \<in> lmeasurable" "?\<mu> (f ` ?I n) \<le> integral (?I n) (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
1.1420 +      using m_diff_image_weak by metis+
1.1421 +    moreover have "integral (?I n) (\<lambda>x. \<bar>det (matrix (f' x))\<bar>) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
1.1422 +      by (simp add: int_In int integral_subset_le)
1.1423 +    ultimately show "f ` ?I n \<in> lmeasurable" ?MN
1.1424 +      by auto
1.1425 +  qed
1.1426 +  have "?I k \<subseteq> ?I n" if "k \<le> n" for k n
1.1427 +    by (rule Int_mono) (use that in \<open>auto simp: subset_interval_imp_cart\<close>)
1.1428 +  then have "(\<Union>k\<le>n. f ` ?I k) = f ` ?I n" for n
1.1429 +    by (fastforce simp add:)
1.1430 +  with mfIn have "?\<mu> (\<Union>k\<le>n. f ` ?I k) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)" for n
1.1431 +    by simp
1.1432 +  then have "(\<Union>n. f ` ?I n) \<in> lmeasurable" "?\<mu> (\<Union>n. f ` ?I n) \<le> integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
1.1433 +    by (rule fmeasurable_countable_Union [OF fIn] measure_countable_Union_le [OF fIn])+
1.1434 +  then show "f ` S \<in> lmeasurable" ?M
1.1435 +    by (metis Seq image_UN)+
1.1436 +qed
1.1437 +
1.1438 +
1.1439 +lemma borel_measurable_simple_function_limit_increasing:
1.1440 +  fixes f :: "'a::euclidean_space \<Rightarrow> real"
1.1441 +  shows "(f \<in> borel_measurable lebesgue \<and> (\<forall>x. 0 \<le> f x)) \<longleftrightarrow>
1.1442 +         (\<exists>g. (\<forall>n x. 0 \<le> g n x \<and> g n x \<le> f x) \<and> (\<forall>n x. g n x \<le> (g(Suc n) x)) \<and>
1.1443 +              (\<forall>n. g n \<in> borel_measurable lebesgue) \<and> (\<forall>n. finite(range (g n))) \<and>
1.1444 +              (\<forall>x. (\<lambda>n. g n x) \<longlonglongrightarrow> f x))"
1.1445 +         (is "?lhs = ?rhs")
1.1446 +proof
1.1447 +  assume f: ?lhs
1.1448 +  have leb_f: "{x. a \<le> f x \<and> f x < b} \<in> sets lebesgue" for a b
1.1449 +  proof -
1.1450 +    have "{x. a \<le> f x \<and> f x < b} = {x. f x < b} - {x. f x < a}"
1.1451 +      by auto
1.1452 +    also have "\<dots> \<in> sets lebesgue"
1.1453 +      using borel_measurable_vimage_halfspace_component_lt [of f UNIV] f by auto
1.1454 +    finally show ?thesis .
1.1455 +  qed
1.1456 +  have "g n x \<le> f x"
1.1457 +        if inc_g: "\<And>n x. 0 \<le> g n x \<and> g n x \<le> g (Suc n) x"
1.1458 +           and meas_g: "\<And>n. g n \<in> borel_measurable lebesgue"
1.1459 +           and fin: "\<And>n. finite(range (g n))" and lim: "\<And>x. (\<lambda>n. g n x) \<longlonglongrightarrow> f x" for g n x
1.1460 +  proof -
1.1461 +    have "\<exists>r>0. \<forall>N. \<exists>n\<ge>N. dist (g n x) (f x) \<ge> r" if "g n x > f x"
1.1462 +    proof -
1.1463 +      have g: "g n x \<le> g (N + n) x" for N
1.1464 +        by (rule transitive_stepwise_le) (use inc_g in auto)
1.1465 +      have "\<exists>na\<ge>N. g n x - f x \<le> dist (g na x) (f x)" for N
1.1466 +        apply (rule_tac x="N+n" in exI)
1.1467 +        using g [of N] by (auto simp: dist_norm)
1.1468 +      with that show ?thesis
1.1469 +        using diff_gt_0_iff_gt by blast
1.1470 +    qed
1.1471 +    with lim show ?thesis
1.1472 +      apply (auto simp: lim_sequentially)
1.1473 +      by (meson less_le_not_le not_le_imp_less)
1.1474 +  qed
1.1475 +  moreover
1.1476 +  let ?\<Omega> = "\<lambda>n k. indicator {y. k/2^n \<le> f y \<and> f y < (k+1)/2^n}"
1.1477 +  let ?g = "\<lambda>n x. (\<Sum>k::real | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k/2^n * ?\<Omega> n k x)"
1.1478 +  have "\<exists>g. (\<forall>n x. 0 \<le> g n x \<and> g n x \<le> (g(Suc n) x)) \<and>
1.1479 +             (\<forall>n. g n \<in> borel_measurable lebesgue) \<and> (\<forall>n. finite(range (g n))) \<and>(\<forall>x. (\<lambda>n. g n x) \<longlonglongrightarrow> f x)"
1.1480 +  proof (intro exI allI conjI)
1.1481 +    show "0 \<le> ?g n x" for n x
1.1482 +    proof (clarify intro!: ordered_comm_monoid_add_class.sum_nonneg)
1.1483 +      fix k::real
1.1484 +      assume "k \<in> \<int>" and k: "\<bar>k\<bar> \<le> 2 ^ (2*n)"
1.1485 +      show "0 \<le> k/2^n * ?\<Omega> n k x"
1.1486 +        using f \<open>k \<in> \<int>\<close> apply (auto simp: indicator_def divide_simps Ints_def)
1.1487 +        apply (drule spec [where x=x])
1.1488 +        using zero_le_power [of "2::real" n] mult_nonneg_nonneg [of "f x" "2^n"]
1.1489 +        by linarith
1.1490 +    qed
1.1491 +    show "?g n x \<le> ?g (Suc n) x" for n x
1.1492 +    proof -
1.1493 +      have "?g n x =
1.1494 +            (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n).
1.1495 +              k/2^n * (indicator {y. k/2^n \<le> f y \<and> f y < (k+1/2)/2^n} x +
1.1496 +              indicator {y. (k+1/2)/2^n \<le> f y \<and> f y < (k+1)/2^n} x))"
1.1497 +        by (rule sum.cong [OF refl]) (simp add: indicator_def divide_simps)
1.1498 +      also have "\<dots> = (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k/2^n * indicator {y. k/2^n \<le> f y \<and> f y < (k+1/2)/2^n} x) +
1.1499 +                       (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k/2^n * indicator {y. (k+1/2)/2^n \<le> f y \<and> f y < (k+1)/2^n} x)"
1.1500 +        by (simp add:  comm_monoid_add_class.sum.distrib algebra_simps)
1.1501 +      also have "\<dots> = (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). (2 * k)/2 ^ Suc n * indicator {y. (2 * k)/2 ^ Suc n \<le> f y \<and> f y < (2 * k+1)/2 ^ Suc n} x) +
1.1502 +                       (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). (2 * k)/2 ^ Suc n * indicator {y. (2 * k+1)/2 ^ Suc n \<le> f y \<and> f y < ((2 * k+1) + 1)/2 ^ Suc n} x)"
1.1503 +        by (force simp: field_simps indicator_def intro: sum.cong)
1.1504 +      also have "\<dots> \<le> (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2 * Suc n). k/2 ^ Suc n * (indicator {y. k/2 ^ Suc n \<le> f y \<and> f y < (k+1)/2 ^ Suc n} x))"
1.1505 +                (is "?a + _ \<le> ?b")
1.1506 +      proof -
1.1507 +        have *: "\<lbrakk>sum f I \<le> sum h I; a + sum h I \<le> b\<rbrakk> \<Longrightarrow> a + sum f I \<le> b" for I a b f and h :: "real\<Rightarrow>real"
1.1508 +          by linarith
1.1509 +        let ?h = "\<lambda>k. (2*k+1)/2 ^ Suc n *
1.1510 +                      (indicator {y. (2 * k+1)/2 ^ Suc n \<le> f y \<and> f y < ((2*k+1) + 1)/2 ^ Suc n} x)"
1.1511 +        show ?thesis
1.1512 +        proof (rule *)
1.1513 +          show "(\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n).
1.1514 +                  2 * k/2 ^ Suc n * indicator {y. (2 * k+1)/2 ^ Suc n \<le> f y \<and> f y < (2 * k+1 + 1)/2 ^ Suc n} x)
1.1515 +                \<le> sum ?h {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}"
1.1516 +            by (rule sum_mono) (simp add: indicator_def divide_simps)
1.1517 +        next
1.1518 +          have \<alpha>: "?a = (\<Sum>k \<in> ( *)2 ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}.
1.1519 +                         k/2 ^ Suc n * indicator {y. k/2 ^ Suc n \<le> f y \<and> f y < (k+1)/2 ^ Suc n} x)"
1.1520 +            by (auto simp: inj_on_def field_simps comm_monoid_add_class.sum.reindex)
1.1521 +          have \<beta>: "sum ?h {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}
1.1522 +                   = (\<Sum>k \<in> (\<lambda>x. 2*x + 1) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}.
1.1523 +                      k/2 ^ Suc n * indicator {y. k/2 ^ Suc n \<le> f y \<and> f y < (k+1)/2 ^ Suc n} x)"
1.1524 +            by (auto simp: inj_on_def field_simps comm_monoid_add_class.sum.reindex)
1.1525 +          have 0: "( *) 2 ` {k \<in> \<int>. P k} \<inter> (\<lambda>x. 2 * x + 1) ` {k \<in> \<int>. P k} = {}" for P :: "real \<Rightarrow> bool"
1.1526 +          proof -
1.1527 +            have "2 * i \<noteq> 2 * j + 1" for i j :: int by arith
1.1528 +            thus ?thesis
1.1529 +              unfolding Ints_def by auto (use of_int_eq_iff in fastforce)
1.1530 +          qed
1.1531 +          have "?a + sum ?h {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}
1.1532 +                = (\<Sum>k \<in> ( *)2 ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)} \<union> (\<lambda>x. 2*x + 1) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}.
1.1533 +                  k/2 ^ Suc n * indicator {y. k/2 ^ Suc n \<le> f y \<and> f y < (k+1)/2 ^ Suc n} x)"
1.1534 +            unfolding \<alpha> \<beta>
1.1535 +            using finite_abs_int_segment [of "2 ^ (2*n)"]
1.1536 +            by (subst sum_Un) (auto simp: 0)
1.1537 +          also have "\<dots> \<le> ?b"
1.1538 +          proof (rule sum_mono2)
1.1539 +            show "finite {k::real. k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2 * Suc n)}"
1.1540 +              by (rule finite_abs_int_segment)
1.1541 +            show "( *) 2 ` {k::real. k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2^(2*n)} \<union> (\<lambda>x. 2*x + 1) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2^(2*n)} \<subseteq> {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2 * Suc n)}"
1.1542 +              apply auto
1.1543 +              using one_le_power [of "2::real" "2*n"]  by linarith
1.1544 +            have *: "\<lbrakk>x \<in> (S \<union> T) - U; \<And>x. x \<in> S \<Longrightarrow> x \<in> U; \<And>x. x \<in> T \<Longrightarrow> x \<in> U\<rbrakk> \<Longrightarrow> P x" for S T U P
1.1545 +              by blast
1.1546 +            have "0 \<le> b" if "b \<in> \<int>" "f x * (2 * 2^n) < b + 1" for b
1.1547 +            proof -
1.1548 +              have "0 \<le> f x * (2 * 2^n)"
1.1549 +                by (simp add: f)
1.1550 +              also have "\<dots> < b+1"
1.1551 +                by (simp add: that)
1.1552 +              finally show "0 \<le> b"
1.1553 +                using \<open>b \<in> \<int>\<close> by (auto simp: elim!: Ints_cases)
1.1554 +            qed
1.1555 +            then show "0 \<le> b/2 ^ Suc n * indicator {y. b/2 ^ Suc n \<le> f y \<and> f y < (b + 1)/2 ^ Suc n} x"
1.1556 +                  if "b \<in> {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2 * Suc n)} -
1.1557 +                          (( *) 2 ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)} \<union> (\<lambda>x. 2*x + 1) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)})" for b
1.1558 +              using that by (simp add: indicator_def divide_simps)
1.1559 +          qed
1.1560 +          finally show "?a + sum ?h {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)} \<le> ?b" .
1.1561 +        qed
1.1562 +      qed
1.1563 +      finally show ?thesis .
1.1564 +    qed
1.1565 +    show "?g n \<in> borel_measurable lebesgue" for n
1.1566 +      apply (intro borel_measurable_indicator borel_measurable_times borel_measurable_sum)
1.1567 +      using leb_f sets_restrict_UNIV by auto
1.1568 +    show "finite (range (?g n))" for n
1.1569 +    proof -
1.1570 +      have "(\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k/2^n * ?\<Omega> n k x)
1.1571 +              \<in> (\<lambda>k. k/2^n) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}" for x
1.1572 +      proof (cases "\<exists>k. k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n) \<and> k/2^n \<le> f x \<and> f x < (k+1)/2^n")
1.1573 +        case True
1.1574 +        then show ?thesis
1.1575 +          by (blast intro: indicator_sum_eq)
1.1576 +      next
1.1577 +        case False
1.1578 +        then have "(\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k/2^n * ?\<Omega> n k x) = 0"
1.1579 +          by auto
1.1580 +        then show ?thesis by force
1.1581 +      qed
1.1582 +      then have "range (?g n) \<subseteq> ((\<lambda>k. (k/2^n)) ` {k. k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n)})"
1.1583 +        by auto
1.1584 +      moreover have "finite ((\<lambda>k::real. (k/2^n)) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)})"
1.1585 +        by (intro finite_imageI finite_abs_int_segment)
1.1586 +      ultimately show ?thesis
1.1587 +        by (rule finite_subset)
1.1588 +    qed
1.1589 +    show "(\<lambda>n. ?g n x) \<longlonglongrightarrow> f x" for x
1.1590 +    proof (clarsimp simp add: lim_sequentially)
1.1591 +      fix e::real
1.1592 +      assume "e > 0"
1.1593 +      obtain N1 where N1: "2 ^ N1 > abs(f x)"
1.1594 +        using real_arch_pow by fastforce
1.1595 +      obtain N2 where N2: "(1/2) ^ N2 < e"
1.1596 +        using real_arch_pow_inv \<open>e > 0\<close> by fastforce
1.1597 +      have "dist (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k/2^n * ?\<Omega> n k x) (f x) < e" if "N1 + N2 \<le> n" for n
1.1598 +      proof -
1.1599 +        let ?m = "real_of_int \<lfloor>2^n * f x\<rfloor>"
1.1600 +        have "\<bar>?m\<bar> \<le> 2^n * 2^N1"
1.1601 +          using N1 apply (simp add: f)
1.1602 +          by (meson floor_mono le_floor_iff less_le_not_le mult_le_cancel_left_pos zero_less_numeral zero_less_power)
1.1603 +        also have "\<dots> \<le> 2 ^ (2*n)"
1.1604 +          by (metis that add_leD1 add_le_cancel_left mult.commute mult_2_right one_less_numeral_iff
1.1605 +                    power_add power_increasing_iff semiring_norm(76))
1.1606 +        finally have m_le: "\<bar>?m\<bar> \<le> 2 ^ (2*n)" .
1.1607 +        have "?m/2^n \<le> f x" "f x < (?m + 1)/2^n"
1.1608 +          by (auto simp: mult.commute pos_divide_le_eq mult_imp_less_div_pos)
1.1609 +        then have eq: "dist (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k/2^n * ?\<Omega> n k x) (f x)
1.1610 +                     = dist (?m/2^n) (f x)"
1.1611 +          by (subst indicator_sum_eq [of ?m]) (auto simp: m_le)
1.1612 +        have "\<bar>2^n\<bar> * \<bar>?m/2^n - f x\<bar> = \<bar>2^n * (?m/2^n - f x)\<bar>"
1.1613 +          by (simp add: abs_mult)
1.1614 +        also have "\<dots> < 2 ^ N2 * e"
1.1615 +          using N2 by (simp add: divide_simps mult.commute) linarith
1.1616 +        also have "\<dots> \<le> \<bar>2^n\<bar> * e"
1.1617 +          using that \<open>e > 0\<close> by auto
1.1618 +        finally have "dist (?m/2^n) (f x) < e"
1.1619 +          by (simp add: dist_norm)
1.1620 +        then show ?thesis
1.1621 +          using eq by linarith
1.1622 +      qed
1.1623 +      then show "\<exists>no. \<forall>n\<ge>no. dist (\<Sum>k | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n). k * ?\<Omega> n k x/2^n) (f x) < e"
1.1624 +        by force
1.1625 +    qed
1.1626 +  qed
1.1627 +  ultimately show ?rhs
1.1628 +    by metis
1.1629 +next
1.1630 +  assume RHS: ?rhs
1.1631 +  with borel_measurable_simple_function_limit [of f UNIV, unfolded borel_measurable_UNIV_eq]
1.1632 +  show ?lhs
1.1633 +    by (blast intro: order_trans)
1.1634 +qed
1.1635 +
1.1636 +subsection\<open>Borel measurable Jacobian determinant\<close>
1.1637 +
1.1638 +lemma lemma_partial_derivatives0:
1.1639 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
1.1640 +  assumes "linear f" and lim0: "((\<lambda>x. f x /\<^sub>R norm x) \<longlongrightarrow> 0) (at 0 within S)"
1.1641 +    and lb: "\<And>v. v \<noteq> 0 \<Longrightarrow> (\<exists>k>0. \<forall>e>0. \<exists>x. x \<in> S - {0} \<and> norm x < e \<and> k * norm x \<le> \<bar>v \<bullet> x\<bar>)"
1.1642 +  shows "f x = 0"
1.1643 +proof -
1.1644 +  have "dim {x. f x = 0} \<le> DIM('a)"
1.1645 +    using dim_subset_UNIV by blast
1.1646 +  moreover have False if less: "dim {x. f x = 0} < DIM('a)"
1.1647 +  proof -
1.1648 +    obtain d where "d \<noteq> 0" and d: "\<And>y. f y = 0 \<Longrightarrow> d \<bullet> y = 0"
1.1649 +      using orthogonal_to_subspace_exists [OF less] orthogonal_def
1.1650 +      by (metis (mono_tags, lifting) mem_Collect_eq span_clauses(1))
1.1651 +    then obtain k where "k > 0"
1.1652 +      and k: "\<And>e. e > 0 \<Longrightarrow> \<exists>y. y \<in> S - {0} \<and> norm y < e \<and> k * norm y \<le> \<bar>d \<bullet> y\<bar>"
1.1653 +      using lb by blast
1.1654 +    have "\<exists>h. \<forall>n. ((h n \<in> S \<and> h n \<noteq> 0 \<and> k * norm (h n) \<le> \<bar>d \<bullet> h n\<bar>) \<and> norm (h n) < 1 / real (Suc n)) \<and>
1.1655 +               norm (h (Suc n)) < norm (h n)"
1.1656 +    proof (rule dependent_nat_choice)
1.1657 +      show "\<exists>y. (y \<in> S \<and> y \<noteq> 0 \<and> k * norm y \<le> \<bar>d \<bullet> y\<bar>) \<and> norm y < 1 / real (Suc 0)"
1.1658 +        by simp (metis DiffE insertCI k not_less not_one_le_zero)
1.1659 +    qed (use k [of "min (norm x) (1/(Suc n + 1))" for x n] in auto)
1.1660 +    then obtain \<alpha> where \<alpha>: "\<And>n. \<alpha> n \<in> S - {0}" and kd: "\<And>n. k * norm(\<alpha> n) \<le> \<bar>d \<bullet> \<alpha> n\<bar>"
1.1661 +         and norm_lt: "\<And>n. norm(\<alpha> n) < 1/(Suc n)"
1.1662 +      by force
1.1663 +    let ?\<beta> = "\<lambda>n. \<alpha> n /\<^sub>R norm (\<alpha> n)"
1.1664 +    have com: "\<And>g. (\<forall>n. g n \<in> sphere (0::'a) 1)
1.1665 +              \<Longrightarrow> \<exists>l \<in> sphere 0 1. \<exists>\<rho>::nat\<Rightarrow>nat. strict_mono \<rho> \<and> (g \<circ> \<rho>) \<longlonglongrightarrow> l"
1.1666 +      using compact_sphere compact_def by metis
1.1667 +    moreover have "\<forall>n. ?\<beta> n \<in> sphere 0 1"
1.1668 +      using \<alpha> by auto
1.1669 +    ultimately obtain l::'a and \<rho>::"nat\<Rightarrow>nat"
1.1670 +       where l: "l \<in> sphere 0 1" and "strict_mono \<rho>" and to_l: "(?\<beta> \<circ> \<rho>) \<longlonglongrightarrow> l"
1.1671 +      by meson
1.1672 +    moreover have "continuous (at l) (\<lambda>x. (\<bar>d \<bullet> x\<bar> - k))"
1.1673 +      by (intro continuous_intros)
1.1674 +    ultimately have lim_dl: "((\<lambda>x. (\<bar>d \<bullet> x\<bar> - k)) \<circ> (?\<beta> \<circ> \<rho>)) \<longlonglongrightarrow> (\<bar>d \<bullet> l\<bar> - k)"
1.1675 +      by (meson continuous_imp_tendsto)
1.1676 +    have "\<forall>\<^sub>F i in sequentially. 0 \<le> ((\<lambda>x. \<bar>d \<bullet> x\<bar> - k) \<circ> ((\<lambda>n. \<alpha> n /\<^sub>R norm (\<alpha> n)) \<circ> \<rho>)) i"
1.1677 +      using \<alpha> kd by (auto simp: divide_simps)
1.1678 +    then have "k \<le> \<bar>d \<bullet> l\<bar>"
1.1679 +      using tendsto_lowerbound [OF lim_dl, of 0] by auto
1.1680 +    moreover have "d \<bullet> l = 0"
1.1681 +    proof (rule d)
1.1682 +      show "f l = 0"
1.1683 +      proof (rule LIMSEQ_unique [of "f \<circ> ?\<beta> \<circ> \<rho>"])
1.1684 +        have "isCont f l"
1.1685 +          using \<open>linear f\<close> linear_continuous_at linear_conv_bounded_linear by blast
1.1686 +        then show "(f \<circ> (\<lambda>n. \<alpha> n /\<^sub>R norm (\<alpha> n)) \<circ> \<rho>) \<longlonglongrightarrow> f l"
1.1687 +          unfolding comp_assoc
1.1688 +          using to_l continuous_imp_tendsto by blast
1.1689 +        have "\<alpha> \<longlonglongrightarrow> 0"
1.1690 +          using norm_lt LIMSEQ_norm_0 by metis
1.1691 +        with \<open>strict_mono \<rho>\<close> have "(\<alpha> \<circ> \<rho>) \<longlonglongrightarrow> 0"
1.1692 +          by (metis LIMSEQ_subseq_LIMSEQ)
1.1693 +        with lim0 \<alpha> have "((\<lambda>x. f x /\<^sub>R norm x) \<circ> (\<alpha> \<circ> \<rho>)) \<longlonglongrightarrow> 0"
1.1694 +          by (force simp: tendsto_at_iff_sequentially)
1.1695 +        then show "(f \<circ> (\<lambda>n. \<alpha> n /\<^sub>R norm (\<alpha> n)) \<circ> \<rho>) \<longlonglongrightarrow> 0"
1.1696 +          by (simp add: o_def linear_cmul \<open>linear f\<close>)
1.1697 +      qed
1.1698 +    qed
1.1699 +    ultimately show False
1.1700 +      using \<open>k > 0\<close> by auto
1.1701 +  qed
1.1702 +  ultimately have dim: "dim {x. f x = 0} = DIM('a)"
1.1703 +    by force
1.1704 +  then show ?thesis
1.1705 +    by (metis (mono_tags, lifting) UNIV_I assms(1) dim_eq_full linear_eq_0_span mem_Collect_eq)
1.1706 +qed
1.1707 +
1.1708 +lemma lemma_partial_derivatives:
1.1709 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
1.1710 +  assumes "linear f" and lim: "((\<lambda>x. f (x - a) /\<^sub>R norm (x - a)) \<longlongrightarrow> 0) (at a within S)"
1.1711 +    and lb: "\<And>v. v \<noteq> 0 \<Longrightarrow> (\<exists>k>0.  \<forall>e>0. \<exists>x \<in> S - {a}. norm(a - x) < e \<and> k * norm(a - x) \<le> \<bar>v \<bullet> (x - a)\<bar>)"
1.1712 +  shows "f x = 0"
1.1713 +proof -
1.1714 +  have "((\<lambda>x. f x /\<^sub>R norm x) \<longlongrightarrow> 0) (at 0 within (\<lambda>x. x-a) ` S)"
1.1715 +    using lim by (simp add: Lim_within dist_norm)
1.1716 +  then show ?thesis
1.1717 +  proof (rule lemma_partial_derivatives0 [OF \<open>linear f\<close>])
1.1718 +    fix v :: "'a"
1.1719 +    assume v: "v \<noteq> 0"
1.1720 +    show "\<exists>k>0. \<forall>e>0. \<exists>x. x \<in> (\<lambda>x. x - a) ` S - {0} \<and> norm x < e \<and> k * norm x \<le> \<bar>v \<bullet> x\<bar>"
1.1721 +      using lb [OF v] by (force simp:  norm_minus_commute)
1.1722 +  qed
1.1723 +qed
1.1724 +
1.1725 +
1.1726 +proposition borel_measurable_partial_derivatives:
1.1727 +  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n"
1.1728 +  assumes S: "S \<in> sets lebesgue"
1.1729 +    and f: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
1.1730 +  shows "(\<lambda>x. (matrix(f' x)\$m\$n)) \<in> borel_measurable (lebesgue_on S)"
1.1731 +proof -
1.1732 +  have contf: "continuous_on S f"
1.1733 +    using continuous_on_eq_continuous_within f has_derivative_continuous by blast
1.1734 +  have "{x \<in> S.  (matrix (f' x)\$m\$n) \<le> b} \<in> sets lebesgue" for b
1.1735 +  proof (rule sets_negligible_symdiff)
1.1736 +    let ?T = "{x \<in> S. \<forall>e>0. \<exists>d>0. \<exists>A. A\$m\$n < b \<and> (\<forall>i j. A\$i\$j \<in> \<rat>) \<and>
1.1737 +                       (\<forall>y \<in> S. norm(y - x) < d \<longrightarrow> norm(f y - f x - A *v (y - x)) \<le> e * norm(y - x))}"
1.1738 +    let ?U = "S \<inter>
1.1739 +              (\<Inter>e \<in> {e \<in> \<rat>. e > 0}.
1.1740 +                \<Union>A \<in> {A. A\$m\$n < b \<and> (\<forall>i j. A\$i\$j \<in> \<rat>)}.
1.1741 +                  \<Union>d \<in> {d \<in> \<rat>. 0 < d}.
1.1742 +                     S \<inter> (\<Inter>y \<in> S. {x \<in> S. norm(y - x) < d \<longrightarrow> norm(f y - f x - A *v (y - x)) \<le> e * norm(y - x)}))"
1.1743 +    have "?T = ?U"
1.1744 +    proof (intro set_eqI iffI)
1.1745 +      fix x
1.1746 +      assume xT: "x \<in> ?T"
1.1747 +      then show "x \<in> ?U"
1.1748 +      proof (clarsimp simp add:)
1.1749 +        fix q :: real
1.1750 +        assume "q \<in> \<rat>" "q > 0"
1.1751 +        then obtain d A where "d > 0" and A: "A \$ m \$ n < b" "\<And>i j. A \$ i \$ j \<in> \<rat>"
1.1752 +          "\<And>y. \<lbrakk>y\<in>S;  norm (y - x) < d\<rbrakk> \<Longrightarrow> norm (f y - f x - A *v (y - x)) \<le> q * norm (y - x)"
1.1753 +          using xT by auto
1.1754 +        then obtain \<delta> where "d > \<delta>" "\<delta> > 0" "\<delta> \<in> \<rat>"
1.1755 +          using Rats_dense_in_real by blast
1.1756 +        with A show "\<exists>A. A \$ m \$ n < b \<and> (\<forall>i j. A \$ i \$ j \<in> \<rat>) \<and>
1.1757 +                         (\<exists>s. s \<in> \<rat> \<and> 0 < s \<and> (\<forall>y\<in>S. norm (y - x) < s \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> q * norm (y - x)))"
1.1758 +          by force
1.1759 +      qed
1.1760 +    next
1.1761 +      fix x
1.1762 +      assume xU: "x \<in> ?U"
1.1763 +      then show "x \<in> ?T"
1.1764 +      proof clarsimp
1.1765 +        fix e :: "real"
1.1766 +        assume "e > 0"
1.1767 +        then obtain \<epsilon> where \<epsilon>: "e > \<epsilon>" "\<epsilon> > 0" "\<epsilon> \<in> \<rat>"
1.1768 +          using Rats_dense_in_real by blast
1.1769 +        with xU obtain A r where "x \<in> S" and Ar: "A \$ m \$ n < b" "\<forall>i j. A \$ i \$ j \<in> \<rat>" "r \<in> \<rat>" "r > 0"
1.1770 +          and "\<forall>y\<in>S. norm (y - x) < r \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> \<epsilon> * norm (y - x)"
1.1771 +          by (auto simp: split: if_split_asm)
1.1772 +        then have "\<forall>y\<in>S. norm (y - x) < r \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> e * norm (y - x)"
1.1773 +          by (meson \<open>e > \<epsilon>\<close> less_eq_real_def mult_right_mono norm_ge_zero order_trans)
1.1774 +        then show "\<exists>d>0. \<exists>A. A \$ m \$ n < b \<and> (\<forall>i j. A \$ i \$ j \<in> \<rat>) \<and> (\<forall>y\<in>S. norm (y - x) < d \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> e * norm (y - x))"
1.1775 +          using \<open>x \<in> S\<close> Ar by blast
1.1776 +      qed
1.1777 +    qed
1.1778 +    moreover have "?U \<in> sets lebesgue"
1.1779 +    proof -
1.1780 +      have coQ: "countable {e \<in> \<rat>. 0 < e}"
1.1781 +        using countable_Collect countable_rat by blast
1.1782 +      have ne: "{e \<in> \<rat>. (0::real) < e} \<noteq> {}"
1.1783 +        using zero_less_one Rats_1 by blast
1.1784 +      have coA: "countable {A. A \$ m \$ n < b \<and> (\<forall>i j. A \$ i \$ j \<in> \<rat>)}"
1.1785 +      proof (rule countable_subset)
1.1786 +        show "countable {A. \<forall>i j. A \$ i \$ j \<in> \<rat>}"
1.1787 +          using countable_vector [OF countable_vector, of "\<lambda>i j. \<rat>"] by (simp add: countable_rat)
1.1788 +      qed blast
1.1789 +      have *: "\<lbrakk>U \<noteq> {} \<Longrightarrow> closedin (subtopology euclidean S) (S \<inter> \<Inter> U)\<rbrakk>
1.1790 +               \<Longrightarrow> closedin (subtopology euclidean S) (S \<inter> \<Inter> U)" for U
1.1791 +        by fastforce
1.1792 +      have eq: "{x::(real,'m)vec. P x \<and> (Q x \<longrightarrow> R x)} = {x. P x \<and> \<not> Q x} \<union> {x. P x \<and> R x}" for P Q R
1.1793 +        by auto
1.1794 +      have sets: "S \<inter> (\<Inter>y\<in>S. {x \<in> S. norm (y - x) < d \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> e * norm (y - x)})
1.1795 +                  \<in> sets lebesgue" for e A d
1.1796 +      proof -
1.1797 +        have clo: "closedin (subtopology euclidean S)
1.1798 +                     {x \<in> S. norm (y - x) < d \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> e * norm (y - x)}"
1.1799 +          for y
1.1800 +        proof -
1.1801 +          have cont1: "continuous_on S (\<lambda>x. norm (y - x))"
1.1802 +          and  cont2: "continuous_on S (\<lambda>x. e * norm (y - x) - norm (f y - f x - (A *v y - A *v x)))"
1.1803 +            by (force intro: contf continuous_intros)+
1.1804 +          have clo1: "closedin (subtopology euclidean S) {x \<in> S. d \<le> norm (y - x)}"
1.1805 +            using continuous_closedin_preimage [OF cont1, of "{d..}"] by (simp add: vimage_def Int_def)
1.1806 +          have clo2: "closedin (subtopology euclidean S)
1.1807 +                       {x \<in> S. norm (f y - f x - (A *v y - A *v x)) \<le> e * norm (y - x)}"
1.1808 +            using continuous_closedin_preimage [OF cont2, of "{0..}"] by (simp add: vimage_def Int_def)
1.1809 +          show ?thesis
1.1810 +            by (auto simp: eq not_less matrix_vector_mult_diff_distrib intro: clo1 clo2)
1.1811 +        qed
1.1812 +        show ?thesis
1.1813 +          by (rule lebesgue_closedin [of S]) (force intro: * S clo)+
1.1814 +      qed
1.1815 +      show ?thesis
1.1816 +        by (intro sets sets.Int S sets.countable_UN'' sets.countable_INT'' coQ coA) auto
1.1817 +    qed
1.1818 +    ultimately show "?T \<in> sets lebesgue"
1.1819 +      by simp
1.1820 +    let ?M = "(?T - {x \<in> S. matrix (f' x) \$ m \$ n \<le> b} \<union> ({x \<in> S. matrix (f' x) \$ m \$ n \<le> b} - ?T))"
1.1821 +    let ?\<Theta> = "\<lambda>x v. \<forall>\<xi>>0. \<exists>e>0. \<forall>y \<in> S-{x}. norm (x - y) < e \<longrightarrow> \<bar>v \<bullet> (y - x)\<bar> < \<xi> * norm (x - y)"
1.1822 +    have nN: "negligible {x \<in> S. \<exists>v\<noteq>0. ?\<Theta> x v}"
1.1823 +      unfolding negligible_eq_zero_density
1.1824 +    proof clarsimp
1.1825 +      fix x v and r e :: "real"
1.1826 +      assume "x \<in> S" "v \<noteq> 0" "r > 0" "e > 0"
1.1827 +      and Theta [rule_format]: "?\<Theta> x v"
1.1828 +      moreover have "(norm v * e / 2) / CARD('m) ^ CARD('m) > 0"
1.1829 +        by (simp add: \<open>v \<noteq> 0\<close> \<open>e > 0\<close>)
1.1830 +      ultimately obtain d where "d > 0"
1.1831 +         and dless: "\<And>y. \<lbrakk>y \<in> S - {x}; norm (x - y) < d\<rbrakk> \<Longrightarrow>
1.1832 +                        \<bar>v \<bullet> (y - x)\<bar> < ((norm v * e / 2) / CARD('m) ^ CARD('m)) * norm (x - y)"
1.1833 +        by metis
1.1834 +      let ?W = "ball x (min d r) \<inter> {y. \<bar>v \<bullet> (y - x)\<bar> < (norm v * e/2 * min d r) / CARD('m) ^ CARD('m)}"
1.1835 +      have "open {x. \<bar>v \<bullet> (x - a)\<bar> < b}" for a b
1.1836 +        by (intro open_Collect_less continuous_intros)
1.1837 +      show "\<exists>d>0. d \<le> r \<and>
1.1838 +            (\<exists>U. {x' \<in> S. \<exists>v\<noteq>0. ?\<Theta> x' v} \<inter> ball x d \<subseteq> U \<and>
1.1839 +                 U \<in> lmeasurable \<and> measure lebesgue U < e * content (ball x d))"
1.1840 +      proof (intro exI conjI)
1.1841 +        show "0 < min d r" "min d r \<le> r"
1.1842 +          using \<open>r > 0\<close> \<open>d > 0\<close> by auto
1.1843 +        show "{x' \<in> S. \<exists>v. v \<noteq> 0 \<and> (\<forall>\<xi>>0. \<exists>e>0. \<forall>z\<in>S - {x'}. norm (x' - z) < e \<longrightarrow> \<bar>v \<bullet> (z - x')\<bar> < \<xi> * norm (x' - z))} \<inter> ball x (min d r) \<subseteq> ?W"
1.1844 +          proof (clarsimp simp: dist_norm norm_minus_commute)
1.1845 +            fix y :: "(real, 'm) vec" and w :: "(real, 'm) vec"
1.1846 +            assume "y \<in> S" "w \<noteq> 0"
1.1847 +              and less [rule_format]:
1.1848 +                    "\<forall>\<xi>>0. \<exists>e>0. \<forall>z\<in>S - {y}. norm (y - z) < e \<longrightarrow> \<bar>w \<bullet> (z - y)\<bar> < \<xi> * norm (y - z)"
1.1849 +              and d: "norm (y - x) < d" and r: "norm (y - x) < r"
1.1850 +            show "\<bar>v \<bullet> (y - x)\<bar> < norm v * e * min d r / (2 * real CARD('m) ^ CARD('m))"
1.1851 +            proof (cases "y = x")
1.1852 +              case True
1.1853 +              with \<open>r > 0\<close> \<open>d > 0\<close> \<open>e > 0\<close> \<open>v \<noteq> 0\<close> show ?thesis
1.1854 +                by simp
1.1855 +            next
1.1856 +              case False
1.1857 +              have "\<bar>v \<bullet> (y - x)\<bar> < norm v * e / 2 / real (CARD('m) ^ CARD('m)) * norm (x - y)"
1.1858 +                apply (rule dless)
1.1859 +                using False \<open>y \<in> S\<close> d by (auto simp: norm_minus_commute)
1.1860 +              also have "\<dots> \<le> norm v * e * min d r / (2 * real CARD('m) ^ CARD('m))"
1.1861 +                using d r \<open>e > 0\<close> by (simp add: field_simps norm_minus_commute mult_left_mono)
1.1862 +              finally show ?thesis .
1.1863 +            qed
1.1864 +          qed
1.1865 +          show "?W \<in> lmeasurable"
1.1866 +            by (simp add: fmeasurable_Int_fmeasurable borel_open)
1.1867 +          obtain k::'m where True
1.1868 +            by metis
1.1869 +          obtain T where T: "orthogonal_transformation T" and v: "v = T(norm v *\<^sub>R axis k (1::real))"
1.1870 +            using rotation_rightward_line by metis
1.1871 +          define b where "b \<equiv> norm v"
1.1872 +          have "b > 0"
1.1873 +            using \<open>v \<noteq> 0\<close> by (auto simp: b_def)
1.1874 +          obtain eqb: "inv T v = b *\<^sub>R axis k (1::real)" and "inj T" "bij T" and invT: "orthogonal_transformation (inv T)"
1.1875 +            by (metis UNIV_I b_def  T v bij_betw_inv_into_left orthogonal_transformation_inj orthogonal_transformation_bij orthogonal_transformation_inv)
1.1876 +          let ?v = "\<chi> i. min d r / CARD('m)"
1.1877 +          let ?v' = "\<chi> i. if i = k then (e/2 * min d r) / CARD('m) ^ CARD('m) else min d r"
1.1878 +          let ?x' = "inv T x"
1.1879 +          let ?W' = "(ball ?x' (min d r) \<inter> {y. \<bar>(y - ?x')\$k\<bar> < e * min d r / (2 * CARD('m) ^ CARD('m))})"
1.1880 +          have abs: "x - e \<le> y \<and> y \<le> x + e \<longleftrightarrow> abs(y - x) \<le> e" for x y e::real
1.1881 +            by auto
1.1882 +          have "?W = T ` ?W'"
1.1883 +          proof -
1.1884 +            have 1: "T ` (ball (inv T x) (min d r)) = ball x (min d r)"
1.1885 +              by (simp add: T image_orthogonal_transformation_ball orthogonal_transformation_surj surj_f_inv_f)
1.1886 +            have 2: "{y. \<bar>v \<bullet> (y - x)\<bar> < b * e * min d r / (2 * real CARD('m) ^ CARD('m))} =
1.1887 +                      T ` {y. \<bar>y \$ k - ?x' \$ k\<bar> < e * min d r / (2 * real CARD('m) ^ CARD('m))}"
1.1888 +            proof -
1.1889 +              have *: "\<bar>T (b *\<^sub>R axis k 1) \<bullet> (y - x)\<bar> = b * \<bar>inv T y \$ k - ?x' \$ k\<bar>" for y
1.1890 +              proof -
1.1891 +                have "\<bar>T (b *\<^sub>R axis k 1) \<bullet> (y - x)\<bar> = \<bar>(b *\<^sub>R axis k 1) \<bullet> inv T (y - x)\<bar>"
1.1892 +                  by (metis (no_types, hide_lams) b_def eqb invT orthogonal_transformation_def v)
1.1893 +                also have "\<dots> = b * \<bar>(axis k 1) \<bullet> inv T (y - x)\<bar>"
1.1894 +                  using \<open>b > 0\<close> by (simp add: abs_mult)
1.1895 +                also have "\<dots> = b * \<bar>inv T y \$ k - ?x' \$ k\<bar>"
1.1896 +                  using orthogonal_transformation_linear [OF invT]
1.1897 +                  by (simp add: inner_axis' linear_diff)
1.1898 +                finally show ?thesis
1.1899 +                  by simp
1.1900 +              qed
1.1901 +              show ?thesis
1.1902 +                using v b_def [symmetric]
1.1903 +                using \<open>b > 0\<close> by (simp add: * bij_image_Collect_eq [OF \<open>bij T\<close>] mult_less_cancel_left_pos times_divide_eq_right [symmetric] del: times_divide_eq_right)
1.1904 +            qed
1.1905 +            show ?thesis
1.1906 +              using \<open>b > 0\<close> by (simp add: image_Int \<open>inj T\<close> 1 2 b_def [symmetric])
1.1907 +          qed
1.1908 +          moreover have "?W' \<in> lmeasurable"
1.1909 +            by (auto intro: fmeasurable_Int_fmeasurable)
1.1910 +          ultimately have "measure lebesgue ?W = measure lebesgue ?W'"
1.1911 +            by (metis measure_orthogonal_image T)
1.1912 +          also have "\<dots> \<le> measure lebesgue (cbox (?x' - ?v') (?x' + ?v'))"
1.1913 +          proof (rule measure_mono_fmeasurable)
1.1914 +            show "?W' \<subseteq> cbox (?x' - ?v') (?x' + ?v')"
1.1915 +              apply (clarsimp simp add: mem_box_cart abs dist_norm norm_minus_commute simp del: min_less_iff_conj min.bounded_iff)
1.1916 +              by (metis component_le_norm_cart less_eq_real_def le_less_trans vector_minus_component)
1.1917 +          qed auto
1.1918 +          also have "\<dots> \<le> e/2 * measure lebesgue (cbox (?x' - ?v) (?x' + ?v))"
1.1919 +          proof -
1.1920 +            have "cbox (?x' - ?v) (?x' + ?v) \<noteq> {}"
1.1921 +              using \<open>r > 0\<close> \<open>d > 0\<close> by (auto simp: interval_eq_empty_cart divide_less_0_iff)
1.1922 +            with \<open>r > 0\<close> \<open>d > 0\<close> \<open>e > 0\<close> show ?thesis
1.1923 +              apply (simp add: content_cbox_if_cart mem_box_cart)
1.1924 +              apply (auto simp: prod_nonneg)
1.1925 +              apply (simp add: abs if_distrib prod.delta_remove prod_constant field_simps power_diff split: if_split_asm)
1.1926 +              done
1.1927 +          qed
1.1928 +          also have "\<dots> \<le> e/2 * measure lebesgue (cball ?x' (min d r))"
1.1929 +          proof (rule mult_left_mono [OF measure_mono_fmeasurable])
1.1930 +            have *: "norm (?x' - y) \<le> min d r"
1.1931 +              if y: "\<And>i. \<bar>?x' \$ i - y \$ i\<bar> \<le> min d r / real CARD('m)" for y
1.1932 +            proof -
1.1933 +              have "norm (?x' - y) \<le> (\<Sum>i\<in>UNIV. \<bar>(?x' - y) \$ i\<bar>)"
1.1934 +                by (rule norm_le_l1_cart)
1.1935 +              also have "\<dots> \<le> real CARD('m) * (min d r / real CARD('m))"
1.1936 +                by (rule sum_bounded_above) (use y in auto)
1.1937 +              finally show ?thesis
1.1938 +                by simp
1.1939 +            qed
1.1940 +            show "cbox (?x' - ?v) (?x' + ?v) \<subseteq> cball ?x' (min d r)"
1.1941 +              apply (clarsimp simp only: mem_box_cart dist_norm mem_cball intro!: *)
1.1942 +              by (simp add: abs_diff_le_iff abs_minus_commute)
1.1943 +          qed (use \<open>e > 0\<close> in auto)
1.1944 +          also have "\<dots> < e * content (cball ?x' (min d r))"
1.1945 +            using \<open>r > 0\<close> \<open>d > 0\<close> \<open>e > 0\<close> by auto
1.1946 +          also have "\<dots> = e * content (ball x (min d r))"
1.1947 +            using \<open>r > 0\<close> \<open>d > 0\<close> by (simp add: content_cball content_ball)
1.1948 +          finally show "measure lebesgue ?W < e * content (ball x (min d r))" .
1.1949 +      qed
1.1950 +    qed
1.1951 +    have *: "(\<And>x. (x \<notin> S) \<Longrightarrow> (x \<in> T \<longleftrightarrow> x \<in> U)) \<Longrightarrow> (T - U) \<union> (U - T) \<subseteq> S" for S T U :: "(real,'m) vec set"
1.1952 +      by blast
1.1953 +    have MN: "?M \<subseteq> {x \<in> S. \<exists>v\<noteq>0. ?\<Theta> x v}"
1.1954 +    proof (rule *)
1.1955 +      fix x
1.1956 +      assume x: "x \<notin> {x \<in> S. \<exists>v\<noteq>0. ?\<Theta> x v}"
1.1957 +      show "(x \<in> ?T) \<longleftrightarrow> (x \<in> {x \<in> S. matrix (f' x) \$ m \$ n \<le> b})"
1.1958 +      proof (cases "x \<in> S")
1.1959 +        case True
1.1960 +        then have x: "\<not> ?\<Theta> x v" if "v \<noteq> 0" for v
1.1961 +          using x that by force
1.1962 +        show ?thesis
1.1963 +        proof (rule iffI; clarsimp)
1.1964 +          assume b: "\<forall>e>0. \<exists>d>0. \<exists>A. A \$ m \$ n < b \<and> (\<forall>i j. A \$ i \$ j \<in> \<rat>) \<and>
1.1965 +                                    (\<forall>y\<in>S. norm (y - x) < d \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> e * norm (y - x))"
1.1966 +                     (is "\<forall>e>0. \<exists>d>0. \<exists>A. ?\<Phi> e d A")
1.1967 +          then have "\<forall>k. \<exists>d>0. \<exists>A. ?\<Phi> (1 / Suc k) d A"
1.1968 +            by (metis (no_types, hide_lams) less_Suc_eq_0_disj of_nat_0_less_iff zero_less_divide_1_iff)
1.1969 +          then obtain \<delta> A where \<delta>: "\<And>k. \<delta> k > 0"
1.1970 +                           and Ab: "\<And>k. A k \$ m \$ n < b"
1.1971 +                           and A: "\<And>k y. \<lbrakk>y \<in> S; norm (y - x) < \<delta> k\<rbrakk> \<Longrightarrow>
1.1972 +                                          norm (f y - f x - A k *v (y - x)) \<le> 1/(Suc k) * norm (y - x)"
1.1973 +            by metis
1.1974 +          have "\<forall>i j. \<exists>a. (\<lambda>n. A n \$ i \$ j) \<longlonglongrightarrow> a"
1.1975 +          proof (intro allI)
1.1976 +            fix i j
1.1977 +            have vax: "(A n *v axis j 1) \$ i = A n \$ i \$ j" for n
1.1978 +              by (metis cart_eq_inner_axis matrix_vector_mul_component)
1.1979 +            let ?CA = "{x. Cauchy (\<lambda>n. (A n) *v x)}"
1.1980 +            have "subspace ?CA"
1.1981 +              unfolding subspace_def convergent_eq_Cauchy [symmetric]
1.1982 +                by (force simp: algebra_simps intro: tendsto_intros)
1.1983 +            then have CA_eq: "?CA = span ?CA"
1.1984 +              by (metis span_eq)
1.1985 +            also have "\<dots> = UNIV"
1.1986 +            proof -
1.1987 +              have "dim ?CA \<le> CARD('m)"
1.1988 +                by (rule dim_subset_UNIV_cart)
1.1989 +              moreover have "False" if less: "dim ?CA < CARD('m)"
1.1990 +              proof -
1.1991 +                obtain d where "d \<noteq> 0" and d: "\<And>y. y \<in> span ?CA \<Longrightarrow> orthogonal d y"
1.1992 +                  using less by (force intro: orthogonal_to_subspace_exists [of ?CA])
1.1993 +                with x [OF \<open>d \<noteq> 0\<close>] obtain \<xi> where "\<xi> > 0"
1.1994 +                  and \<xi>: "\<And>e. e > 0 \<Longrightarrow> \<exists>y \<in> S - {x}. norm (x - y) < e \<and> \<xi> * norm (x - y) \<le> \<bar>d \<bullet> (y - x)\<bar>"
1.1995 +                  by (fastforce simp: not_le Bex_def)
1.1996 +                obtain \<gamma> z where \<gamma>Sx: "\<And>i. \<gamma> i \<in> S - {x}"
1.1997 +                           and \<gamma>le:   "\<And>i. \<xi> * norm(\<gamma> i - x) \<le> \<bar>d \<bullet> (\<gamma> i - x)\<bar>"
1.1998 +                           and \<gamma>x:    "\<gamma> \<longlonglongrightarrow> x"
1.1999 +                           and z:     "(\<lambda>n. (\<gamma> n - x) /\<^sub>R norm (\<gamma> n - x)) \<longlonglongrightarrow> z"
1.2000 +                proof -
1.2001 +                  have "\<exists>\<gamma>. (\<forall>i. (\<gamma> i \<in> S - {x} \<and>
1.2002 +                                  \<xi> * norm(\<gamma> i - x) \<le> \<bar>d \<bullet> (\<gamma> i - x)\<bar> \<and> norm(\<gamma> i - x) < 1/Suc i) \<and>
1.2003 +                                 norm(\<gamma>(Suc i) - x) < norm(\<gamma> i - x))"
1.2004 +                  proof (rule dependent_nat_choice)
1.2005 +                    show "\<exists>y. y \<in> S - {x} \<and> \<xi> * norm (y - x) \<le> \<bar>d \<bullet> (y - x)\<bar> \<and> norm (y - x) < 1 / Suc 0"
1.2006 +                      using \<xi> [of 1] by (auto simp: dist_norm norm_minus_commute)
1.2007 +                  next
1.2008 +                    fix y i
1.2009 +                    assume "y \<in> S - {x} \<and> \<xi> * norm (y - x) \<le> \<bar>d \<bullet> (y - x)\<bar> \<and> norm (y - x) < 1/Suc i"
1.2010 +                    then have "min (norm(y - x)) (1/((Suc i) + 1)) > 0"
1.2011 +                      by auto
1.2012 +                    then obtain y' where "y' \<in> S - {x}" and y': "norm (x - y') < min (norm (y - x)) (1/((Suc i) + 1))"
1.2013 +                                         "\<xi> * norm (x - y') \<le> \<bar>d \<bullet> (y' - x)\<bar>"
1.2014 +                      using \<xi> by metis
1.2015 +                    with \<xi> show "\<exists>y'. (y' \<in> S - {x} \<and> \<xi> * norm (y' - x) \<le> \<bar>d \<bullet> (y' - x)\<bar> \<and>
1.2016 +                              norm (y' - x) < 1/(Suc (Suc i))) \<and> norm (y' - x) < norm (y - x)"
1.2017 +                      by (auto simp: dist_norm norm_minus_commute)
1.2018 +                  qed
1.2019 +                  then obtain \<gamma> where
1.2020 +                        \<gamma>Sx: "\<And>i. \<gamma> i \<in> S - {x}"
1.2021 +                        and \<gamma>le: "\<And>i. \<xi> * norm(\<gamma> i - x) \<le> \<bar>d \<bullet> (\<gamma> i - x)\<bar>"
1.2022 +                        and \<gamma>conv: "\<And>i. norm(\<gamma> i - x) < 1/(Suc i)"
1.2023 +                    by blast
1.2024 +                  let ?f = "\<lambda>i. (\<gamma> i - x) /\<^sub>R norm (\<gamma> i - x)"
1.2025 +                  have "?f i \<in> sphere 0 1" for i
1.2026 +                    using \<gamma>Sx by auto
1.2027 +                  then obtain l \<rho> where "l \<in> sphere 0 1" "strict_mono \<rho>" and l: "(?f \<circ> \<rho>) \<longlonglongrightarrow> l"
1.2028 +                    using compact_sphere [of "0::(real,'m) vec" 1]  unfolding compact_def by meson
1.2029 +                  show thesis
1.2030 +                  proof
1.2031 +                    show "(\<gamma> \<circ> \<rho>) i \<in> S - {x}" "\<xi> * norm ((\<gamma> \<circ> \<rho>) i - x) \<le> \<bar>d \<bullet> ((\<gamma> \<circ> \<rho>) i - x)\<bar>" for i
1.2032 +                      using \<gamma>Sx \<gamma>le by auto
1.2033 +                    have "\<gamma> \<longlonglongrightarrow> x"
1.2034 +                    proof (clarsimp simp add: LIMSEQ_def dist_norm)
1.2035 +                      fix r :: "real"
1.2036 +                      assume "r > 0"
1.2037 +                      with real_arch_invD obtain no where "no \<noteq> 0" "real no > 1/r"
1.2038 +                        by (metis divide_less_0_1_iff not_less_iff_gr_or_eq of_nat_0_eq_iff reals_Archimedean2)
1.2039 +                      with \<gamma>conv show "\<exists>no. \<forall>n\<ge>no. norm (\<gamma> n - x) < r"
1.2040 +                        by (metis \<open>r > 0\<close> add.commute divide_inverse inverse_inverse_eq inverse_less_imp_less less_trans mult.left_neutral nat_le_real_less of_nat_Suc)
1.2041 +                    qed
1.2042 +                    with \<open>strict_mono \<rho>\<close> show "(\<gamma> \<circ> \<rho>) \<longlonglongrightarrow> x"
1.2043 +                      by (metis LIMSEQ_subseq_LIMSEQ)
1.2044 +                    show "(\<lambda>n. ((\<gamma> \<circ> \<rho>) n - x) /\<^sub>R norm ((\<gamma> \<circ> \<rho>) n - x)) \<longlonglongrightarrow> l"
1.2045 +                      using l by (auto simp: o_def)
1.2046 +                  qed
1.2047 +                qed
1.2048 +                have "isCont (\<lambda>x. (\<bar>d \<bullet> x\<bar> - \<xi>)) z"
1.2049 +                  by (intro continuous_intros)
1.2050 +                from isCont_tendsto_compose [OF this z]
1.2051 +                have lim: "(\<lambda>y. \<bar>d \<bullet> ((\<gamma> y - x) /\<^sub>R norm (\<gamma> y - x))\<bar> - \<xi>) \<longlonglongrightarrow> \<bar>d \<bullet> z\<bar> - \<xi>"
1.2052 +                  by auto
1.2053 +                moreover have "\<forall>\<^sub>F i in sequentially. 0 \<le> \<bar>d \<bullet> ((\<gamma> i - x) /\<^sub>R norm (\<gamma> i - x))\<bar> - \<xi>"
1.2054 +                proof (rule eventuallyI)
1.2055 +                  fix n
1.2056 +                  show "0 \<le> \<bar>d \<bullet> ((\<gamma> n - x) /\<^sub>R norm (\<gamma> n - x))\<bar> - \<xi>"
1.2057 +                  using \<gamma>le [of n] \<gamma>Sx by (auto simp: abs_mult divide_simps)
1.2058 +                qed
1.2059 +                ultimately have "\<xi> \<le> \<bar>d \<bullet> z\<bar>"
1.2060 +                  using tendsto_lowerbound [where a=0] by fastforce
1.2061 +                have "Cauchy (\<lambda>n. (A n) *v z)"
1.2062 +                proof (clarsimp simp add: Cauchy_def)
1.2063 +                  fix \<epsilon> :: "real"
1.2064 +                  assume "0 < \<epsilon>"
1.2065 +                  then obtain N::nat where "N > 0" and N: "\<epsilon>/2 > 1/N"
1.2066 +                    by (metis half_gt_zero inverse_eq_divide neq0_conv real_arch_inverse)
1.2067 +                  show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (A m *v z) (A n *v z) < \<epsilon>"
1.2068 +                  proof (intro exI allI impI)
1.2069 +                    fix i j
1.2070 +                    assume ij: "N \<le> i" "N \<le> j"
1.2071 +                    let ?V = "\<lambda>i k. A i *v ((\<gamma> k - x) /\<^sub>R norm (\<gamma> k - x))"
1.2072 +                    have "\<forall>\<^sub>F k in sequentially. dist (\<gamma> k) x < min (\<delta> i) (\<delta> j)"
1.2073 +                      using \<gamma>x [unfolded tendsto_iff] by (meson min_less_iff_conj \<delta>)
1.2074 +                    then have even: "\<forall>\<^sub>F k in sequentially. norm (?V i k - ?V j k) - 2 / N \<le> 0"
1.2075 +                    proof (rule eventually_mono, clarsimp)
1.2076 +                      fix p
1.2077 +                      assume p: "dist (\<gamma> p) x < \<delta> i" "dist (\<gamma> p) x < \<delta> j"
1.2078 +                      let ?C = "\<lambda>k. f (\<gamma> p) - f x - A k *v (\<gamma> p - x)"
1.2079 +                      have "norm ((A i - A j) *v (\<gamma> p - x)) = norm (?C j - ?C i)"
1.2080 +                        by (simp add: algebra_simps)
1.2081 +                      also have "\<dots> \<le> norm (?C j) + norm (?C i)"
1.2082 +                        using norm_triangle_ineq4 by blast
1.2083 +                      also have "\<dots> \<le> 1/(Suc j) * norm (\<gamma> p - x) + 1/(Suc i) * norm (\<gamma> p - x)"
1.2084 +                        by (metis A Diff_iff \<gamma>Sx dist_norm p add_mono)
1.2085 +                      also have "\<dots> \<le> 1/N * norm (\<gamma> p - x) + 1/N * norm (\<gamma> p - x)"
1.2086 +                        apply (intro add_mono mult_right_mono)
1.2087 +                        using ij \<open>N > 0\<close> by (auto simp: field_simps)
1.2088 +                      also have "\<dots> = 2 / N * norm (\<gamma> p - x)"
1.2089 +                        by simp
1.2090 +                      finally have no_le: "norm ((A i - A j) *v (\<gamma> p - x)) \<le> 2 / N * norm (\<gamma> p - x)" .
1.2091 +                      have "norm (?V i p - ?V j p) =
1.2092 +                            norm ((A i - A j) *v ((\<gamma> p - x) /\<^sub>R norm (\<gamma> p - x)))"
1.2093 +                        by (simp add: algebra_simps)
1.2094 +                      also have "\<dots> = norm ((A i - A j) *v (\<gamma> p - x)) / norm (\<gamma> p - x)"
1.2095 +                        by (simp add: divide_inverse matrix_vector_mult_scaleR)
1.2096 +                      also have "\<dots> \<le> 2 / N"
1.2097 +                        using no_le by (auto simp: divide_simps)
1.2098 +                      finally show "norm (?V i p - ?V j p) \<le> 2 / N" .
1.2099 +                    qed
1.2100 +                    have "isCont (\<lambda>w. (norm(A i *v w - A j *v w) - 2 / N)) z"
1.2101 +                      by (intro continuous_intros)
1.2102 +                    from isCont_tendsto_compose [OF this z]
1.2103 +                    have lim: "(\<lambda>w. norm (A i *v ((\<gamma> w - x) /\<^sub>R norm (\<gamma> w - x)) -
1.2104 +                                    A j *v ((\<gamma> w - x) /\<^sub>R norm (\<gamma> w - x))) - 2 / N)
1.2105 +                               \<longlonglongrightarrow> norm (A i *v z - A j *v z) - 2 / N"
1.2106 +                      by auto
1.2107 +                    have "dist (A i *v z) (A j *v z) \<le> 2 / N"
1.2108 +                      using tendsto_upperbound [OF lim even] by (auto simp: dist_norm)
1.2109 +                    with N show "dist (A i *v z) (A j *v z) < \<epsilon>"
1.2110 +                      by linarith
1.2111 +                  qed
1.2112 +                qed
1.2113 +                then have "d \<bullet> z = 0"
1.2114 +                  using CA_eq d orthogonal_def by auto
1.2115 +                then show False
1.2116 +                  using \<open>0 < \<xi>\<close> \<open>\<xi> \<le> \<bar>d \<bullet> z\<bar>\<close> by auto
1.2117 +              qed
1.2118 +              ultimately show ?thesis
1.2119 +                using dim_eq_full by fastforce
1.2120 +            qed
1.2121 +            finally have "?CA = UNIV" .
1.2122 +            then have "Cauchy (\<lambda>n. (A n) *v axis j 1)"
1.2123 +              by auto
1.2124 +            then obtain L where "(\<lambda>n. A n *v axis j 1) \<longlonglongrightarrow> L"
1.2125 +              by (auto simp: Cauchy_convergent_iff convergent_def)
1.2126 +            then have "(\<lambda>x. (A x *v axis j 1) \$ i) \<longlonglongrightarrow> L \$ i"
1.2127 +              by (rule tendsto_vec_nth)
1.2128 +            then show "\<exists>a. (\<lambda>n. A n \$ i \$ j) \<longlonglongrightarrow> a"
1.2129 +              by (force simp: vax)
1.2130 +          qed
1.2131 +          then obtain B where B: "\<And>i j. (\<lambda>n. A n \$ i \$ j) \<longlonglongrightarrow> B \$ i \$ j"
1.2132 +            by (auto simp: lambda_skolem)
1.2133 +          have lin_df: "linear (f' x)"
1.2134 +               and lim_df: "((\<lambda>y. (1 / norm (y - x)) *\<^sub>R (f y - (f x + f' x (y - x)))) \<longlongrightarrow> 0) (at x within S)"
1.2135 +            using \<open>x \<in> S\<close> assms by (auto simp: has_derivative_within linear_linear)
1.2136 +          moreover have "(matrix (f' x) - B) *v w = 0" for w
1.2137 +          proof (rule lemma_partial_derivatives [of "( *v) (matrix (f' x) - B)"])
1.2138 +            show "linear (( *v) (matrix (f' x) - B))"
1.2139 +              by (rule matrix_vector_mul_linear)
1.2140 +            have "((\<lambda>y. ((f x + f' x (y - x)) - f y) /\<^sub>R norm (y - x)) \<longlongrightarrow> 0) (at x within S)"
1.2141 +              using tendsto_minus [OF lim_df] by (simp add: algebra_simps divide_simps)
1.2142 +            then show "((\<lambda>y. (matrix (f' x) - B) *v (y - x) /\<^sub>R norm (y - x)) \<longlongrightarrow> 0) (at x within S)"
1.2143 +            proof (rule Lim_transform)
1.2144 +              have "((\<lambda>y. ((f y + B *v x - (f x + B *v y)) /\<^sub>R norm (y - x))) \<longlongrightarrow> 0) (at x within S)"
1.2145 +              proof (clarsimp simp add: Lim_within dist_norm)
1.2146 +                fix e :: "real"
1.2147 +                assume "e > 0"
1.2148 +                then obtain q::nat where "q \<noteq> 0" and qe2: "1/q < e/2"
1.2149 +                  by (metis divide_pos_pos inverse_eq_divide real_arch_inverse zero_less_numeral)
1.2150 +                let ?g = "\<lambda>p. sum  (\<lambda>i. sum (\<lambda>j. abs((A p - B)\$i\$j)) UNIV) UNIV"
1.2151 +                have "(\<lambda>k. onorm (\<lambda>y. (A k - B) *v y)) \<longlonglongrightarrow> 0"
1.2152 +                proof (rule Lim_null_comparison)
1.2153 +                  show "\<forall>\<^sub>F k in sequentially. norm (onorm (\<lambda>y. (A k - B) *v y)) \<le> ?g k"
1.2154 +                  proof (rule eventually_sequentiallyI)
1.2155 +                    fix k :: "nat"
1.2156 +                    assume "0 \<le> k"
1.2157 +                    have "0 \<le> onorm (( *v) (A k - B))"
1.2158 +                      by (simp add: linear_linear onorm_pos_le matrix_vector_mul_linear)
1.2159 +                    then show "norm (onorm (( *v) (A k - B))) \<le> (\<Sum>i\<in>UNIV. \<Sum>j\<in>UNIV. \<bar>(A k - B) \$ i \$ j\<bar>)"
1.2160 +                      by (simp add: onorm_le_matrix_component_sum del: vector_minus_component)
1.2161 +                  qed
1.2162 +                next
1.2163 +                  show "?g \<longlonglongrightarrow> 0"
1.2164 +                    using B Lim_null tendsto_rabs_zero_iff by (fastforce intro!: tendsto_null_sum)
1.2165 +                qed
1.2166 +                with \<open>e > 0\<close> obtain p where "\<And>n. n \<ge> p \<Longrightarrow> \<bar>onorm (( *v) (A n - B))\<bar> < e/2"
1.2167 +                  unfolding lim_sequentially by (metis diff_zero dist_real_def divide_pos_pos zero_less_numeral)
1.2168 +                then have pqe2: "\<bar>onorm (( *v) (A (p + q) - B))\<bar> < e/2" (*17 [`abs (onorm (\y. A (p + q) ** y - B ** y)) < e / &2`]*)
1.2169 +                  using le_add1 by blast
1.2170 +                show "\<exists>d>0. \<forall>y\<in>S. y \<noteq> x \<and> norm (y - x) < d \<longrightarrow>
1.2171 +                           inverse (norm (y - x)) * norm (f y + B *v x - (f x + B *v y)) < e"
1.2172 +                proof (intro exI, safe)
1.2173 +                  show "0 < \<delta>(p + q)"
1.2174 +                    by (simp add: \<delta>)
1.2175 +                next
1.2176 +                  fix y
1.2177 +                  assume y: "y \<in> S" "norm (y - x) < \<delta>(p + q)" and "y \<noteq> x"
1.2178 +                  have *: "\<lbrakk>norm(b - c) < e - d; norm(y - x - b) \<le> d\<rbrakk> \<Longrightarrow> norm(y - x - c) < e"
1.2179 +                    for b c d e x and y:: "real^'n"
1.2180 +                    using norm_triangle_ineq2 [of "y - x - c" "y - x - b"] by simp
1.2181 +                  have "norm (f y - f x - B *v (y - x)) < e * norm (y - x)"
1.2182 +                  proof (rule *)
1.2183 +                    show "norm (f y - f x - A (p + q) *v (y - x)) \<le> norm (y - x) / (Suc (p + q))"
1.2184 +                      using A [OF y] by simp
1.2185 +                    have "norm (A (p + q) *v (y - x) - B *v (y - x)) \<le> onorm(\<lambda>x. (A(p + q) - B) *v x) * norm(y - x)"
1.2186 +                      by (metis linear_linear matrix_vector_mul_linear matrix_vector_mult_diff_rdistrib onorm)
1.2187 +                    also have "\<dots> < (e/2) * norm (y - x)"
1.2188 +                      using \<open>y \<noteq> x\<close> pqe2 by auto
1.2189 +                    also have "\<dots> \<le> (e - 1 / (Suc (p + q))) * norm (y - x)"
1.2190 +                    proof (rule mult_right_mono)
1.2191 +                      have "1 / Suc (p + q) \<le> 1 / q"
1.2192 +                        using \<open>q \<noteq> 0\<close> by (auto simp: divide_simps)
1.2193 +                      also have "\<dots> < e/2"
1.2194 +                        using qe2 by auto
1.2195 +                      finally show "e / 2 \<le> e - 1 / real (Suc (p + q))"
1.2196 +                        by linarith
1.2197 +                    qed auto
1.2198 +                    finally show "norm (A (p + q) *v (y - x) - B *v (y - x)) < e * norm (y - x) - norm (y - x) / real (Suc (p + q))"
1.2199 +                      by (simp add: algebra_simps)
1.2200 +                  qed
1.2201 +                  then show "inverse (norm (y - x)) * norm (f y + B *v x - (f x + B *v y)) < e"
1.2202 +                    using \<open>y \<noteq> x\<close> by (simp add: divide_simps algebra_simps)
1.2203 +                qed
1.2204 +              qed
1.2205 +              then show "((\<lambda>y. (matrix (f' x) - B) *v (y - x) /\<^sub>R
1.2206 +                           norm (y - x) - (f x + f' x (y - x) - f y) /\<^sub>R norm (y - x)) \<longlongrightarrow> 0)
1.2207 +                          (at x within S)"
1.2208 +                by (simp add: algebra_simps lin_df linear_diff matrix_vector_mul_linear)
1.2209 +            qed
1.2210 +          qed (use x in \<open>simp; auto simp: not_less\<close>)
1.2211 +          ultimately have "f' x = ( *v) B"
1.2212 +            by (force simp: algebra_simps)
1.2213 +          show "matrix (f' x) \$ m \$ n \<le> b"
1.2214 +          proof (rule tendsto_upperbound [of "\<lambda>i. (A i \$ m \$ n)" _ sequentially])
1.2215 +            show "(\<lambda>i. A i \$ m \$ n) \<longlonglongrightarrow> matrix (f' x) \$ m \$ n"
1.2216 +              by (simp add: B \<open>f' x = ( *v) B\<close>)
1.2217 +            show "\<forall>\<^sub>F i in sequentially. A i \$ m \$ n \<le> b"
1.2218 +              by (simp add: Ab less_eq_real_def)
1.2219 +          qed auto
1.2220 +        next
1.2221 +          fix e :: "real"
1.2222 +          assume "x \<in> S" and b: "matrix (f' x) \$ m \$ n \<le> b" and "e > 0"
1.2223 +          then obtain d where "d>0"
1.2224 +            and d: "\<And>y. y\<in>S \<Longrightarrow> 0 < dist y x \<and> dist y x < d \<longrightarrow> norm (f y - f x - f' x (y - x)) / (norm (y - x))
1.2225 +                  < e/2"
1.2226 +            using f [OF \<open>x \<in> S\<close>] unfolding Deriv.has_derivative_at_within Lim_within
1.2227 +            by (auto simp: field_simps dest: spec [of _ "e/2"])
1.2228 +          let ?A = "matrix(f' x) - (\<chi> i j. if i = m \<and> j = n then e / 4 else 0)"
1.2229 +          obtain B where BRats: "\<And>i j. B\$i\$j \<in> \<rat>" and Bo_e6: "onorm(( *v) (?A - B)) < e/6"
1.2230 +            using matrix_rational_approximation \<open>e > 0\<close>
1.2231 +            by (metis zero_less_divide_iff zero_less_numeral)
1.2232 +          show "\<exists>d>0. \<exists>A. A \$ m \$ n < b \<and> (\<forall>i j. A \$ i \$ j \<in> \<rat>) \<and>
1.2233 +                (\<forall>y\<in>S. norm (y - x) < d \<longrightarrow> norm (f y - f x - A *v (y - x)) \<le> e * norm (y - x))"
1.2234 +          proof (intro exI conjI ballI allI impI)
1.2235 +            show "d>0"
1.2236 +              by (rule \<open>d>0\<close>)
1.2237 +            show "B \$ m \$ n < b"
1.2238 +            proof -
1.2239 +              have "\<bar>matrix (( *v) (?A - B)) \$ m \$ n\<bar> \<le> onorm (( *v) (?A - B))"
1.2240 +                using component_le_onorm [OF matrix_vector_mul_linear, of _ m n] by metis
1.2241 +              then show ?thesis
1.2242 +                using b Bo_e6 by simp
1.2243 +            qed
1.2244 +            show "B \$ i \$ j \<in> \<rat>" for i j
1.2245 +              using BRats by auto
1.2246 +            show "norm (f y - f x - B *v (y - x)) \<le> e * norm (y - x)"
1.2247 +              if "y \<in> S" and y: "norm (y - x) < d" for y
1.2248 +            proof (cases "y = x")
1.2249 +              case True then show ?thesis
1.2250 +                by simp
1.2251 +            next
1.2252 +              case False
1.2253 +              have *: "norm(d' - d) \<le> e/2 \<Longrightarrow> norm(y - (x + d')) < e/2 \<Longrightarrow> norm(y - x - d) \<le> e" for d d' e and x y::"real^'n"
1.2254 +                using norm_triangle_le [of "d' - d" "y - (x + d')"] by simp
1.2255 +              show ?thesis
1.2256 +              proof (rule *)
1.2257 +                have split246: "\<lbrakk>norm y \<le> e / 6; norm(x - y) \<le> e / 4\<rbrakk> \<Longrightarrow> norm x \<le> e/2" if "e > 0" for e and x y :: "real^'n"
1.2258 +                  using norm_triangle_le [of y "x-y" "e/2"] \<open>e > 0\<close> by simp
1.2259 +                have "linear (f' x)"
1.2260 +                  using True f has_derivative_linear by blast
1.2261 +                then have "norm (f' x (y - x) - B *v (y - x)) = norm ((matrix (f' x) - B) *v (y - x))"
1.2262 +                  by (metis matrix_vector_mul matrix_vector_mult_diff_rdistrib)
1.2263 +                also have "\<dots> \<le> (e * norm (y - x)) / 2"
1.2264 +                proof (rule split246)
1.2265 +                  have "norm ((?A - B) *v (y - x)) / norm (y - x) \<le> onorm(\<lambda>x. (?A - B) *v x)"
1.2266 +                    by (simp add: le_onorm linear_linear matrix_vector_mul_linear)
1.2267 +                  also have  "\<dots> < e/6"
1.2268 +                    by (rule Bo_e6)
1.2269 +                  finally have "norm ((?A - B) *v (y - x)) / norm (y - x) < e / 6" .
1.2270 +                  then show "norm ((?A - B) *v (y - x)) \<le> e * norm (y - x) / 6"
1.2271 +                    by (simp add: divide_simps False)
1.2272 +                  have "norm ((matrix (f' x) - B) *v (y - x) - ((?A - B) *v (y - x))) = norm ((\<chi> i j. if i = m \<and> j = n then e / 4 else 0) *v (y - x))"
1.2273 +                    by (simp add: algebra_simps)
1.2274 +                  also have "\<dots> = norm((e/4) *\<^sub>R (y - x)\$n *\<^sub>R axis m (1::real))"
1.2275 +                  proof -
1.2276 +                    have "(\<Sum>j\<in>UNIV. (if i = m \<and> j = n then e / 4 else 0) * (y \$ j - x \$ j)) * 4 = e * (y \$ n - x \$ n) * axis m 1 \$ i" for i
1.2277 +                    proof (cases "i=m")
1.2278 +                      case True then show ?thesis
1.2279 +                        by (auto simp: if_distrib [of "\<lambda>z. z * _"] cong: if_cong)
1.2280 +                    next
1.2281 +                      case False then show ?thesis
1.2282 +                        by (simp add: axis_def)
1.2283 +                    qed
1.2284 +                    then have "(\<chi> i j. if i = m \<and> j = n then e / 4 else 0) *v (y - x) = (e/4) *\<^sub>R (y - x)\$n *\<^sub>R axis m (1::real)"
1.2285 +                      by (auto simp: vec_eq_iff matrix_vector_mult_def)
1.2286 +                    then show ?thesis
1.2287 +                      by metis
1.2288 +                  qed
1.2289 +                  also have "\<dots> \<le> e * norm (y - x) / 4"
1.2290 +                    using \<open>e > 0\<close> apply (simp add: norm_mult abs_mult)
1.2291 +                    by (metis component_le_norm_cart vector_minus_component)
1.2292 +                  finally show "norm ((matrix (f' x) - B) *v (y - x) - ((?A - B) *v (y - x))) \<le> e * norm (y - x) / 4" .
1.2293 +                  show "0 < e * norm (y - x)"
1.2294 +                    by (simp add: False \<open>e > 0\<close>)
1.2295 +                qed
1.2296 +                finally show "norm (f' x (y - x) - B *v (y - x)) \<le> (e * norm (y - x)) / 2" .
1.2297 +                show "norm (f y - (f x + f' x (y - x))) < (e * norm (y - x)) / 2"
1.2298 +                  using False d [OF \<open>y \<in> S\<close>] y by (simp add: dist_norm field_simps)
1.2299 +              qed
1.2300 +            qed
1.2301 +          qed
1.2302 +        qed
1.2303 +      qed auto
1.2304 +    qed
1.2305 +    show "negligible ?M"
1.2306 +      using negligible_subset [OF nN MN] .
1.2307 +  qed
1.2308 +  then show ?thesis
1.2309 +    by (simp add: borel_measurable_vimage_halfspace_component_le sets_restrict_space_iff assms)
1.2310 +qed
1.2311 +
1.2312 +
1.2313 +theorem borel_measurable_det_Jacobian:
1.2314 + fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
1.2315 +  assumes S: "S \<in> sets lebesgue" and f: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
1.2316 +  shows "(\<lambda>x. det(matrix(f' x))) \<in> borel_measurable (lebesgue_on S)"
1.2317 +  unfolding det_def
1.2318 +  by (intro measurable) (auto intro: f borel_measurable_partial_derivatives [OF S])
1.2319 +
1.2320 +text\<open>The localisation wrt S uses the same argument for many similar results.
1.2321 +See HOL Light's MEASURABLE_ON_LEBESGUE_MEASURABLE_PREIMAGE_BOREL, etc.\<close>
1.2322 +lemma borel_measurable_lebesgue_on_preimage_borel:
1.2323 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
1.2324 +  assumes "S \<in> sets lebesgue"
1.2325 +  shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
1.2326 +         (\<forall>T. T \<in> sets borel \<longrightarrow> {x \<in> S. f x \<in> T} \<in> sets lebesgue)"
1.2327 +proof -
1.2328 +  have "{x. (if x \<in> S then f x else 0) \<in> T} \<in> sets lebesgue \<longleftrightarrow> {x \<in> S. f x \<in> T} \<in> sets lebesgue"
1.2329 +         if "T \<in> sets borel" for T
1.2330 +    proof (cases "0 \<in> T")
1.2331 +      case True
1.2332 +      then have "{x \<in> S. f x \<in> T} = {x. (if x \<in> S then f x else 0) \<in> T} \<inter> S"
1.2333 +                "{x. (if x \<in> S then f x else 0) \<in> T} = {x \<in> S. f x \<in> T} \<union> -S"
1.2334 +        by auto
1.2335 +      then show ?thesis
1.2336 +        by (metis (no_types, lifting) Compl_in_sets_lebesgue assms sets.Int sets.Un)
1.2337 +    next
1.2338 +      case False
1.2339 +      then have "{x. (if x \<in> S then f x else 0) \<in> T} = {x \<in> S. f x \<in> T}"
1.2340 +        by auto
1.2341 +      then show ?thesis
1.2342 +        by auto
1.2343 +    qed
1.2344 +    then show ?thesis
1.2345 +      unfolding borel_measurable_lebesgue_preimage_borel borel_measurable_UNIV [OF assms, symmetric]
1.2346 +      by blast
1.2347 +qed
1.2348 +
1.2349 +lemma sets_lebesgue_almost_borel:
1.2350 +  assumes "S \<in> sets lebesgue"
1.2351 +  obtains B N where "B \<in> sets borel" "negligible N" "B \<union> N = S"
1.2352 +proof -
1.2353 +  obtain T N N' where "S = T \<union> N" "N \<subseteq> N'" "N' \<in> null_sets lborel" "T \<in> sets borel"
1.2354 +    using sets_completionE [OF assms] by auto
1.2355 +  then show thesis
1.2356 +    by (metis negligible_iff_null_sets negligible_subset null_sets_completionI that)
1.2357 +qed
1.2358 +
1.2359 +lemma double_lebesgue_sets:
1.2360 + assumes S: "S \<in> sets lebesgue" and T: "T \<in> sets lebesgue" and fim: "f ` S \<subseteq> T"
1.2361 + shows "(\<forall>U. U \<in> sets lebesgue \<and> U \<subseteq> T \<longrightarrow> {x \<in> S. f x \<in> U} \<in> sets lebesgue) \<longleftrightarrow>
1.2362 +          f \<in> borel_measurable (lebesgue_on S) \<and>
1.2363 +          (\<forall>U. negligible U \<and> U \<subseteq> T \<longrightarrow> {x \<in> S. f x \<in> U} \<in> sets lebesgue)"
1.2364 +         (is "?lhs \<longleftrightarrow> _ \<and> ?rhs")
1.2365 +  unfolding borel_measurable_lebesgue_on_preimage_borel [OF S]
1.2366 +proof (intro iffI allI conjI impI, safe)
1.2367 +  fix V :: "'b set"
1.2368 +  assume *: "\<forall>U. U \<in> sets lebesgue \<and> U \<subseteq> T \<longrightarrow> {x \<in> S. f x \<in> U} \<in> sets lebesgue"
1.2369 +    and "V \<in> sets borel"
1.2370 +  then have V: "V \<in> sets lebesgue"
1.2371 +    by simp
1.2372 +  have "{x \<in> S. f x \<in> V} = {x \<in> S. f x \<in> T \<inter> V}"
1.2373 +    using fim by blast
1.2374 +  also have "{x \<in> S. f x \<in> T \<inter> V} \<in> sets lebesgue"
1.2375 +    using T V * le_inf_iff by blast
1.2376 +  finally show "{x \<in> S. f x \<in> V} \<in> sets lebesgue" .
1.2377 +next
1.2378 +  fix U :: "'b set"
1.2379 +  assume "\<forall>U. U \<in> sets lebesgue \<and> U \<subseteq> T \<longrightarrow> {x \<in> S. f x \<in> U} \<in> sets lebesgue"
1.2380 +         "negligible U" "U \<subseteq> T"
1.2381 +  then show "{x \<in> S. f x \<in> U} \<in> sets lebesgue"
1.2382 +    using negligible_imp_sets by blast
1.2383 +next
1.2384 +  fix U :: "'b set"
1.2385 +  assume 1 [rule_format]: "(\<forall>T. T \<in> sets borel \<longrightarrow> {x \<in> S. f x \<in> T} \<in> sets lebesgue)"
1.2386 +     and 2 [rule_format]: "\<forall>U. negligible U \<and> U \<subseteq> T \<longrightarrow> {x \<in> S. f x \<in> U} \<in> sets lebesgue"
1.2387 +     and "U \<in> sets lebesgue" "U \<subseteq> T"
1.2388 +  then obtain C N where C: "C \<in> sets borel \<and> negligible N \<and> C \<union> N = U"
1.2389 +    using sets_lebesgue_almost_borel
1.2390 +    by metis
1.2391 +  then have "{x \<in> S. f x \<in> C} \<in> sets lebesgue"
1.2392 +    by (blast intro: 1)
1.2393 +  moreover have "{x \<in> S. f x \<in> N} \<in> sets lebesgue"
1.2394 +    using C \<open>U \<subseteq> T\<close> by (blast intro: 2)
1.2395 +  moreover have "{x \<in> S. f x \<in> C \<union> N} = {x \<in> S. f x \<in> C} \<union> {x \<in> S. f x \<in> N}"
1.2396 +    by auto
1.2397 +  ultimately show "{x \<in> S. f x \<in> U} \<in> sets lebesgue"
1.2398 +    using C by auto
1.2399 +qed
1.2400 +
1.2401 +
1.2402 +
1.2403 +thm integrable_on_subcbox
1.2404 +
1.2405 +proposition measurable_bounded_by_integrable_imp_integrable:
1.2406 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
1.2407 +  assumes f: "f \<in> borel_measurable (lebesgue_on S)" and g: "g integrable_on S"
1.2408 +    and normf: "\<And>x. x \<in> S \<Longrightarrow> norm(f x) \<le> g x" and S: "S \<in> sets lebesgue"
1.2409 +  shows "f integrable_on S"
1.2410 +proof (rule integrable_on_all_intervals_integrable_bound [OF _ normf g])
1.2411 +  show "(\<lambda>x. if x \<in> S then f x else 0) integrable_on cbox a b" for a b
1.2412 +  proof (rule measurable_bounded_lemma)
1.2413 +    show "(\<lambda>x. if x \<in> S then f x else 0) \<in> borel_measurable lebesgue"
1.2414 +      by (simp add: S borel_measurable_UNIV f)
1.2415 +    show "(\<lambda>x. if x \<in> S then g x else 0) integrable_on cbox a b"
1.2416 +      by (simp add: g integrable_altD(1))
1.2417 +    show "norm (if x \<in> S then f x else 0) \<le> (if x \<in> S then g x else 0)" for x
1.2418 +      using normf by simp
1.2419 +  qed
1.2420 +qed
1.2421 +
1.2422 +
1.2423 +subsection\<open>Simplest case of Sard's theorem (we don't need continuity of derivative)\<close>
1.2424 +
1.2425 +lemma Sard_lemma00:
1.2426 +  fixes P :: "'b::euclidean_space set"
1.2427 +  assumes "a \<ge> 0" and a: "a *\<^sub>R i \<noteq> 0" and i: "i \<in> Basis"
1.2428 +    and P: "P \<subseteq> {x. a *\<^sub>R i \<bullet> x = 0}"
1.2429 +    and "0 \<le> m" "0 \<le> e"
1.2430 + obtains S where "S \<in> lmeasurable"
1.2431 +            and "{z. norm z \<le> m \<and> (\<exists>t \<in> P. norm(z - t) \<le> e)} \<subseteq> S"
1.2432 +            and "measure lebesgue S \<le> (2 * e) * (2 * m) ^ (DIM('b) - 1)"
1.2433 +proof -
1.2434 +  have "a > 0"
1.2435 +    using assms by simp
1.2436 +  let ?v = "(\<Sum>j\<in>Basis. (if j = i then e else m) *\<^sub>R j)"
1.2437 +  show thesis
1.2438 +  proof
1.2439 +    have "- e \<le> x \<bullet> i" "x \<bullet> i \<le> e"
1.2440 +      if "t \<in> P" "norm (x - t) \<le> e" for x t
1.2441 +      using \<open>a > 0\<close> that Basis_le_norm [of i "x-t"] P i
1.2442 +      by (auto simp: inner_commute algebra_simps)
1.2443 +    moreover have "- m \<le> x \<bullet> j" "x \<bullet> j \<le> m"
1.2444 +      if "norm x \<le> m" "t \<in> P" "norm (x - t) \<le> e" "j \<in> Basis" and "j \<noteq> i"
1.2445 +      for x t j
1.2446 +      using that Basis_le_norm [of j x] by auto
1.2447 +    ultimately
1.2448 +    show "{z. norm z \<le> m \<and> (\<exists>t\<in>P. norm (z - t) \<le> e)} \<subseteq> cbox (-?v) ?v"
1.2449 +      by (auto simp: mem_box)
1.2450 +    have *: "\<forall>k\<in>Basis. - ?v \<bullet> k \<le> ?v \<bullet> k"
1.2451 +      using \<open>0 \<le> m\<close> \<open>0 \<le> e\<close> by (auto simp: inner_Basis)
1.2452 +    have 2: "2 ^ DIM('b) = 2 * 2 ^ (DIM('b) - Suc 0)"
1.2453 +      by (metis DIM_positive Suc_pred power_Suc)
1.2454 +    show "measure lebesgue (cbox (-?v) ?v) \<le> 2 * e * (2 * m) ^ (DIM('b) - 1)"
1.2455 +      using \<open>i \<in> Basis\<close>
1.2456 +      by (simp add: content_cbox [OF *] prod.distrib prod.If_cases Diff_eq [symmetric] 2)
1.2457 +  qed blast
1.2458 +qed
1.2459 +
1.2460 +text\<open>As above, but reorienting the vector (HOL Light's @text{GEOM_BASIS_MULTIPLE_TAC})\<close>
1.2461 +lemma Sard_lemma0:
1.2462 +  fixes P :: "(real^'n::{finite,wellorder}) set"
1.2463 +  assumes "a \<noteq> 0"
1.2464 +    and P: "P \<subseteq> {x. a \<bullet> x = 0}" and "0 \<le> m" "0 \<le> e"
1.2465 +  obtains S where "S \<in> lmeasurable"
1.2466 +    and "{z. norm z \<le> m \<and> (\<exists>t \<in> P. norm(z - t) \<le> e)} \<subseteq> S"
1.2467 +    and "measure lebesgue S \<le> (2 * e) * (2 * m) ^ (CARD('n) - 1)"
1.2468 +proof -
1.2469 +  obtain T and k::'n where T: "orthogonal_transformation T" and a: "a = T (norm a *\<^sub>R axis k (1::real))"
1.2470 +    using rotation_rightward_line by metis
1.2471 +  have Tinv [simp]: "T (inv T x) = x" for x
1.2472 +    by (simp add: T orthogonal_transformation_surj surj_f_inv_f)
1.2473 +  obtain S where S: "S \<in> lmeasurable"
1.2474 +    and subS: "{z. norm z \<le> m \<and> (\<exists>t \<in> T-`P. norm(z - t) \<le> e)} \<subseteq> S"
1.2475 +    and mS: "measure lebesgue S \<le> (2 * e) * (2 * m) ^ (CARD('n) - 1)"
1.2476 +  proof (rule Sard_lemma00 [of "norm a" "axis k (1::real)" "T-`P" m e])
1.2477 +    have "norm a *\<^sub>R axis k 1 \<bullet> x = 0" if "T x \<in> P" for x
1.2478 +    proof -
1.2479 +      have "a \<bullet> T x = 0"
1.2480 +        using P that by blast
1.2481 +      then show ?thesis
1.2482 +        by (metis (no_types, lifting) T a orthogonal_orthogonal_transformation orthogonal_def)
1.2483 +    qed
1.2484 +    then show "T -` P \<subseteq> {x. norm a *\<^sub>R axis k 1 \<bullet> x = 0}"
1.2485 +      by auto
1.2486 +  qed (use assms T in auto)
1.2487 +  show thesis
1.2488 +  proof
1.2489 +    show "T ` S \<in> lmeasurable"
1.2490 +      using S measurable_orthogonal_image T by blast
1.2491 +    have "{z. norm z \<le> m \<and> (\<exists>t\<in>P. norm (z - t) \<le> e)} \<subseteq> T ` {z. norm z \<le> m \<and> (\<exists>t\<in>T -` P. norm (z - t) \<le> e)}"
1.2492 +    proof clarsimp
1.2493 +      fix x t
1.2494 +      assume "norm x \<le> m" "t \<in> P" "norm (x - t) \<le> e"
1.2495 +      then have "norm (inv T x) \<le> m"
1.2496 +        using orthogonal_transformation_inv [OF T] by (simp add: orthogonal_transformation_norm)
1.2497 +      moreover have "\<exists>t\<in>T -` P. norm (inv T x - t) \<le> e"
1.2498 +      proof
1.2499 +        have "T (inv T x - inv T t) = x - t"
1.2500 +          using T linear_diff orthogonal_transformation_def by fastforce
1.2501 +        then have "norm (inv T x - inv T t) = norm (x - t)"
1.2502 +          by (metis T orthogonal_transformation_norm)
1.2503 +        then show "norm (inv T x - inv T t) \<le> e"
1.2504 +          using \<open>norm (x - t) \<le> e\<close> by linarith
1.2505 +       next
1.2506 +         show "inv T t \<in> T -` P"
1.2507 +           using \<open>t \<in> P\<close> by force
1.2508 +      qed
1.2509 +      ultimately show "x \<in> T ` {z. norm z \<le> m \<and> (\<exists>t\<in>T -` P. norm (z - t) \<le> e)}"
1.2510 +        by force
1.2511 +    qed
1.2512 +    then show "{z. norm z \<le> m \<and> (\<exists>t\<in>P. norm (z - t) \<le> e)} \<subseteq> T ` S"
1.2513 +      using image_mono [OF subS] by (rule order_trans)
1.2514 +    show "measure lebesgue (T ` S) \<le> 2 * e * (2 * m) ^ (CARD('n) - 1)"
1.2515 +      using mS T by (simp add: S measure_orthogonal_image)
1.2516 +  qed
1.2517 +qed
1.2518 +
1.2519 +(*As above, but translating the sets (HOL Light's GEN_GEOM_ORIGIN_TAC)*)
1.2520 +lemma Sard_lemma1:
1.2521 +  fixes P :: "(real^'n::{finite,wellorder}) set"
1.2522 +   assumes P: "dim P < CARD('n)" and "0 \<le> m" "0 \<le> e"
1.2523 + obtains S where "S \<in> lmeasurable"
1.2524 +            and "{z. norm(z - w) \<le> m \<and> (\<exists>t \<in> P. norm(z - w - t) \<le> e)} \<subseteq> S"
1.2525 +            and "measure lebesgue S \<le> (2 * e) * (2 * m) ^ (CARD('n) - 1)"
1.2526 +proof -
1.2527 +  obtain a where "a \<noteq> 0" "P \<subseteq> {x. a \<bullet> x = 0}"
1.2528 +    using lowdim_subset_hyperplane [of P] P span_inc by auto
1.2529 +  then obtain S where S: "S \<in> lmeasurable"
1.2530 +    and subS: "{z. norm z \<le> m \<and> (\<exists>t \<in> P. norm(z - t) \<le> e)} \<subseteq> S"
1.2531 +    and mS: "measure lebesgue S \<le> (2 * e) * (2 * m) ^ (CARD('n) - 1)"
1.2532 +    by (rule Sard_lemma0 [OF _ _ \<open>0 \<le> m\<close> \<open>0 \<le> e\<close>])
1.2533 +  show thesis
1.2534 +  proof
1.2535 +    show "(+)w ` S \<in> lmeasurable"
1.2536 +      by (metis measurable_translation S)
1.2537 +    show "{z. norm (z - w) \<le> m \<and> (\<exists>t\<in>P. norm (z - w - t) \<le> e)} \<subseteq> (+)w ` S"
1.2538 +      using subS by force
1.2539 +    show "measure lebesgue ((+)w ` S) \<le> 2 * e * (2 * m) ^ (CARD('n) - 1)"
1.2540 +      by (metis measure_translation mS)
1.2541 +  qed
1.2542 +qed
1.2543 +
1.2544 +lemma Sard_lemma2:
1.2545 +  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n::{finite,wellorder}"
1.2546 +  assumes mlen: "CARD('m) \<le> CARD('n)" (is "?m \<le> ?n")
1.2547 +    and "B > 0" "bounded S"
1.2548 +    and derS: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
1.2549 +    and rank: "\<And>x. x \<in> S \<Longrightarrow> rank(matrix(f' x)) < CARD('n)"
1.2550 +    and B: "\<And>x. x \<in> S \<Longrightarrow> onorm(f' x) \<le> B"
1.2551 +  shows "negligible(f ` S)"
1.2552 +proof -
1.2553 +  have lin_f': "\<And>x. x \<in> S \<Longrightarrow> linear(f' x)"
1.2554 +    using derS has_derivative_linear by blast
1.2555 +  show ?thesis
1.2556 +  proof (clarsimp simp add: negligible_outer_le)
1.2557 +    fix e :: "real"
1.2558 +    assume "e > 0"
1.2559 +    obtain c where csub: "S \<subseteq> cbox (- (vec c)) (vec c)" and "c > 0"
1.2560 +    proof -
1.2561 +      obtain b where b: "\<And>x. x \<in> S \<Longrightarrow> norm x \<le> b"
1.2562 +        using \<open>bounded S\<close> by (auto simp: bounded_iff)
1.2563 +      show thesis
1.2564 +      proof
1.2565 +        have "- \<bar>b\<bar> - 1 \<le> x \$ i \<and> x \$ i \<le> \<bar>b\<bar> + 1" if "x \<in> S" for x i
1.2566 +          using component_le_norm_cart [of x i] b [OF that] by auto
1.2567 +        then show "S \<subseteq> cbox (- vec (\<bar>b\<bar> + 1)) (vec (\<bar>b\<bar> + 1))"
1.2568 +          by (auto simp: mem_box_cart)
1.2569 +      qed auto
1.2570 +    qed
1.2571 +    then have box_cc: "box (- (vec c)) (vec c) \<noteq> {}" and cbox_cc: "cbox (- (vec c)) (vec c) \<noteq> {}"
1.2572 +      by (auto simp: interval_eq_empty_cart)
1.2573 +    obtain d where "d > 0" "d \<le> B"
1.2574 +             and d: "(d * 2) * (4 * B) ^ (?n - 1) \<le> e / (2*c) ^ ?m / ?m ^ ?m"
1.2575 +      apply (rule that [of "min B (e / (2*c) ^ ?m / ?m ^ ?m / (4 * B) ^ (?n - 1) / 2)"])
1.2576 +      using \<open>B > 0\<close> \<open>c > 0\<close> \<open>e > 0\<close>
1.2577 +      by (simp_all add: divide_simps min_mult_distrib_right)
1.2578 +    have "\<exists>r. 0 < r \<and> r \<le> 1/2 \<and>
1.2579 +              (x \<in> S
1.2580 +               \<longrightarrow> (\<forall>y. y \<in> S \<and> norm(y - x) < r
1.2581 +                       \<longrightarrow> norm(f y - f x - f' x (y - x)) \<le> d * norm(y - x)))" for x
1.2582 +    proof (cases "x \<in> S")
1.2583 +      case True
1.2584 +      then obtain r where "r > 0"
1.2585 +              and "\<And>y. \<lbrakk>y \<in> S; norm (y - x) < r\<rbrakk>
1.2586 +                       \<Longrightarrow> norm (f y - f x - f' x (y - x)) \<le> d * norm (y - x)"
1.2587 +        using derS \<open>d > 0\<close> by (force simp: has_derivative_within_alt)
1.2588 +      then show ?thesis
1.2589 +        by (rule_tac x="min r (1/2)" in exI) simp
1.2590 +    next
1.2591 +      case False
1.2592 +      then show ?thesis
1.2593 +        by (rule_tac x="1/2" in exI) simp
1.2594 +    qed
1.2595 +    then obtain r where r12: "\<And>x. 0 < r x \<and> r x \<le> 1/2"
1.2596 +            and r: "\<And>x y. \<lbrakk>x \<in> S; y \<in> S; norm(y - x) < r x\<rbrakk>
1.2597 +                          \<Longrightarrow> norm(f y - f x - f' x (y - x)) \<le> d * norm(y - x)"
1.2598 +      by metis
1.2599 +    then have ga: "gauge (\<lambda>x. ball x (r x))"
1.2600 +      by (auto simp: gauge_def)
1.2601 +    obtain \<D> where \<D>: "countable \<D>" and sub_cc: "\<Union>\<D> \<subseteq> cbox (- vec c) (vec c)"
1.2602 +      and cbox: "\<And>K. K \<in> \<D> \<Longrightarrow> interior K \<noteq> {} \<and> (\<exists>u v. K = cbox u v)"
1.2603 +      and djointish: "pairwise (\<lambda>A B. interior A \<inter> interior B = {}) \<D>"
1.2604 +      and covered: "\<And>K. K \<in> \<D> \<Longrightarrow> \<exists>x \<in> S \<inter> K. K \<subseteq> ball x (r x)"
1.2605 +      and close: "\<And>u v. cbox u v \<in> \<D> \<Longrightarrow> \<exists>n. \<forall>i::'m. v \$ i - u \$ i = 2*c / 2^n"
1.2606 +      and covers: "S \<subseteq> \<Union>\<D>"
1.2607 +      apply (rule covering_lemma [OF csub box_cc ga])
1.2608 +      apply (auto simp: Basis_vec_def cart_eq_inner_axis [symmetric])
1.2609 +      done
1.2610 +    let ?\<mu> = "measure lebesgue"
1.2611 +    have "\<exists>T. T \<in> lmeasurable \<and> f ` (K \<inter> S) \<subseteq> T \<and> ?\<mu> T \<le> e / (2*c) ^ ?m * ?\<mu> K"
1.2612 +      if "K \<in> \<D>" for K
1.2613 +    proof -
1.2614 +      obtain u v where uv: "K = cbox u v"
1.2615 +        using cbox \<open>K \<in> \<D>\<close> by blast
1.2616 +      then have uv_ne: "cbox u v \<noteq> {}"
1.2617 +        using cbox that by fastforce
1.2618 +      obtain x where x: "x \<in> S \<inter> cbox u v" "cbox u v \<subseteq> ball x (r x)"
1.2619 +        using \<open>K \<in> \<D>\<close> covered uv by blast
1.2620 +      then have "dim (range (f' x)) < ?n"
1.2621 +        using rank_dim_range [of "matrix (f' x)"] lin_f' rank by fastforce
1.2622 +      then obtain T where T: "T \<in> lmeasurable"
1.2623 +            and subT: "{z. norm(z - f x) \<le> (2 * B) * norm(v - u) \<and> (\<exists>t \<in> range (f' x). norm(z - f x - t) \<le> d * norm(v - u))} \<subseteq> T"
1.2624 +            and measT: "?\<mu> T \<le> (2 * (d * norm(v - u))) * (2 * ((2 * B) * norm(v - u))) ^ (?n - 1)"
1.2625 +                        (is "_ \<le> ?DVU")
1.2626 +        apply (rule Sard_lemma1 [of "range (f' x)" "(2 * B) * norm(v - u)" "d * norm(v - u)" "f x"])
1.2627 +        using \<open>B > 0\<close> \<open>d > 0\<close> by simp_all
1.2628 +      show ?thesis
1.2629 +      proof (intro exI conjI)
1.2630 +        have "f ` (K \<inter> S) \<subseteq> {z. norm(z - f x) \<le> (2 * B) * norm(v - u) \<and> (\<exists>t \<in> range (f' x). norm(z - f x - t) \<le> d * norm(v - u))}"
1.2631 +          unfolding uv
1.2632 +        proof (clarsimp simp: mult.assoc, intro conjI)
1.2633 +          fix y
1.2634 +          assume y: "y \<in> cbox u v" and "y \<in> S"
1.2635 +          then have "norm (y - x) < r x"
1.2636 +            by (metis dist_norm mem_ball norm_minus_commute subsetCE x(2))
1.2637 +          then have le_dyx: "norm (f y - f x - f' x (y - x)) \<le> d * norm (y - x)"
1.2638 +            using r [of x y] x \<open>y \<in> S\<close> by blast
1.2639 +          have yx_le: "norm (y - x) \<le> norm (v - u)"
1.2640 +          proof (rule norm_le_componentwise_cart)
1.2641 +            show "\<bar>(y - x) \$ i\<bar> \<le> \<bar>(v - u) \$ i\<bar>" for i
1.2642 +            using x y by (force simp: mem_box_cart dest!: spec [where x=i])
1.2643 +          qed
1.2644 +          have *: "\<lbrakk>norm(y - x - z) \<le> d; norm z \<le> B; d \<le> B\<rbrakk> \<Longrightarrow> norm(y - x) \<le> 2 * B"
1.2645 +            for x y z :: "real^'n::_" and d B
1.2646 +            using norm_triangle_ineq2 [of "y - x" z] by auto
1.2647 +          show "norm (f y - f x) \<le> 2 * (B * norm (v - u))"
1.2648 +          proof (rule * [OF le_dyx])
1.2649 +            have "norm (f' x (y - x)) \<le> onorm (f' x) * norm (y - x)"
1.2650 +              using onorm [of "f' x" "y-x"] by (meson IntE lin_f' linear_linear x(1))
1.2651 +            also have "\<dots> \<le> B * norm (v - u)"
1.2652 +            proof (rule mult_mono)
1.2653 +              show "onorm (f' x) \<le> B"
1.2654 +                using B x by blast
1.2655 +            qed (use \<open>B > 0\<close> yx_le in auto)
1.2656 +            finally show "norm (f' x (y - x)) \<le> B * norm (v - u)" .
1.2657 +            show "d * norm (y - x) \<le> B * norm (v - u)"
1.2658 +              using \<open>B > 0\<close> by (auto intro: mult_mono [OF \<open>d \<le> B\<close> yx_le])
1.2659 +          qed
1.2660 +          show "\<exists>t. norm (f y - f x - f' x t) \<le> d * norm (v - u)"
1.2661 +            apply (rule_tac x="y-x" in exI)
1.2662 +            using \<open>d > 0\<close> yx_le le_dyx mult_left_mono [where c=d]
1.2663 +            by (meson order_trans real_mult_le_cancel_iff2)
1.2664 +        qed
1.2665 +        with subT show "f ` (K \<inter> S) \<subseteq> T" by blast
1.2666 +        show "?\<mu> T \<le> e / (2*c) ^ ?m * ?\<mu> K"
1.2667 +        proof (rule order_trans [OF measT])
1.2668 +          have "?DVU = (d * 2 * (4 * B) ^ (?n - 1)) * norm (v - u)^?n"
1.2669 +            using \<open>c > 0\<close>
1.2670 +            apply (simp add: algebra_simps power_mult_distrib)
1.2671 +            by (metis Suc_pred power_Suc zero_less_card_finite)
1.2672 +          also have "\<dots> \<le> (e / (2*c) ^ ?m / (?m ^ ?m)) * norm(v - u) ^ ?n"
1.2673 +            by (rule mult_right_mono [OF d]) auto
1.2674 +          also have "\<dots> \<le> e / (2*c) ^ ?m * ?\<mu> K"
1.2675 +          proof -
1.2676 +            have "u \<in> ball (x) (r x)" "v \<in> ball x (r x)"
1.2677 +              using box_ne_empty(1) contra_subsetD [OF x(2)] mem_box(2) uv_ne by fastforce+
1.2678 +            moreover have "r x \<le> 1/2"
1.2679 +              using r12 by auto
1.2680 +            ultimately have "norm (v - u) \<le> 1"
1.2681 +              using norm_triangle_half_r [of x u 1 v]
1.2682 +              by (metis (no_types, hide_lams) dist_commute dist_norm less_eq_real_def less_le_trans mem_ball)
1.2683 +            then have "norm (v - u) ^ ?n \<le> norm (v - u) ^ ?m"
1.2684 +              by (simp add: power_decreasing [OF mlen])
1.2685 +            also have "\<dots> \<le> ?\<mu> K * real (?m ^ ?m)"
1.2686 +            proof -
1.2687 +              obtain n where n: "\<And>i. v\$i - u\$i = 2 * c / 2^n"
1.2688 +                using close [of u v] \<open>K \<in> \<D>\<close> uv by blast
1.2689 +              have "norm (v - u) ^ ?m \<le> (\<Sum>i\<in>UNIV. \<bar>(v - u) \$ i\<bar>) ^ ?m"
1.2690 +                by (intro norm_le_l1_cart power_mono) auto
1.2691 +              also have "\<dots> \<le> (\<Prod>i\<in>UNIV. v \$ i - u \$ i) * real CARD('m) ^ CARD('m)"
1.2692 +                by (simp add: n field_simps \<open>c > 0\<close> less_eq_real_def)
1.2693 +              also have "\<dots> = ?\<mu> K * real (?m ^ ?m)"
1.2694 +                by (simp add: uv uv_ne content_cbox_cart)
1.2695 +              finally show ?thesis .
1.2696 +            qed
1.2697 +            finally have *: "1 / real (?m ^ ?m) * norm (v - u) ^ ?n \<le> ?\<mu> K"
1.2698 +              by (simp add: divide_simps)
1.2699 +            show ?thesis
1.2700 +              using mult_left_mono [OF *, of "e / (2*c) ^ ?m"] \<open>c > 0\<close> \<open>e > 0\<close> by auto
1.2701 +          qed
1.2702 +          finally show "?DVU \<le> e / (2*c) ^ ?m * ?\<mu> K" .
1.2703 +        qed
1.2704 +      qed (use T in auto)
1.2705 +    qed
1.2706 +    then obtain g where meas_g: "\<And>K. K \<in> \<D> \<Longrightarrow> g K \<in> lmeasurable"
1.2707 +                    and sub_g: "\<And>K. K \<in> \<D> \<Longrightarrow> f ` (K \<inter> S) \<subseteq> g K"
1.2708 +                    and le_g: "\<And>K. K \<in> \<D> \<Longrightarrow> ?\<mu> (g K) \<le> e / (2*c)^?m * ?\<mu> K"
1.2709 +      by metis
1.2710 +    have le_e: "?\<mu> (\<Union>i\<in>\<F>. g i) \<le> e"
1.2711 +      if "\<F> \<subseteq> \<D>" "finite \<F>" for \<F>
1.2712 +    proof -
1.2713 +      have "?\<mu> (\<Union>i\<in>\<F>. g i) \<le> (\<Sum>i\<in>\<F>. ?\<mu> (g i))"
1.2714 +        using meas_g \<open>\<F> \<subseteq> \<D>\<close> by (auto intro: measure_UNION_le [OF \<open>finite \<F>\<close>])
1.2715 +      also have "\<dots> \<le> (\<Sum>K\<in>\<F>. e / (2*c) ^ ?m * ?\<mu> K)"
1.2716 +        using \<open>\<F> \<subseteq> \<D>\<close> sum_mono [OF le_g] by (meson le_g subsetCE sum_mono)
1.2717 +      also have "\<dots> = e / (2*c) ^ ?m * (\<Sum>K\<in>\<F>. ?\<mu> K)"
1.2718 +        by (simp add: sum_distrib_left)
1.2719 +      also have "\<dots> \<le> e"
1.2720 +      proof -
1.2721 +        have "\<F> division_of \<Union>\<F>"
1.2722 +        proof (rule division_ofI)
1.2723 +          show "K \<subseteq> \<Union>\<F>"  "K \<noteq> {}" "\<exists>a b. K = cbox a b" if "K \<in> \<F>" for K
1.2724 +            using \<open>K \<in> \<F>\<close> covered cbox \<open>\<F> \<subseteq> \<D>\<close> by (auto simp: Union_upper)
1.2725 +          show "interior K \<inter> interior L = {}" if "K \<in> \<F>" and "L \<in> \<F>" and "K \<noteq> L" for K L
1.2726 +            by (metis (mono_tags, lifting) \<open>\<F> \<subseteq> \<D>\<close> pairwiseD djointish pairwise_subset that)
1.2727 +        qed (use that in auto)
1.2728 +        then have "sum ?\<mu> \<F> \<le> ?\<mu> (\<Union>\<F>)"
1.2729 +          by (simp add: content_division)
1.2730 +        also have "\<dots> \<le> ?\<mu> (cbox (- vec c) (vec c) :: (real, 'm) vec set)"
1.2731 +        proof (rule measure_mono_fmeasurable)
1.2732 +          show "\<Union>\<F> \<subseteq> cbox (- vec c) (vec c)"
1.2733 +            by (meson Sup_subset_mono sub_cc order_trans \<open>\<F> \<subseteq> \<D>\<close>)
1.2734 +        qed (use \<open>\<F> division_of \<Union>\<F>\<close> lmeasurable_division in auto)
1.2735 +        also have "\<dots> = content (cbox (- vec c) (vec c) :: (real, 'm) vec set)"
1.2736 +          by simp
1.2737 +        also have "\<dots> \<le> (2 ^ ?m * c ^ ?m)"
1.2738 +          using \<open>c > 0\<close> by (simp add: content_cbox_if_cart)
1.2739 +        finally have "sum ?\<mu> \<F> \<le> (2 ^ ?m * c ^ ?m)" .
1.2740 +        then show ?thesis
1.2741 +          using \<open>e > 0\<close> \<open>c > 0\<close> by (auto simp: divide_simps mult_less_0_iff)
1.2742 +      qed
1.2743 +      finally show ?thesis .
1.2744 +    qed
1.2745 +    show "\<exists>T. f ` S \<subseteq> T \<and> T \<in> lmeasurable \<and> ?\<mu> T \<le> e"
1.2746 +    proof (intro exI conjI)
1.2747 +      show "f ` S \<subseteq> UNION \<D> g"
1.2748 +        using covers sub_g by force
1.2749 +      show "UNION \<D> g \<in> lmeasurable"
1.2750 +        by (rule fmeasurable_UN_bound [OF \<open>countable \<D>\<close> meas_g le_e])
1.2751 +      show "?\<mu> (UNION \<D> g) \<le> e"
1.2752 +        by (rule measure_UN_bound [OF \<open>countable \<D>\<close> meas_g le_e])
1.2753 +    qed
1.2754 +  qed
1.2755 +qed
1.2756 +
1.2757 +
1.2758 +theorem baby_Sard:
1.2759 +  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n::{finite,wellorder}"
1.2760 +  assumes mlen: "CARD('m) \<le> CARD('n)"
1.2761 +    and der: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
1.2762 +    and rank: "\<And>x. x \<in> S \<Longrightarrow> rank(matrix(f' x)) < CARD('n)"
1.2763 +  shows "negligible(f ` S)"
1.2764 +proof -
1.2765 +  let ?U = "\<lambda>n. {x \<in> S. norm(x) \<le> n \<and> onorm(f' x) \<le> real n}"
1.2766 +  have "\<And>x. x \<in> S \<Longrightarrow> \<exists>n. norm x \<le> real n \<and> onorm (f' x) \<le> real n"
1.2767 +    by (meson linear order_trans real_arch_simple)
1.2768 +  then have eq: "S = (\<Union>n. ?U n)"
1.2769 +    by auto
1.2770 +  have "negligible (f ` ?U n)" for n
1.2771 +  proof (rule Sard_lemma2 [OF mlen])
1.2772 +    show "0 < real n + 1"
1.2773 +      by auto
1.2774 +    show "bounded (?U n)"
1.2775 +      using bounded_iff by blast
1.2776 +    show "(f has_derivative f' x) (at x within ?U n)" if "x \<in> ?U n" for x
1.2777 +      using der that by (force intro: has_derivative_subset)
1.2778 +  qed (use rank in auto)
1.2779 +  then show ?thesis
1.2780 +    by (subst eq) (simp add: image_Union negligible_Union_nat)
1.2781 +qed
1.2782 +
1.2783 +
1.2784 +subsection\<open>A one-way version of change-of-variables not assuming injectivity. \<close>
1.2785 +
1.2786 +lemma integral_on_image_ubound_weak:
1.2787 +  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real"
1.2788 +  assumes S: "S \<in> sets lebesgue"
1.2789 +      and f: "f \<in> borel_measurable (lebesgue_on (g ` S))"
1.2790 +      and nonneg_fg:  "\<And>x. x \<in> S \<Longrightarrow> 0 \<le> f(g x)"
1.2791 +      and der_g:   "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
1.2792 +      and det_int_fg: "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) integrable_on S"
1.2793 +      and meas_gim: "\<And>T. \<lbrakk>T \<subseteq> g ` S; T \<in> sets lebesgue\<rbrakk> \<Longrightarrow> {x \<in> S. g x \<in> T} \<in> sets lebesgue"
1.2794 +    shows "f integrable_on (g ` S) \<and>
1.2795 +           integral (g ` S) f \<le> integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x))"
1.2796 +         (is "_ \<and> _ \<le> ?b")
1.2797 +proof -
1.2798 +  let ?D = "\<lambda>x. \<bar>det (matrix (g' x))\<bar>"
1.2799 +  have cont_g: "continuous_on S g"
1.2800 +    using der_g has_derivative_continuous_on by blast
1.2801 +  have [simp]: "space (lebesgue_on S) = S"
1.2802 +    by (simp add: S)
1.2803 +  have gS_in_sets_leb: "g ` S \<in> sets lebesgue"
1.2804 +    apply (rule differentiable_image_in_sets_lebesgue)
1.2805 +    using der_g by (auto simp: S differentiable_def differentiable_on_def)
1.2806 +  obtain h where nonneg_h: "\<And>n x. 0 \<le> h n x"
1.2807 +    and h_le_f: "\<And>n x. x \<in> S \<Longrightarrow> h n (g x) \<le> f (g x)"
1.2808 +    and h_inc: "\<And>n x. h n x \<le> h (Suc n) x"
1.2809 +    and h_meas: "\<And>n. h n \<in> borel_measurable lebesgue"
1.2810 +    and fin_R: "\<And>n. finite(range (h n))"
1.2811 +    and lim: "\<And>x. x \<in> g ` S \<Longrightarrow> (\<lambda>n. h n x) \<longlonglongrightarrow> f x"
1.2812 +  proof -
1.2813 +    let ?f = "\<lambda>x. if x \<in> g ` S then f x else 0"
1.2814 +    have "?f \<in> borel_measurable lebesgue \<and> (\<forall>x. 0 \<le> ?f x)"
1.2815 +      by (auto simp: gS_in_sets_leb f nonneg_fg measurable_restrict_space_iff [symmetric])
1.2816 +    then show ?thesis
1.2817 +      apply (clarsimp simp add: borel_measurable_simple_function_limit_increasing)
1.2818 +      apply (rename_tac h)
1.2819 +      by (rule_tac h=h in that) (auto split: if_split_asm)
1.2820 +  qed
1.2821 +  have h_lmeas: "{t. h n (g t) = y} \<inter> S \<in> sets lebesgue" for y n
1.2822 +  proof -
1.2823 +    have "space (lebesgue_on (UNIV::(real,'n) vec set)) = UNIV"
1.2824 +      by simp
1.2825 +    then have "((h n) -`{y} \<inter> g ` S) \<in> sets (lebesgue_on (g ` S))"
1.2826 +      by (metis Int_commute borel_measurable_vimage h_meas image_eqI inf_top.right_neutral sets_restrict_space space_borel space_completion space_lborel)
1.2827 +    then have "({u. h n u = y} \<inter> g ` S) \<in> sets lebesgue"
1.2828 +      using gS_in_sets_leb
1.2829 +      by (simp add: integral_indicator fmeasurableI2 sets_restrict_space_iff vimage_def)
1.2830 +    then have "{x \<in> S. g x \<in> ({u. h n u = y} \<inter> g ` S)} \<in> sets lebesgue"
1.2831 +      using meas_gim[of "({u. h n u = y} \<inter> g ` S)"] by force
1.2832 +    moreover have "{t. h n (g t) = y} \<inter> S = {x \<in> S. g x \<in> ({u. h n u = y} \<inter> g ` S)}"
1.2833 +      by blast
1.2834 +    ultimately show ?thesis
1.2835 +      by auto
1.2836 +  qed
1.2837 +  have hint: "h n integrable_on g ` S \<and> integral (g ` S) (h n) \<le> integral S (\<lambda>x. ?D x * h n (g x))"
1.2838 +          (is "?INT \<and> ?lhs \<le> ?rhs")  for n
1.2839 +  proof -
1.2840 +    let ?R = "range (h n)"
1.2841 +    have hn_eq: "h n = (\<lambda>x. \<Sum>y\<in>?R. y * indicat_real {x. h n x = y} x)"
1.2842 +      by (simp add: indicator_def if_distrib fin_R cong: if_cong)
1.2843 +    have yind: "(\<lambda>t. y * indicator{x. h n x = y} t) integrable_on (g ` S) \<and>
1.2844 +                (integral (g ` S) (\<lambda>t. y * indicator {x. h n x = y} t))
1.2845 +                 \<le> integral S (\<lambda>t. \<bar>det (matrix (g' t))\<bar> * y * indicator {x. h n x = y} (g t))"
1.2846 +       if y: "y \<in> ?R" for y::real
1.2847 +    proof (cases "y=0")
1.2848 +      case True
1.2849 +      then show ?thesis using gS_in_sets_leb integrable_0 by force
1.2850 +    next
1.2851 +      case False
1.2852 +      with that have "y > 0"
1.2853 +        using less_eq_real_def nonneg_h by fastforce
1.2854 +      have "(\<lambda>x. if x \<in> {t. h n (g t) = y} then ?D x else 0) integrable_on S"
1.2855 +      proof (rule measurable_bounded_by_integrable_imp_integrable)
1.2856 +        have "(\<lambda>x. ?D x) \<in> borel_measurable (lebesgue_on ({t. h n (g t) = y} \<inter> S))"
1.2857 +          apply (intro borel_measurable_abs borel_measurable_det_Jacobian [OF h_lmeas, where f=g])
1.2858 +          by (meson der_g IntD2 has_derivative_within_subset inf_le2)
1.2859 +        then have "(\<lambda>x. if x \<in> {t. h n (g t) = y} \<inter> S then ?D x else 0) \<in> borel_measurable lebesgue"
1.2860 +          by (rule borel_measurable_If_I [OF _ h_lmeas])
1.2861 +        then show "(\<lambda>x. if x \<in> {t. h n (g t) = y} then ?D x else 0) \<in> borel_measurable (lebesgue_on S)"
1.2862 +          by (simp add: if_if_eq_conj Int_commute borel_measurable_UNIV [OF S, symmetric])
1.2863 +        show "(\<lambda>x. ?D x *\<^sub>R f (g x) /\<^sub>R y) integrable_on S"
1.2864 +          by (rule integrable_cmul) (use det_int_fg in auto)
1.2865 +        show "norm (if x \<in> {t. h n (g t) = y} then ?D x else 0) \<le> ?D x *\<^sub>R f (g x) /\<^sub>R y"
1.2866 +          if "x \<in> S" for x
1.2867 +          using nonneg_h [of n x] \<open>y > 0\<close> nonneg_fg [of x] h_le_f [of x n] that
1.2868 +          by (auto simp: divide_simps ordered_semiring_class.mult_left_mono)
1.2869 +      qed (use S in auto)
1.2870 +      then have int_det: "(\<lambda>t. \<bar>det (matrix (g' t))\<bar>) integrable_on ({t. h n (g t) = y} \<inter> S)"
1.2871 +        using integrable_restrict_Int by force
1.2872 +      have "(g ` ({t. h n (g t) = y} \<inter> S)) \<in> lmeasurable"
1.2873 +        apply (rule measurable_differentiable_image [OF h_lmeas])
1.2874 +         apply (blast intro: has_derivative_within_subset [OF der_g])
1.2875 +        apply (rule int_det)
1.2876 +        done
1.2877 +      moreover have "g ` ({t. h n (g t) = y} \<inter> S) = {x. h n x = y} \<inter> g ` S"
1.2878 +        by blast
1.2879 +      moreover have "measure lebesgue (g ` ({t. h n (g t) = y} \<inter> S))
1.2880 +                     \<le> integral ({t. h n (g t) = y} \<inter> S) (\<lambda>t. \<bar>det (matrix (g' t))\<bar>)"
1.2881 +        apply (rule measure_differentiable_image [OF h_lmeas _ int_det])
1.2882 +        apply (blast intro: has_derivative_within_subset [OF der_g])
1.2883 +        done
1.2884 +      ultimately show ?thesis
1.2885 +        using \<open>y > 0\<close> integral_restrict_Int [of S "{t. h n (g t) = y}" "\<lambda>t. \<bar>det (matrix (g' t))\<bar> * y"]
1.2886 +        apply (simp add: integrable_on_indicator integrable_on_cmult_iff integral_indicator)
1.2887 +        apply (simp add: indicator_def if_distrib cong: if_cong)
1.2888 +        done
1.2889 +    qed
1.2890 +    have hn_int: "h n integrable_on g ` S"
1.2891 +      apply (subst hn_eq)
1.2892 +      using yind by (force intro: integrable_sum [OF fin_R])
1.2893 +    then show ?thesis
1.2894 +    proof
1.2895 +      have "?lhs = integral (g ` S) (\<lambda>x. \<Sum>y\<in>range (h n). y * indicat_real {x. h n x = y} x)"
1.2896 +        by (metis hn_eq)
1.2897 +      also have "\<dots> = (\<Sum>y\<in>range (h n). integral (g ` S) (\<lambda>x. y * indicat_real {x. h n x = y} x))"
1.2898 +        by (rule integral_sum [OF fin_R]) (use yind in blast)
1.2899 +      also have "\<dots> \<le> (\<Sum>y\<in>range (h n). integral S (\<lambda>u. \<bar>det (matrix (g' u))\<bar> * y * indicat_real {x. h n x = y} (g u)))"
1.2900 +        using yind by (force intro: sum_mono)
1.2901 +      also have "\<dots> = integral S (\<lambda>u. \<Sum>y\<in>range (h n). \<bar>det (matrix (g' u))\<bar> * y * indicat_real {x. h n x = y} (g u))"
1.2902 +      proof (rule integral_sum [OF fin_R, symmetric])
1.2903 +        fix y assume y: "y \<in> ?R"
1.2904 +        with nonneg_h have "y \<ge> 0"
1.2905 +          by auto
1.2906 +        show "(\<lambda>u. \<bar>det (matrix (g' u))\<bar> * y * indicat_real {x. h n x = y} (g u)) integrable_on S"
1.2907 +        proof (rule measurable_bounded_by_integrable_imp_integrable)
1.2908 +          have "(\<lambda>x. indicat_real {x. h n x = y} (g x)) \<in> borel_measurable (lebesgue_on S)"
1.2909 +            using h_lmeas S
1.2910 +            by (auto simp: indicator_vimage [symmetric] borel_measurable_indicator_iff sets_restrict_space_iff)
1.2911 +          then show "(\<lambda>u. \<bar>det (matrix (g' u))\<bar> * y * indicat_real {x. h n x = y} (g u)) \<in> borel_measurable (lebesgue_on S)"
1.2912 +            by (intro borel_measurable_times borel_measurable_abs borel_measurable_const borel_measurable_det_Jacobian [OF S der_g])
1.2913 +        next
1.2914 +          fix x
1.2915 +          assume "x \<in> S"
1.2916 +          have "y * indicat_real {x. h n x = y} (g x) \<le> f (g x)"
1.2917 +            by (metis (full_types) \<open>x \<in> S\<close> h_le_f indicator_def mem_Collect_eq mult.right_neutral mult_zero_right nonneg_fg)
1.2918 +          with \<open>y \<ge> 0\<close> show "norm (?D x * y * indicat_real {x. h n x = y} (g x)) \<le> ?D x * f(g x)"
1.2919 +            by (simp add: abs_mult mult.assoc mult_left_mono)
1.2920 +        qed (use S det_int_fg in auto)
1.2921 +      qed
1.2922 +      also have "\<dots> = integral S (\<lambda>T. \<bar>det (matrix (g' T))\<bar> *
1.2923 +                                        (\<Sum>y\<in>range (h n). y * indicat_real {x. h n x = y} (g T)))"
1.2924 +        by (simp add: sum_distrib_left mult.assoc)
1.2925 +      also have "\<dots> = ?rhs"
1.2926 +        by (metis hn_eq)
1.2927 +      finally show "integral (g ` S) (h n) \<le> ?rhs" .
1.2928 +    qed
1.2929 +  qed
1.2930 +  have le: "integral S (\<lambda>T. \<bar>det (matrix (g' T))\<bar> * h n (g T)) \<le> ?b" for n
1.2931 +  proof (rule integral_le)
1.2932 +    show "(\<lambda>T. \<bar>det (matrix (g' T))\<bar> * h n (g T)) integrable_on S"
1.2933 +    proof (rule measurable_bounded_by_integrable_imp_integrable)
1.2934 +      have "(\<lambda>T. \<bar>det (matrix (g' T))\<bar> *\<^sub>R h n (g T)) \<in> borel_measurable (lebesgue_on S)"
1.2935 +      proof (intro borel_measurable_scaleR borel_measurable_abs borel_measurable_det_Jacobian \<open>S \<in> sets lebesgue\<close>)
1.2936 +        have eq: "{x \<in> S. f x \<le> a} = (\<Union>b \<in> (f ` S) \<inter> atMost a. {x. f x = b} \<inter> S)" for f and a::real
1.2937 +          by auto
1.2938 +        have "finite ((\<lambda>x. h n (g x)) ` S \<inter> {..a})" for a
1.2939 +          by (force intro: finite_subset [OF _ fin_R])
1.2940 +        with h_lmeas [of n] show "(\<lambda>x. h n (g x)) \<in> borel_measurable (lebesgue_on S)"
1.2941 +          apply (simp add: borel_measurable_vimage_halfspace_component_le \<open>S \<in> sets lebesgue\<close> sets_restrict_space_iff eq)
1.2942 +          by (metis (mono_tags) SUP_inf sets.finite_UN)
1.2943 +      qed (use der_g in blast)
1.2944 +      then show "(\<lambda>T. \<bar>det (matrix (g' T))\<bar> * h n (g T)) \<in> borel_measurable (lebesgue_on S)"
1.2945 +        by simp
1.2946 +      show "norm (?D x * h n (g x)) \<le> ?D x *\<^sub>R f (g x)"
1.2947 +        if "x \<in> S" for x
1.2948 +        by (simp add: h_le_f mult_left_mono nonneg_h that)
1.2949 +    qed (use S det_int_fg in auto)
1.2950 +    show "?D x * h n (g x) \<le> ?D x * f (g x)" if "x \<in> S" for x
1.2951 +      by (simp add: \<open>x \<in> S\<close> h_le_f mult_left_mono)
1.2952 +    show "(\<lambda>x. ?D x * f (g x)) integrable_on S"
1.2953 +      using det_int_fg by blast
1.2954 +  qed
1.2955 +  have "f integrable_on g ` S \<and> (\<lambda>k. integral (g ` S) (h k)) \<longlonglongrightarrow> integral (g ` S) f"
1.2956 +  proof (rule monotone_convergence_increasing)
1.2957 +    have "\<bar>integral (g ` S) (h n)\<bar> \<le> integral S (\<lambda>x. ?D x * f (g x))" for n
1.2958 +    proof -
1.2959 +      have "\<bar>integral (g ` S) (h n)\<bar> = integral (g ` S) (h n)"
1.2960 +        using hint by (simp add: integral_nonneg nonneg_h)
1.2961 +      also have "\<dots> \<le> integral S (\<lambda>x. ?D x * f (g x))"
1.2962 +        using hint le by (meson order_trans)
1.2963 +      finally show ?thesis .
1.2964 +    qed
1.2965 +    then show "bounded (range (\<lambda>k. integral (g ` S) (h k)))"
1.2966 +      by (force simp: bounded_iff)
1.2967 +  qed (use h_inc lim hint in auto)
1.2968 +  moreover have "integral (g ` S) (h n) \<le> integral S (\<lambda>x. ?D x * f (g x))" for n
1.2969 +    using hint by (blast intro: le order_trans)
1.2970 +  ultimately show ?thesis
1.2971 +    by (auto intro: Lim_bounded_ereal)
1.2972 +qed
1.2973 +
1.2974 +
1.2975 +lemma integral_on_image_ubound_nonneg:
1.2976 +  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real"
1.2977 +  assumes nonneg_fg: "\<And>x. x \<in> S \<Longrightarrow> 0 \<le> f(g x)"
1.2978 +      and der_g:   "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
1.2979 +      and intS: "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) integrable_on S"
1.2980 +  shows "f integrable_on (g ` S) \<and> integral (g ` S) f \<le> integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x))"
1.2981 +         (is "_ \<and> _ \<le> ?b")
1.2982 +proof -
1.2983 +  let ?D = "\<lambda>x. det (matrix (g' x))"
1.2984 +  define S' where "S' \<equiv> {x \<in> S. ?D x * f(g x) \<noteq> 0}"
1.2985 +  then have der_gS': "\<And>x. x \<in> S' \<Longrightarrow> (g has_derivative g' x) (at x within S')"
1.2986 +    by (metis (mono_tags, lifting) der_g has_derivative_within_subset mem_Collect_eq subset_iff)
1.2987 +  have "(\<lambda>x. if x \<in> S then \<bar>?D x\<bar> * f (g x) else 0) integrable_on UNIV"
1.2988 +    by (simp add: integrable_restrict_UNIV intS)
1.2989 +  then have Df_borel: "(\<lambda>x. if x \<in> S then \<bar>?D x\<bar> * f (g x) else 0) \<in> borel_measurable lebesgue"
1.2990 +    using integrable_imp_measurable borel_measurable_UNIV_eq by blast
1.2991 +  have S': "S' \<in> sets lebesgue"
1.2992 +  proof -
1.2993 +    from Df_borel borel_measurable_vimage_open [of _ UNIV]
1.2994 +    have "{x. (if x \<in> S then \<bar>?D x\<bar> * f (g x) else 0) \<in> T} \<in> sets lebesgue"
1.2995 +      if "open T" for T
1.2996 +      using that unfolding borel_measurable_UNIV_eq
1.2997 +      by (fastforce simp add: dest!: spec)
1.2998 +    then have "{x. (if x \<in> S then \<bar>?D x\<bar> * f (g x) else 0) \<in> -{0}} \<in> sets lebesgue"
1.2999 +      using open_Compl by blast
1.3000 +    then show ?thesis
1.3001 +      by (simp add: S'_def conj_ac split: if_split_asm cong: conj_cong)
1.3002 +  qed
1.3003 +  then have gS': "g ` S' \<in> sets lebesgue"
1.3004 +  proof (rule differentiable_image_in_sets_lebesgue)
1.3005 +    show "g differentiable_on S'"
1.3006 +      using der_g unfolding S'_def differentiable_def differentiable_on_def
1.3007 +      by (blast intro: has_derivative_within_subset)
1.3008 +  qed auto
1.3009 +  have f: "f \<in> borel_measurable (lebesgue_on (g ` S'))"
1.3010 +  proof (clarsimp simp add: borel_measurable_vimage_open)
1.3011 +    fix T :: "real set"
1.3012 +    assume "open T"
1.3013 +    have "{x \<in> g ` S'. f x \<in> T} = g ` {x \<in> S'. f(g x) \<in> T}"
1.3014 +      by blast
1.3015 +    moreover have "g ` {x \<in> S'. f(g x) \<in> T} \<in> sets lebesgue"
1.3016 +    proof (rule differentiable_image_in_sets_lebesgue)
1.3017 +      let ?h = "\<lambda>x. \<bar>?D x\<bar> * f (g x) /\<^sub>R \<bar>?D x\<bar>"
1.3018 +      have "(\<lambda>x. if x \<in> S' then \<bar>?D x\<bar> * f (g x) else 0) = (\<lambda>x. if x \<in> S then \<bar>?D x\<bar> * f (g x) else 0)"
1.3019 +        by (auto simp: S'_def)
1.3020 +      also have "\<dots> \<in> borel_measurable lebesgue"
1.3021 +        by (rule Df_borel)
1.3022 +      finally have *: "(\<lambda>x. \<bar>?D x\<bar> * f (g x)) \<in> borel_measurable (lebesgue_on S')"
1.3023 +        by (simp add: borel_measurable_If_D)
1.3024 +      have "?h \<in> borel_measurable (lebesgue_on S')"
1.3025 +        by (intro * S' der_gS' borel_measurable_det_Jacobian measurable) (blast intro: der_gS')
1.3026 +      moreover have "?h x = f(g x)" if "x \<in> S'" for x
1.3027 +        using that by (auto simp: S'_def)
1.3028 +      ultimately have "(\<lambda>x. f(g x)) \<in> borel_measurable (lebesgue_on S')"
1.3029 +        by (metis (no_types, lifting) measurable_lebesgue_cong)
1.3030 +      then show "{x \<in> S'. f (g x) \<in> T} \<in> sets lebesgue"
1.3031 +        by (simp add: \<open>S' \<in> sets lebesgue\<close> \<open>open T\<close> borel_measurable_vimage_open sets_restrict_space_iff)
1.3032 +      show "g differentiable_on {x \<in> S'. f (g x) \<in> T}"
1.3033 +        using der_g unfolding S'_def differentiable_def differentiable_on_def
1.3034 +        by (blast intro: has_derivative_within_subset)
1.3035 +    qed auto
1.3036 +    ultimately have "{x \<in> g ` S'. f x \<in> T} \<in> sets lebesgue"
1.3037 +      by metis
1.3038 +    then show "{x \<in> g ` S'. f x \<in> T} \<in> sets (lebesgue_on (g ` S'))"
1.3039 +      by (simp add: \<open>g ` S' \<in> sets lebesgue\<close> sets_restrict_space_iff)
1.3040 +  qed
1.3041 +  have intS': "(\<lambda>x. \<bar>?D x\<bar> * f (g x)) integrable_on S'"
1.3042 +    using intS
1.3043 +    by (rule integrable_spike_set) (auto simp: S'_def intro: empty_imp_negligible)
1.3044 +  have lebS': "{x \<in> S'. g x \<in> T} \<in> sets lebesgue" if "T \<subseteq> g ` S'" "T \<in> sets lebesgue" for T
1.3045 +  proof -
1.3046 +    have "g \<in> borel_measurable (lebesgue_on S')"
1.3047 +      using der_gS' has_derivative_continuous_on S'
1.3048 +      by (blast intro: continuous_imp_measurable_on_sets_lebesgue)
1.3049 +    moreover have "{x \<in> S'. g x \<in> U} \<in> sets lebesgue" if "negligible U" "U \<subseteq> g ` S'" for U
1.3050 +    proof (intro negligible_imp_sets negligible_differentiable_vimage that)
1.3051 +      fix x
1.3052 +      assume x: "x \<in> S'"
1.3053 +      then have "linear (g' x)"
1.3054 +        using der_gS' has_derivative_linear by blast
1.3055 +      with x show "inj (g' x)"
1.3056 +        by (auto simp: S'_def det_nz_iff_inj)
1.3057 +    qed (use der_gS' in auto)
1.3058 +    ultimately show ?thesis
1.3059 +      using double_lebesgue_sets [OF S' gS' order_refl] that by blast
1.3060 +  qed
1.3061 +  have int_gS': "f integrable_on g ` S' \<and> integral (g ` S') f \<le> integral S' (\<lambda>x. \<bar>?D x\<bar> * f(g x))"
1.3062 +    using integral_on_image_ubound_weak [OF S' f nonneg_fg der_gS' intS' lebS'] S'_def by blast
1.3063 +  have "negligible (g ` {x \<in> S. det(matrix(g' x)) = 0})"
1.3064 +  proof (rule baby_Sard, simp_all)
1.3065 +    fix x
1.3066 +    assume x: "x \<in> S \<and> det (matrix (g' x)) = 0"
1.3067 +    then show "(g has_derivative g' x) (at x within {x \<in> S. det (matrix (g' x)) = 0})"
1.3068 +      by (metis (no_types, lifting) der_g has_derivative_within_subset mem_Collect_eq subsetI)
1.3069 +    then show "rank (matrix (g' x)) < CARD('n)"
1.3070 +      using det_nz_iff_inj matrix_vector_mul_linear x
1.3071 +      by (fastforce simp add: less_rank_noninjective)
1.3072 +  qed
1.3073 +  then have negg: "negligible (g ` S - g ` {x \<in> S. ?D x \<noteq> 0})"
1.3074 +    by (rule negligible_subset) (auto simp: S'_def)
1.3075 +  have null: "g ` {x \<in> S. ?D x \<noteq> 0} - g ` S = {}"
1.3076 +    by (auto simp: S'_def)
1.3077 +  let ?F = "{x \<in> S. f (g x) \<noteq> 0}"
1.3078 +  have eq: "g ` S' = g ` ?F \<inter> g ` {x \<in> S. ?D x \<noteq> 0}"
1.3079 +    by (auto simp: S'_def image_iff)
1.3080 +  show ?thesis
1.3081 +  proof
1.3082 +    have "((\<lambda>x. if x \<in> g ` ?F then f x else 0) integrable_on g ` {x \<in> S. ?D x \<noteq> 0})"
1.3083 +      using int_gS' eq integrable_restrict_Int [where f=f]
1.3084 +      by simp
1.3085 +    then have "f integrable_on g ` {x \<in> S. ?D x \<noteq> 0}"
1.3086 +      by (auto simp: image_iff elim!: integrable_eq)
1.3087 +    then show "f integrable_on g ` S"
1.3088 +      apply (rule integrable_spike_set [OF _ empty_imp_negligible negligible_subset])
1.3089 +      using negg null by auto
1.3090 +    have "integral (g ` S) f = integral (g ` {x \<in> S. ?D x \<noteq> 0}) f"
1.3091 +      using negg by (auto intro: negligible_subset integral_spike_set)
1.3092 +    also have "\<dots> = integral (g ` {x \<in> S. ?D x \<noteq> 0}) (\<lambda>x. if x \<in> g ` ?F then f x else 0)"
1.3093 +      by (auto simp: image_iff intro!: integral_cong)
1.3094 +    also have "\<dots> = integral (g ` S') f"
1.3095 +      using  eq integral_restrict_Int by simp
1.3096 +    also have "\<dots> \<le> integral S' (\<lambda>x. \<bar>?D x\<bar> * f(g x))"
1.3097 +      by (metis int_gS')
1.3098 +    also have "\<dots> \<le> ?b"
1.3099 +      by (rule integral_subset_le [OF _ intS' intS]) (use nonneg_fg S'_def in auto)
1.3100 +    finally show "integral (g ` S) f \<le> ?b" .
1.3101 +  qed
1.3102 +qed
1.3103 +
1.3104 +
1.3105 +
1.3106 +lemma absolutely_integrable_on_image_real:
1.3107 +  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real" and g :: "real^'n::_ \<Rightarrow> real^'n::_"
1.3108 +  assumes der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
1.3109 +    and intS: "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) absolutely_integrable_on S"
1.3110 +  shows "f absolutely_integrable_on (g ` S)"
1.3111 +proof -
1.3112 +  let ?D = "\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f (g x)"
1.3113 +  let ?N = "{x \<in> S. f (g x) < 0}" and ?P = "{x \<in> S. f (g x) > 0}"
1.3114 +  have eq: "{x. (if x \<in> S then ?D x else 0) > 0} = {x \<in> S. ?D x > 0}"
1.3115 +           "{x. (if x \<in> S then ?D x else 0) < 0} = {x \<in> S. ?D x < 0}"
1.3116 +    by auto
1.3117 +  have "?D integrable_on S"
1.3118 +    using intS absolutely_integrable_on_def by blast
1.3119 +  then have "(\<lambda>x. if x \<in> S then ?D x else 0) integrable_on UNIV"
1.3120 +    by (simp add: integrable_restrict_UNIV)
1.3121 +  then have D_borel: "(\<lambda>x. if x \<in> S then ?D x else 0) \<in> borel_measurable (lebesgue_on UNIV)"
1.3122 +    using integrable_imp_measurable borel_measurable_UNIV_eq by blast
1.3123 +  then have Dlt: "{x \<in> S. ?D x < 0} \<in> sets lebesgue"
1.3124 +    unfolding borel_measurable_vimage_halfspace_component_lt
1.3125 +    by (drule_tac x=0 in spec) (auto simp: eq)
1.3126 +  from D_borel have Dgt: "{x \<in> S. ?D x > 0} \<in> sets lebesgue"
1.3127 +    unfolding borel_measurable_vimage_halfspace_component_gt
1.3128 +    by (drule_tac x=0 in spec) (auto simp: eq)
1.3129 +
1.3130 +  have dfgbm: "?D \<in> borel_measurable (lebesgue_on S)"
1.3131 +    using intS absolutely_integrable_on_def integrable_imp_measurable by blast
1.3132 +  have der_gN: "(g has_derivative g' x) (at x within ?N)" if "x \<in> ?N" for x
1.3133 +      using der_g has_derivative_within_subset that by force
1.3134 +  have "(\<lambda>x. - f x) integrable_on g ` ?N \<and>
1.3135 +         integral (g ` ?N) (\<lambda>x. - f x) \<le> integral ?N (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * - f (g x))"
1.3136 +  proof (rule integral_on_image_ubound_nonneg [OF _ der_gN])
1.3137 +    have 1: "?D integrable_on {x \<in> S. ?D x < 0}"
1.3138 +      using Dlt
1.3139 +      by (auto intro: set_lebesgue_integral_eq_integral [OF set_integrable_subset] intS)
1.3140 +    have "uminus \<circ> (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * - f (g x)) integrable_on ?N"
1.3141 +      by (simp add: o_def mult_less_0_iff empty_imp_negligible integrable_spike_set [OF 1])
1.3142 +    then show "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> * - f (g x)) integrable_on ?N"
1.3143 +      by (simp add: integrable_neg_iff o_def)
1.3144 +  qed auto
1.3145 +  then have "f integrable_on g ` ?N"
1.3146 +    by (simp add: integrable_neg_iff)
1.3147 +  moreover have "g ` ?N = {y \<in> g ` S. f y < 0}"
1.3148 +    by auto
1.3149 +  ultimately have "f integrable_on {y \<in> g ` S. f y < 0}"
1.3150 +    by simp
1.3151 +  then have N: "f absolutely_integrable_on {y \<in> g ` S. f y < 0}"
1.3152 +    by (rule absolutely_integrable_absolutely_integrable_ubound) auto
1.3153 +
1.3154 +  have der_gP: "(g has_derivative g' x) (at x within ?P)" if "x \<in> ?P" for x
1.3155 +      using der_g has_derivative_within_subset that by force
1.3156 +  have "f integrable_on g ` ?P \<and> integral (g ` ?P) f \<le> integral ?P ?D"
1.3157 +  proof (rule integral_on_image_ubound_nonneg [OF _ der_gP])
1.3158 +    have "?D integrable_on {x \<in> S. 0 < ?D x}"
1.3159 +      using Dgt
1.3160 +      by (auto intro: set_lebesgue_integral_eq_integral [OF set_integrable_subset] intS)
1.3161 +    then show "?D integrable_on ?P"
1.3162 +      apply (rule integrable_spike_set)
1.3163 +      by (auto simp: zero_less_mult_iff empty_imp_negligible)
1.3164 +  qed auto
1.3165 +  then have "f integrable_on g ` ?P"
1.3166 +    by metis
1.3167 +  moreover have "g ` ?P = {y \<in> g ` S. f y > 0}"
1.3168 +    by auto
1.3169 +  ultimately have "f integrable_on {y \<in> g ` S. f y > 0}"
1.3170 +    by simp
1.3171 +  then have P: "f absolutely_integrable_on {y \<in> g ` S. f y > 0}"
1.3172 +    by (rule absolutely_integrable_absolutely_integrable_lbound) auto
1.3173 +  have "(\<lambda>x. if x \<in> g ` S \<and> f x < 0 \<or> x \<in> g ` S \<and> 0 < f x then f x else 0) = (\<lambda>x. if x \<in> g ` S then f x else 0)"
1.3174 +    by auto
1.3175 +  then show ?thesis
1.3176 +    using absolutely_integrable_Un [OF N P] absolutely_integrable_restrict_UNIV [symmetric, where f=f]
1.3177 +    by simp
1.3178 +qed
1.3179 +
1.3180 +
1.3181 +proposition absolutely_integrable_on_image:
1.3182 +  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
1.3183 +  assumes der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
1.3184 +    and intS: "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S"
1.3185 +  shows "f absolutely_integrable_on (g ` S)"
1.3186 +  apply (rule absolutely_integrable_componentwise [OF absolutely_integrable_on_image_real [OF der_g]])
1.3187 +  using absolutely_integrable_component [OF intS]  by auto
1.3188 +
1.3189 +proposition integral_on_image_ubound:
1.3190 +  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real" and g :: "real^'n::_ \<Rightarrow> real^'n::_"
1.3191 +  assumes"\<And>x. x \<in> S \<Longrightarrow> 0 \<le> f(g x)"
1.3192 +    and "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
1.3193 +    and "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) integrable_on S"
1.3194 +  shows "integral (g ` S) f \<le> integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x))"
1.3195 +  using integral_on_image_ubound_nonneg [OF assms] by simp
1.3196 +
1.3197 +
1.3198 +
1.3199 +subsection\<open>Change-of-variables theorem\<close>
1.3200 +
1.3201 +text\<open>The classic change-of-variables theorem. We have two versions with quite general hypotheses,
1.3202 +the first that the transforming function has a continuous inverse, the second that the base set is
1.3203 +Lebesgue measurable.\<close>
1.3204 +lemma cov_invertible_nonneg_le:
1.3205 +  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real" and g :: "real^'n::_ \<Rightarrow> real^'n::_"
1.3206 +  assumes der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
1.3207 +    and der_h: "\<And>y. y \<in> T \<Longrightarrow> (h has_derivative h' y) (at y within T)"
1.3208 +    and f0: "\<And>y. y \<in> T \<Longrightarrow> 0 \<le> f y"
1.3209 +    and hg: "\<And>x. x \<in> S \<Longrightarrow> g x \<in> T \<and> h(g x) = x"
1.3210 +    and gh: "\<And>y. y \<in> T \<Longrightarrow> h y \<in> S \<and> g(h y) = y"
1.3211 +    and id: "\<And>y. y \<in> T \<Longrightarrow> h' y \<circ> g'(h y) = id"
1.3212 +  shows "f integrable_on T \<and> (integral T f) \<le> b \<longleftrightarrow>
1.3213 +             (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) integrable_on S \<and>
1.3214 +             integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) \<le> b"
1.3215 +        (is "?lhs = ?rhs")
1.3216 +proof -
1.3217 +  have Teq: "T = g`S" and Seq: "S = h`T"
1.3218 +    using hg gh image_iff by fastforce+
1.3219 +  have gS: "g differentiable_on S"
1.3220 +    by (meson der_g differentiable_def differentiable_on_def)
1.3221 +  let ?D = "\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f (g x)"
1.3222 +  show ?thesis
1.3223 +  proof
1.3224 +    assume ?lhs
1.3225 +    then have fT: "f integrable_on T" and intf: "integral T f \<le> b"
1.3226 +      by blast+
1.3227 +    show ?rhs
1.3228 +    proof
1.3229 +      let ?fgh = "\<lambda>x. \<bar>det (matrix (h' x))\<bar> * (\<bar>det (matrix (g' (h x)))\<bar> * f (g (h x)))"
1.3230 +      have ddf: "?fgh x = f x"
1.3231 +        if "x \<in> T" for x
1.3232 +      proof -
1.3233 +        have "matrix (h' x) ** matrix (g' (h x)) = mat 1"
1.3234 +          using that id matrix_compose
1.3235 +          by (metis der_g gh has_derivative_linear left_inverse_linear matrix_id_mat_1)
1.3236 +        then have "\<bar>det (matrix (h' x))\<bar> * \<bar>det (matrix (g' (h x)))\<bar> = 1"
1.3237 +          by (metis abs_1 abs_mult det_I det_mul)
1.3238 +        then show ?thesis
1.3239 +          by (simp add: gh that)
1.3240 +      qed
1.3241 +      have "?D integrable_on (h ` T)"
1.3242 +      proof (intro set_lebesgue_integral_eq_integral absolutely_integrable_on_image_real)
1.3243 +        show "(\<lambda>x. ?fgh x) absolutely_integrable_on T"
1.3244 +        proof (subst absolutely_integrable_on_iff_nonneg)
1.3245 +          show "(\<lambda>x. ?fgh x) integrable_on T"
1.3246 +            using ddf fT integrable_eq by force
1.3247 +        qed (simp add: zero_le_mult_iff f0 gh)
1.3248 +      qed (use der_h in auto)
1.3249 +      with Seq show "(\<lambda>x. ?D x) integrable_on S"
1.3250 +        by simp
1.3251 +      have "integral S (\<lambda>x. ?D x) \<le> integral T (\<lambda>x. ?fgh x)"
1.3252 +        unfolding Seq
1.3253 +      proof (rule integral_on_image_ubound)
1.3254 +        show "(\<lambda>x. ?fgh x) integrable_on T"
1.3255 +        using ddf fT integrable_eq by force
1.3256 +      qed (use f0 gh der_h in auto)
1.3257 +      also have "\<dots> = integral T f"
1.3258 +        by (force simp: ddf intro: integral_cong)
1.3259 +      also have "\<dots> \<le> b"
1.3260 +        by (rule intf)
1.3261 +      finally show "integral S (\<lambda>x. ?D x) \<le> b" .
1.3262 +    qed
1.3263 +  next
1.3264 +    assume R: ?rhs
1.3265 +    then have "f integrable_on g ` S"
1.3266 +      using der_g f0 hg integral_on_image_ubound_nonneg by blast
1.3267 +    moreover have "integral (g ` S) f \<le> integral S (\<lambda>x. ?D x)"
1.3268 +      by (rule integral_on_image_ubound [OF f0 der_g]) (use R Teq in auto)
1.3269 +    ultimately show ?lhs
1.3270 +      using R by (simp add: Teq)
1.3271 +  qed
1.3272 +qed
1.3273 +
1.3274 +
1.3275 +lemma cov_invertible_nonneg_eq:
1.3276 +  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real" and g :: "real^'n::_ \<Rightarrow> real^'n::_"
1.3277 +  assumes "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
1.3278 +      and "\<And>y. y \<in> T \<Longrightarrow> (h has_derivative h' y) (at y within T)"
1.3279 +      and "\<And>y. y \<in> T \<Longrightarrow> 0 \<le> f y"
1.3280 +      and "\<And>x. x \<in> S \<Longrightarrow> g x \<in> T \<and> h(g x) = x"
1.3281 +      and "\<And>y. y \<in> T \<Longrightarrow> h y \<in> S \<and> g(h y) = y"
1.3282 +      and "\<And>y. y \<in> T \<Longrightarrow> h' y \<circ> g'(h y) = id"
1.3283 +  shows "((\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) has_integral b) S \<longleftrightarrow> (f has_integral b) T"
1.3284 +  using cov_invertible_nonneg_le [OF assms]
1.3285 +  by (simp add: has_integral_iff) (meson eq_iff)
1.3286 +
1.3287 +
1.3288 +lemma cov_invertible_real:
1.3289 +  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real" and g :: "real^'n::_ \<Rightarrow> real^'n::_"
1.3290 +  assumes der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
1.3291 +      and der_h: "\<And>y. y \<in> T \<Longrightarrow> (h has_derivative h' y) (at y within T)"
1.3292 +      and hg: "\<And>x. x \<in> S \<Longrightarrow> g x \<in> T \<and> h(g x) = x"
1.3293 +      and gh: "\<And>y. y \<in> T \<Longrightarrow> h y \<in> S \<and> g(h y) = y"
1.3294 +      and id: "\<And>y. y \<in> T \<Longrightarrow> h' y \<circ> g'(h y) = id"
1.3295 +  shows "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) absolutely_integrable_on S \<and>
1.3296 +           integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)) = b \<longleftrightarrow>
1.3297 +         f absolutely_integrable_on T \<and> integral T f = b"
1.3298 +         (is "?lhs = ?rhs")
1.3299 +proof -
1.3300 +  have Teq: "T = g`S" and Seq: "S = h`T"
1.3301 +    using hg gh image_iff by fastforce+
1.3302 +  let ?DP = "\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x)" and ?DN = "\<lambda>x. \<bar>det (matrix (g' x))\<bar> * -f(g x)"
1.3303 +  have "+": "(?DP has_integral b) {x \<in> S. f (g x) > 0} \<longleftrightarrow> (f has_integral b) {y \<in> T. f y > 0}" for b
1.3304 +  proof (rule cov_invertible_nonneg_eq)
1.3305 +    have *: "(\<lambda>x. f (g x)) -` Y \<inter> {x \<in> S. f (g x) > 0}
1.3306 +          = ((\<lambda>x. f (g x)) -` Y \<inter> S) \<inter> {x \<in> S. f (g x) > 0}" for Y
1.3307 +      by auto
1.3308 +    show "(g has_derivative g' x) (at x within {x \<in> S. f (g x) > 0})" if "x \<in> {x \<in> S. f (g x) > 0}" for x
1.3309 +      using that der_g has_derivative_within_subset by fastforce
1.3310 +    show "(h has_derivative h' y) (at y within {y \<in> T. f y > 0})" if "y \<in> {y \<in> T. f y > 0}" for y
1.3311 +      using that der_h has_derivative_within_subset by fastforce
1.3312 +  qed (use gh hg id in auto)
1.3313 +  have "-": "(?DN has_integral b) {x \<in> S. f (g x) < 0} \<longleftrightarrow> ((\<lambda>x. - f x) has_integral b) {y \<in> T. f y < 0}" for b
1.3314 +  proof (rule cov_invertible_nonneg_eq)
1.3315 +    have *: "(\<lambda>x. - f (g x)) -` y \<inter> {x \<in> S. f (g x) < 0}
1.3316 +          = ((\<lambda>x. f (g x)) -` uminus ` y \<inter> S) \<inter> {x \<in> S. f (g x) < 0}" for y
1.3317 +      using image_iff by fastforce
1.3318 +    show "(g has_derivative g' x) (at x within {x \<in> S. f (g x) < 0})" if "x \<in> {x \<in> S. f (g x) < 0}" for x
1.3319 +      using that der_g has_derivative_within_subset by fastforce
1.3320 +    show "(h has_derivative h' y) (at y within {y \<in> T. f y < 0})" if "y \<in> {y \<in> T. f y < 0}" for y
1.3321 +      using that der_h has_derivative_within_subset by fastforce
1.3322 +  qed (use gh hg id in auto)
1.3323 +  show ?thesis
1.3324 +  proof
1.3325 +    assume LHS: ?lhs
1.3326 +    have eq: "{x. (if x \<in> S then ?DP x else 0) > 0} = {x \<in> S. ?DP x > 0}"
1.3327 +      "{x. (if x \<in> S then ?DP x else 0) < 0} = {x \<in> S. ?DP x < 0}"
1.3328 +      by auto
1.3329 +    have "?DP integrable_on S"
1.3330 +      using LHS absolutely_integrable_on_def by blast
1.3331 +    then have "(\<lambda>x. if x \<in> S then ?DP x else 0) integrable_on UNIV"
1.3332 +      by (simp add: integrable_restrict_UNIV)
1.3333 +    then have D_borel: "(\<lambda>x. if x \<in> S then ?DP x else 0) \<in> borel_measurable (lebesgue_on UNIV)"
1.3334 +      using integrable_imp_measurable borel_measurable_UNIV_eq by blast
1.3335 +    then have SN: "{x \<in> S. ?DP x < 0} \<in> sets lebesgue"
1.3336 +      unfolding borel_measurable_vimage_halfspace_component_lt
1.3337 +      by (drule_tac x=0 in spec) (auto simp: eq)
1.3338 +    from D_borel have SP: "{x \<in> S. ?DP x > 0} \<in> sets lebesgue"
1.3339 +      unfolding borel_measurable_vimage_halfspace_component_gt
1.3340 +      by (drule_tac x=0 in spec) (auto simp: eq)
1.3341 +    have "?DP absolutely_integrable_on {x \<in> S. ?DP x > 0}"
1.3342 +      using LHS by (fast intro!: set_integrable_subset [OF _, of _ S] SP)
1.3343 +    then have aP: "?DP absolutely_integrable_on {x \<in> S. f (g x) > 0}"
1.3344 +      by (rule absolutely_integrable_spike_set) (auto simp: zero_less_mult_iff empty_imp_negligible)
1.3345 +    have "?DP absolutely_integrable_on {x \<in> S. ?DP x < 0}"
1.3346 +      using LHS by (fast intro!: set_integrable_subset [OF _, of _ S] SN)
1.3347 +    then have aN: "?DP absolutely_integrable_on {x \<in> S. f (g x) < 0}"
1.3348 +      by (rule absolutely_integrable_spike_set) (auto simp: mult_less_0_iff empty_imp_negligible)
1.3349 +    have fN: "f integrable_on {y \<in> T. f y < 0}"
1.3350 +             "integral {y \<in> T. f y < 0} f = integral {x \<in> S. f (g x) < 0} ?DP"
1.3351 +      using "-" [of "integral {x \<in> S. f(g x) < 0} ?DN"] aN
1.3352 +      by (auto simp: set_lebesgue_integral_eq_integral has_integral_iff integrable_neg_iff)
1.3353 +    have faN: "f absolutely_integrable_on {y \<in> T. f y < 0}"
1.3354 +      apply (rule absolutely_integrable_integrable_bound [where g = "\<lambda>x. - f x"])
1.3355 +      using fN by (auto simp: integrable_neg_iff)
1.3356 +    have fP: "f integrable_on {y \<in> T. f y > 0}"
1.3357 +             "integral {y \<in> T. f y > 0} f = integral {x \<in> S. f (g x) > 0} ?DP"
1.3358 +      using "+" [of "integral {x \<in> S. f(g x) > 0} ?DP"] aP
1.3359 +      by (auto simp: set_lebesgue_integral_eq_integral has_integral_iff integrable_neg_iff)
1.3360 +    have faP: "f absolutely_integrable_on {y \<in> T. f y > 0}"
1.3361 +      apply (rule absolutely_integrable_integrable_bound [where g = f])
1.3362 +      using fP by auto
1.3363 +    have fa: "f absolutely_integrable_on ({y \<in> T. f y < 0} \<union> {y \<in> T. f y > 0})"
1.3364 +      by (rule absolutely_integrable_Un [OF faN faP])
1.3365 +    show ?rhs
1.3366 +    proof
1.3367 +      have eq: "((if x \<in> T \<and> f x < 0 \<or> x \<in> T \<and> 0 < f x then 1 else 0) * f x)
1.3368 +              = (if x \<in> T then 1 else 0) * f x" for x
1.3369 +        by auto
1.3370 +      show "f absolutely_integrable_on T"
1.3371 +        using fa by (simp add: indicator_def set_integrable_def eq)
1.3372 +      have [simp]: "{y \<in> T. f y < 0} \<inter> {y \<in> T. 0 < f y} = {}" for T and f :: "(real^'n::_) \<Rightarrow> real"
1.3373 +        by auto
1.3374 +      have "integral T f = integral ({y \<in> T. f y < 0} \<union> {y \<in> T. f y > 0}) f"
1.3375 +        by (intro empty_imp_negligible integral_spike_set) (auto simp: eq)
1.3376 +      also have "\<dots> = integral {y \<in> T. f y < 0} f + integral {y \<in> T. f y > 0} f"
1.3377 +        using fN fP by simp
1.3378 +      also have "\<dots> = integral {x \<in> S. f (g x) < 0} ?DP + integral {x \<in> S. 0 < f (g x)} ?DP"
1.3379 +        by (simp add: fN fP)
1.3380 +      also have "\<dots> = integral ({x \<in> S. f (g x) < 0} \<union> {x \<in> S. 0 < f (g x)}) ?DP"
1.3381 +        using aP aN by (simp add: set_lebesgue_integral_eq_integral)
1.3382 +      also have "\<dots> = integral S ?DP"
1.3383 +        by (intro empty_imp_negligible integral_spike_set) auto
1.3384 +      also have "\<dots> = b"
1.3385 +        using LHS by simp
1.3386 +      finally show "integral T f = b" .
1.3387 +    qed
1.3388 +  next
1.3389 +    assume RHS: ?rhs
1.3390 +    have eq: "{x. (if x \<in> T then f x else 0) > 0} = {x \<in> T. f x > 0}"
1.3391 +             "{x. (if x \<in> T then f x else 0) < 0} = {x \<in> T. f x < 0}"
1.3392 +      by auto
1.3393 +    have "f integrable_on T"
1.3394 +      using RHS absolutely_integrable_on_def by blast
1.3395 +    then have "(\<lambda>x. if x \<in> T then f x else 0) integrable_on UNIV"
1.3396 +      by (simp add: integrable_restrict_UNIV)
1.3397 +    then have D_borel: "(\<lambda>x. if x \<in> T then f x else 0) \<in> borel_measurable (lebesgue_on UNIV)"
1.3398 +      using integrable_imp_measurable borel_measurable_UNIV_eq by blast
1.3399 +    then have TN: "{x \<in> T. f x < 0} \<in> sets lebesgue"
1.3400 +      unfolding borel_measurable_vimage_halfspace_component_lt
1.3401 +      by (drule_tac x=0 in spec) (auto simp: eq)
1.3402 +    from D_borel have TP: "{x \<in> T. f x > 0} \<in> sets lebesgue"
1.3403 +      unfolding borel_measurable_vimage_halfspace_component_gt
1.3404 +      by (drule_tac x=0 in spec) (auto simp: eq)
1.3405 +    have aint: "f absolutely_integrable_on {y. y \<in> T \<and> 0 < (f y)}"
1.3406 +               "f absolutely_integrable_on {y. y \<in> T \<and> (f y) < 0}"
1.3407 +         and intT: "integral T f = b"
1.3408 +      using set_integrable_subset [of _ T] TP TN RHS
1.3409 +      by blast+
1.3410 +    show ?lhs
1.3411 +    proof
1.3412 +      have fN: "f integrable_on {v \<in> T. f v < 0}"
1.3413 +        using absolutely_integrable_on_def aint by blast
1.3414 +      then have DN: "(?DN has_integral integral {y \<in> T. f y < 0} (\<lambda>x. - f x)) {x \<in> S. f (g x) < 0}"
1.3415 +        using "-" [of "integral {y \<in> T. f y < 0} (\<lambda>x. - f x)"]
1.3416 +        by (simp add: has_integral_neg_iff integrable_integral)
1.3417 +      have aDN: "?DP absolutely_integrable_on {x \<in> S. f (g x) < 0}"
1.3418 +        apply (rule absolutely_integrable_integrable_bound [where g = ?DN])
1.3419 +        using DN hg by (fastforce simp: abs_mult integrable_neg_iff)+
1.3420 +      have fP: "f integrable_on {v \<in> T. f v > 0}"
1.3421 +        using absolutely_integrable_on_def aint by blast
1.3422 +      then have DP: "(?DP has_integral integral {y \<in> T. f y > 0} f) {x \<in> S. f (g x) > 0}"
1.3423 +        using "+" [of "integral {y \<in> T. f y > 0} f"]
1.3424 +        by (simp add: has_integral_neg_iff integrable_integral)
1.3425 +      have aDP: "?DP absolutely_integrable_on {x \<in> S. f (g x) > 0}"
1.3426 +        apply (rule absolutely_integrable_integrable_bound [where g = ?DP])
1.3427 +        using DP hg by (fastforce simp: integrable_neg_iff)+
1.3428 +      have eq: "(if x \<in> S then 1 else 0) * ?DP x = (if x \<in> S \<and> f (g x) < 0 \<or> x \<in> S \<and> f (g x) > 0 then 1 else 0) * ?DP x" for x
1.3429 +        by force
1.3430 +      have "?DP absolutely_integrable_on ({x \<in> S. f (g x) < 0} \<union> {x \<in> S. f (g x) > 0})"
1.3431 +        by (rule absolutely_integrable_Un [OF aDN aDP])
1.3432 +      then show I: "?DP absolutely_integrable_on S"
1.3433 +        by (simp add: indicator_def eq set_integrable_def)
1.3434 +      have [simp]: "{y \<in> S. f y < 0} \<inter> {y \<in> S. 0 < f y} = {}" for S and f :: "(real^'n::_) \<Rightarrow> real"
1.3435 +        by auto
1.3436 +      have "integral S ?DP = integral ({x \<in> S. f (g x) < 0} \<union> {x \<in> S. f (g x) > 0}) ?DP"
1.3437 +        by (intro empty_imp_negligible integral_spike_set) auto
1.3438 +      also have "\<dots> = integral {x \<in> S. f (g x) < 0} ?DP + integral {x \<in> S. 0 < f (g x)} ?DP"
1.3440 +      also have "\<dots> = - integral {y \<in> T. f y < 0} (\<lambda>x. - f x) + integral {y \<in> T. f y > 0} f"
1.3441 +        using DN DP by (auto simp: has_integral_iff)
1.3442 +      also have "\<dots> = integral ({x \<in> T. f x < 0} \<union> {x \<in> T. 0 < f x}) f"
1.3443 +        by (simp add: fN fP)
1.3444 +      also have "\<dots> = integral T f"
1.3445 +        by (intro empty_imp_negligible integral_spike_set) auto
1.3446 +      also have "\<dots> = b"
1.3447 +        using intT by simp
1.3448 +      finally show "integral S ?DP = b" .
1.3449 +    qed
1.3450 +  qed
1.3451 +qed
1.3452 +
1.3453 +
1.3454 +lemma cv_inv_version3:
1.3455 +  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
1.3456 +  assumes der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
1.3457 +    and der_h: "\<And>y. y \<in> T \<Longrightarrow> (h has_derivative h' y) (at y within T)"
1.3458 +    and hg: "\<And>x. x \<in> S \<Longrightarrow> g x \<in> T \<and> h(g x) = x"
1.3459 +    and gh: "\<And>y. y \<in> T \<Longrightarrow> h y \<in> S \<and> g(h y) = y"
1.3460 +    and id: "\<And>y. y \<in> T \<Longrightarrow> h' y \<circ> g'(h y) = id"
1.3461 +  shows "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S \<and>
1.3462 +             integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) = b
1.3463 +         \<longleftrightarrow> f absolutely_integrable_on T \<and> integral T f = b"
1.3464 +proof -
1.3465 +  let ?D = "\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)"
1.3466 +  have "((\<lambda>x. \<bar>det (matrix (g' x))\<bar> * f(g x) \$ i) absolutely_integrable_on S \<and> integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> * (f(g x) \$ i)) = b \$ i) \<longleftrightarrow>
1.3467 +        ((\<lambda>x. f x \$ i) absolutely_integrable_on T \<and> integral T (\<lambda>x. f x \$ i) = b \$ i)" for i
1.3468 +    by (rule cov_invertible_real [OF der_g der_h hg gh id])
1.3469 +  then have "?D absolutely_integrable_on S \<and> (?D has_integral b) S \<longleftrightarrow>
1.3470 +        f absolutely_integrable_on T \<and> (f has_integral b) T"
1.3471 +    unfolding absolutely_integrable_componentwise_iff [where f=f] has_integral_componentwise_iff [of f]
1.3472 +              absolutely_integrable_componentwise_iff [where f="?D"] has_integral_componentwise_iff [of ?D]
1.3473 +    by (auto simp: all_conj_distrib Basis_vec_def cart_eq_inner_axis [symmetric]
1.3474 +           has_integral_iff set_lebesgue_integral_eq_integral)
1.3475 +  then show ?thesis
1.3476 +    using absolutely_integrable_on_def by blast
1.3477 +qed
1.3478 +
1.3479 +
1.3480 +lemma cv_inv_version4:
1.3481 +  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
1.3482 +  assumes der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S) \<and> invertible(matrix(g' x))"
1.3483 +    and hg: "\<And>x. x \<in> S \<Longrightarrow> continuous_on (g ` S) h \<and> h(g x) = x"
1.3484 +  shows "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S \<and>
1.3485 +             integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) = b
1.3486 +         \<longleftrightarrow> f absolutely_integrable_on (g ` S) \<and> integral (g ` S) f = b"
1.3487 +proof -
1.3488 +  have "\<forall>x. \<exists>h'. x \<in> S
1.3489 +           \<longrightarrow> (g has_derivative g' x) (at x within S) \<and> linear h' \<and> g' x \<circ> h' = id \<and> h' \<circ> g' x = id"
1.3490 +    using der_g matrix_invertible has_derivative_linear by blast
1.3491 +  then obtain h' where h':
1.3492 +    "\<And>x. x \<in> S
1.3493 +           \<Longrightarrow> (g has_derivative g' x) (at x within S) \<and>
1.3494 +               linear (h' x) \<and> g' x \<circ> (h' x) = id \<and> (h' x) \<circ> g' x = id"
1.3495 +    by metis
1.3496 +  show ?thesis
1.3497 +  proof (rule cv_inv_version3)
1.3498 +    show "\<And>y. y \<in> g ` S \<Longrightarrow> (h has_derivative h' (h y)) (at y within g ` S)"
1.3499 +      using h' hg
1.3500 +      by (force simp: continuous_on_eq_continuous_within intro!: has_derivative_inverse_within)
1.3501 +  qed (use h' hg in auto)
1.3502 +qed
1.3503 +
1.3504 +
1.3505 +proposition has_absolute_integral_change_of_variables_invertible:
1.3506 +  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
1.3507 +  assumes der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
1.3508 +      and hg: "\<And>x. x \<in> S \<Longrightarrow> h(g x) = x"
1.3509 +      and conth: "continuous_on (g ` S) h"
1.3510 +  shows "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S \<and> integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) = b \<longleftrightarrow>
1.3511 +         f absolutely_integrable_on (g ` S) \<and> integral (g ` S) f = b"
1.3512 +    (is "?lhs = ?rhs")
1.3513 +proof -
1.3514 +  let ?S = "{x \<in> S. invertible (matrix (g' x))}" and ?D = "\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)"
1.3515 +  have *: "?D absolutely_integrable_on ?S \<and> integral ?S ?D = b
1.3516 +           \<longleftrightarrow> f absolutely_integrable_on (g ` ?S) \<and> integral (g ` ?S) f = b"
1.3517 +  proof (rule cv_inv_version4)
1.3518 +    show "(g has_derivative g' x) (at x within ?S) \<and> invertible (matrix (g' x))"
1.3519 +      if "x \<in> ?S" for x
1.3520 +      using der_g that has_derivative_within_subset that by fastforce
1.3521 +    show "continuous_on (g ` ?S) h \<and> h (g x) = x"
1.3522 +      if "x \<in> ?S" for x
1.3523 +      using that continuous_on_subset [OF conth]  by (simp add: hg image_mono)
1.3524 +  qed
1.3525 +  have "(g has_derivative g' x) (at x within {x \<in> S. rank (matrix (g' x)) < CARD('m)})" if "x \<in> S" for x
1.3526 +    by (metis (no_types, lifting) der_g has_derivative_within_subset mem_Collect_eq subsetI that)
1.3527 +  then have "negligible (g ` {x \<in> S. \<not> invertible (matrix (g' x))})"
1.3528 +    by (auto simp: invertible_det_nz det_eq_0_rank intro: baby_Sard)
1.3529 +  then have neg: "negligible {x \<in> g ` S. x \<notin> g ` ?S \<and> f x \<noteq> 0}"
1.3530 +    by (auto intro: negligible_subset)
1.3531 +  have [simp]: "{x \<in> g ` ?S. x \<notin> g ` S \<and> f x \<noteq> 0} = {}"
1.3532 +    by auto
1.3533 +  have "?D absolutely_integrable_on ?S \<and> integral ?S ?D = b
1.3534 +    \<longleftrightarrow> ?D absolutely_integrable_on S \<and> integral S ?D = b"
1.3535 +    apply (intro conj_cong absolutely_integrable_spike_set_eq)
1.3536 +      apply(auto simp: integral_spike_set invertible_det_nz empty_imp_negligible neg)
1.3537 +    done
1.3538 +  moreover
1.3539 +  have "f absolutely_integrable_on (g ` ?S) \<and> integral (g ` ?S) f = b
1.3540 +    \<longleftrightarrow> f absolutely_integrable_on (g ` S) \<and> integral (g ` S) f = b"
1.3541 +    by (auto intro!: conj_cong absolutely_integrable_spike_set_eq integral_spike_set neg)
1.3542 +  ultimately
1.3543 +  show ?thesis
1.3544 +    using "*" by blast
1.3545 +qed
1.3546 +
1.3547 +
1.3548 +
1.3549 +lemma has_absolute_integral_change_of_variables_compact:
1.3550 +  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
1.3551 +  assumes "compact S"
1.3552 +      and der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
1.3553 +      and inj: "inj_on g S"
1.3554 +  shows "((\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S \<and>
1.3555 +             integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) = b
1.3556 +      \<longleftrightarrow> f absolutely_integrable_on (g ` S) \<and> integral (g ` S) f = b)"
1.3557 +proof -
1.3558 +  obtain h where hg: "\<And>x. x \<in> S \<Longrightarrow> h(g x) = x"
1.3559 +    using inj by (metis the_inv_into_f_f)
1.3560 +  have conth: "continuous_on (g ` S) h"
1.3561 +    by (metis \<open>compact S\<close> continuous_on_inv der_g has_derivative_continuous_on hg)
1.3562 +  show ?thesis
1.3563 +    by (rule has_absolute_integral_change_of_variables_invertible [OF der_g hg conth])
1.3564 +qed
1.3565 +
1.3566 +
1.3567 +lemma has_absolute_integral_change_of_variables_compact_family:
1.3568 +  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
1.3569 +  assumes compact: "\<And>n::nat. compact (F n)"
1.3570 +      and der_g: "\<And>x. x \<in> (\<Union>n. F n) \<Longrightarrow> (g has_derivative g' x) (at x within (\<Union>n. F n))"
1.3571 +      and inj: "inj_on g (\<Union>n. F n)"
1.3572 +  shows "((\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on (\<Union>n. F n) \<and>
1.3573 +             integral (\<Union>n. F n) (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) = b
1.3574 +      \<longleftrightarrow> f absolutely_integrable_on (g ` (\<Union>n. F n)) \<and> integral (g ` (\<Union>n. F n)) f = b)"
1.3575 +proof -
1.3576 +  let ?D = "\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f (g x)"
1.3577 +  let ?U = "\<lambda>n. \<Union>m\<le>n. F m"
1.3578 +  let ?lift = "vec::real\<Rightarrow>real^1"
1.3579 +  have F_leb: "F m \<in> sets lebesgue" for m
1.3580 +    by (simp add: compact borel_compact)
1.3581 +  have iff: "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f (g x)) absolutely_integrable_on (?U n) \<and>
1.3582 +             integral (?U n) (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f (g x)) = b
1.3583 +         \<longleftrightarrow> f absolutely_integrable_on (g ` (?U n)) \<and> integral (g ` (?U n)) f = b" for n b and f :: "real^'m::_ \<Rightarrow> real^'k"
1.3584 +  proof (rule has_absolute_integral_change_of_variables_compact)
1.3585 +    show "compact (?U n)"
1.3586 +      by (simp add: compact compact_UN)
1.3587 +    show "(g has_derivative g' x) (at x within (?U n))"
1.3588 +      if "x \<in> ?U n" for x
1.3589 +      using that by (blast intro!: has_derivative_within_subset [OF der_g])
1.3590 +    show "inj_on g (?U n)"
1.3591 +      using inj by (auto simp: inj_on_def)
1.3592 +  qed
1.3593 +  show ?thesis
1.3594 +    unfolding image_UN
1.3595 +  proof safe
1.3596 +    assume DS: "?D absolutely_integrable_on (\<Union>n. F n)"
1.3597 +      and b: "b = integral (\<Union>n. F n) ?D"
1.3598 +    have DU: "\<And>n. ?D absolutely_integrable_on (?U n)"
1.3599 +             "(\<lambda>n. integral (?U n) ?D) \<longlonglongrightarrow> integral (\<Union>n. F n) ?D"
1.3600 +      using integral_countable_UN [OF DS F_leb] by auto
1.3601 +    with iff have fag: "f absolutely_integrable_on g ` (?U n)"
1.3602 +      and fg_int: "integral (\<Union>m\<le>n. g ` F m) f = integral (?U n) ?D" for n
1.3603 +      by (auto simp: image_UN)
1.3604 +    let ?h = "\<lambda>x. if x \<in> (\<Union>m. g ` F m) then norm(f x) else 0"
1.3605 +    have "(\<lambda>x. if x \<in> (\<Union>m. g ` F m) then f x else 0) absolutely_integrable_on UNIV"
1.3606 +    proof (rule dominated_convergence_absolutely_integrable)
1.3607 +      show "(\<lambda>x. if x \<in> (\<Union>m\<le>k. g ` F m) then f x else 0) absolutely_integrable_on UNIV" for k
1.3608 +        unfolding absolutely_integrable_restrict_UNIV
1.3609 +        using fag by (simp add: image_UN)
1.3610 +      let ?nf = "\<lambda>n x. if x \<in> (\<Union>m\<le>n. g ` F m) then norm(f x) else 0"
1.3611 +      show "?h integrable_on UNIV"
1.3612 +      proof (rule monotone_convergence_increasing [THEN conjunct1])
1.3613 +        show "?nf k integrable_on UNIV" for k
1.3614 +          using fag
1.3615 +          unfolding integrable_restrict_UNIV absolutely_integrable_on_def by (simp add: image_UN)
1.3616 +        { fix n
1.3617 +          have "(norm \<circ> ?D) absolutely_integrable_on ?U n"
1.3618 +            by (intro absolutely_integrable_norm DU)
1.3619 +          then have "integral (g ` ?U n) (norm \<circ> f) = integral (?U n) (norm \<circ> ?D)"
1.3620 +            using iff [of n "vec \<circ> norm \<circ> f" "integral (?U n) (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R (?lift \<circ> norm \<circ> f) (g x))"]
1.3621 +            unfolding absolutely_integrable_on_1_iff integral_on_1_eq by (auto simp: o_def)
1.3622 +        }
1.3623 +        moreover have "bounded (range (\<lambda>k. integral (?U k) (norm \<circ> ?D)))"
1.3624 +          unfolding bounded_iff
1.3625 +        proof (rule exI, clarify)
1.3626 +          fix k
1.3627 +          show "norm (integral (?U k) (norm \<circ> ?D)) \<le> integral (\<Union>n. F n) (norm \<circ> ?D)"
1.3628 +            unfolding integral_restrict_UNIV [of _ "norm \<circ> ?D", symmetric]
1.3629 +          proof (rule integral_norm_bound_integral)
1.3630 +            show "(\<lambda>x. if x \<in> UNION {..k} F then (norm \<circ> ?D) x else 0) integrable_on UNIV"
1.3631 +              "(\<lambda>x. if x \<in> (\<Union>n. F n) then (norm \<circ> ?D) x else 0) integrable_on UNIV"
1.3632 +              using DU(1) DS
1.3633 +              unfolding absolutely_integrable_on_def o_def integrable_restrict_UNIV by auto
1.3634 +          qed auto
1.3635 +        qed
1.3636 +        ultimately show "bounded (range (\<lambda>k. integral UNIV (?nf k)))"
1.3637 +          by (simp add: integral_restrict_UNIV image_UN [symmetric] o_def)
1.3638 +      next
1.3639 +        show "(\<lambda>k. if x \<in> (\<Union>m\<le>k. g ` F m) then norm (f x) else 0)
1.3640 +              \<longlonglongrightarrow> (if x \<in> (\<Union>m. g ` F m) then norm (f x) else 0)" for x
1.3641 +          by (force intro: Lim_eventually eventually_sequentiallyI)
1.3642 +      qed auto
1.3643 +    next
1.3644 +      show "(\<lambda>k. if x \<in> (\<Union>m\<le>k. g ` F m) then f x else 0)
1.3645 +            \<longlonglongrightarrow> (if x \<in> (\<Union>m. g ` F m) then f x else 0)" for x
1.3646 +      proof clarsimp
1.3647 +        fix m y
1.3648 +        assume "y \<in> F m"
1.3649 +        show "(\<lambda>k. if \<exists>x\<in>{..k}. g y \<in> g ` F x then f (g y) else 0) \<longlonglongrightarrow> f (g y)"
1.3650 +          using \<open>y \<in> F m\<close> by (force intro: Lim_eventually eventually_sequentiallyI [of m])
1.3651 +      qed
1.3652 +    qed auto
1.3653 +    then show fai: "f absolutely_integrable_on (\<Union>m. g ` F m)"
1.3654 +      using absolutely_integrable_restrict_UNIV by blast
1.3655 +    show "integral ((\<Union>x. g ` F x)) f = integral (\<Union>n. F n) ?D"
1.3656 +    proof (rule LIMSEQ_unique)
1.3657 +      show "(\<lambda>n. integral (?U n) ?D) \<longlonglongrightarrow> integral (\<Union>x. g ` F x) f"
1.3658 +        unfolding fg_int [symmetric]
1.3659 +      proof (rule integral_countable_UN [OF fai])
1.3660 +        show "g ` F m \<in> sets lebesgue" for m
1.3661 +        proof (rule differentiable_image_in_sets_lebesgue [OF F_leb])
1.3662 +          show "g differentiable_on F m"
1.3663 +            by (meson der_g differentiableI UnionI differentiable_on_def differentiable_on_subset rangeI subsetI)
1.3664 +        qed auto
1.3665 +      qed
1.3666 +    next
1.3667 +      show "(\<lambda>n. integral (?U n) ?D) \<longlonglongrightarrow> integral (\<Union>n. F n) ?D"
1.3668 +        by (rule DU)
1.3669 +    qed
1.3670 +  next
1.3671 +    assume fs: "f absolutely_integrable_on (\<Union>x. g ` F x)"
1.3672 +      and b: "b = integral ((\<Union>x. g ` F x)) f"
1.3673 +    have gF_leb: "g ` F m \<in> sets lebesgue" for m
1.3674 +    proof (rule differentiable_image_in_sets_lebesgue [OF F_leb])
1.3675 +      show "g differentiable_on F m"
1.3676 +        using der_g unfolding differentiable_def differentiable_on_def
1.3677 +        by (meson Sup_upper UNIV_I UnionI has_derivative_within_subset image_eqI)
1.3678 +    qed auto
1.3679 +    have fgU: "\<And>n. f absolutely_integrable_on (\<Union>m\<le>n. g ` F m)"
1.3680 +      "(\<lambda>n. integral (\<Union>m\<le>n. g ` F m) f) \<longlonglongrightarrow> integral (\<Union>m. g ` F m) f"
1.3681 +      using integral_countable_UN [OF fs gF_leb] by auto
1.3682 +    with iff have DUn: "?D absolutely_integrable_on ?U n"
1.3683 +      and D_int: "integral (?U n) ?D = integral (\<Union>m\<le>n. g ` F m) f" for n
1.3684 +      by (auto simp: image_UN)
1.3685 +    let ?h = "\<lambda>x. if x \<in> (\<Union>n. F n) then norm(?D x) else 0"
1.3686 +    have "(\<lambda>x. if x \<in> (\<Union>n. F n) then ?D x else 0) absolutely_integrable_on UNIV"
1.3687 +    proof (rule dominated_convergence_absolutely_integrable)
1.3688 +      show "(\<lambda>x. if x \<in> ?U k then ?D x else 0) absolutely_integrable_on UNIV" for k
1.3689 +        unfolding absolutely_integrable_restrict_UNIV using DUn by simp
1.3690 +      let ?nD = "\<lambda>n x. if x \<in> ?U n then norm(?D x) else 0"
1.3691 +      show "?h integrable_on UNIV"
1.3692 +      proof (rule monotone_convergence_increasing [THEN conjunct1])
1.3693 +        show "?nD k integrable_on UNIV" for k
1.3694 +          using DUn
1.3695 +          unfolding integrable_restrict_UNIV absolutely_integrable_on_def by (simp add: image_UN)
1.3696 +        { fix n::nat
1.3697 +          have "(norm \<circ> f) absolutely_integrable_on (\<Union>m\<le>n. g ` F m)"
1.3698 +            apply (rule absolutely_integrable_norm)
1.3699 +            using fgU by blast
1.3700 +          then have "integral (?U n) (norm \<circ> ?D) = integral (g ` ?U n) (norm \<circ> f)"
1.3701 +            using iff [of n "?lift \<circ> norm \<circ> f" "integral (g ` ?U n) (?lift \<circ> norm \<circ> f)"]
1.3702 +            unfolding absolutely_integrable_on_1_iff integral_on_1_eq image_UN by (auto simp: o_def)
1.3703 +        }
1.3704 +        moreover have "bounded (range (\<lambda>k. integral (g ` ?U k) (norm \<circ> f)))"
1.3705 +          unfolding bounded_iff
1.3706 +        proof (rule exI, clarify)
1.3707 +          fix k
1.3708 +          show "norm (integral (g ` ?U k) (norm \<circ> f)) \<le> integral (g ` (\<Union>n. F n)) (norm \<circ> f)"
1.3709 +            unfolding integral_restrict_UNIV [of _ "norm \<circ> f", symmetric]
1.3710 +          proof (rule integral_norm_bound_integral)
1.3711 +            show "(\<lambda>x. if x \<in> g ` ?U k then (norm \<circ> f) x else 0) integrable_on UNIV"
1.3712 +                 "(\<lambda>x. if x \<in> g ` (\<Union>n. F n) then (norm \<circ> f) x else 0) integrable_on UNIV"
1.3713 +              using fgU fs
1.3714 +              unfolding absolutely_integrable_on_def o_def integrable_restrict_UNIV
1.3715 +              by (auto simp: image_UN)
1.3716 +          qed auto
1.3717 +        qed
1.3718 +        ultimately show "bounded (range (\<lambda>k. integral UNIV (?nD k)))"
1.3719 +          unfolding integral_restrict_UNIV image_UN [symmetric] o_def by simp
1.3720 +      next
1.3721 +        show "(\<lambda>k. if x \<in> ?U k then norm (?D x) else 0) \<longlonglongrightarrow> (if x \<in> (\<Union>n. F n) then norm (?D x) else 0)" for x
1.3722 +          by (force intro: Lim_eventually eventually_sequentiallyI)
1.3723 +      qed auto
1.3724 +    next
1.3725 +      show "(\<lambda>k. if x \<in> ?U k then ?D x else 0) \<longlonglongrightarrow> (if x \<in> (\<Union>n. F n) then ?D x else 0)" for x
1.3726 +      proof clarsimp
1.3727 +        fix n
1.3728 +        assume "x \<in> F n"
1.3729 +        show "(\<lambda>m. if \<exists>j\<in>{..m}. x \<in> F j then ?D x else 0) \<longlonglongrightarrow> ?D x"
1.3730 +          using \<open>x \<in> F n\<close> by (auto intro!: Lim_eventually eventually_sequentiallyI [of n])
1.3731 +      qed
1.3732 +    qed auto
1.3733 +    then show Dai: "?D absolutely_integrable_on (\<Union>n. F n)"
1.3734 +      unfolding absolutely_integrable_restrict_UNIV by simp
1.3735 +    show "integral (\<Union>n. F n) ?D = integral ((\<Union>x. g ` F x)) f"
1.3736 +    proof (rule LIMSEQ_unique)
1.3737 +      show "(\<lambda>n. integral (\<Union>m\<le>n. g ` F m) f) \<longlonglongrightarrow> integral (\<Union>x. g ` F x) f"
1.3738 +        by (rule fgU)
1.3739 +      show "(\<lambda>n. integral (\<Union>m\<le>n. g ` F m) f) \<longlonglongrightarrow> integral (\<Union>n. F n) ?D"
1.3740 +        unfolding D_int [symmetric] by (rule integral_countable_UN [OF Dai F_leb])
1.3741 +    qed
1.3742 +  qed
1.3743 +qed
1.3744 +
1.3745 +
1.3746 +proposition has_absolute_integral_change_of_variables:
1.3747 +  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
1.3748 +  assumes S: "S \<in> sets lebesgue"
1.3749 +    and der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
1.3750 +    and inj: "inj_on g S"
1.3751 +  shows "(\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S \<and>
1.3752 +           integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) = b
1.3753 +     \<longleftrightarrow> f absolutely_integrable_on (g ` S) \<and> integral (g ` S) f = b"
1.3754 +proof -
1.3755 +  obtain C N where "fsigma C" "negligible N" and CNS: "C \<union> N = S" and "disjnt C N"
1.3756 +    using lebesgue_set_almost_fsigma [OF S] .
1.3757 +  then obtain F :: "nat \<Rightarrow> (real^'m::_) set"
1.3758 +    where F: "range F \<subseteq> Collect compact" and Ceq: "C = Union(range F)"
1.3759 +    using fsigma_Union_compact by metis
1.3760 +  let ?D = "\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f (g x)"
1.3761 +  have "?D absolutely_integrable_on C \<and> integral C ?D = b
1.3762 +    \<longleftrightarrow> f absolutely_integrable_on (g ` C) \<and> integral (g ` C) f = b"
1.3763 +    unfolding Ceq
1.3764 +  proof (rule has_absolute_integral_change_of_variables_compact_family)
1.3765 +    fix n x
1.3766 +    assume "x \<in> UNION UNIV F"
1.3767 +    then show "(g has_derivative g' x) (at x within UNION UNIV F)"
1.3768 +      using Ceq \<open>C \<union> N = S\<close> der_g has_derivative_within_subset by blast
1.3769 +  next
1.3770 +    have "UNION UNIV F \<subseteq> S"
1.3771 +      using Ceq \<open>C \<union> N = S\<close> by blast
1.3772 +    then show "inj_on g (UNION UNIV F)"
1.3773 +      using inj by (meson inj_on_subset)
1.3774 +  qed (use F in auto)
1.3775 +  moreover
1.3776 +  have "?D absolutely_integrable_on C \<and> integral C ?D = b
1.3777 +    \<longleftrightarrow> ?D absolutely_integrable_on S \<and> integral S ?D = b"
1.3778 +  proof (rule conj_cong)
1.3779 +    have neg: "negligible {x \<in> C - S. ?D x \<noteq> 0}" "negligible {x \<in> S - C. ?D x \<noteq> 0}"
1.3780 +      using CNS by (blast intro: negligible_subset [OF \<open>negligible N\<close>])+
1.3781 +    then show "(?D absolutely_integrable_on C) = (?D absolutely_integrable_on S)"
1.3782 +      by (rule absolutely_integrable_spike_set_eq)
1.3783 +    show "(integral C ?D = b) \<longleftrightarrow> (integral S ?D = b)"
1.3784 +      using integral_spike_set [OF neg] by simp
1.3785 +  qed
1.3786 +  moreover
1.3787 +  have "f absolutely_integrable_on (g ` C) \<and> integral (g ` C) f = b
1.3788 +    \<longleftrightarrow> f absolutely_integrable_on (g ` S) \<and> integral (g ` S) f = b"
1.3789 +  proof (rule conj_cong)
1.3790 +    have "g differentiable_on N"
1.3791 +      by (metis CNS der_g differentiable_def differentiable_on_def differentiable_on_subset sup.cobounded2)
1.3792 +    with \<open>negligible N\<close>
1.3793 +    have neg_gN: "negligible (g ` N)"
1.3794 +      by (blast intro: negligible_differentiable_image_negligible)
1.3795 +    have neg: "negligible {x \<in> g ` C - g ` S. f x \<noteq> 0}"
1.3796 +              "negligible {x \<in> g ` S - g ` C. f x \<noteq> 0}"
1.3797 +      using CNS by (blast intro: negligible_subset [OF neg_gN])+
1.3798 +    then show "(f absolutely_integrable_on g ` C) = (f absolutely_integrable_on g ` S)"
1.3799 +      by (rule absolutely_integrable_spike_set_eq)
1.3800 +    show "(integral (g ` C) f = b) \<longleftrightarrow> (integral (g ` S) f = b)"
1.3801 +      using integral_spike_set [OF neg] by simp
1.3802 +  qed
1.3803 +  ultimately show ?thesis
1.3804 +    by simp
1.3805 +qed
1.3806 +
1.3807 +
1.3808 +corollary absolutely_integrable_change_of_variables:
1.3809 +  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
1.3810 +  assumes "S \<in> sets lebesgue"
1.3811 +    and "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
1.3812 +    and "inj_on g S"
1.3813 +  shows "f absolutely_integrable_on (g ` S)
1.3814 +     \<longleftrightarrow> (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S"
1.3815 +  using assms has_absolute_integral_change_of_variables by blast
1.3816 +
1.3817 +corollary integral_change_of_variables:
1.3818 +  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
1.3819 +  assumes S: "S \<in> sets lebesgue"
1.3820 +    and der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_derivative g' x) (at x within S)"
1.3821 +    and inj: "inj_on g S"
1.3822 +    and disj: "(f absolutely_integrable_on (g ` S) \<or>
1.3823 +        (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S)"
1.3824 +  shows "integral (g ` S) f = integral S (\<lambda>x. \<bar>det (matrix (g' x))\<bar> *\<^sub>R f(g x))"
1.3825 +  using has_absolute_integral_change_of_variables [OF S der_g inj] disj
1.3826 +  by blast
1.3827 +
1.3828 +lemma has_absolute_integral_change_of_variables_1:
1.3829 +  fixes f :: "real \<Rightarrow> real^'n::{finite,wellorder}" and g :: "real \<Rightarrow> real"
1.3830 +  assumes S: "S \<in> sets lebesgue"
1.3831 +    and der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_vector_derivative g' x) (at x within S)"
1.3832 +    and inj: "inj_on g S"
1.3833 +  shows "(\<lambda>x. \<bar>g' x\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S \<and>
1.3834 +           integral S (\<lambda>x. \<bar>g' x\<bar> *\<^sub>R f(g x)) = b
1.3835 +     \<longleftrightarrow> f absolutely_integrable_on (g ` S) \<and> integral (g ` S) f = b"
1.3836 +proof -
1.3837 +  let ?lift = "vec :: real \<Rightarrow> real^1"
1.3838 +  let ?drop = "(\<lambda>x::real^1. x \$ 1)"
1.3839 +  have S': "?lift ` S \<in> sets lebesgue"
1.3840 +    by (auto intro: differentiable_image_in_sets_lebesgue [OF S] differentiable_vec)
1.3841 +  have "((\<lambda>x. vec (g (x \$ 1))) has_derivative ( *\<^sub>R) (g' z)) (at (vec z) within ?lift ` S)"
1.3842 +    if "z \<in> S" for z
1.3843 +    using der_g [OF that]
1.3844 +    by (simp add: has_vector_derivative_def has_derivative_vector_1)
1.3845 +  then have der': "\<And>x. x \<in> ?lift ` S \<Longrightarrow>
1.3846 +        (?lift \<circ> g \<circ> ?drop has_derivative ( *\<^sub>R) (g' (?drop x))) (at x within ?lift ` S)"
1.3847 +    by (auto simp: o_def)
1.3848 +  have inj': "inj_on (vec \<circ> g \<circ> ?drop) (vec ` S)"
1.3849 +    using inj by (simp add: inj_on_def)
1.3850 +  let ?fg = "\<lambda>x. \<bar>g' x\<bar> *\<^sub>R f(g x)"
1.3851 +  have "((\<lambda>x. ?fg x \$ i) absolutely_integrable_on S \<and> ((\<lambda>x. ?fg x \$ i) has_integral b \$ i) S
1.3852 +    \<longleftrightarrow> (\<lambda>x. f x \$ i) absolutely_integrable_on g ` S \<and> ((\<lambda>x. f x \$ i) has_integral b \$ i) (g ` S))" for i
1.3853 +    using has_absolute_integral_change_of_variables [OF S' der' inj', of "\<lambda>x. ?lift(f (?drop x) \$ i)" "?lift (b\$i)"]
1.3854 +    unfolding integrable_on_1_iff integral_on_1_eq absolutely_integrable_on_1_iff absolutely_integrable_drop absolutely_integrable_on_def
1.3855 +    by (auto simp: image_comp o_def integral_vec1_eq has_integral_iff)
1.3856 +  then have "?fg absolutely_integrable_on S \<and> (?fg has_integral b) S
1.3857 +         \<longleftrightarrow> f absolutely_integrable_on (g ` S) \<and> (f has_integral b) (g ` S)"
1.3858 +    unfolding has_integral_componentwise_iff [where y=b]
1.3859 +           absolutely_integrable_componentwise_iff [where f=f]
1.3860 +           absolutely_integrable_componentwise_iff [where f = ?fg]
1.3861 +    by (force simp: Basis_vec_def cart_eq_inner_axis)
1.3862 +  then show ?thesis
1.3863 +    using absolutely_integrable_on_def by blast
1.3864 +qed
1.3865 +
1.3866 +
1.3867 +corollary absolutely_integrable_change_of_variables_1:
1.3868 +  fixes f :: "real \<Rightarrow> real^'n::{finite,wellorder}" and g :: "real \<Rightarrow> real"
1.3869 +  assumes S: "S \<in> sets lebesgue"
1.3870 +    and der_g: "\<And>x. x \<in> S \<Longrightarrow> (g has_vector_derivative g' x) (at x within S)"
1.3871 +    and inj: "inj_on g S"
1.3872 +  shows "(f absolutely_integrable_on g ` S \<longleftrightarrow>
1.3873 +             (\<lambda>x. \<bar>g' x\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S)"
1.3874 +  using has_absolute_integral_change_of_variables_1 [OF assms] by auto
1.3875 +
1.3876 +
1.3877 +subsection\<open>Change of variables for integrals: special case of linear function\<close>
1.3878 +
1.3879 +lemma has_absolute_integral_change_of_variables_linear:
1.3880 +  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
1.3881 +  assumes "linear g"
1.3882 +  shows "(\<lambda>x. \<bar>det (matrix g)\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S \<and>
1.3883 +           integral S (\<lambda>x. \<bar>det (matrix g)\<bar> *\<^sub>R f(g x)) = b
1.3884 +     \<longleftrightarrow> f absolutely_integrable_on (g ` S) \<and> integral (g ` S) f = b"
1.3885 +proof (cases "det(matrix g) = 0")
1.3886 +  case True
1.3887 +  then have "negligible(g ` S)"
1.3888 +    using assms det_nz_iff_inj negligible_linear_singular_image by blast
1.3889 +  with True show ?thesis
1.3890 +    by (auto simp: absolutely_integrable_on_def integrable_negligible integral_negligible)
1.3891 +next
1.3892 +  case False
1.3893 +  then obtain h where h: "\<And>x. x \<in> S \<Longrightarrow> h (g x) = x" "linear h"
1.3894 +    using assms det_nz_iff_inj linear_injective_isomorphism by blast
1.3895 +  show ?thesis
1.3896 +  proof (rule has_absolute_integral_change_of_variables_invertible)
1.3897 +    show "(g has_derivative g) (at x within S)" for x
1.3898 +      by (simp add: assms linear_imp_has_derivative)
1.3899 +    show "continuous_on (g ` S) h"
1.3900 +      using continuous_on_eq_continuous_within has_derivative_continuous linear_imp_has_derivative h by blast
1.3901 +  qed (use h in auto)
1.3902 +qed
1.3903 +
1.3904 +lemma absolutely_integrable_change_of_variables_linear:
1.3905 +  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
1.3906 +  assumes "linear g"
1.3907 +  shows "(\<lambda>x. \<bar>det (matrix g)\<bar> *\<^sub>R f(g x)) absolutely_integrable_on S
1.3908 +     \<longleftrightarrow> f absolutely_integrable_on (g ` S)"
1.3909 +  using assms has_absolute_integral_change_of_variables_linear by blast
1.3910 +
1.3911 +lemma absolutely_integrable_on_linear_image:
1.3912 +  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
1.3913 +  assumes "linear g"
1.3914 +  shows "f absolutely_integrable_on (g ` S)
1.3915 +     \<longleftrightarrow> (f \<circ> g) absolutely_integrable_on S \<or> det(matrix g) = 0"
1.3916 +  unfolding assms absolutely_integrable_change_of_variables_linear [OF assms, symmetric] absolutely_integrable_on_scaleR_iff
1.3917 +  by (auto simp: set_integrable_def)
1.3918 +
1.3919 +lemma integral_change_of_variables_linear:
1.3920 +  fixes f :: "real^'m::{finite,wellorder} \<Rightarrow> real^'n" and g :: "real^'m::_ \<Rightarrow> real^'m::_"
1.3921 +  assumes "linear g"
1.3922 +      and "f absolutely_integrable_on (g ` S) \<or> (f \<circ> g) absolutely_integrable_on S"
1.3923 +    shows "integral (g ` S) f = \<bar>det (matrix g)\<bar> *\<^sub>R integral S (f \<circ> g)"
1.3924 +proof -
1.3925 +  have "((\<lambda>x. \<bar>det (matrix g)\<bar> *\<^sub>R f (g x)) absolutely_integrable_on S) \<or> (f absolutely_integrable_on g ` S)"
1.3926 +    using absolutely_integrable_on_linear_image assms by blast
1.3927 +  moreover
1.3928 +  have ?thesis if "((\<lambda>x. \<bar>det (matrix g)\<bar> *\<^sub>R f (g x)) absolutely_integrable_on S)" "(f absolutely_integrable_on g ` S)"
1.3929 +    using has_absolute_integral_change_of_variables_linear [OF \<open>linear g\<close>] that
1.3930 +    by (auto simp: o_def)
1.3931 +  ultimately show ?thesis
1.3932 +    using absolutely_integrable_change_of_variables_linear [OF \<open>linear g\<close>]
1.3933 +    by blast
1.3934 +qed
1.3935 +
1.3936 +subsection\<open>Change of variable for measure\<close>
1.3937 +
1.3938 +lemma has_measure_differentiable_image:
1.3939 +  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
1.3940 +  assumes "S \<in> sets lebesgue"
1.3941 +      and "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
1.3942 +      and "inj_on f S"
1.3943 +  shows "f ` S \<in> lmeasurable \<and> measure lebesgue (f ` S) = m
1.3944 +     \<longleftrightarrow> ((\<lambda>x. \<bar>det (matrix (f' x))\<bar>) has_integral m) S"
1.3945 +  using has_absolute_integral_change_of_variables [OF assms, of "\<lambda>x. (1::real^1)" "vec m"]
1.3946 +  unfolding absolutely_integrable_on_1_iff integral_on_1_eq integrable_on_1_iff absolutely_integrable_on_def
1.3947 +  by (auto simp: has_integral_iff lmeasurable_iff_integrable_on lmeasure_integral)
1.3948 +
1.3949 +lemma measurable_differentiable_image_eq:
1.3950 +  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
1.3951 +  assumes "S \<in> sets lebesgue"
1.3952 +      and "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
1.3953 +      and "inj_on f S"
1.3954 +  shows "f ` S \<in> lmeasurable \<longleftrightarrow> (\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on S"
1.3955 +  using has_measure_differentiable_image [OF assms]
1.3956 +  by blast
1.3957 +
1.3958 +lemma measurable_differentiable_image_alt:
1.3959 +  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
1.3960 +  assumes "S \<in> sets lebesgue"
1.3961 +    and "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
1.3962 +    and "inj_on f S"
1.3963 +  shows "f ` S \<in> lmeasurable \<longleftrightarrow> (\<lambda>x. \<bar>det (matrix (f' x))\<bar>) absolutely_integrable_on S"
1.3964 +  using measurable_differentiable_image_eq [OF assms]
1.3965 +  by (simp only: absolutely_integrable_on_iff_nonneg)
1.3966 +
1.3967 +lemma measure_differentiable_image_eq:
1.3968 +  fixes f :: "real^'n::{finite,wellorder} \<Rightarrow> real^'n::_"
1.3969 +  assumes S: "S \<in> sets lebesgue"
1.3970 +    and der_f: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
1.3971 +    and inj: "inj_on f S"
1.3972 +    and intS: "(\<lambda>x. \<bar>det (matrix (f' x))\<bar>) integrable_on S"
1.3973 +  shows "measure lebesgue (f ` S) = integral S (\<lambda>x. \<bar>det (matrix (f' x))\<bar>)"
1.3974 +  using measurable_differentiable_image_eq [OF S der_f inj]
1.3975 +        assms has_measure_differentiable_image by blast
1.3976 +
1.3977 +end
```
```     2.1 --- a/src/HOL/Analysis/Equivalence_Lebesgue_Henstock_Integration.thy	Tue Apr 17 18:04:49 2018 +0100
2.2 +++ b/src/HOL/Analysis/Equivalence_Lebesgue_Henstock_Integration.thy	Tue Apr 17 22:35:48 2018 +0100
2.3 @@ -1550,14 +1550,15 @@
2.4    assume "0 < e"
2.5    have "S \<in> lmeasurable"
2.6      using \<open>negligible S\<close> by (simp add: negligible_iff_null_sets fmeasurableI_null_sets)
2.7 +  then have "S \<in> sets lebesgue"
2.8 +    by blast
2.9    have e22: "0 < e/2 / (2 * B * real DIM('M)) ^ DIM('N)"
2.10      using \<open>0 < e\<close> \<open>0 < B\<close> by (simp add: divide_simps)
2.11 -  obtain T
2.12 -    where "open T" "S \<subseteq> T" "T \<in> lmeasurable"
2.13 -      and "measure lebesgue T \<le> measure lebesgue S + e/2 / (2 * B * DIM('M)) ^ DIM('N)"
2.14 -    by (rule lmeasurable_outer_open [OF \<open>S \<in> lmeasurable\<close> e22])
2.15 +  obtain T where "open T" "S \<subseteq> T" "(T - S) \<in> lmeasurable"
2.16 +                 "measure lebesgue (T - S) < e/2 / (2 * B * DIM('M)) ^ DIM('N)"
2.17 +    by (rule lmeasurable_outer_open [OF \<open>S \<in> sets lebesgue\<close> e22])
2.18    then have T: "measure lebesgue T \<le> e/2 / (2 * B * DIM('M)) ^ DIM('N)"
2.19 -    using \<open>negligible S\<close> by (simp add: negligible_iff_null_sets measure_eq_0_null_sets)
2.20 +    using \<open>negligible S\<close> by (simp add: measure_Diff_null_set negligible_iff_null_sets)
2.21    have "\<exists>r. 0 < r \<and> r \<le> 1/2 \<and>
2.22              (x \<in> S \<longrightarrow> (\<forall>y. norm(y - x) < r
2.23                         \<longrightarrow> y \<in> T \<and> (y \<in> S \<longrightarrow> norm(f y - f x) \<le> B * norm(y - x))))"
2.24 @@ -1690,7 +1691,7 @@
2.25          using pairwise_subset [OF pw \<open>\<D>' \<subseteq> \<D>\<close>] unfolding pairwise_def apply force+
2.26          done
2.27        have le_meaT: "measure lebesgue (\<Union>\<D>') \<le> measure lebesgue T"
2.28 -      proof (rule measure_mono_fmeasurable [OF _ _ \<open>T \<in> lmeasurable\<close>])
2.29 +      proof (rule measure_mono_fmeasurable)
2.30          show "(\<Union>\<D>') \<in> sets lebesgue"
2.31            using div lmeasurable_division by auto
2.32          have "\<Union>\<D>' \<subseteq> \<Union>\<D>"
2.33 @@ -1704,7 +1705,9 @@
2.34              by (metis \<open>x \<in> D\<close> Int_iff dist_norm mem_ball norm_minus_commute subsetD RT)
2.35          qed
2.36          finally show "\<Union>\<D>' \<subseteq> T" .
2.37 -      qed
2.38 +        show "T \<in> lmeasurable"
2.39 +          using \<open>S \<in> lmeasurable\<close> \<open>S \<subseteq> T\<close> \<open>T - S \<in> lmeasurable\<close> fmeasurable_Diff_D by blast
2.40 +      qed
2.41        have "sum (measure lebesgue) \<D>' = sum content \<D>'"
2.42          using  \<open>\<D>' \<subseteq> \<D>\<close> cbox by (force intro: sum.cong)
2.43        then have "(2 * B * DIM('M)) ^ DIM('N) * sum (measure lebesgue) \<D>' =
2.44 @@ -3991,4 +3994,705 @@
2.45        (auto simp: emeasure_lborel_box_eq emeasure_lborel_cbox_eq algebra_simps mem_box)
2.46  qed
2.47
2.48 +subsection\<open>Various common equivalent forms of function measurability\<close>
2.49 +
2.50 +lemma indicator_sum_eq:
2.51 +  fixes m::real and f :: "'a \<Rightarrow> real"
2.52 +  assumes "\<bar>m\<bar> \<le> 2 ^ (2*n)" "m/2^n \<le> f x" "f x < (m+1)/2^n" "m \<in> \<int>"
2.53 +  shows   "(\<Sum>k::real | k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n).
2.54 +            k/2^n * indicator {y. k/2^n \<le> f y \<and> f y < (k+1)/2^n} x)  = m/2^n"
2.55 +          (is "sum ?f ?S = _")
2.56 +proof -
2.57 +  have "sum ?f ?S = sum (\<lambda>k. k/2^n * indicator {y. k/2^n \<le> f y \<and> f y < (k+1)/2^n} x) {m}"
2.58 +  proof (rule comm_monoid_add_class.sum.mono_neutral_right)
2.59 +    show "finite ?S"
2.60 +      by (rule finite_abs_int_segment)
2.61 +    show "{m} \<subseteq> {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}"
2.62 +      using assms by auto
2.63 +    show "\<forall>i\<in>{k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)} - {m}. ?f i = 0"
2.64 +      using assms by (auto simp: indicator_def Ints_def abs_le_iff divide_simps)
2.65 +  qed
2.66 +  also have "\<dots> = m/2^n"
2.67 +    using assms by (auto simp: indicator_def not_less)
2.68 +  finally show ?thesis .
2.69 +qed
2.70 +
2.71 +lemma measurable_on_sf_limit_lemma1:
2.72 +  fixes f :: "'a::euclidean_space \<Rightarrow> real"
2.73 +  assumes "\<And>a b. {x \<in> S. a \<le> f x \<and> f x < b} \<in> sets (lebesgue_on S)"
2.74 +  obtains g where "\<And>n. g n \<in> borel_measurable (lebesgue_on S)"
2.75 +                  "\<And>n. finite(range (g n))"
2.76 +                  "\<And>x. (\<lambda>n. g n x) \<longlonglongrightarrow> f x"
2.77 +proof
2.78 +  show "(\<lambda>x. sum (\<lambda>k::real. k/2^n * indicator {y. k/2^n \<le> f y \<and> f y < (k+1)/2^n} x)
2.79 +                 {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}) \<in> borel_measurable (lebesgue_on S)"
2.80 +        (is "?g \<in> _")  for n
2.81 +  proof -
2.82 +    have "\<And>k. \<lbrakk>k \<in> \<int>; \<bar>k\<bar> \<le> 2 ^ (2*n)\<rbrakk>
2.83 +         \<Longrightarrow> Measurable.pred (lebesgue_on S) (\<lambda>x. k / (2^n) \<le> f x \<and> f x < (k+1) / (2^n))"
2.84 +      using assms by (force simp: pred_def space_restrict_space)
2.85 +    then show ?thesis
2.86 +      by (simp add: field_class.field_divide_inverse)
2.87 +  qed
2.88 +  show "finite (range (?g n))" for n
2.89 +  proof -
2.90 +    have "range (?g n) \<subseteq> (\<lambda>k. k/2^n) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}"
2.91 +    proof clarify
2.92 +      fix x
2.93 +      show "?g n x  \<in> (\<lambda>k. k/2^n) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)}"
2.94 +      proof (cases "\<exists>k::real. k \<in> \<int> \<and> \<bar>k\<bar> \<le> 2 ^ (2*n) \<and> k/2^n \<le> (f x) \<and> (f x) < (k+1)/2^n")
2.95 +        case True
2.96 +        then show ?thesis
2.97 +          apply clarify
2.98 +          by (subst indicator_sum_eq) auto
2.99 +      next
2.100 +        case False
2.101 +        then have "?g n x = 0" by auto
2.102 +        then show ?thesis by force
2.103 +      qed
2.104 +    qed
2.105 +    moreover have "finite ((\<lambda>k::real. (k/2^n)) ` {k \<in> \<int>. \<bar>k\<bar> \<le> 2 ^ (2*n)})"
2.106 +      by (simp add: finite_abs_int_segment)
2.107 +    ultimately show ?thesis
2.108 +      using finite_subset by blast
2.109 +  qed
2.110 +  show "(\<lambda>n. ?g n x) \<longlonglongrightarrow> f x" for x
2.111 +  proof (rule LIMSEQ_I)
2.112 +    fix e::real
2.113 +    assume "e > 0"
2.114 +    obtain N1 where N1: "\<bar>f x\<bar> < 2 ^ N1"
2.115 +      using real_arch_pow by fastforce
2.116 +    obtain N2 where N2: "(1/2) ^ N2 < e"
2.117 +      using real_arch_pow_inv \<open>e > 0\<close> by force
2.118 +    have "norm (?g n x - f x) < e" if n: "n \<ge> max N1 N2" for n
2.119 +    proof -
2.120 +      define m where "m \<equiv> floor(2^n * (f x))"
2.121 +      have "1 \<le> \<bar>2^n\<bar> * e"
2.122 +        using n N2 \<open>e > 0\<close> less_eq_real_def less_le_trans by (fastforce simp add: divide_simps)
2.123 +      then have *: "\<lbrakk>x \<le> y; y < x + 1\<rbrakk> \<Longrightarrow> abs(x - y) < \<bar>2^n\<bar> * e" for x y::real
2.124 +        by linarith
2.125 +      have "\<bar>2^n\<bar> * \<bar>m/2^n - f x\<bar> = \<bar>2^n * (m/2^n - f x)\<bar>"
2.126 +        by (simp add: abs_mult)
2.127 +      also have "\<dots> = \<bar>real_of_int \<lfloor>2^n * f x\<rfloor> - f x * 2^n\<bar>"
2.128 +        by (simp add: algebra_simps m_def)
2.129 +      also have "\<dots> < \<bar>2^n\<bar> * e"
2.130 +        by (rule *; simp add: mult.commute)
2.131 +      finally have "\<bar>2^n\<bar> * \<bar>m/2^n - f x\<bar> < \<bar>2^n\<bar> * e" .
2.132 +      then have me: "\<bar>m/2^n - f x\<bar> < e"
2.133 +        by simp
2.134 +      have "\<bar>real_of_int m\<bar> \<le> 2 ^ (2*n)"
2.135 +      proof (cases "f x < 0")
2.136 +        case True
2.137 +        then have "-\<lfloor>f x\<rfloor> \<le> \<lfloor>(2::real) ^ N1\<rfloor>"
2.138 +          using N1 le_floor_iff minus_le_iff by fastforce
2.139 +        with n True have "\<bar>real_of_int\<lfloor>f x\<rfloor>\<bar> \<le> 2 ^ N1"
2.140 +          by linarith
2.141 +        also have "\<dots> \<le> 2^n"
2.142 +          using n by (simp add: m_def)
2.143 +        finally have "\<bar>real_of_int \<lfloor>f x\<rfloor>\<bar> * 2^n \<le> 2^n * 2^n"
2.144 +          by simp
2.145 +        moreover
2.146 +        have "\<bar>real_of_int \<lfloor>2^n * f x\<rfloor>\<bar> \<le> \<bar>real_of_int \<lfloor>f x\<rfloor>\<bar> * 2^n"
2.147 +        proof -
2.148 +          have "\<bar>real_of_int \<lfloor>2^n * f x\<rfloor>\<bar> = - (real_of_int \<lfloor>2^n * f x\<rfloor>)"
2.149 +            using True by (simp add: abs_if mult_less_0_iff)
2.150 +          also have "\<dots> \<le> - (real_of_int (\<lfloor>(2::real) ^ n\<rfloor> * \<lfloor>f x\<rfloor>))"
2.151 +            using le_mult_floor_Ints [of "(2::real)^n"] by simp
2.152 +          also have "\<dots> \<le> \<bar>real_of_int \<lfloor>f x\<rfloor>\<bar> * 2^n"
2.153 +            using True
2.154 +            by simp
2.155 +          finally show ?thesis .
2.156 +        qed
2.157 +        ultimately show ?thesis
2.158 +          by (metis (no_types, hide_lams) m_def order_trans power2_eq_square power_even_eq)
2.159 +      next
2.160 +        case False
2.161 +        with n N1 have "f x \<le> 2^n"
2.162 +          by (simp add: not_less) (meson less_eq_real_def one_le_numeral order_trans power_increasing)
2.163 +        moreover have "0 \<le> m"
2.164 +          using False m_def by force
2.165 +        ultimately show ?thesis
2.166 +          by (metis abs_of_nonneg floor_mono le_floor_iff m_def of_int_0_le_iff power2_eq_square power_mult real_mult_le_cancel_iff1 zero_less_numeral mult.commute zero_less_power)
2.167 +      qed
2.168 +      then have "?g n x = m/2^n"
2.169 +        by (rule indicator_sum_eq) (auto simp: m_def mult.commute divide_simps)
2.170 +      then have "norm (?g n x - f x) = norm (m/2^n - f x)"
2.171 +        by simp
2.172 +      also have "\<dots> < e"
2.173 +        by (simp add: me)
2.174 +      finally show ?thesis .
2.175 +    qed
2.176 +    then show "\<exists>no. \<forall>n\<ge>no. norm (?g n x - f x) < e"
2.177 +      by blast
2.178 +  qed
2.179 +qed
2.180 +
2.181 +
2.182 +lemma borel_measurable_vimage_halfspace_component_lt:
2.183 +     "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
2.184 +      (\<forall>a i. i \<in> Basis \<longrightarrow> {x \<in> S. f x \<bullet> i < a} \<in> sets (lebesgue_on S))"
2.185 +  apply (rule trans [OF borel_measurable_iff_halfspace_less])
2.186 +  apply (fastforce simp add: space_restrict_space)
2.187 +  done
2.188 +
2.189 +lemma borel_measurable_simple_function_limit:
2.190 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.191 +  shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
2.192 +         (\<exists>g. (\<forall>n. (g n) \<in> borel_measurable (lebesgue_on S)) \<and>
2.193 +              (\<forall>n. finite (range (g n))) \<and> (\<forall>x. (\<lambda>n. g n x) \<longlonglongrightarrow> f x))"
2.194 +proof -
2.195 +  have "\<exists>g. (\<forall>n. (g n) \<in> borel_measurable (lebesgue_on S)) \<and>
2.196 +            (\<forall>n. finite (range (g n))) \<and> (\<forall>x. (\<lambda>n. g n x) \<longlonglongrightarrow> f x)"
2.197 +       if f: "\<And>a i. i \<in> Basis \<Longrightarrow> {x \<in> S. f x \<bullet> i < a} \<in> sets (lebesgue_on S)"
2.198 +  proof -
2.199 +    have "\<exists>g. (\<forall>n. (g n) \<in> borel_measurable (lebesgue_on S)) \<and>
2.200 +                  (\<forall>n. finite(image (g n) UNIV)) \<and>
2.201 +                  (\<forall>x. ((\<lambda>n. g n x) \<longlonglongrightarrow> f x \<bullet> i))" if "i \<in> Basis" for i
2.202 +    proof (rule measurable_on_sf_limit_lemma1 [of S "\<lambda>x. f x \<bullet> i"])
2.203 +      show "{x \<in> S. a \<le> f x \<bullet> i \<and> f x \<bullet> i < b} \<in> sets (lebesgue_on S)" for a b
2.204 +      proof -
2.205 +        have "{x \<in> S. a \<le> f x \<bullet> i \<and> f x \<bullet> i < b} = {x \<in> S. f x \<bullet> i < b} - {x \<in> S. a > f x \<bullet> i}"
2.206 +          by auto
2.207 +        also have "\<dots> \<in> sets (lebesgue_on S)"
2.208 +          using f that by blast
2.209 +        finally show ?thesis .
2.210 +      qed
2.211 +    qed blast
2.212 +    then obtain g where g:
2.213 +          "\<And>i n. i \<in> Basis \<Longrightarrow> g i n \<in> borel_measurable (lebesgue_on S)"
2.214 +          "\<And>i n. i \<in> Basis \<Longrightarrow> finite(range (g i n))"
2.215 +          "\<And>i x. i \<in> Basis \<Longrightarrow> ((\<lambda>n. g i n x) \<longlonglongrightarrow> f x \<bullet> i)"
2.216 +      by metis
2.217 +    show ?thesis
2.218 +    proof (intro conjI allI exI)
2.219 +      show "(\<lambda>x. \<Sum>i\<in>Basis. g i n x *\<^sub>R i) \<in> borel_measurable (lebesgue_on S)" for n
2.220 +        by (intro borel_measurable_sum borel_measurable_scaleR) (auto intro: g)
2.221 +      show "finite (range (\<lambda>x. \<Sum>i\<in>Basis. g i n x *\<^sub>R i))" for n
2.222 +      proof -
2.223 +        have "range (\<lambda>x. \<Sum>i\<in>Basis. g i n x *\<^sub>R i) \<subseteq> (\<lambda>h. \<Sum>i\<in>Basis. h i *\<^sub>R i) ` PiE Basis (\<lambda>i. range (g i n))"
2.224 +        proof clarify
2.225 +          fix x
2.226 +          show "(\<Sum>i\<in>Basis. g i n x *\<^sub>R i) \<in> (\<lambda>h. \<Sum>i\<in>Basis. h i *\<^sub>R i) ` (\<Pi>\<^sub>E i\<in>Basis. range (g i n))"
2.227 +            by (rule_tac x="\<lambda>i\<in>Basis. g i n x" in image_eqI) auto
2.228 +        qed
2.229 +        moreover have "finite(PiE Basis (\<lambda>i. range (g i n)))"
2.230 +          by (simp add: g finite_PiE)
2.231 +        ultimately show ?thesis
2.232 +          by (metis (mono_tags, lifting) finite_surj)
2.233 +      qed
2.234 +      show "(\<lambda>n. \<Sum>i\<in>Basis. g i n x *\<^sub>R i) \<longlonglongrightarrow> f x" for x
2.235 +      proof -
2.236 +        have "(\<lambda>n. \<Sum>i\<in>Basis. g i n x *\<^sub>R i) \<longlonglongrightarrow> (\<Sum>i\<in>Basis. (f x \<bullet> i) *\<^sub>R i)"
2.237 +          by (auto intro!: tendsto_sum tendsto_scaleR g)
2.238 +        moreover have "(\<Sum>i\<in>Basis. (f x \<bullet> i) *\<^sub>R i) = f x"
2.239 +          using euclidean_representation by blast
2.240 +        ultimately show ?thesis
2.241 +          by metis
2.242 +      qed
2.243 +    qed
2.244 +  qed
2.245 +  moreover have "f \<in> borel_measurable (lebesgue_on S)"
2.246 +              if meas_g: "\<And>n. g n \<in> borel_measurable (lebesgue_on S)"
2.247 +                 and fin: "\<And>n. finite (range (g n))"
2.248 +                 and to_f: "\<And>x. (\<lambda>n. g n x) \<longlonglongrightarrow> f x" for  g
2.249 +    by (rule borel_measurable_LIMSEQ_metric [OF meas_g to_f])
2.250 +  ultimately show ?thesis
2.251 +    using borel_measurable_vimage_halfspace_component_lt by blast
2.252 +qed
2.253 +
2.254 +lemma borel_measurable_vimage_halfspace_component_ge:
2.255 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.256 +  shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
2.257 +         (\<forall>a i. i \<in> Basis \<longrightarrow> {x \<in> S. f x \<bullet> i \<ge> a} \<in> sets (lebesgue_on S))"
2.258 +  apply (rule trans [OF borel_measurable_iff_halfspace_ge])
2.259 +  apply (fastforce simp add: space_restrict_space)
2.260 +  done
2.261 +
2.262 +lemma borel_measurable_vimage_halfspace_component_gt:
2.263 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.264 +  shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
2.265 +         (\<forall>a i. i \<in> Basis \<longrightarrow> {x \<in> S. f x \<bullet> i > a} \<in> sets (lebesgue_on S))"
2.266 +  apply (rule trans [OF borel_measurable_iff_halfspace_greater])
2.267 +  apply (fastforce simp add: space_restrict_space)
2.268 +  done
2.269 +
2.270 +lemma borel_measurable_vimage_halfspace_component_le:
2.271 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.272 +  shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
2.273 +         (\<forall>a i. i \<in> Basis \<longrightarrow> {x \<in> S. f x \<bullet> i \<le> a} \<in> sets (lebesgue_on S))"
2.274 +  apply (rule trans [OF borel_measurable_iff_halfspace_le])
2.275 +  apply (fastforce simp add: space_restrict_space)
2.276 +  done
2.277 +
2.278 +lemma
2.279 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.280 +  shows borel_measurable_vimage_open_interval:
2.281 +         "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
2.282 +         (\<forall>a b. {x \<in> S. f x \<in> box a b} \<in> sets (lebesgue_on S))" (is ?thesis1)
2.283 +   and borel_measurable_vimage_open:
2.284 +         "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
2.285 +         (\<forall>T. open T \<longrightarrow> {x \<in> S. f x \<in> T} \<in> sets (lebesgue_on S))" (is ?thesis2)
2.286 +proof -
2.287 +  have "{x \<in> S. f x \<in> box a b} \<in> sets (lebesgue_on S)" if "f \<in> borel_measurable (lebesgue_on S)" for a b
2.288 +  proof -
2.289 +    have "S = S \<inter> space lebesgue"
2.290 +      by simp
2.291 +    then have "S \<inter> (f -` box a b) \<in> sets (lebesgue_on S)"
2.292 +      by (metis (no_types) box_borel in_borel_measurable_borel inf_sup_aci(1) space_restrict_space that)
2.293 +    then show ?thesis
2.294 +      by (simp add: Collect_conj_eq vimage_def)
2.295 +  qed
2.296 +  moreover
2.297 +  have "{x \<in> S. f x \<in> T} \<in> sets (lebesgue_on S)"
2.298 +       if T: "\<And>a b. {x \<in> S. f x \<in> box a b} \<in> sets (lebesgue_on S)" "open T" for T
2.299 +  proof -
2.300 +    obtain \<D> where "countable \<D>" and \<D>: "\<And>X. X \<in> \<D> \<Longrightarrow> \<exists>a b. X = box a b" "\<Union>\<D> = T"
2.301 +      using open_countable_Union_open_box that \<open>open T\<close> by metis
2.302 +    then have eq: "{x \<in> S. f x \<in> T} = (\<Union>U \<in> \<D>. {x \<in> S. f x \<in> U})"
2.303 +      by blast
2.304 +    have "{x \<in> S. f x \<in> U} \<in> sets (lebesgue_on S)" if "U \<in> \<D>" for U
2.305 +      using that T \<D> by blast
2.306 +    then show ?thesis
2.307 +      by (auto simp: eq intro: Sigma_Algebra.sets.countable_UN' [OF \<open>countable \<D>\<close>])
2.308 +  qed
2.309 +  moreover
2.310 +  have eq: "{x \<in> S. f x \<bullet> i < a} = {x \<in> S. f x \<in> {y. y \<bullet> i < a}}" for i a
2.311 +    by auto
2.312 +  have "f \<in> borel_measurable (lebesgue_on S)"
2.313 +    if "\<And>T. open T \<Longrightarrow> {x \<in> S. f x \<in> T} \<in> sets (lebesgue_on S)"
2.314 +    by (metis (no_types) eq borel_measurable_vimage_halfspace_component_lt open_halfspace_component_lt that)
2.315 +  ultimately show "?thesis1" "?thesis2"
2.316 +    by blast+
2.317 +qed
2.318 +
2.319 +
2.320 +lemma borel_measurable_vimage_closed:
2.321 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.322 +  shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
2.323 +         (\<forall>T. closed T \<longrightarrow> {x \<in> S. f x \<in> T} \<in> sets (lebesgue_on S))"
2.324 +        (is "?lhs = ?rhs")
2.325 +proof -
2.326 +  have eq: "{x \<in> S. f x \<in> T} = S - {x \<in> S. f x \<in> (- T)}" for T
2.327 +    by auto
2.328 +  show ?thesis
2.329 +    apply (simp add: borel_measurable_vimage_open, safe)
2.330 +     apply (simp_all (no_asm) add: eq)
2.331 +     apply (intro sets.Diff sets_lebesgue_on_refl, force simp: closed_open)
2.332 +    apply (intro sets.Diff sets_lebesgue_on_refl, force simp: open_closed)
2.333 +    done
2.334 +qed
2.335 +
2.336 +lemma borel_measurable_vimage_closed_interval:
2.337 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.338 +  shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
2.339 +         (\<forall>a b. {x \<in> S. f x \<in> cbox a b} \<in> sets (lebesgue_on S))"
2.340 +        (is "?lhs = ?rhs")
2.341 +proof
2.342 +  assume ?lhs then show ?rhs
2.343 +    using borel_measurable_vimage_closed by blast
2.344 +next
2.345 +  assume RHS: ?rhs
2.346 +  have "{x \<in> S. f x \<in> T} \<in> sets (lebesgue_on S)" if "open T" for T
2.347 +  proof -
2.348 +    obtain \<D> where "countable \<D>" and \<D>: "\<D> \<subseteq> Pow T" "\<And>X. X \<in> \<D> \<Longrightarrow> \<exists>a b. X = cbox a b" "\<Union>\<D> = T"
2.349 +      using open_countable_Union_open_cbox that \<open>open T\<close> by metis
2.350 +    then have eq: "{x \<in> S. f x \<in> T} = (\<Union>U \<in> \<D>. {x \<in> S. f x \<in> U})"
2.351 +      by blast
2.352 +    have "{x \<in> S. f x \<in> U} \<in> sets (lebesgue_on S)" if "U \<in> \<D>" for U
2.353 +      using that \<D> by (metis RHS)
2.354 +    then show ?thesis
2.355 +      by (auto simp: eq intro: Sigma_Algebra.sets.countable_UN' [OF \<open>countable \<D>\<close>])
2.356 +  qed
2.357 +  then show ?lhs
2.358 +    by (simp add: borel_measurable_vimage_open)
2.359 +qed
2.360 +
2.361 +lemma borel_measurable_UNIV_eq: "borel_measurable (lebesgue_on UNIV) = borel_measurable lebesgue"
2.362 +  by auto
2.363 +
2.364 +lemma borel_measurable_vimage_borel:
2.365 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.366 +  shows "f \<in> borel_measurable (lebesgue_on S) \<longleftrightarrow>
2.367 +         (\<forall>T. T \<in> sets borel \<longrightarrow> {x \<in> S. f x \<in> T} \<in> sets (lebesgue_on S))"
2.368 +        (is "?lhs = ?rhs")
2.369 +proof
2.370 +  assume f: ?lhs
2.371 +  then show ?rhs
2.372 +    using measurable_sets [OF f]
2.373 +      by (simp add: Collect_conj_eq inf_sup_aci(1) space_restrict_space vimage_def)
2.374 +qed (simp add: borel_measurable_vimage_open_interval)
2.375 +
2.376 +lemma lebesgue_measurable_vimage_borel:
2.377 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.378 +  assumes "f \<in> borel_measurable lebesgue" "T \<in> sets borel"
2.379 +  shows "{x. f x \<in> T} \<in> sets lebesgue"
2.380 +  using assms borel_measurable_vimage_borel [of f UNIV] by auto
2.381 +
2.382 +lemma borel_measurable_If_I:
2.383 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.384 +  assumes f: "f \<in> borel_measurable (lebesgue_on S)" and S: "S \<in> sets lebesgue"
2.385 +  shows "(\<lambda>x. if x \<in> S then f x else 0) \<in> borel_measurable lebesgue"
2.386 +proof -
2.387 +  have eq: "{x. x \<notin> S} \<union> {x. f x \<in> Y} = {x. x \<notin> S} \<union> {x. f x \<in> Y} \<inter> S" for Y
2.388 +    by blast
2.389 +  show ?thesis
2.390 +  using f S
2.391 +  apply (simp add: vimage_def in_borel_measurable_borel Ball_def)
2.392 +  apply (elim all_forward imp_forward asm_rl)
2.393 +  apply (simp only: Collect_conj_eq Collect_disj_eq imp_conv_disj eq)
2.394 +  apply (auto simp: Compl_eq [symmetric] Compl_in_sets_lebesgue sets_restrict_space_iff)
2.395 +  done
2.396 +qed
2.397 +
2.398 +lemma borel_measurable_If_D:
2.399 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.400 +  assumes "(\<lambda>x. if x \<in> S then f x else 0) \<in> borel_measurable lebesgue"
2.401 +  shows "f \<in> borel_measurable (lebesgue_on S)"
2.402 +  using assms
2.403 +  apply (simp add: in_borel_measurable_borel Ball_def)
2.404 +  apply (elim all_forward imp_forward asm_rl)
2.405 +  apply (force simp: space_restrict_space sets_restrict_space image_iff intro: rev_bexI)
2.406 +  done
2.407 +
2.408 +lemma borel_measurable_UNIV:
2.409 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.410 +  assumes "S \<in> sets lebesgue"
2.411 +  shows "(\<lambda>x. if x \<in> S then f x else 0) \<in> borel_measurable lebesgue \<longleftrightarrow> f \<in> borel_measurable (lebesgue_on S)"
2.412 +  using assms borel_measurable_If_D borel_measurable_If_I by blast
2.413 +
2.414 +lemma borel_measurable_lebesgue_preimage_borel:
2.415 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.416 +  shows "f \<in> borel_measurable lebesgue \<longleftrightarrow>
2.417 +         (\<forall>T. T \<in> sets borel \<longrightarrow> {x. f x \<in> T} \<in> sets lebesgue)"
2.418 +  apply (intro iffI allI impI lebesgue_measurable_vimage_borel)
2.419 +    apply (auto simp: in_borel_measurable_borel vimage_def)
2.420 +  done
2.421 +
2.422 +subsection\<open>More results on integrability\<close>
2.423 +
2.424 +lemma integrable_on_all_intervals_UNIV:
2.425 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
2.426 +  assumes intf: "\<And>a b. f integrable_on cbox a b"
2.427 +    and normf: "\<And>x. norm(f x) \<le> g x" and g: "g integrable_on UNIV"
2.428 +  shows "f integrable_on UNIV"
2.429 +proof -
2.430 +have intg: "(\<forall>a b. g integrable_on cbox a b)"
2.431 +    and gle_e: "\<forall>e>0. \<exists>B>0. \<forall>a b c d.
2.432 +                    ball 0 B \<subseteq> cbox a b \<and> cbox a b \<subseteq> cbox c d \<longrightarrow>
2.433 +                    \<bar>integral (cbox a b) g - integral (cbox c d) g\<bar>
2.434 +                    < e"
2.435 +    using g
2.436 +    by (auto simp: integrable_alt_subset [of _ UNIV] intf)
2.437 +  have le: "norm (integral (cbox a b) f - integral (cbox c d) f) \<le> \<bar>integral (cbox a b) g - integral (cbox c d) g\<bar>"
2.438 +    if "cbox a b \<subseteq> cbox c d" for a b c d
2.439 +  proof -
2.440 +    have "norm (integral (cbox a b) f - integral (cbox c d) f) = norm (integral (cbox c d - cbox a b) f)"
2.441 +      using intf that by (simp add: norm_minus_commute integral_setdiff)
2.442 +    also have "\<dots> \<le> integral (cbox c d - cbox a b) g"
2.443 +    proof (rule integral_norm_bound_integral [OF _ _ normf])
2.444 +      show "f integrable_on cbox c d - cbox a b" "g integrable_on cbox c d - cbox a b"
2.445 +        by (meson integrable_integral integrable_setdiff intf intg negligible_setdiff that)+
2.446 +    qed
2.447 +    also have "\<dots> = integral (cbox c d) g - integral (cbox a b) g"
2.448 +      using intg that by (simp add: integral_setdiff)
2.449 +    also have "\<dots> \<le> \<bar>integral (cbox a b) g - integral (cbox c d) g\<bar>"
2.450 +      by simp
2.451 +    finally show ?thesis .
2.452 +  qed
2.453 +  show ?thesis
2.454 +    using gle_e
2.455 +    apply (simp add: integrable_alt_subset [of _ UNIV] intf)
2.456 +    apply (erule imp_forward all_forward ex_forward asm_rl)+
2.457 +    by (meson not_less order_trans le)
2.458 +qed
2.459 +
2.460 +lemma integrable_on_all_intervals_integrable_bound:
2.461 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
2.462 +  assumes intf: "\<And>a b. (\<lambda>x. if x \<in> S then f x else 0) integrable_on cbox a b"
2.463 +    and normf: "\<And>x. x \<in> S \<Longrightarrow> norm(f x) \<le> g x" and g: "g integrable_on S"
2.464 +  shows "f integrable_on S"
2.465 +  using integrable_on_all_intervals_UNIV [OF intf, of "(\<lambda>x. if x \<in> S then g x else 0)"]
2.466 +  by (simp add: g integrable_restrict_UNIV normf)
2.467 +
2.468 +lemma measurable_bounded_lemma:
2.469 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.470 +  assumes f: "f \<in> borel_measurable lebesgue" and g: "g integrable_on cbox a b"
2.471 +    and normf: "\<And>x. x \<in> cbox a b \<Longrightarrow> norm(f x) \<le> g x"
2.472 +  shows "f integrable_on cbox a b"
2.473 +proof -
2.474 +  have "g absolutely_integrable_on cbox a b"
2.475 +    by (metis (full_types) add_increasing g le_add_same_cancel1 nonnegative_absolutely_integrable_1 norm_ge_zero normf)
2.476 +  then have "integrable (lebesgue_on (cbox a b)) g"
2.477 +    by (simp add: integrable_restrict_space set_integrable_def)
2.478 +  then have "integrable (lebesgue_on (cbox a b)) f"
2.479 +  proof (rule Bochner_Integration.integrable_bound)
2.480 +    show "AE x in lebesgue_on (cbox a b). norm (f x) \<le> norm (g x)"
2.481 +      by (rule AE_I2) (auto intro: normf order_trans)
2.482 +  qed (simp add: f measurable_restrict_space1)
2.483 +  then show ?thesis
2.484 +    by (simp add: integrable_on_lebesgue_on)
2.485 +qed
2.486 +
2.487 +proposition measurable_bounded_by_integrable_imp_integrable:
2.488 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.489 +  assumes f: "f \<in> borel_measurable (lebesgue_on S)" and g: "g integrable_on S"
2.490 +    and normf: "\<And>x. x \<in> S \<Longrightarrow> norm(f x) \<le> g x" and S: "S \<in> sets lebesgue"
2.491 +  shows "f integrable_on S"
2.492 +proof (rule integrable_on_all_intervals_integrable_bound [OF _ normf g])
2.493 +  show "(\<lambda>x. if x \<in> S then f x else 0) integrable_on cbox a b" for a b
2.494 +  proof (rule measurable_bounded_lemma)
2.495 +    show "(\<lambda>x. if x \<in> S then f x else 0) \<in> borel_measurable lebesgue"
2.496 +      by (simp add: S borel_measurable_UNIV f)
2.497 +    show "(\<lambda>x. if x \<in> S then g x else 0) integrable_on cbox a b"
2.498 +      by (simp add: g integrable_altD(1))
2.499 +    show "norm (if x \<in> S then f x else 0) \<le> (if x \<in> S then g x else 0)" for x
2.500 +      using normf by simp
2.501 +  qed
2.502 +qed
2.503 +
2.504 +subsection\<open> Relation between Borel measurability and integrability.\<close>
2.505 +
2.506 +lemma integrable_imp_measurable_weak:
2.507 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.508 +  assumes "S \<in> sets lebesgue" "f integrable_on S"
2.509 +  shows "f \<in> borel_measurable (lebesgue_on S)"
2.510 +  by (metis (mono_tags, lifting) assms has_integral_implies_lebesgue_measurable borel_measurable_restrict_space_iff integrable_on_def sets.Int_space_eq2)
2.511 +
2.512 +lemma integrable_imp_measurable:
2.513 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.514 +  assumes "f integrable_on S"
2.515 +  shows "f \<in> borel_measurable (lebesgue_on S)"
2.516 +proof -
2.517 +  have "(UNIV::'a set) \<in> sets lborel"
2.518 +    by simp
2.519 +  then show ?thesis
2.520 +    using assms borel_measurable_If_D borel_measurable_UNIV_eq integrable_imp_measurable_weak integrable_restrict_UNIV by blast
2.521 +qed
2.522 +
2.523 +proposition negligible_differentiable_vimage:
2.524 +  fixes f :: "'a \<Rightarrow> 'a::euclidean_space"
2.525 +  assumes "negligible T"
2.526 +    and f': "\<And>x. x \<in> S \<Longrightarrow> inj(f' x)"
2.527 +    and derf: "\<And>x. x \<in> S \<Longrightarrow> (f has_derivative f' x) (at x within S)"
2.528 +  shows "negligible {x \<in> S. f x \<in> T}"
2.529 +proof -
2.530 +  define U where
2.531 +    "U \<equiv> \<lambda>n::nat. {x \<in> S. \<forall>y. y \<in> S \<and> norm(y - x) < 1/n
2.532 +                     \<longrightarrow> norm(y - x) \<le> n * norm(f y - f x)}"
2.533 +  have "negligible {x \<in> U n. f x \<in> T}" if "n > 0" for n
2.534 +  proof (subst locally_negligible_alt, clarify)
2.535 +    fix a
2.536 +    assume a: "a \<in> U n" and fa: "f a \<in> T"
2.537 +    define V where "V \<equiv> {x. x \<in> U n \<and> f x \<in> T} \<inter> ball a (1 / n / 2)"
2.538 +    show "\<exists>V. openin (subtopology euclidean {x \<in> U n. f x \<in> T}) V \<and> a \<in> V \<and> negligible V"
2.539 +    proof (intro exI conjI)
2.540 +      have noxy: "norm(x - y) \<le> n * norm(f x - f y)" if "x \<in> V" "y \<in> V" for x y
2.541 +        using that unfolding U_def V_def mem_Collect_eq Int_iff mem_ball dist_norm
2.542 +        by (meson norm_triangle_half_r)
2.543 +      then have "inj_on f V"
2.544 +        by (force simp: inj_on_def)
2.545 +      then obtain g where g: "\<And>x. x \<in> V \<Longrightarrow> g(f x) = x"
2.546 +        by (metis inv_into_f_f)
2.547 +      have "\<exists>T' B. open T' \<and> f x \<in> T' \<and>
2.548 +                   (\<forall>y\<in>f ` V \<inter> T \<inter> T'. norm (g y - g (f x)) \<le> B * norm (y - f x))"
2.549 +        if "f x \<in> T" "x \<in> V" for x
2.550 +        apply (rule_tac x = "ball (f x) 1" in exI)
2.551 +        using that noxy by (force simp: g)
2.552 +      then have "negligible (g ` (f ` V \<inter> T))"
2.553 +        by (force simp: \<open>negligible T\<close> negligible_Int intro!: negligible_locally_Lipschitz_image)
2.554 +      moreover have "V \<subseteq> g ` (f ` V \<inter> T)"
2.555 +        by (force simp: g image_iff V_def)
2.556 +      ultimately show "negligible V"
2.557 +        by (rule negligible_subset)
2.558 +    qed (use a fa V_def that in auto)
2.559 +  qed
2.560 +  with negligible_countable_Union have "negligible (\<Union>n \<in> {0<..}. {x. x \<in> U n \<and> f x \<in> T})"
2.561 +    by auto
2.562 +  moreover have "{x \<in> S. f x \<in> T} \<subseteq> (\<Union>n \<in> {0<..}. {x. x \<in> U n \<and> f x \<in> T})"
2.563 +  proof clarsimp
2.564 +    fix x
2.565 +    assume "x \<in> S" and "f x \<in> T"
2.566 +    then obtain inj: "inj(f' x)" and der: "(f has_derivative f' x) (at x within S)"
2.567 +      using assms by metis
2.568 +    moreover have "linear(f' x)"
2.569 +      and eps: "\<And>\<epsilon>. \<epsilon> > 0 \<Longrightarrow> \<exists>\<delta>>0. \<forall>y\<in>S. norm (y - x) < \<delta> \<longrightarrow>
2.570 +                      norm (f y - f x - f' x (y - x)) \<le> \<epsilon> * norm (y - x)"
2.571 +      using der by (auto simp: has_derivative_within_alt linear_linear)
2.572 +    ultimately obtain g where "linear g" and g: "g \<circ> f' x = id"
2.573 +      using linear_injective_left_inverse by metis
2.574 +    then obtain B where "B > 0" and B: "\<And>z. B * norm z \<le> norm(f' x z)"
2.575 +      using linear_invertible_bounded_below_pos \<open>linear (f' x)\<close> by blast
2.576 +    then obtain i where "i \<noteq> 0" and i: "1 / real i < B"
2.577 +      by (metis (full_types) inverse_eq_divide real_arch_invD)
2.578 +    then obtain \<delta> where "\<delta> > 0"
2.579 +         and \<delta>: "\<And>y. \<lbrakk>y\<in>S; norm (y - x) < \<delta>\<rbrakk> \<Longrightarrow>
2.580 +                  norm (f y - f x - f' x (y - x)) \<le> (B - 1 / real i) * norm (y - x)"
2.581 +      using eps [of "B - 1/i"] by auto
2.582 +    then obtain j where "j \<noteq> 0" and j: "inverse (real j) < \<delta>"
2.583 +      using real_arch_inverse by blast
2.584 +    have "norm (y - x)/(max i j) \<le> norm (f y - f x)"
2.585 +      if "y \<in> S" and less: "norm (y - x) < 1 / (max i j)" for y
2.586 +    proof -
2.587 +      have "1 / real (max i j) < \<delta>"
2.588 +        using j \<open>j \<noteq> 0\<close> \<open>0 < \<delta>\<close>
2.589 +        by (auto simp: divide_simps max_mult_distrib_left of_nat_max)
2.590 +    then have "norm (y - x) < \<delta>"
2.591 +      using less by linarith
2.592 +    with \<delta> \<open>y \<in> S\<close> have le: "norm (f y - f x - f' x (y - x)) \<le> B * norm (y - x) - norm (y - x)/i"
2.593 +      by (auto simp: algebra_simps)
2.594 +    have *: "\<lbrakk>norm(f - f' - y) \<le> b - c; b \<le> norm y; d \<le> c\<rbrakk> \<Longrightarrow> d \<le> norm(f - f')"
2.595 +      for b c d and y f f'::'a
2.596 +      using norm_triangle_ineq3 [of "f - f'" y] by simp
2.597 +    show ?thesis
2.598 +      apply (rule * [OF le B])
2.599 +      using \<open>i \<noteq> 0\<close> \<open>j \<noteq> 0\<close> by (simp add: divide_simps max_mult_distrib_left of_nat_max less_max_iff_disj)
2.600 +  qed
2.601 +  with \<open>x \<in> S\<close> \<open>i \<noteq> 0\<close> \<open>j \<noteq> 0\<close> show "\<exists>n\<in>{0<..}. x \<in> U n"
2.602 +    by (rule_tac x="max i j" in bexI) (auto simp: field_simps U_def less_max_iff_disj)
2.603 +qed
2.604 +  ultimately show ?thesis
2.605 +    by (rule negligible_subset)
2.606 +qed
2.607 +
2.608 +lemma absolutely_integrable_Un:
2.609 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
2.610 +  assumes S: "f absolutely_integrable_on S" and T: "f absolutely_integrable_on T"
2.611 +  shows "f absolutely_integrable_on (S \<union> T)"
2.612 +proof -
2.613 +  have [simp]: "{x. (if x \<in> A then f x else 0) \<noteq> 0} = {x \<in> A. f x \<noteq> 0}" for A
2.614 +    by auto
2.615 +  let ?ST = "{x \<in> S. f x \<noteq> 0} \<inter> {x \<in> T. f x \<noteq> 0}"
2.616 +  have "?ST \<in> sets lebesgue"
2.617 +  proof (rule Sigma_Algebra.sets.Int)
2.618 +    have "f integrable_on S"
2.619 +      using S absolutely_integrable_on_def by blast
2.620 +    then have "(\<lambda>x. if x \<in> S then f x else 0) integrable_on UNIV"
2.621 +      by (simp add: integrable_restrict_UNIV)
2.622 +    then have borel: "(\<lambda>x. if x \<in> S then f x else 0) \<in> borel_measurable (lebesgue_on UNIV)"
2.623 +      using integrable_imp_measurable borel_measurable_UNIV_eq by blast
2.624 +    then show "{x \<in> S. f x \<noteq> 0} \<in> sets lebesgue"
2.625 +      unfolding borel_measurable_vimage_open
2.626 +      by (rule allE [where x = "-{0}"]) auto
2.627 +  next
2.628 +    have "f integrable_on T"
2.629 +      using T absolutely_integrable_on_def by blast
2.630 +    then have "(\<lambda>x. if x \<in> T then f x else 0) integrable_on UNIV"
2.631 +      by (simp add: integrable_restrict_UNIV)
2.632 +    then have borel: "(\<lambda>x. if x \<in> T then f x else 0) \<in> borel_measurable (lebesgue_on UNIV)"
2.633 +      using integrable_imp_measurable borel_measurable_UNIV_eq by blast
2.634 +    then show "{x \<in> T. f x \<noteq> 0} \<in> sets lebesgue"
2.635 +      unfolding borel_measurable_vimage_open
2.636 +      by (rule allE [where x = "-{0}"]) auto
2.637 +  qed
2.638 +  then have "f absolutely_integrable_on ?ST"
2.639 +    by (rule set_integrable_subset [OF S]) auto
2.640 +  then have Int: "(\<lambda>x. if x \<in> ?ST then f x else 0) absolutely_integrable_on UNIV"
2.641 +    using absolutely_integrable_restrict_UNIV by blast
2.642 +  have "(\<lambda>x. if x \<in> S then f x else 0) absolutely_integrable_on UNIV"
2.643 +       "(\<lambda>x. if x \<in> T then f x else 0) absolutely_integrable_on UNIV"
2.644 +    using S T absolutely_integrable_restrict_UNIV by blast+
2.645 +  then have "(\<lambda>x. (if x \<in> S then f x else 0) + (if x \<in> T then f x else 0)) absolutely_integrable_on UNIV"
2.646 +    by (rule absolutely_integrable_add)
2.647 +  then have "(\<lambda>x. ((if x \<in> S then f x else 0) + (if x \<in> T then f x else 0)) - (if x \<in> ?ST then f x else 0)) absolutely_integrable_on UNIV"
2.648 +    using Int by (rule absolutely_integrable_diff)
2.649 +  then have "(\<lambda>x. if x \<in> S \<union> T then f x else 0) absolutely_integrable_on UNIV"
2.650 +    by (rule absolutely_integrable_spike) (auto intro: empty_imp_negligible)
2.651 +  then show ?thesis
2.652 +    unfolding absolutely_integrable_restrict_UNIV .
2.653 +qed
2.654 +
2.655 +
2.656 +
2.657 +
2.658 +subsubsection\<open>Differentiability of inverse function (most basic form)\<close>
2.659 +
2.660 +proposition has_derivative_inverse_within:
2.661 +  fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::euclidean_space"
2.662 +  assumes der_f: "(f has_derivative f') (at a within S)"
2.663 +    and cont_g: "continuous (at (f a) within f ` S) g"
2.664 +    and "a \<in> S" "linear g'" and id: "g' \<circ> f' = id"
2.665 +    and gf: "\<And>x. x \<in> S \<Longrightarrow> g(f x) = x"
2.666 +  shows "(g has_derivative g') (at (f a) within f ` S)"
2.667 +proof -
2.668 +  have [simp]: "g' (f' x) = x" for x
2.669 +    by (simp add: local.id pointfree_idE)
2.670 +  have "bounded_linear f'"
2.671 +    and f': "\<And>e. e>0 \<Longrightarrow> \<exists>d>0. \<forall>y\<in>S. norm (y - a) < d \<longrightarrow>
2.672 +                        norm (f y - f a - f' (y - a)) \<le> e * norm (y - a)"
2.673 +    using der_f by (auto simp: has_derivative_within_alt)
2.674 +  obtain C where "C > 0" and C: "\<And>x. norm (g' x) \<le> C * norm x"
2.675 +    using linear_bounded_pos [OF \<open>linear g'\<close>] by metis
2.676 +  obtain B k where "B > 0" "k > 0"
2.677 +    and Bk: "\<And>x. \<lbrakk>x \<in> S; norm(f x - f a) < k\<rbrakk> \<Longrightarrow> norm(x - a) \<le> B * norm(f x - f a)"
2.678 +  proof -
2.679 +    obtain B where "B > 0" and B: "\<And>x. B * norm x \<le> norm (f' x)"
2.680 +      using linear_inj_bounded_below_pos [of f'] \<open>linear g'\<close> id der_f has_derivative_linear
2.681 +        linear_invertible_bounded_below_pos by blast
2.682 +    then obtain d where "d>0"
2.683 +      and d: "\<And>y. \<lbrakk>y \<in> S; norm (y - a) < d\<rbrakk> \<Longrightarrow>
2.684 +                    norm (f y - f a - f' (y - a)) \<le> B / 2 * norm (y - a)"
2.685 +      using f' [of "B/2"] by auto
2.686 +    then obtain e where "e > 0"
2.687 +      and e: "\<And>x. \<lbrakk>x \<in> S; norm (f x - f a) < e\<rbrakk> \<Longrightarrow> norm (g (f x) - g (f a)) < d"
2.688 +      using cont_g by (auto simp: continuous_within_eps_delta dist_norm)
2.689 +    show thesis
2.690 +    proof
2.691 +      show "2/B > 0"
2.692 +        using \<open>B > 0\<close> by simp
2.693 +      show "norm (x - a) \<le> 2 / B * norm (f x - f a)"
2.694 +        if "x \<in> S" "norm (f x - f a) < e" for x
2.695 +      proof -
2.696 +        have xa: "norm (x - a) < d"
2.697 +          using e [OF that] gf by (simp add: \<open>a \<in> S\<close> that)
2.698 +        have *: "\<lbrakk>norm(y - f') \<le> B / 2 * norm x; B * norm x \<le> norm f'\<rbrakk>
2.699 +                 \<Longrightarrow> norm y \<ge> B / 2 * norm x" for y f'::'b and x::'a
2.700 +          using norm_triangle_ineq3 [of y f'] by linarith
2.701 +        show ?thesis
2.702 +          using * [OF d [OF \<open>x \<in> S\<close> xa] B] \<open>B > 0\<close> by (simp add: field_simps)
2.703 +      qed
2.704 +    qed (use \<open>e > 0\<close> in auto)
2.705 +  qed
2.706 +  show ?thesis
2.707 +    unfolding has_derivative_within_alt
2.708 +  proof (intro conjI impI allI)
2.709 +    show "bounded_linear g'"
2.710 +      using \<open>linear g'\<close> by (simp add: linear_linear)
2.711 +  next
2.712 +    fix e :: "real"
2.713 +    assume "e > 0"
2.714 +    then obtain d where "d>0"
2.715 +      and d: "\<And>y. \<lbrakk>y \<in> S; norm (y - a) < d\<rbrakk> \<Longrightarrow>
2.716 +                    norm (f y - f a - f' (y - a)) \<le> e / (B * C) * norm (y - a)"
2.717 +      using f' [of "e / (B * C)"] \<open>B > 0\<close> \<open>C > 0\<close> by auto
2.718 +    have "norm (x - a - g' (f x - f a)) \<le> e * norm (f x - f a)"
2.719 +      if "x \<in> S" and lt_k: "norm (f x - f a) < k" and lt_dB: "norm (f x - f a) < d/B" for x
2.720 +    proof -
2.721 +      have "norm (x - a) \<le> B * norm(f x - f a)"
2.722 +        using Bk lt_k \<open>x \<in> S\<close> by blast
2.723 +      also have "\<dots> < d"
2.724 +        by (metis \<open>0 < B\<close> lt_dB mult.commute pos_less_divide_eq)
2.725 +      finally have lt_d: "norm (x - a) < d" .
2.726 +      have "norm (x - a - g' (f x - f a)) \<le> norm(g'(f x - f a - (f' (x - a))))"
2.727 +        by (simp add: linear_diff [OF \<open>linear g'\<close>] norm_minus_commute)
2.728 +      also have "\<dots> \<le> C * norm (f x - f a - f' (x - a))"
2.729 +        using C by blast
2.730 +      also have "\<dots> \<le> e * norm (f x - f a)"
2.731 +      proof -
2.732 +        have "norm (f x - f a - f' (x - a)) \<le> e / (B * C) * norm (x - a)"
2.733 +          using d [OF \<open>x \<in> S\<close> lt_d] .
2.734 +        also have "\<dots> \<le> (norm (f x - f a) * e) / C"
2.735 +          using \<open>B > 0\<close> \<open>C > 0\<close> \<open>e > 0\<close> by (simp add: field_simps Bk lt_k \<open>x \<in> S\<close>)
2.736 +        finally show ?thesis
2.737 +          using \<open>C > 0\<close> by (simp add: field_simps)
2.738 +      qed
2.739 +    finally show ?thesis .
2.740 +    qed
2.741 +    then show "\<exists>d>0. \<forall>y\<in>f ` S.
2.742 +               norm (y - f a) < d \<longrightarrow>
2.743 +               norm (g y - g (f a) - g' (y - f a)) \<le> e * norm (y - f a)"
2.744 +      apply (rule_tac x="min k (d / B)" in exI)
2.745 +      using \<open>k > 0\<close> \<open>B > 0\<close> \<open>d > 0\<close> \<open>a \<in> S\<close> by (auto simp: gf)
2.746 +  qed
2.747 +qed
2.748 +
2.749  end
```
```     3.1 --- a/src/HOL/Analysis/Henstock_Kurzweil_Integration.thy	Tue Apr 17 18:04:49 2018 +0100
3.2 +++ b/src/HOL/Analysis/Henstock_Kurzweil_Integration.thy	Tue Apr 17 22:35:48 2018 +0100
3.3 @@ -5069,6 +5069,56 @@
3.4        norm (integral (cbox a b) (\<lambda>x. if x \<in> s then f x else 0) - integral (cbox c d)  (\<lambda>x. if x \<in> s then f x else 0)) < e"
3.5    using assms[unfolded integrable_alt[of f]] by auto
3.6
3.7 +lemma integrable_alt_subset:
3.8 +  fixes f :: "'a::euclidean_space \<Rightarrow> 'b::banach"
3.9 +  shows
3.10 +     "f integrable_on S \<longleftrightarrow>
3.11 +      (\<forall>a b. (\<lambda>x. if x \<in> S then f x else 0) integrable_on cbox a b) \<and>
3.12 +      (\<forall>e>0. \<exists>B>0. \<forall>a b c d.
3.13 +                      ball 0 B \<subseteq> cbox a b \<and> cbox a b \<subseteq> cbox c d
3.14 +                      \<longrightarrow> norm(integral (cbox a b) (\<lambda>x. if x \<in> S then f x else 0) -
3.15 +                               integral (cbox c d) (\<lambda>x. if x \<in> S then f x else 0)) < e)"
3.16 +      (is "_ = ?rhs")
3.17 +proof -
3.18 +  let ?g = "\<lambda>x. if x \<in> S then f x else 0"
3.19 +  have "f integrable_on S \<longleftrightarrow>
3.20 +        (\<forall>a b. ?g integrable_on cbox a b) \<and>
3.21 +        (\<forall>e>0. \<exists>B>0. \<forall>a b c d. ball 0 B \<subseteq> cbox a b \<and> ball 0 B \<subseteq> cbox c d \<longrightarrow>
3.22 +           norm (integral (cbox a b) ?g - integral (cbox c d)  ?g) < e)"
3.23 +    by (rule integrable_alt)
3.24 +  also have "\<dots> = ?rhs"
3.25 +  proof -
3.26 +    { fix e :: "real"
3.27 +      assume e: "\<And>e. e>0 \<Longrightarrow> \<exists>B>0. \<forall>a b c d. ball 0 B \<subseteq> cbox a b \<and> cbox a b \<subseteq> cbox c d \<longrightarrow>
3.28 +                                   norm (integral (cbox a b) ?g - integral (cbox c d) ?g) < e"
3.29 +        and "e > 0"
3.30 +      obtain B where "B > 0"
3.31 +        and B: "\<And>a b c d. \<lbrakk>ball 0 B \<subseteq> cbox a b; cbox a b \<subseteq> cbox c d\<rbrakk> \<Longrightarrow>
3.32 +                           norm (integral (cbox a b) ?g - integral (cbox c d) ?g) < e/2"
3.33 +        using \<open>e > 0\<close> e [of "e/2"] by force
3.34 +      have "\<exists>B>0. \<forall>a b c d.
3.35 +               ball 0 B \<subseteq> cbox a b \<and> ball 0 B \<subseteq> cbox c d \<longrightarrow>
3.36 +               norm (integral (cbox a b) ?g - integral (cbox c d) ?g) < e"
3.37 +      proof (intro exI allI conjI impI)
3.38 +        fix a b c d :: "'a"
3.39 +        let ?\<alpha> = "\<Sum>i\<in>Basis. max (a \<bullet> i) (c \<bullet> i) *\<^sub>R i"
3.40 +        let ?\<beta> = "\<Sum>i\<in>Basis. min (b \<bullet> i) (d \<bullet> i) *\<^sub>R i"
3.41 +        show "norm (integral (cbox a b) ?g - integral (cbox c d) ?g) < e"
3.42 +          if ball: "ball 0 B \<subseteq> cbox a b \<and> ball 0 B \<subseteq> cbox c d"
3.43 +        proof -
3.44 +          have B': "norm (integral (cbox a b \<inter> cbox c d) ?g - integral (cbox x y) ?g) < e/2"
3.45 +            if "cbox a b \<inter> cbox c d \<subseteq> cbox x y" for x y
3.46 +            using B [of ?\<alpha> ?\<beta> x y] ball that by (simp add: Int_interval [symmetric])
3.47 +          show ?thesis
3.48 +            using B' [of a b] B' [of c d] norm_triangle_half_r by blast
3.49 +        qed
3.50 +      qed (use \<open>B > 0\<close> in auto)}
3.51 +  then show ?thesis
3.52 +    by force
3.53 +qed
3.54 +  finally show ?thesis .
3.55 +qed
3.56 +
3.57  lemma integrable_on_subcbox:
3.58    fixes f :: "'n::euclidean_space \<Rightarrow> 'a::banach"
3.59    assumes intf: "f integrable_on S"
```
```     4.1 --- a/src/HOL/Analysis/Lebesgue_Measure.thy	Tue Apr 17 18:04:49 2018 +0100
4.2 +++ b/src/HOL/Analysis/Lebesgue_Measure.thy	Tue Apr 17 22:35:48 2018 +0100
4.3 @@ -1168,29 +1168,27 @@
4.4    "compact S \<Longrightarrow> (\<And>c x. \<lbrakk>(c *\<^sub>R x) \<in> S; 0 \<le> c; x \<in> S\<rbrakk> \<Longrightarrow> c = 1) \<Longrightarrow> S \<in> null_sets lebesgue"
4.5    using starlike_negligible_bounded_gmeasurable[of S] by (auto simp: compact_eq_bounded_closed)
4.6
4.7 -lemma outer_regular_lborel:
4.8 -  assumes B: "B \<in> fmeasurable lborel" "0 < (e::real)"
4.9 -  shows "\<exists>U. open U \<and> B \<subseteq> U \<and> emeasure lborel U \<le> emeasure lborel B + e"
4.10 +proposition outer_regular_lborel_le:
4.11 +  assumes B[measurable]: "B \<in> sets borel" and "0 < (e::real)"
4.12 +  obtains U where "open U" "B \<subseteq> U" and "emeasure lborel (U - B) \<le> e"
4.13  proof -
4.14    let ?\<mu> = "emeasure lborel"
4.15    let ?B = "\<lambda>n::nat. ball 0 n :: 'a set"
4.16 -  have B[measurable]: "B \<in> sets borel"
4.17 -    using B by auto
4.18    let ?e = "\<lambda>n. e*((1/2)^Suc n)"
4.19    have "\<forall>n. \<exists>U. open U \<and> ?B n \<inter> B \<subseteq> U \<and> ?\<mu> (U - B) < ?e n"
4.20    proof
4.21      fix n :: nat
4.22      let ?A = "density lborel (indicator (?B n))"
4.23      have emeasure_A: "X \<in> sets borel \<Longrightarrow> emeasure ?A X = ?\<mu> (?B n \<inter> X)" for X
4.24 -      by (auto simp add: emeasure_density borel_measurable_indicator indicator_inter_arith[symmetric])
4.25 +      by (auto simp: emeasure_density borel_measurable_indicator indicator_inter_arith[symmetric])
4.26
4.27      have finite_A: "emeasure ?A (space ?A) \<noteq> \<infinity>"
4.28 -      using emeasure_bounded_finite[of "?B n"] by (auto simp add: emeasure_A)
4.29 +      using emeasure_bounded_finite[of "?B n"] by (auto simp: emeasure_A)
4.30      interpret A: finite_measure ?A
4.31        by rule fact
4.32      have "emeasure ?A B + ?e n > (INF U:{U. B \<subseteq> U \<and> open U}. emeasure ?A U)"
4.33        using \<open>0<e\<close> by (auto simp: outer_regular[OF _ finite_A B, symmetric])
4.34 -    then obtain U where U: "B \<subseteq> U" "open U" "?\<mu> (?B n \<inter> B) + ?e n > ?\<mu> (?B n \<inter> U)"
4.35 +    then obtain U where U: "B \<subseteq> U" "open U" and muU: "?\<mu> (?B n \<inter> B) + ?e n > ?\<mu> (?B n \<inter> U)"
4.36        unfolding INF_less_iff by (auto simp: emeasure_A)
4.37      moreover
4.38      { have "?\<mu> ((?B n \<inter> U) - B) = ?\<mu> ((?B n \<inter> U) - (?B n \<inter> B))"
4.39 @@ -1199,7 +1197,7 @@
4.40          using U A.emeasure_finite[of B]
4.41          by (intro emeasure_Diff) (auto simp del: A.emeasure_finite simp: emeasure_A)
4.42        also have "\<dots> < ?e n"
4.43 -        using U(1,2,3) A.emeasure_finite[of B]
4.44 +        using U muU A.emeasure_finite[of B]
4.45          by (subst minus_less_iff_ennreal)
4.46            (auto simp del: A.emeasure_finite simp: emeasure_A less_top ac_simps intro!: emeasure_mono)
4.47        finally have "?\<mu> ((?B n \<inter> U) - B) < ?e n" . }
4.48 @@ -1209,50 +1207,93 @@
4.49    then obtain U
4.50      where U: "\<And>n. open (U n)" "\<And>n. ?B n \<inter> B \<subseteq> U n" "\<And>n. ?\<mu> (U n - B) < ?e n"
4.51      by metis
4.52 -  then show ?thesis
4.53 -  proof (intro exI conjI)
4.54 +  show ?thesis
4.55 +  proof
4.56      { fix x assume "x \<in> B"
4.57        moreover
4.58 -      have "\<exists>n. norm x < real n"
4.59 -        by (simp add: reals_Archimedean2)
4.60 -      then guess n ..
4.61 +      obtain n where "norm x < real n"
4.62 +        using reals_Archimedean2 by blast
4.63        ultimately have "x \<in> (\<Union>n. U n)"
4.64          using U(2)[of n] by auto }
4.65      note * = this
4.66      then show "open (\<Union>n. U n)" "B \<subseteq> (\<Union>n. U n)"
4.67 -      using U(1,2) by auto
4.68 -    have "?\<mu> (\<Union>n. U n) = ?\<mu> (B \<union> (\<Union>n. U n - B))"
4.69 -      using * U(2) by (intro arg_cong[where ?f="?\<mu>"]) auto
4.70 -    also have "\<dots> = ?\<mu> B + ?\<mu> (\<Union>n. U n - B)"
4.71 -      using U(1) by (intro plus_emeasure[symmetric]) auto
4.72 -    also have "\<dots> \<le> ?\<mu> B + (\<Sum>n. ?\<mu> (U n - B))"
4.73 -      using U(1) by (intro add_mono emeasure_subadditive_countably) auto
4.74 -    also have "\<dots> \<le> ?\<mu> B + (\<Sum>n. ennreal (?e n))"
4.75 -      using U(3) by (intro add_mono suminf_le) (auto intro: less_imp_le)
4.76 -    also have "(\<Sum>n. ennreal (?e n)) = ennreal (e * 1)"
4.77 +      using U by auto
4.78 +    have "?\<mu> (\<Union>n. U n - B) \<le> (\<Sum>n. ?\<mu> (U n - B))"
4.79 +      using U(1) by (intro emeasure_subadditive_countably) auto
4.80 +    also have "\<dots> \<le> (\<Sum>n. ennreal (?e n))"
4.81 +      using U(3) by (intro suminf_le) (auto intro: less_imp_le)
4.82 +    also have "\<dots> = ennreal (e * 1)"
4.83        using \<open>0<e\<close> by (intro suminf_ennreal_eq sums_mult power_half_series) auto
4.84 -    finally show "emeasure lborel (\<Union>n. U n) \<le> emeasure lborel B + ennreal e"
4.85 +    finally show "emeasure lborel ((\<Union>n. U n) - B) \<le> ennreal e"
4.86        by simp
4.87    qed
4.88  qed
4.89
4.90 -lemma lmeasurable_outer_open:
4.91 -  assumes S: "S \<in> lmeasurable" and "0 < e"
4.92 -  obtains T where "open T" "S \<subseteq> T" "T \<in> lmeasurable" "measure lebesgue T \<le> measure lebesgue S + e"
4.93 +lemma outer_regular_lborel:
4.94 +  assumes B: "B \<in> sets borel" and "0 < (e::real)"
4.95 +  obtains U where "open U" "B \<subseteq> U" "emeasure lborel (U - B) < e"
4.96 +proof -
4.97 +  obtain U where U: "open U" "B \<subseteq> U" and "emeasure lborel (U-B) \<le> e/2"
4.98 +    using outer_regular_lborel_le [OF B, of "e/2"] \<open>e > 0\<close>
4.99 +    by force
4.100 +  moreover have "ennreal (e/2) < ennreal e"
4.101 +    using \<open>e > 0\<close> by (simp add: ennreal_lessI)
4.102 +  ultimately have "emeasure lborel (U-B) < e"
4.103 +    by auto
4.104 +  with U show ?thesis
4.105 +    using that by auto
4.106 +qed
4.107 +
4.108 +lemma completion_upper:
4.109 +  assumes A: "A \<in> sets (completion M)"
4.110 +  obtains A' where "A \<subseteq> A'" "A' \<in> sets M" "A' - A \<in> null_sets (completion M)"
4.111 +                   "emeasure (completion M) A = emeasure M A'"
4.112  proof -
4.113 -  obtain S' where S': "S \<subseteq> S'" "S' \<in> sets borel" "emeasure lborel S' = emeasure lebesgue S"
4.114 +  from AE_notin_null_part[OF A] obtain N where N: "N \<in> null_sets M" "null_part M A \<subseteq> N"
4.115 +    unfolding eventually_ae_filter using null_part_null_sets[OF A, THEN null_setsD2, THEN sets.sets_into_space] by auto
4.116 +  let ?A' = "main_part M A \<union> N"
4.117 +  show ?thesis
4.118 +  proof
4.119 +    show "A \<subseteq> ?A'"
4.120 +      using \<open>null_part M A \<subseteq> N\<close> by (subst main_part_null_part_Un[symmetric, OF A]) auto
4.121 +    have "main_part M A \<subseteq> A"
4.122 +      using assms main_part_null_part_Un by auto
4.123 +    then have "?A' - A \<subseteq> N"
4.124 +      by blast
4.125 +    with N show "?A' - A \<in> null_sets (completion M)"
4.126 +      by (blast intro: null_sets_completionI completion.complete_measure_axioms complete_measure.complete2)
4.127 +    show "emeasure (completion M) A = emeasure M (main_part M A \<union> N)"
4.128 +      using A \<open>N \<in> null_sets M\<close> by (simp add: emeasure_Un_null_set)
4.129 +  qed (use A N in auto)
4.130 +qed
4.131 +
4.132 +lemma lmeasurable_outer_open:
4.133 +  assumes S: "S \<in> sets lebesgue" and "e > 0"
4.134 +  obtains T where "open T" "S \<subseteq> T" "(T - S) \<in> lmeasurable" "measure lebesgue (T - S) < e"
4.135 +proof -
4.136 +  obtain S' where S': "S \<subseteq> S'" "S' \<in> sets borel"
4.137 +              and null: "S' - S \<in> null_sets lebesgue"
4.138 +              and em: "emeasure lebesgue S = emeasure lborel S'"
4.139      using completion_upper[of S lborel] S by auto
4.140 -  then have f_S': "S' \<in> fmeasurable lborel"
4.141 +  then have f_S': "S' \<in> sets borel"
4.142      using S by (auto simp: fmeasurable_def)
4.143 -  from outer_regular_lborel[OF this \<open>0<e\<close>] guess U .. note U = this
4.144 +  with outer_regular_lborel[OF _ \<open>0<e\<close>]
4.145 +  obtain U where U: "open U" "S' \<subseteq> U" "emeasure lborel (U - S') < e"
4.146 +    by blast
4.147    show thesis
4.148 -  proof (rule that)
4.149 -    show "open U" "S \<subseteq> U" "U \<in> lmeasurable"
4.150 -      using f_S' U S' by (auto simp: fmeasurable_def less_top[symmetric] top_unique)
4.151 -    then have "U \<in> fmeasurable lborel"
4.152 -      by (auto simp: fmeasurable_def)
4.153 -    with S U \<open>0<e\<close> show "measure lebesgue U \<le> measure lebesgue S + e"
4.154 -      unfolding S'(3) by (simp add: emeasure_eq_measure2 ennreal_plus[symmetric] del: ennreal_plus)
4.155 +  proof
4.156 +    show "open U" "S \<subseteq> U"
4.157 +      using f_S' U S' by auto
4.158 +  have "(U - S) = (U - S') \<union> (S' - S)"
4.159 +    using S' U by auto
4.160 +  then have eq: "emeasure lebesgue (U - S) = emeasure lborel (U - S')"
4.161 +    using null  by (simp add: U(1) emeasure_Un_null_set f_S' sets.Diff)
4.162 +  have "(U - S) \<in> sets lebesgue"
4.163 +    by (simp add: S U(1) sets.Diff)
4.164 +  then show "(U - S) \<in> lmeasurable"
4.165 +    unfolding fmeasurable_def using U(3) eq less_le_trans by fastforce
4.166 +  with eq U show "measure lebesgue (U - S) < e"
4.167 +    by (metis \<open>U - S \<in> lmeasurable\<close> emeasure_eq_measure2 ennreal_leI not_le)
4.168    qed
4.169  qed
4.170
```
```     5.1 --- a/src/HOL/Analysis/Vitali_Covering_Theorem.thy	Tue Apr 17 18:04:49 2018 +0100
5.2 +++ b/src/HOL/Analysis/Vitali_Covering_Theorem.thy	Tue Apr 17 22:35:48 2018 +0100
5.3 @@ -629,7 +629,6 @@
5.4      by metis
5.5  qed
5.6
5.7 -
5.8  proposition negligible_eq_zero_density:
5.9     "negligible S \<longleftrightarrow>
5.10      (\<forall>x\<in>S. \<forall>r>0. \<forall>e>0. \<exists>d. 0 < d \<and> d \<le> r \<and>
```