Rewrite the Probability theory.
authorhoelzl
Mon Aug 23 19:35:57 2010 +0200 (2010-08-23)
changeset 38656d5d342611edb
parent 38655 5001ed24e129
child 38657 2e0ebdaac59b
child 38666 12096ea0cc1c
child 38705 aaee86c0e237
Rewrite the Probability theory.

Introduced pinfreal as real numbers with infinity.
Use pinfreal as value for measures.
Introduces Lebesgue Measure based on the integral in Multivariate Analysis.
Proved Radon Nikodym for arbitrary sigma finite measure spaces.
CONTRIBUTORS
NEWS
src/HOL/IsaMakefile
src/HOL/Library/FuncSet.thy
src/HOL/Multivariate_Analysis/Cartesian_Euclidean_Space.thy
src/HOL/Multivariate_Analysis/Euclidean_Space.thy
src/HOL/Multivariate_Analysis/Finite_Cartesian_Product.thy
src/HOL/Multivariate_Analysis/Gauge_Measure.thy
src/HOL/Multivariate_Analysis/Integration.thy
src/HOL/Multivariate_Analysis/Multivariate_Analysis.thy
src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy
src/HOL/Probability/Borel.thy
src/HOL/Probability/Caratheodory.thy
src/HOL/Probability/Euclidean_Lebesgue.thy
src/HOL/Probability/Information.thy
src/HOL/Probability/Lebesgue.thy
src/HOL/Probability/Lebesgue_Integration.thy
src/HOL/Probability/Lebesgue_Measure.thy
src/HOL/Probability/Measure.thy
src/HOL/Probability/Positive_Infinite_Real.thy
src/HOL/Probability/Probability_Space.thy
src/HOL/Probability/Product_Measure.thy
src/HOL/Probability/Radon_Nikodym.thy
src/HOL/Probability/SeriesPlus.thy
src/HOL/Probability/Sigma_Algebra.thy
src/HOL/Probability/document/root.tex
src/HOL/Probability/ex/Dining_Cryptographers.thy
     1.1 --- a/CONTRIBUTORS	Mon Aug 23 17:46:13 2010 +0200
     1.2 +++ b/CONTRIBUTORS	Mon Aug 23 19:35:57 2010 +0200
     1.3 @@ -6,6 +6,9 @@
     1.4  Contributions to this Isabelle version
     1.5  --------------------------------------
     1.6  
     1.7 +* August 2010: Johannes Hoelzl, Armin Heller, and Robert Himmelmann, TUM
     1.8 +  Rewriting the Probability theory.
     1.9 +
    1.10  * July 2010: Florian Haftmann, TUM
    1.11    Reworking and extension of the Isabelle/HOL framework.
    1.12  
     2.1 --- a/NEWS	Mon Aug 23 17:46:13 2010 +0200
     2.2 +++ b/NEWS	Mon Aug 23 19:35:57 2010 +0200
     2.3 @@ -155,6 +155,13 @@
     2.4  
     2.5  INCOMPATIBILITY.
     2.6  
     2.7 +* Probability: Introduced pinfreal as real numbers with infinity. Use pinfreal
     2.8 +as value for measures. Introduces Lebesgue Measure based on the integral in
     2.9 +Multivariate Analysis. Proved Radon Nikodym for arbitrary sigma finite measure
    2.10 +spaces.
    2.11 +
    2.12 + INCOMPATIBILITY.
    2.13 +
    2.14  * Inductive package: offers new command "inductive_simps" to automatically
    2.15  derive instantiated and simplified equations for inductive predicates,
    2.16  similar to inductive_cases.
     3.1 --- a/src/HOL/IsaMakefile	Mon Aug 23 17:46:13 2010 +0200
     3.2 +++ b/src/HOL/IsaMakefile	Mon Aug 23 19:35:57 2010 +0200
     3.3 @@ -1104,6 +1104,7 @@
     3.4    Multivariate_Analysis/Finite_Cartesian_Product.thy			\
     3.5    Multivariate_Analysis/Integration.certs				\
     3.6    Multivariate_Analysis/Integration.thy					\
     3.7 +  Multivariate_Analysis/Gauge_Measure.thy					\
     3.8    Multivariate_Analysis/L2_Norm.thy					\
     3.9    Multivariate_Analysis/Multivariate_Analysis.thy			\
    3.10    Multivariate_Analysis/Operator_Norm.thy				\
    3.11 @@ -1121,20 +1122,19 @@
    3.12  
    3.13  ## HOL-Probability
    3.14  
    3.15 -HOL-Probability: HOL $(OUT)/HOL-Probability
    3.16 +HOL-Probability: HOL-Multivariate_Analysis $(OUT)/HOL-Probability
    3.17  
    3.18 -$(OUT)/HOL-Probability: $(OUT)/HOL Probability/ROOT.ML			\
    3.19 +$(OUT)/HOL-Probability: $(OUT)/HOL-Multivariate_Analysis Probability/ROOT.ML	\
    3.20    Probability/Probability.thy Probability/Sigma_Algebra.thy		\
    3.21 -  Probability/SeriesPlus.thy Probability/Caratheodory.thy		\
    3.22 +  Probability/Caratheodory.thy		\
    3.23    Probability/Borel.thy Probability/Measure.thy				\
    3.24 -  Probability/Lebesgue.thy Probability/Product_Measure.thy		\
    3.25 +  Probability/Lebesgue_Integration.thy Probability/Lebesgue_Measure.thy		\
    3.26 +  Probability/Positive_Infinite_Real.thy Probability/Product_Measure.thy	\
    3.27    Probability/Probability_Space.thy Probability/Information.thy		\
    3.28    Probability/ex/Dining_Cryptographers.thy Library/FuncSet.thy		\
    3.29 -  Library/Convex.thy Library/Product_Vector.thy 			\
    3.30 -  Library/Product_plus.thy Library/Inner_Product.thy			\
    3.31 -  Library/Nat_Bijection.thy
    3.32 -	@cd Probability; $(ISABELLE_TOOL) usedir -b -g true $(OUT)/HOL HOL-Probability
    3.33 -
    3.34 +  Probability/Lebesgue_Measure.thy \
    3.35 +  Library/Nat_Bijection.thy Library/Countable.thy
    3.36 +	@cd Probability; $(ISABELLE_TOOL) usedir -b -g true $(OUT)/HOL-Multivariate_Analysis HOL-Probability
    3.37  
    3.38  ## HOL-Nominal
    3.39  
     4.1 --- a/src/HOL/Library/FuncSet.thy	Mon Aug 23 17:46:13 2010 +0200
     4.2 +++ b/src/HOL/Library/FuncSet.thy	Mon Aug 23 19:35:57 2010 +0200
     4.3 @@ -67,6 +67,10 @@
     4.4    "f : Pi A B ==> (f x : B x ==> Q) ==> (x ~: A ==> Q) ==> Q"
     4.5  by(auto simp: Pi_def)
     4.6  
     4.7 +lemma Pi_cong:
     4.8 +  "(\<And> w. w \<in> A \<Longrightarrow> f w = g w) \<Longrightarrow> f \<in> Pi A B \<longleftrightarrow> g \<in> Pi A B"
     4.9 +  by (auto simp: Pi_def)
    4.10 +
    4.11  lemma funcset_id [simp]: "(\<lambda>x. x) \<in> A \<rightarrow> A"
    4.12    by (auto intro: Pi_I)
    4.13  
     5.1 --- a/src/HOL/Multivariate_Analysis/Cartesian_Euclidean_Space.thy	Mon Aug 23 17:46:13 2010 +0200
     5.2 +++ b/src/HOL/Multivariate_Analysis/Cartesian_Euclidean_Space.thy	Mon Aug 23 19:35:57 2010 +0200
     5.3 @@ -5,149 +5,6 @@
     5.4  imports Finite_Cartesian_Product Integration
     5.5  begin
     5.6  
     5.7 -instantiation prod :: (real_basis, real_basis) real_basis
     5.8 -begin
     5.9 -
    5.10 -definition "basis i = (if i < DIM('a) then (basis i, 0) else (0, basis (i - DIM('a))))"
    5.11 -
    5.12 -instance
    5.13 -proof
    5.14 -  let ?b = "basis :: nat \<Rightarrow> 'a \<times> 'b"
    5.15 -  let ?b_a = "basis :: nat \<Rightarrow> 'a"
    5.16 -  let ?b_b = "basis :: nat \<Rightarrow> 'b"
    5.17 -
    5.18 -  note image_range =
    5.19 -    image_add_atLeastLessThan[symmetric, of 0 "DIM('a)" "DIM('b)", simplified]
    5.20 -
    5.21 -  have split_range:
    5.22 -    "{..<DIM('b) + DIM('a)} = {..<DIM('a)} \<union> {DIM('a)..<DIM('b) + DIM('a)}"
    5.23 -    by auto
    5.24 -  have *: "?b ` {DIM('a)..<DIM('b) + DIM('a)} = {0} \<times> (?b_b ` {..<DIM('b)})"
    5.25 -    "?b ` {..<DIM('a)} = (?b_a ` {..<DIM('a)}) \<times> {0}"
    5.26 -    unfolding image_range image_image basis_prod_def_raw range_basis
    5.27 -    by (auto simp: zero_prod_def basis_eq_0_iff)
    5.28 -  hence b_split:
    5.29 -    "?b ` {..<DIM('b) + DIM('a)} = (?b_a ` {..<DIM('a)}) \<times> {0} \<union> {0} \<times> (?b_b ` {..<DIM('b)})" (is "_ = ?prod")
    5.30 -    by (subst split_range) (simp add: image_Un)
    5.31 -
    5.32 -  have b_0: "?b ` {DIM('b) + DIM('a)..} = {0}" unfolding basis_prod_def_raw
    5.33 -    by (auto simp: zero_prod_def image_iff basis_eq_0_iff elim!: ballE[of _ _ "DIM('a) + DIM('b)"])
    5.34 -
    5.35 -  have split_UNIV:
    5.36 -    "UNIV = {..<DIM('b) + DIM('a)} \<union> {DIM('b)+DIM('a)..}"
    5.37 -    by auto
    5.38 -
    5.39 -  have range_b: "range ?b = ?prod \<union> {0}"
    5.40 -    by (subst split_UNIV) (simp add: image_Un b_split b_0)
    5.41 -
    5.42 -  have prod: "\<And>f A B. setsum f (A \<times> B) = (\<Sum>a\<in>A. \<Sum>b\<in>B. f (a, b))"
    5.43 -    by (simp add: setsum_cartesian_product)
    5.44 -
    5.45 -  show "span (range ?b) = UNIV"
    5.46 -    unfolding span_explicit range_b
    5.47 -  proof safe
    5.48 -    fix a::'a and b::'b
    5.49 -    from in_span_basis[of a] in_span_basis[of b]
    5.50 -    obtain Sa ua Sb ub where span:
    5.51 -        "finite Sa" "Sa \<subseteq> basis ` {..<DIM('a)}" "a = (\<Sum>v\<in>Sa. ua v *\<^sub>R v)"
    5.52 -        "finite Sb" "Sb \<subseteq> basis ` {..<DIM('b)}" "b = (\<Sum>v\<in>Sb. ub v *\<^sub>R v)"
    5.53 -      unfolding span_explicit by auto
    5.54 -
    5.55 -    let ?S = "((Sa - {0}) \<times> {0} \<union> {0} \<times> (Sb - {0}))"
    5.56 -    have *:
    5.57 -      "?S \<inter> {v. fst v = 0} \<inter> {v. snd v = 0} = {}"
    5.58 -      "?S \<inter> - {v. fst v = 0} \<inter> {v. snd v = 0} = (Sa - {0}) \<times> {0}"
    5.59 -      "?S \<inter> {v. fst v = 0} \<inter> - {v. snd v = 0} = {0} \<times> (Sb - {0})"
    5.60 -      by (auto simp: zero_prod_def)
    5.61 -    show "\<exists>S u. finite S \<and> S \<subseteq> ?prod \<union> {0} \<and> (\<Sum>v\<in>S. u v *\<^sub>R v) = (a, b)"
    5.62 -      apply (rule exI[of _ ?S])
    5.63 -      apply (rule exI[of _ "\<lambda>(v, w). (if w = 0 then ua v else 0) + (if v = 0 then ub w else 0)"])
    5.64 -      using span
    5.65 -      apply (simp add: prod_case_unfold setsum_addf if_distrib cond_application_beta setsum_cases prod *)
    5.66 -      by (auto simp add: setsum_prod intro!: setsum_mono_zero_cong_left)
    5.67 -  qed simp
    5.68 -
    5.69 -  show "\<exists>d>0. ?b ` {d..} = {0} \<and> independent (?b ` {..<d}) \<and> inj_on ?b {..<d}"
    5.70 -    apply (rule exI[of _ "DIM('b) + DIM('a)"]) unfolding b_0
    5.71 -  proof (safe intro!: DIM_positive del: notI)
    5.72 -    show inj_on: "inj_on ?b {..<DIM('b) + DIM('a)}" unfolding split_range
    5.73 -      using inj_on_iff[OF basis_inj[where 'a='a]] inj_on_iff[OF basis_inj[where 'a='b]]
    5.74 -      by (auto intro!: inj_onI simp: basis_prod_def basis_eq_0_iff)
    5.75 -
    5.76 -    show "independent (?b ` {..<DIM('b) + DIM('a)})"
    5.77 -      unfolding independent_eq_inj_on[OF inj_on]
    5.78 -    proof safe
    5.79 -      fix i u assume i_upper: "i < DIM('b) + DIM('a)" and
    5.80 -          "(\<Sum>j\<in>{..<DIM('b) + DIM('a)} - {i}. u (?b j) *\<^sub>R ?b j) = ?b i" (is "?SUM = _")
    5.81 -      let ?left = "{..<DIM('a)}" and ?right = "{DIM('a)..<DIM('b) + DIM('a)}"
    5.82 -      show False
    5.83 -      proof cases
    5.84 -        assume "i < DIM('a)"
    5.85 -        hence "(basis i, 0) = ?SUM" unfolding `?SUM = ?b i` unfolding basis_prod_def by auto
    5.86 -        also have "\<dots> = (\<Sum>j\<in>?left - {i}. u (?b j) *\<^sub>R ?b j) +
    5.87 -          (\<Sum>j\<in>?right. u (?b j) *\<^sub>R ?b j)"
    5.88 -          using `i < DIM('a)` by (subst setsum_Un_disjoint[symmetric]) (auto intro!: setsum_cong)
    5.89 -        also have "\<dots> =  (\<Sum>j\<in>?left - {i}. u (?b_a j, 0) *\<^sub>R (?b_a j, 0)) +
    5.90 -          (\<Sum>j\<in>?right. u (0, ?b_b (j-DIM('a))) *\<^sub>R (0, ?b_b (j-DIM('a))))"
    5.91 -          unfolding basis_prod_def by auto
    5.92 -        finally have "basis i = (\<Sum>j\<in>?left - {i}. u (?b_a j, 0) *\<^sub>R ?b_a j)"
    5.93 -          by (simp add: setsum_prod)
    5.94 -        moreover
    5.95 -        note independent_basis[where 'a='a, unfolded independent_eq_inj_on[OF basis_inj]]
    5.96 -        note this[rule_format, of i "\<lambda>v. u (v, 0)"]
    5.97 -        ultimately show False using `i < DIM('a)` by auto
    5.98 -      next
    5.99 -        let ?i = "i - DIM('a)"
   5.100 -        assume not: "\<not> i < DIM('a)" hence "DIM('a) \<le> i" by auto
   5.101 -        hence "?i < DIM('b)" using `i < DIM('b) + DIM('a)` by auto
   5.102 -
   5.103 -        have inj_on: "inj_on (\<lambda>j. j - DIM('a)) {DIM('a)..<DIM('b) + DIM('a)}"
   5.104 -          by (auto intro!: inj_onI)
   5.105 -        with i_upper not have *: "{..<DIM('b)} - {?i} = (\<lambda>j. j-DIM('a))`(?right - {i})"
   5.106 -          by (auto simp: inj_on_image_set_diff image_minus_const_atLeastLessThan_nat)
   5.107 -
   5.108 -        have "(0, basis ?i) = ?SUM" unfolding `?SUM = ?b i`
   5.109 -          unfolding basis_prod_def using not `?i < DIM('b)` by auto
   5.110 -        also have "\<dots> = (\<Sum>j\<in>?left. u (?b j) *\<^sub>R ?b j) +
   5.111 -          (\<Sum>j\<in>?right - {i}. u (?b j) *\<^sub>R ?b j)"
   5.112 -          using not by (subst setsum_Un_disjoint[symmetric]) (auto intro!: setsum_cong)
   5.113 -        also have "\<dots> =  (\<Sum>j\<in>?left. u (?b_a j, 0) *\<^sub>R (?b_a j, 0)) +
   5.114 -          (\<Sum>j\<in>?right - {i}. u (0, ?b_b (j-DIM('a))) *\<^sub>R (0, ?b_b (j-DIM('a))))"
   5.115 -          unfolding basis_prod_def by auto
   5.116 -        finally have "basis ?i = (\<Sum>j\<in>{..<DIM('b)} - {?i}. u (0, ?b_b j) *\<^sub>R ?b_b j)"
   5.117 -          unfolding *
   5.118 -          by (subst setsum_reindex[OF inj_on[THEN subset_inj_on]])
   5.119 -             (auto simp: setsum_prod)
   5.120 -        moreover
   5.121 -        note independent_basis[where 'a='b, unfolded independent_eq_inj_on[OF basis_inj]]
   5.122 -        note this[rule_format, of ?i "\<lambda>v. u (0, v)"]
   5.123 -        ultimately show False using `?i < DIM('b)` by auto
   5.124 -      qed
   5.125 -    qed
   5.126 -  qed
   5.127 -qed
   5.128 -end
   5.129 -
   5.130 -lemma DIM_prod[simp]: "DIM('a \<times> 'b) = DIM('b::real_basis) + DIM('a::real_basis)"
   5.131 -  by (rule dimension_eq) (auto simp: basis_prod_def zero_prod_def basis_eq_0_iff)
   5.132 -
   5.133 -instance prod :: (euclidean_space, euclidean_space) euclidean_space
   5.134 -proof (default, safe)
   5.135 -  let ?b = "basis :: nat \<Rightarrow> 'a \<times> 'b"
   5.136 -  fix i j assume "i < DIM('a \<times> 'b)" "j < DIM('a \<times> 'b)"
   5.137 -  thus "?b i \<bullet> ?b j = (if i = j then 1 else 0)"
   5.138 -    unfolding basis_prod_def by (auto simp: dot_basis)
   5.139 -qed
   5.140 -
   5.141 -instantiation prod :: (ordered_euclidean_space, ordered_euclidean_space) ordered_euclidean_space
   5.142 -begin
   5.143 -
   5.144 -definition "x \<le> (y::('a\<times>'b)) \<longleftrightarrow> (\<forall>i<DIM('a\<times>'b). x $$ i \<le> y $$ i)"
   5.145 -definition "x < (y::('a\<times>'b)) \<longleftrightarrow> (\<forall>i<DIM('a\<times>'b). x $$ i < y $$ i)"
   5.146 -
   5.147 -instance proof qed (auto simp: less_prod_def less_eq_prod_def)
   5.148 -end
   5.149 -
   5.150  lemma delta_mult_idempotent:
   5.151    "(if k=a then 1 else (0::'a::semiring_1)) * (if k=a then 1 else 0) = (if k=a then 1 else 0)" by (cases "k=a", auto)
   5.152  
   5.153 @@ -1450,10 +1307,6 @@
   5.154    unfolding nth_conv_component
   5.155    using component_le_infnorm[of x] .
   5.156  
   5.157 -lemma dist_nth_le_cart: "dist (x $ i) (y $ i) \<le> dist x y"
   5.158 -  unfolding dist_vector_def
   5.159 -  by (rule member_le_setL2) simp_all
   5.160 -
   5.161  instance cart :: (perfect_space, finite) perfect_space
   5.162  proof
   5.163    fix x :: "'a ^ 'b"
     6.1 --- a/src/HOL/Multivariate_Analysis/Euclidean_Space.thy	Mon Aug 23 17:46:13 2010 +0200
     6.2 +++ b/src/HOL/Multivariate_Analysis/Euclidean_Space.thy	Mon Aug 23 19:35:57 2010 +0200
     6.3 @@ -1934,6 +1934,16 @@
     6.4    assumes eucl_le: "x \<le> y \<longleftrightarrow> (\<forall>i < DIM('a). x $$ i \<le> y $$ i)"
     6.5    and eucl_less: "x < y \<longleftrightarrow> (\<forall>i < DIM('a). x $$ i < y $$ i)"
     6.6  
     6.7 +lemma eucl_less_not_refl[simp, intro!]: "\<not> x < (x::'a::ordered_euclidean_space)"
     6.8 +  unfolding eucl_less[where 'a='a] by auto
     6.9 +
    6.10 +lemma euclidean_trans[trans]:
    6.11 +  fixes x y z :: "'a::ordered_euclidean_space"
    6.12 +  shows "x < y \<Longrightarrow> y < z \<Longrightarrow> x < z"
    6.13 +  and "x \<le> y \<Longrightarrow> y < z \<Longrightarrow> x < z"
    6.14 +  and "x \<le> y \<Longrightarrow> y \<le> z \<Longrightarrow> x \<le> z"
    6.15 +  by (force simp: eucl_less[where 'a='a] eucl_le[where 'a='a])+
    6.16 +
    6.17  subsection {* Linearity and Bilinearity continued *}
    6.18  
    6.19  lemma linear_bounded:
    6.20 @@ -3388,4 +3398,150 @@
    6.21  instance complex :: euclidean_space
    6.22    proof qed (auto simp add: basis_complex_def inner_complex_def)
    6.23  
    6.24 +section {* Products Spaces *}
    6.25 +
    6.26 +instantiation prod :: (real_basis, real_basis) real_basis
    6.27 +begin
    6.28 +
    6.29 +definition "basis i = (if i < DIM('a) then (basis i, 0) else (0, basis (i - DIM('a))))"
    6.30 +
    6.31 +instance
    6.32 +proof
    6.33 +  let ?b = "basis :: nat \<Rightarrow> 'a \<times> 'b"
    6.34 +  let ?b_a = "basis :: nat \<Rightarrow> 'a"
    6.35 +  let ?b_b = "basis :: nat \<Rightarrow> 'b"
    6.36 +
    6.37 +  note image_range =
    6.38 +    image_add_atLeastLessThan[symmetric, of 0 "DIM('a)" "DIM('b)", simplified]
    6.39 +
    6.40 +  have split_range:
    6.41 +    "{..<DIM('b) + DIM('a)} = {..<DIM('a)} \<union> {DIM('a)..<DIM('b) + DIM('a)}"
    6.42 +    by auto
    6.43 +  have *: "?b ` {DIM('a)..<DIM('b) + DIM('a)} = {0} \<times> (?b_b ` {..<DIM('b)})"
    6.44 +    "?b ` {..<DIM('a)} = (?b_a ` {..<DIM('a)}) \<times> {0}"
    6.45 +    unfolding image_range image_image basis_prod_def_raw range_basis
    6.46 +    by (auto simp: zero_prod_def basis_eq_0_iff)
    6.47 +  hence b_split:
    6.48 +    "?b ` {..<DIM('b) + DIM('a)} = (?b_a ` {..<DIM('a)}) \<times> {0} \<union> {0} \<times> (?b_b ` {..<DIM('b)})" (is "_ = ?prod")
    6.49 +    by (subst split_range) (simp add: image_Un)
    6.50 +
    6.51 +  have b_0: "?b ` {DIM('b) + DIM('a)..} = {0}" unfolding basis_prod_def_raw
    6.52 +    by (auto simp: zero_prod_def image_iff basis_eq_0_iff elim!: ballE[of _ _ "DIM('a) + DIM('b)"])
    6.53 +
    6.54 +  have split_UNIV:
    6.55 +    "UNIV = {..<DIM('b) + DIM('a)} \<union> {DIM('b)+DIM('a)..}"
    6.56 +    by auto
    6.57 +
    6.58 +  have range_b: "range ?b = ?prod \<union> {0}"
    6.59 +    by (subst split_UNIV) (simp add: image_Un b_split b_0)
    6.60 +
    6.61 +  have prod: "\<And>f A B. setsum f (A \<times> B) = (\<Sum>a\<in>A. \<Sum>b\<in>B. f (a, b))"
    6.62 +    by (simp add: setsum_cartesian_product)
    6.63 +
    6.64 +  show "span (range ?b) = UNIV"
    6.65 +    unfolding span_explicit range_b
    6.66 +  proof safe
    6.67 +    fix a::'a and b::'b
    6.68 +    from in_span_basis[of a] in_span_basis[of b]
    6.69 +    obtain Sa ua Sb ub where span:
    6.70 +        "finite Sa" "Sa \<subseteq> basis ` {..<DIM('a)}" "a = (\<Sum>v\<in>Sa. ua v *\<^sub>R v)"
    6.71 +        "finite Sb" "Sb \<subseteq> basis ` {..<DIM('b)}" "b = (\<Sum>v\<in>Sb. ub v *\<^sub>R v)"
    6.72 +      unfolding span_explicit by auto
    6.73 +
    6.74 +    let ?S = "((Sa - {0}) \<times> {0} \<union> {0} \<times> (Sb - {0}))"
    6.75 +    have *:
    6.76 +      "?S \<inter> {v. fst v = 0} \<inter> {v. snd v = 0} = {}"
    6.77 +      "?S \<inter> - {v. fst v = 0} \<inter> {v. snd v = 0} = (Sa - {0}) \<times> {0}"
    6.78 +      "?S \<inter> {v. fst v = 0} \<inter> - {v. snd v = 0} = {0} \<times> (Sb - {0})"
    6.79 +      by (auto simp: zero_prod_def)
    6.80 +    show "\<exists>S u. finite S \<and> S \<subseteq> ?prod \<union> {0} \<and> (\<Sum>v\<in>S. u v *\<^sub>R v) = (a, b)"
    6.81 +      apply (rule exI[of _ ?S])
    6.82 +      apply (rule exI[of _ "\<lambda>(v, w). (if w = 0 then ua v else 0) + (if v = 0 then ub w else 0)"])
    6.83 +      using span
    6.84 +      apply (simp add: prod_case_unfold setsum_addf if_distrib cond_application_beta setsum_cases prod *)
    6.85 +      by (auto simp add: setsum_prod intro!: setsum_mono_zero_cong_left)
    6.86 +  qed simp
    6.87 +
    6.88 +  show "\<exists>d>0. ?b ` {d..} = {0} \<and> independent (?b ` {..<d}) \<and> inj_on ?b {..<d}"
    6.89 +    apply (rule exI[of _ "DIM('b) + DIM('a)"]) unfolding b_0
    6.90 +  proof (safe intro!: DIM_positive del: notI)
    6.91 +    show inj_on: "inj_on ?b {..<DIM('b) + DIM('a)}" unfolding split_range
    6.92 +      using inj_on_iff[OF basis_inj[where 'a='a]] inj_on_iff[OF basis_inj[where 'a='b]]
    6.93 +      by (auto intro!: inj_onI simp: basis_prod_def basis_eq_0_iff)
    6.94 +
    6.95 +    show "independent (?b ` {..<DIM('b) + DIM('a)})"
    6.96 +      unfolding independent_eq_inj_on[OF inj_on]
    6.97 +    proof safe
    6.98 +      fix i u assume i_upper: "i < DIM('b) + DIM('a)" and
    6.99 +          "(\<Sum>j\<in>{..<DIM('b) + DIM('a)} - {i}. u (?b j) *\<^sub>R ?b j) = ?b i" (is "?SUM = _")
   6.100 +      let ?left = "{..<DIM('a)}" and ?right = "{DIM('a)..<DIM('b) + DIM('a)}"
   6.101 +      show False
   6.102 +      proof cases
   6.103 +        assume "i < DIM('a)"
   6.104 +        hence "(basis i, 0) = ?SUM" unfolding `?SUM = ?b i` unfolding basis_prod_def by auto
   6.105 +        also have "\<dots> = (\<Sum>j\<in>?left - {i}. u (?b j) *\<^sub>R ?b j) +
   6.106 +          (\<Sum>j\<in>?right. u (?b j) *\<^sub>R ?b j)"
   6.107 +          using `i < DIM('a)` by (subst setsum_Un_disjoint[symmetric]) (auto intro!: setsum_cong)
   6.108 +        also have "\<dots> =  (\<Sum>j\<in>?left - {i}. u (?b_a j, 0) *\<^sub>R (?b_a j, 0)) +
   6.109 +          (\<Sum>j\<in>?right. u (0, ?b_b (j-DIM('a))) *\<^sub>R (0, ?b_b (j-DIM('a))))"
   6.110 +          unfolding basis_prod_def by auto
   6.111 +        finally have "basis i = (\<Sum>j\<in>?left - {i}. u (?b_a j, 0) *\<^sub>R ?b_a j)"
   6.112 +          by (simp add: setsum_prod)
   6.113 +        moreover
   6.114 +        note independent_basis[where 'a='a, unfolded independent_eq_inj_on[OF basis_inj]]
   6.115 +        note this[rule_format, of i "\<lambda>v. u (v, 0)"]
   6.116 +        ultimately show False using `i < DIM('a)` by auto
   6.117 +      next
   6.118 +        let ?i = "i - DIM('a)"
   6.119 +        assume not: "\<not> i < DIM('a)" hence "DIM('a) \<le> i" by auto
   6.120 +        hence "?i < DIM('b)" using `i < DIM('b) + DIM('a)` by auto
   6.121 +
   6.122 +        have inj_on: "inj_on (\<lambda>j. j - DIM('a)) {DIM('a)..<DIM('b) + DIM('a)}"
   6.123 +          by (auto intro!: inj_onI)
   6.124 +        with i_upper not have *: "{..<DIM('b)} - {?i} = (\<lambda>j. j-DIM('a))`(?right - {i})"
   6.125 +          by (auto simp: inj_on_image_set_diff image_minus_const_atLeastLessThan_nat)
   6.126 +
   6.127 +        have "(0, basis ?i) = ?SUM" unfolding `?SUM = ?b i`
   6.128 +          unfolding basis_prod_def using not `?i < DIM('b)` by auto
   6.129 +        also have "\<dots> = (\<Sum>j\<in>?left. u (?b j) *\<^sub>R ?b j) +
   6.130 +          (\<Sum>j\<in>?right - {i}. u (?b j) *\<^sub>R ?b j)"
   6.131 +          using not by (subst setsum_Un_disjoint[symmetric]) (auto intro!: setsum_cong)
   6.132 +        also have "\<dots> =  (\<Sum>j\<in>?left. u (?b_a j, 0) *\<^sub>R (?b_a j, 0)) +
   6.133 +          (\<Sum>j\<in>?right - {i}. u (0, ?b_b (j-DIM('a))) *\<^sub>R (0, ?b_b (j-DIM('a))))"
   6.134 +          unfolding basis_prod_def by auto
   6.135 +        finally have "basis ?i = (\<Sum>j\<in>{..<DIM('b)} - {?i}. u (0, ?b_b j) *\<^sub>R ?b_b j)"
   6.136 +          unfolding *
   6.137 +          by (subst setsum_reindex[OF inj_on[THEN subset_inj_on]])
   6.138 +             (auto simp: setsum_prod)
   6.139 +        moreover
   6.140 +        note independent_basis[where 'a='b, unfolded independent_eq_inj_on[OF basis_inj]]
   6.141 +        note this[rule_format, of ?i "\<lambda>v. u (0, v)"]
   6.142 +        ultimately show False using `?i < DIM('b)` by auto
   6.143 +      qed
   6.144 +    qed
   6.145 +  qed
   6.146 +qed
   6.147  end
   6.148 +
   6.149 +lemma DIM_prod[simp]: "DIM('a \<times> 'b) = DIM('b::real_basis) + DIM('a::real_basis)"
   6.150 +  by (rule dimension_eq) (auto simp: basis_prod_def zero_prod_def basis_eq_0_iff)
   6.151 +
   6.152 +instance prod :: (euclidean_space, euclidean_space) euclidean_space
   6.153 +proof (default, safe)
   6.154 +  let ?b = "basis :: nat \<Rightarrow> 'a \<times> 'b"
   6.155 +  fix i j assume "i < DIM('a \<times> 'b)" "j < DIM('a \<times> 'b)"
   6.156 +  thus "?b i \<bullet> ?b j = (if i = j then 1 else 0)"
   6.157 +    unfolding basis_prod_def by (auto simp: dot_basis)
   6.158 +qed
   6.159 +
   6.160 +instantiation prod :: (ordered_euclidean_space, ordered_euclidean_space) ordered_euclidean_space
   6.161 +begin
   6.162 +
   6.163 +definition "x \<le> (y::('a\<times>'b)) \<longleftrightarrow> (\<forall>i<DIM('a\<times>'b). x $$ i \<le> y $$ i)"
   6.164 +definition "x < (y::('a\<times>'b)) \<longleftrightarrow> (\<forall>i<DIM('a\<times>'b). x $$ i < y $$ i)"
   6.165 +
   6.166 +instance proof qed (auto simp: less_prod_def less_eq_prod_def)
   6.167 +end
   6.168 +
   6.169 +
   6.170 +end
     7.1 --- a/src/HOL/Multivariate_Analysis/Finite_Cartesian_Product.thy	Mon Aug 23 17:46:13 2010 +0200
     7.2 +++ b/src/HOL/Multivariate_Analysis/Finite_Cartesian_Product.thy	Mon Aug 23 19:35:57 2010 +0200
     7.3 @@ -254,7 +254,7 @@
     7.4  definition dist_vector_def:
     7.5    "dist x y = setL2 (\<lambda>i. dist (x$i) (y$i)) UNIV"
     7.6  
     7.7 -lemma dist_nth_le: "dist (x $ i) (y $ i) \<le> dist x y"
     7.8 +lemma dist_nth_le_cart: "dist (x $ i) (y $ i) \<le> dist x y"
     7.9  unfolding dist_vector_def
    7.10  by (rule member_le_setL2) simp_all
    7.11  
    7.12 @@ -283,7 +283,7 @@
    7.13        apply (rule_tac x=e in exI, clarify)
    7.14        apply (drule spec, erule mp, clarify)
    7.15        apply (drule spec, drule spec, erule mp)
    7.16 -      apply (erule le_less_trans [OF dist_nth_le])
    7.17 +      apply (erule le_less_trans [OF dist_nth_le_cart])
    7.18       apply (subgoal_tac "\<forall>i\<in>UNIV. \<exists>e>0. \<forall>y. dist y (x$i) < e \<longrightarrow> y \<in> A i")
    7.19        apply (drule finite_choice [OF finite], clarify)
    7.20        apply (rule_tac x="Min (range f)" in exI, simp)
    7.21 @@ -315,7 +315,7 @@
    7.22  
    7.23  lemma Cauchy_Cart_nth:
    7.24    "Cauchy (\<lambda>n. X n) \<Longrightarrow> Cauchy (\<lambda>n. X n $ i)"
    7.25 -unfolding Cauchy_def by (fast intro: le_less_trans [OF dist_nth_le])
    7.26 +unfolding Cauchy_def by (fast intro: le_less_trans [OF dist_nth_le_cart])
    7.27  
    7.28  lemma Cauchy_vector:
    7.29    fixes X :: "nat \<Rightarrow> 'a::metric_space ^ 'n"
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/src/HOL/Multivariate_Analysis/Gauge_Measure.thy	Mon Aug 23 19:35:57 2010 +0200
     8.3 @@ -0,0 +1,3447 @@
     8.4 +
     8.5 +header {* Lebsegue measure (defined via the gauge integral). *}
     8.6 +(*  Author:                     John Harrison
     8.7 +    Translation from HOL light: Robert Himmelmann, TU Muenchen *)
     8.8 +
     8.9 +theory Gauge_Measure
    8.10 +  imports Integration 
    8.11 +begin
    8.12 +
    8.13 +(* ------------------------------------------------------------------------- *)
    8.14 +(* Lebesgue measure (in the case where the measure is finite).               *)
    8.15 +(* For the non-finite version, please see Probability/Lebesgue_Measure.thy   *)
    8.16 +(* ------------------------------------------------------------------------- *)
    8.17 +
    8.18 +definition has_gmeasure (infixr "has'_gmeasure" 80) where
    8.19 +  "s has_gmeasure m \<equiv> ((\<lambda>x. 1::real) has_integral m) s"
    8.20 +
    8.21 +definition gmeasurable :: "('n::ordered_euclidean_space) set \<Rightarrow> bool" where 
    8.22 +  "gmeasurable s \<equiv> (\<exists>m. s has_gmeasure m)"
    8.23 +
    8.24 +lemma gmeasurableI[dest]:"s has_gmeasure m \<Longrightarrow> gmeasurable s"
    8.25 +  unfolding gmeasurable_def by auto
    8.26 +
    8.27 +definition gmeasure where
    8.28 +  "gmeasure s \<equiv> (if gmeasurable s then (SOME m. s has_gmeasure m) else 0)"
    8.29 +
    8.30 +lemma has_gmeasure_measure: "gmeasurable s \<longleftrightarrow> s has_gmeasure (gmeasure s)"
    8.31 +  unfolding gmeasure_def gmeasurable_def
    8.32 +  apply meson apply(subst if_P) defer apply(rule someI) by auto
    8.33 +
    8.34 +lemma has_gmeasure_measureI[intro]:"gmeasurable s \<Longrightarrow> s has_gmeasure (gmeasure s)"
    8.35 +  using has_gmeasure_measure by auto
    8.36 +  
    8.37 +lemma has_gmeasure_unique: "s has_gmeasure m1 \<Longrightarrow> s has_gmeasure m2 \<Longrightarrow> m1 = m2"
    8.38 +  unfolding has_gmeasure_def apply(rule has_integral_unique) by auto
    8.39 +
    8.40 +lemma measure_unique[intro]: assumes "s has_gmeasure m" shows "gmeasure s = m"
    8.41 +  apply(rule has_gmeasure_unique[OF _ assms]) using assms 
    8.42 +  unfolding has_gmeasure_measure[THEN sym] by auto
    8.43 +
    8.44 +lemma has_gmeasure_measurable_measure:
    8.45 + "s has_gmeasure m \<longleftrightarrow> gmeasurable s \<and> gmeasure s = m"
    8.46 +  by(auto intro!:measure_unique simp:has_gmeasure_measure[THEN sym])
    8.47 +
    8.48 +lemmas has_gmeasure_imp_measurable = gmeasurableI
    8.49 +
    8.50 +lemma has_gmeasure:
    8.51 + "s has_gmeasure m \<longleftrightarrow> ((\<lambda>x. if x \<in> s then 1 else 0) has_integral m) UNIV"
    8.52 +  unfolding has_integral_restrict_univ has_gmeasure_def ..
    8.53 +
    8.54 +lemma gmeasurable: "gmeasurable s \<longleftrightarrow> (\<lambda>x. 1::real) integrable_on s"
    8.55 +  unfolding gmeasurable_def integrable_on_def has_gmeasure_def by auto
    8.56 +
    8.57 +lemma gmeasurable_integrable:
    8.58 + "gmeasurable s \<longleftrightarrow> (\<lambda>x. if x \<in> s then 1 else (0::real)) integrable_on UNIV"
    8.59 +  unfolding gmeasurable_def integrable_on_def has_gmeasure ..
    8.60 +
    8.61 +lemma measure_integral:
    8.62 +  assumes "gmeasurable s" shows "gmeasure s = (integral s (\<lambda>x. 1))"
    8.63 +  apply(rule integral_unique[THEN sym])
    8.64 +  unfolding has_gmeasure_def[symmetric] using assms by auto 
    8.65 +
    8.66 +lemma measure_integral_univ: assumes "gmeasurable s"
    8.67 +  shows "gmeasure s = (integral UNIV (\<lambda>x. if x \<in> s then 1 else 0))"
    8.68 +  apply(rule integral_unique[THEN sym])
    8.69 +  using assms by(auto simp:has_gmeasure[THEN sym])
    8.70 +
    8.71 +lemmas integral_measure = measure_integral[THEN sym]
    8.72 +
    8.73 +lemmas integral_measure_univ = measure_integral_univ[THEN sym]
    8.74 +
    8.75 +lemma has_gmeasure_interval[intro]:
    8.76 +  "{a..b} has_gmeasure content{a..b}" (is ?t1)
    8.77 +  "{a<..<b} has_gmeasure content{a..b}" (is ?t2)
    8.78 +proof- show ?t1 unfolding has_gmeasure_def using has_integral_const[where c="1::real"] by auto
    8.79 +  show ?t2 unfolding has_gmeasure apply(rule has_integral_spike[of "{a..b} - {a<..<b}",
    8.80 +    where f="\<lambda>x. (if x \<in> {a..b} then 1 else 0)"]) apply(rule negligible_frontier_interval) 
    8.81 +    using interval_open_subset_closed[of a b]
    8.82 +    using `?t1` unfolding has_gmeasure by auto
    8.83 +qed
    8.84 +
    8.85 +lemma gmeasurable_interval[intro]: "gmeasurable {a..b}" "gmeasurable {a<..<b}"
    8.86 +  by(auto intro:gmeasurableI)
    8.87 +
    8.88 +lemma measure_interval[simp]: "gmeasure{a..b} = content{a..b}"  "gmeasure({a<..<b}) = content{a..b}"
    8.89 +  by(auto intro:measure_unique)
    8.90 +
    8.91 +lemma nonnegative_absolutely_integrable: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'm::ordered_euclidean_space"
    8.92 +  assumes "\<forall>x\<in>s. \<forall>i<DIM('m). 0 \<le> f(x)$$i" "f integrable_on s"
    8.93 +  shows "f absolutely_integrable_on s"
    8.94 +  unfolding absolutely_integrable_abs_eq apply rule defer
    8.95 +  apply(rule integrable_eq[of _ f]) using assms apply-apply(subst euclidean_eq) by auto
    8.96 +
    8.97 +lemma gmeasurable_inter[dest]: assumes "gmeasurable s" "gmeasurable t" shows "gmeasurable (s \<inter> t)"
    8.98 +proof- have *:"(\<lambda>x. if x \<in> s \<inter> t then 1 else (0::real)) =
    8.99 +    (\<lambda>x. \<chi>\<chi> i. min (((if x \<in> s then 1 else 0)::real)$$i) (((if x \<in> t then 1 else 0)::real)$$i))"
   8.100 +    apply(rule ext) by auto
   8.101 +  show ?thesis unfolding gmeasurable_integrable apply(rule absolutely_integrable_onD)
   8.102 +    unfolding * apply(rule absolutely_integrable_min)
   8.103 +    apply(rule_tac[!] nonnegative_absolutely_integrable)
   8.104 +    using assms unfolding gmeasurable_integrable by auto
   8.105 +qed
   8.106 +
   8.107 +lemma gmeasurable_union: assumes "gmeasurable s" "gmeasurable t"
   8.108 +  shows "gmeasurable (s \<union> t)"
   8.109 +proof- have *:"(\<lambda>x. if x \<in> s \<union> t then 1 else (0::real)) =
   8.110 +    (\<lambda>x. \<chi>\<chi> i. max (((if x \<in> s then 1 else 0)::real)$$i) (((if x \<in> t then 1 else 0)::real)$$i)) "
   8.111 +    by(rule ext,auto)
   8.112 +  show ?thesis unfolding gmeasurable_integrable apply(rule absolutely_integrable_onD)
   8.113 +    unfolding * apply(rule absolutely_integrable_max)
   8.114 +    apply(rule_tac[!]nonnegative_absolutely_integrable)
   8.115 +    using assms unfolding gmeasurable_integrable by auto
   8.116 +qed
   8.117 +
   8.118 +lemma has_gmeasure_disjoint_union: 
   8.119 +  assumes "s1 has_gmeasure m1" "s2 has_gmeasure m2" "s1 \<inter> s2 = {}"
   8.120 +  shows "(s1 \<union> s2) has_gmeasure (m1 + m2)"
   8.121 +proof- have *:"\<And>x. (if x \<in> s1 then 1 else 0) + (if x \<in> s2 then 1 else 0) =
   8.122 +    (if x \<in> s1 \<union> s2 then 1 else (0::real))" using assms(3) by auto
   8.123 +  show ?thesis using has_integral_add[OF assms(1-2)[unfolded has_gmeasure]]
   8.124 +    unfolding has_gmeasure * .
   8.125 +qed
   8.126 +
   8.127 +lemma measure_disjoint_union: assumes "gmeasurable s" "gmeasurable t" "s \<inter> t = {}"
   8.128 +  shows "gmeasure(s \<union> t) = gmeasure s + gmeasure t"
   8.129 +  apply rule apply(rule has_gmeasure_disjoint_union) using assms by auto
   8.130 +
   8.131 +lemma has_gmeasure_pos_le[dest]: assumes "s has_gmeasure m" shows "0 \<le> m"
   8.132 +  apply(rule has_integral_nonneg) using assms unfolding has_gmeasure by auto
   8.133 +
   8.134 +lemma not_measurable_measure:"\<not> gmeasurable s \<Longrightarrow> gmeasure s = 0"
   8.135 +  unfolding gmeasure_def if_not_P ..
   8.136 +
   8.137 +lemma measure_pos_le[intro]: "0 <= gmeasure s"
   8.138 +  apply(cases "gmeasurable s") unfolding not_measurable_measure
   8.139 +  unfolding has_gmeasure_measure by auto
   8.140 +
   8.141 +lemma has_gmeasure_subset[dest]:
   8.142 +  assumes "s1 has_gmeasure m1" "s2 has_gmeasure m2" "s1 \<subseteq> s2"
   8.143 +  shows "m1 <= m2"
   8.144 +  using has_integral_subset_le[OF assms(3,1,2)[unfolded has_gmeasure_def]] by auto
   8.145 +
   8.146 +lemma measure_subset[dest]: assumes "gmeasurable s" "gmeasurable t" "s \<subseteq> t"
   8.147 +  shows "gmeasure s \<le> gmeasure t"
   8.148 +  using assms unfolding has_gmeasure_measure by auto
   8.149 +
   8.150 +lemma has_gmeasure_0:"s has_gmeasure 0 \<longleftrightarrow> negligible s" (is "?l = ?r")
   8.151 +proof assume ?r thus ?l unfolding indicator_def_raw negligible apply(erule_tac x="UNIV" in allE)
   8.152 +    unfolding has_integral_restrict_univ has_gmeasure_def .
   8.153 +next assume ?l note this[unfolded has_gmeasure_def has_integral_alt']
   8.154 +  note * = conjunctD2[OF this,rule_format]
   8.155 +  show ?r unfolding negligible_def 
   8.156 +  proof safe case goal1
   8.157 +    from *(1)[of a b,unfolded integrable_on_def] guess y apply-
   8.158 +      apply(subst (asm) has_integral_restrict_univ[THEN sym]) by (erule exE) note y=this
   8.159 +    have "0 \<le> y" apply(rule has_integral_nonneg[OF y]) by auto
   8.160 +    moreover have "y \<le> 0" apply(rule has_integral_le[OF y]) 
   8.161 +      apply(rule `?l`[unfolded has_gmeasure_def has_integral_restrict_univ[THEN sym,of"\<lambda>x. 1"]]) by auto
   8.162 +    ultimately have "y = 0" by auto
   8.163 +    thus ?case using y unfolding has_integral_restrict_univ indicator_def_raw by auto
   8.164 +  qed
   8.165 +qed
   8.166 +
   8.167 +lemma measure_eq_0: "negligible s ==> gmeasure s = 0"
   8.168 +  apply(rule measure_unique) unfolding has_gmeasure_0 by auto
   8.169 +
   8.170 +lemma has_gmeasure_empty[intro]: "{} has_gmeasure 0"
   8.171 +  unfolding has_gmeasure_0 by auto
   8.172 +
   8.173 +lemma measure_empty[simp]: "gmeasure {} = 0"
   8.174 +  apply(rule measure_eq_0) by auto
   8.175 +
   8.176 +lemma gmeasurable_empty[intro]: "gmeasurable {}" by(auto intro:gmeasurableI)
   8.177 +
   8.178 +lemma gmeasurable_measure_eq_0:
   8.179 +  "gmeasurable s ==> (gmeasure s = 0 \<longleftrightarrow> negligible s)"
   8.180 +  unfolding has_gmeasure_measure has_gmeasure_0[THEN sym] by(auto intro:measure_unique)
   8.181 +
   8.182 +lemma gmeasurable_measure_pos_lt:
   8.183 + "gmeasurable s ==> (0 < gmeasure s \<longleftrightarrow> ~negligible s)"
   8.184 +  unfolding gmeasurable_measure_eq_0[THEN sym]
   8.185 +  using measure_pos_le[of s] unfolding le_less by fastsimp
   8.186 +
   8.187 +lemma negligible_interval:True .. (*
   8.188 + "(!a b. negligible{a..b} \<longleftrightarrow> {a<..<b} = {}) \<and>
   8.189 +   (!a b. negligible({a<..<b}) \<longleftrightarrow> {a<..<b} = {})"
   8.190 +qed   REWRITE_TAC[GSYM HAS_GMEASURE_0] THEN
   8.191 +  MESON_TAC[HAS_GMEASURE_INTERVAL; CONTENT_EQ_0_INTERIOR;
   8.192 +            INTERIOR_CLOSED_INTERVAL; HAS_GMEASURE_UNIQUE]);;*)
   8.193 +
   8.194 +lemma gmeasurable_finite_unions:
   8.195 +  assumes "finite f" "\<And>s. s \<in> f \<Longrightarrow> gmeasurable s"
   8.196 +  shows "gmeasurable (\<Union> f)" using assms(1,2) 
   8.197 +proof induct case (insert s F)
   8.198 +  show ?case unfolding Union_insert apply(rule gmeasurable_union)
   8.199 +    using insert by auto
   8.200 +qed auto  
   8.201 +
   8.202 +lemma has_gmeasure_diff_subset: assumes "s1 has_gmeasure m1" "s2 has_gmeasure m2" "s2 \<subseteq> s1"
   8.203 +  shows "(s1 - s2) has_gmeasure (m1 - m2)"
   8.204 +proof- have *:"(\<lambda>x. (if x \<in> s1 then 1 else 0) - (if x \<in> s2 then 1 else (0::real))) =
   8.205 +    (\<lambda>x. if x \<in> s1 - s2 then 1 else 0)" apply(rule ext) using assms(3) by auto
   8.206 +  show ?thesis using has_integral_sub[OF assms(1-2)[unfolded has_gmeasure]] 
   8.207 +    unfolding has_gmeasure * . 
   8.208 +qed
   8.209 +
   8.210 +lemma gmeasurable_diff: assumes "gmeasurable s" "gmeasurable t" 
   8.211 +  shows "gmeasurable (s - t)"
   8.212 +proof- have *:"\<And>s t. gmeasurable s \<Longrightarrow> gmeasurable t \<Longrightarrow> t \<subseteq> s ==> gmeasurable (s - t)"
   8.213 +    unfolding gmeasurable_def apply(erule exE)+ apply(rule,rule has_gmeasure_diff_subset)
   8.214 +    by assumption+
   8.215 +  have **:"s - t = s - (s \<inter> t)" by auto
   8.216 +  show ?thesis unfolding ** apply(rule *) using assms by auto
   8.217 +qed
   8.218 +
   8.219 +lemma measure_diff_subset: True .. (*
   8.220 + "!s t. gmeasurable s \<and> gmeasurable t \<and> t \<subseteq> s
   8.221 +         ==> measure(s DIFF t) = gmeasure s - gmeasure t"
   8.222 +qed   REPEAT STRIP_TAC THEN MATCH_MP_TAC MEASURE_UNIQUE THEN
   8.223 +  ASM_SIMP_TAC[HAS_GMEASURE_DIFF_SUBSET; GSYM HAS_GMEASURE_MEASURE]);; *)
   8.224 +
   8.225 +lemma has_gmeasure_union_negligible[dest]:
   8.226 +  assumes "s has_gmeasure m" "negligible t"
   8.227 +  shows "(s \<union> t) has_gmeasure m" unfolding has_gmeasure
   8.228 +  apply(rule has_integral_spike[OF assms(2) _ assms(1)[unfolded has_gmeasure]]) by auto
   8.229 +
   8.230 +lemma has_gmeasure_diff_negligible[dest]:
   8.231 +  assumes "s has_gmeasure m" "negligible t" 
   8.232 +  shows "(s - t) has_gmeasure m" unfolding has_gmeasure
   8.233 +  apply(rule has_integral_spike[OF assms(2) _ assms(1)[unfolded has_gmeasure]]) by auto
   8.234 +
   8.235 +lemma has_gmeasure_union_negligible_eq: True .. (*
   8.236 + "!s t:real^N->bool m.
   8.237 +     negligible t ==> ((s \<union> t) has_gmeasure m \<longleftrightarrow> s has_gmeasure m)"
   8.238 +qed   REPEAT STRIP_TAC THEN EQ_TAC THEN DISCH_TAC THEN
   8.239 +  ASM_SIMP_TAC[HAS_GMEASURE_UNION_NEGLIGIBLE] THEN
   8.240 +  SUBST1_TAC(SET_RULE `s:real^N->bool = (s \<union> t) DIFF (t DIFF s)`) THEN
   8.241 +  MATCH_MP_TAC HAS_GMEASURE_DIFF_NEGLIGIBLE THEN ASM_REWRITE_TAC[] THEN
   8.242 +  MATCH_MP_TAC NEGLIGIBLE_DIFF THEN ASM_REWRITE_TAC[]);; *)
   8.243 +
   8.244 +lemma has_gmeasure_diff_negligible_eq: True .. (*
   8.245 + "!s t:real^N->bool m.
   8.246 +     negligible t ==> ((s DIFF t) has_gmeasure m \<longleftrightarrow> s has_gmeasure m)"
   8.247 +qed   REPEAT STRIP_TAC THEN EQ_TAC THEN DISCH_TAC THEN
   8.248 +  ASM_SIMP_TAC[HAS_GMEASURE_DIFF_NEGLIGIBLE] THEN
   8.249 +  SUBST1_TAC(SET_RULE `s:real^N->bool = (s DIFF t) \<union> (t \<inter> s)`) THEN
   8.250 +  MATCH_MP_TAC HAS_GMEASURE_UNION_NEGLIGIBLE THEN
   8.251 +  ASM_SIMP_TAC[NEGLIGIBLE_INTER]);; *)
   8.252 +
   8.253 +lemma has_gmeasure_almost: assumes "s has_gmeasure m" "negligible t" "s \<union> t = s' \<union> t"
   8.254 +  shows "s' has_gmeasure m"
   8.255 +proof- have *:"s' \<union> t - (t - s') = s'" by blast
   8.256 +  show ?thesis using has_gmeasure_union_negligible[OF assms(1-2)] unfolding assms(3)
   8.257 +    apply-apply(drule has_gmeasure_diff_negligible[where t="t - s'"])
   8.258 +    apply(rule negligible_diff) using assms(2) unfolding * by auto
   8.259 +qed
   8.260 +
   8.261 +lemma has_gmeasure_almost_eq: True .. (*
   8.262 + "!s s' t. negligible t \<and> s \<union> t = s' \<union> t
   8.263 +            ==> (s has_gmeasure m \<longleftrightarrow> s' has_gmeasure m)"
   8.264 +qed   MESON_TAC[HAS_GMEASURE_ALMOST]);; *)
   8.265 +
   8.266 +lemma gmeasurable_almost: True .. (*
   8.267 + "!s s' t. gmeasurable s \<and> negligible t \<and> s \<union> t = s' \<union> t
   8.268 +            ==> gmeasurable s'"
   8.269 +qed   REWRITE_TAC[measurable] THEN MESON_TAC[HAS_GMEASURE_ALMOST]);; *)
   8.270 +
   8.271 +lemma has_gmeasure_negligible_union:
   8.272 +  assumes "s1 has_gmeasure m1" "s2 has_gmeasure m2" "negligible(s1 \<inter> s2)"
   8.273 +  shows "(s1 \<union> s2) has_gmeasure (m1 + m2)" 
   8.274 +  apply(rule has_gmeasure_almost[of "(s1 - (s1 \<inter> s2)) \<union> (s2 - (s1 \<inter> s2))" _ "s1 \<inter> s2"])
   8.275 +  apply(rule has_gmeasure_disjoint_union)
   8.276 +  apply(rule has_gmeasure_almost[of s1,OF _ assms(3)]) prefer 3
   8.277 +  apply(rule has_gmeasure_almost[of s2,OF _ assms(3)])
   8.278 +  using assms by auto
   8.279 +
   8.280 +lemma measure_negligible_union: True .. (*
   8.281 +  "!s t. gmeasurable s \<and> gmeasurable t \<and> negligible(s \<inter> t)
   8.282 +         ==> measure(s \<union> t) = gmeasure s + gmeasure t"
   8.283 +qed   REPEAT STRIP_TAC THEN MATCH_MP_TAC MEASURE_UNIQUE THEN
   8.284 +  ASM_SIMP_TAC[HAS_GMEASURE_NEGLIGIBLE_UNION; GSYM HAS_GMEASURE_MEASURE]);; *)
   8.285 +
   8.286 +lemma has_gmeasure_negligible_symdiff: True .. (*
   8.287 + "!s t:real^N->bool m.
   8.288 +        s has_gmeasure m \<and>
   8.289 +        negligible((s DIFF t) \<union> (t DIFF s))
   8.290 +        ==> t has_gmeasure m"
   8.291 +qed   REPEAT STRIP_TAC THEN MATCH_MP_TAC HAS_GMEASURE_ALMOST THEN
   8.292 +  MAP_EVERY EXISTS_TAC
   8.293 +   [`s:real^N->bool`; `(s DIFF t) \<union> (t DIFF s):real^N->bool`] THEN
   8.294 +  ASM_REWRITE_TAC[] THEN SET_TAC[]);; *)
   8.295 +
   8.296 +lemma gmeasurable_negligible_symdiff: True .. (*
   8.297 + "!s t:real^N->bool.
   8.298 +        gmeasurable s \<and> negligible((s DIFF t) \<union> (t DIFF s))
   8.299 +        ==> gmeasurable t"
   8.300 +qed   REWRITE_TAC[measurable] THEN
   8.301 +  MESON_TAC[HAS_GMEASURE_NEGLIGIBLE_SYMDIFF]);; *)
   8.302 +
   8.303 +lemma measure_negligible_symdiff: True .. (*
   8.304 + "!s t:real^N->bool.
   8.305 +        (measurable s \/ gmeasurable t) \<and>
   8.306 +        negligible((s DIFF t) \<union> (t DIFF s))
   8.307 +        ==> gmeasure s = gmeasure t"
   8.308 +qed   MESON_TAC[HAS_GMEASURE_NEGLIGIBLE_SYMDIFF; MEASURE_UNIQUE; UNION_COMM;
   8.309 +                HAS_GMEASURE_MEASURE]);; *)
   8.310 +
   8.311 +lemma has_gmeasure_negligible_unions: assumes "finite f"
   8.312 +  "\<And>s. s \<in> f ==> s has_gmeasure (m s)"
   8.313 +  "\<And>s t. s \<in> f \<Longrightarrow> t \<in> f \<Longrightarrow> ~(s = t) ==> negligible(s \<inter> t)"
   8.314 +  shows "(\<Union> f) has_gmeasure (setsum m f)" using assms
   8.315 +proof induct case (insert x s)
   8.316 +  have *:"(x \<inter> \<Union>s) = \<Union>{x \<inter> y| y. y\<in>s}"by auto
   8.317 +  show ?case unfolding Union_insert ring_class.setsum.insert[OF insert(1-2)] 
   8.318 +    apply(rule has_gmeasure_negligible_union) unfolding *
   8.319 +    apply(rule insert) defer apply(rule insert) apply(rule insert) defer
   8.320 +    apply(rule insert) prefer 4 apply(rule negligible_unions)
   8.321 +    defer apply safe apply(rule insert) using insert by auto
   8.322 +qed auto
   8.323 +
   8.324 +lemma measure_negligible_unions: 
   8.325 +  assumes "finite f" "\<And>s. s \<in> f ==> s has_gmeasure (m s)"
   8.326 +  "\<And>s t. s \<in> f \<Longrightarrow> t \<in> f \<Longrightarrow> s \<noteq> t ==> negligible(s \<inter> t)"
   8.327 +  shows "gmeasure(\<Union> f) = setsum m f"
   8.328 +  apply rule apply(rule has_gmeasure_negligible_unions)
   8.329 +  using assms by auto
   8.330 +
   8.331 +lemma has_gmeasure_disjoint_unions:
   8.332 +  assumes"finite f" "\<And>s. s \<in> f ==> s has_gmeasure (m s)"
   8.333 +  "\<And>s t. s \<in> f \<Longrightarrow> t \<in> f \<Longrightarrow> s \<noteq> t ==> s \<inter> t = {}"
   8.334 +  shows "(\<Union> f) has_gmeasure (setsum m f)"
   8.335 +  apply(rule has_gmeasure_negligible_unions[OF assms(1-2)]) using assms(3) by auto
   8.336 +
   8.337 +lemma measure_disjoint_unions: 
   8.338 +  assumes "finite f" "\<And>s. s \<in> f ==> s has_gmeasure (m s)" 
   8.339 +  "\<And>s t. s \<in> f \<Longrightarrow> t \<in> f \<Longrightarrow> s \<noteq> t ==> s \<inter> t = {}"
   8.340 +  shows "gmeasure(\<Union> f) = setsum m f"
   8.341 +  apply rule apply(rule has_gmeasure_disjoint_unions[OF assms]) by auto
   8.342 +
   8.343 +lemma has_gmeasure_negligible_unions_image:
   8.344 +  assumes "finite s" "\<And>x. x \<in> s ==> gmeasurable(f x)"
   8.345 +  "\<And>x y. x \<in> s \<Longrightarrow> y \<in> s \<Longrightarrow> x \<noteq> y \<Longrightarrow> negligible((f x) \<inter> (f y))"
   8.346 +  shows "(\<Union> (f ` s)) has_gmeasure (setsum (\<lambda>x. gmeasure(f x)) s)"
   8.347 +proof- have *:"setsum (\<lambda>x. gmeasure(f x)) s = setsum gmeasure (f ` s)"
   8.348 +    apply(subst setsum_reindex_nonzero) defer
   8.349 +    apply(subst gmeasurable_measure_eq_0)
   8.350 +  proof- case goal2 thus ?case using assms(3)[of x y] by auto
   8.351 +  qed(insert assms, auto)
   8.352 +  show ?thesis unfolding * apply(rule has_gmeasure_negligible_unions) using assms by auto
   8.353 +qed
   8.354 +
   8.355 +lemma measure_negligible_unions_image: True .. (*
   8.356 + "!f:A->real^N->bool s.
   8.357 +        FINITE s \<and>
   8.358 +        (!x. x \<in> s ==> gmeasurable(f x)) \<and>
   8.359 +        (!x y. x \<in> s \<and> y \<in> s \<and> ~(x = y) ==> negligible((f x) \<inter> (f y)))
   8.360 +        ==> measure(UNIONS (IMAGE f s)) = sum s (\<lambda>x. measure(f x))"
   8.361 +qed   REPEAT STRIP_TAC THEN MATCH_MP_TAC MEASURE_UNIQUE THEN
   8.362 +  ASM_SIMP_TAC[HAS_GMEASURE_NEGLIGIBLE_UNIONS_IMAGE]);;*)
   8.363 +
   8.364 +lemma has_gmeasure_disjoint_unions_image: True .. (*
   8.365 + "!f:A->real^N->bool s.
   8.366 +        FINITE s \<and>
   8.367 +        (!x. x \<in> s ==> gmeasurable(f x)) \<and>
   8.368 +        (!x y. x \<in> s \<and> y \<in> s \<and> ~(x = y) ==> DISJOINT (f x) (f y))
   8.369 +        ==> (UNIONS (IMAGE f s)) has_gmeasure (sum s (\<lambda>x. measure(f x)))"
   8.370 +qed   REWRITE_TAC[DISJOINT] THEN REPEAT STRIP_TAC THEN
   8.371 +  MATCH_MP_TAC HAS_GMEASURE_NEGLIGIBLE_UNIONS_IMAGE THEN
   8.372 +  ASM_SIMP_TAC[NEGLIGIBLE_EMPTY]);;*)
   8.373 +
   8.374 +lemma measure_disjoint_unions_image: True .. (*
   8.375 + "!f:A->real^N->bool s.
   8.376 +        FINITE s \<and>
   8.377 +        (!x. x \<in> s ==> gmeasurable(f x)) \<and>
   8.378 +        (!x y. x \<in> s \<and> y \<in> s \<and> ~(x = y) ==> DISJOINT (f x) (f y))
   8.379 +        ==> measure(UNIONS (IMAGE f s)) = sum s (\<lambda>x. measure(f x))"
   8.380 +qed   REPEAT STRIP_TAC THEN MATCH_MP_TAC MEASURE_UNIQUE THEN
   8.381 +  ASM_SIMP_TAC[HAS_GMEASURE_DISJOINT_UNIONS_IMAGE]);;*)
   8.382 +
   8.383 +lemma has_gmeasure_negligible_unions_image_strong: True .. (*
   8.384 + "!f:A->real^N->bool s.
   8.385 +        FINITE {x | x \<in> s \<and> ~(f x = {})} \<and>
   8.386 +        (!x. x \<in> s ==> gmeasurable(f x)) \<and>
   8.387 +        (!x y. x \<in> s \<and> y \<in> s \<and> ~(x = y) ==> negligible((f x) \<inter> (f y)))
   8.388 +        ==> (UNIONS (IMAGE f s)) has_gmeasure (sum s (\<lambda>x. measure(f x)))"
   8.389 +qed   REPEAT STRIP_TAC THEN
   8.390 +  MP_TAC(ISPECL [`f:A->real^N->bool`;
   8.391 +                 `{x | x \<in> s \<and> ~((f:A->real^N->bool) x = {})}`]
   8.392 +        HAS_GMEASURE_NEGLIGIBLE_UNIONS_IMAGE) THEN
   8.393 +  ASM_SIMP_TAC[IN_ELIM_THM; FINITE_RESTRICT] THEN
   8.394 +  MATCH_MP_TAC EQ_IMP THEN BINOP_TAC THENL
   8.395 +   [GEN_REWRITE_TAC I [EXTENSION] THEN
   8.396 +    REWRITE_TAC[IN_UNIONS; IN_IMAGE; IN_ELIM_THM] THEN
   8.397 +    MESON_TAC[NOT_IN_EMPTY];
   8.398 +    CONV_TAC SYM_CONV THEN MATCH_MP_TAC SUM_SUPERSET THEN
   8.399 +    SIMP_TAC[SUBSET; IN_ELIM_THM; TAUT `a \<and> ~(a \<and> b) \<longleftrightarrow> a \<and> ~b`] THEN
   8.400 +    REWRITE_TAC[MEASURE_EMPTY]]);; *)
   8.401 +
   8.402 +lemma measure_negligible_unions_image_strong: True .. (*
   8.403 + "!f:A->real^N->bool s.
   8.404 +        FINITE {x | x \<in> s \<and> ~(f x = {})} \<and>
   8.405 +        (!x. x \<in> s ==> gmeasurable(f x)) \<and>
   8.406 +        (!x y. x \<in> s \<and> y \<in> s \<and> ~(x = y) ==> negligible((f x) \<inter> (f y)))
   8.407 +        ==> measure(UNIONS (IMAGE f s)) = sum s (\<lambda>x. measure(f x))"
   8.408 +qed   REPEAT STRIP_TAC THEN MATCH_MP_TAC MEASURE_UNIQUE THEN
   8.409 +  ASM_SIMP_TAC[HAS_GMEASURE_NEGLIGIBLE_UNIONS_IMAGE_STRONG]);; *)
   8.410 +
   8.411 +lemma has_gmeasure_disjoint_unions_image_strong: True .. (*
   8.412 + "!f:A->real^N->bool s.
   8.413 +        FINITE {x | x \<in> s \<and> ~(f x = {})} \<and>
   8.414 +        (!x. x \<in> s ==> gmeasurable(f x)) \<and>
   8.415 +        (!x y. x \<in> s \<and> y \<in> s \<and> ~(x = y) ==> DISJOINT (f x) (f y))
   8.416 +        ==> (UNIONS (IMAGE f s)) has_gmeasure (sum s (\<lambda>x. measure(f x)))"
   8.417 +qed   REWRITE_TAC[DISJOINT] THEN REPEAT STRIP_TAC THEN
   8.418 +  MATCH_MP_TAC HAS_GMEASURE_NEGLIGIBLE_UNIONS_IMAGE_STRONG THEN
   8.419 +  ASM_SIMP_TAC[NEGLIGIBLE_EMPTY]);; *)
   8.420 +
   8.421 +lemma measure_disjoint_unions_image_strong: True .. (*
   8.422 + "!f:A->real^N->bool s.
   8.423 +        FINITE {x | x \<in> s \<and> ~(f x = {})} \<and>
   8.424 +        (!x. x \<in> s ==> gmeasurable(f x)) \<and>
   8.425 +        (!x y. x \<in> s \<and> y \<in> s \<and> ~(x = y) ==> DISJOINT (f x) (f y))
   8.426 +        ==> measure(UNIONS (IMAGE f s)) = sum s (\<lambda>x. measure(f x))"
   8.427 +qed   REPEAT STRIP_TAC THEN MATCH_MP_TAC MEASURE_UNIQUE THEN
   8.428 +  ASM_SIMP_TAC[HAS_GMEASURE_DISJOINT_UNIONS_IMAGE_STRONG]);; *)
   8.429 +
   8.430 +lemma measure_union: True .. (*
   8.431 + "!s t:real^N->bool.
   8.432 +        gmeasurable s \<and> gmeasurable t
   8.433 +        ==> measure(s \<union> t) = measure(s) + measure(t) - measure(s \<inter> t)"
   8.434 +qed   REPEAT STRIP_TAC THEN
   8.435 +  ONCE_REWRITE_TAC[SET_RULE
   8.436 +   `s \<union> t = (s \<inter> t) \<union> (s DIFF t) \<union> (t DIFF s)`] THEN
   8.437 +  ONCE_REWRITE_TAC[REAL_ARITH `a + b - c = c + (a - c) + (b - c)`] THEN
   8.438 +  MP_TAC(ISPECL [`s DIFF t:real^N->bool`; `t DIFF s:real^N->bool`]
   8.439 +        MEASURE_DISJOINT_UNION) THEN
   8.440 +  ASM_SIMP_TAC[MEASURABLE_DIFF] THEN
   8.441 +  ANTS_TAC THENL [SET_TAC[]; ALL_TAC] THEN
   8.442 +  MP_TAC(ISPECL [`s \<inter> t:real^N->bool`;
   8.443 +                 `(s DIFF t) \<union> (t DIFF s):real^N->bool`]
   8.444 +                MEASURE_DISJOINT_UNION) THEN
   8.445 +  ASM_SIMP_TAC[MEASURABLE_DIFF; GMEASURABLE_UNION; GMEASURABLE_INTER] THEN
   8.446 +  ANTS_TAC THENL [SET_TAC[]; ALL_TAC] THEN
   8.447 +  REPEAT(DISCH_THEN SUBST1_TAC) THEN AP_TERM_TAC THEN BINOP_TAC THEN
   8.448 +  REWRITE_TAC[REAL_EQ_SUB_LADD] THEN MATCH_MP_TAC EQ_TRANS THENL
   8.449 +   [EXISTS_TAC `measure((s DIFF t) \<union> (s \<inter> t):real^N->bool)`;
   8.450 +    EXISTS_TAC `measure((t DIFF s) \<union> (s \<inter> t):real^N->bool)`] THEN
   8.451 +  (CONJ_TAC THENL
   8.452 +    [CONV_TAC SYM_CONV THEN MATCH_MP_TAC MEASURE_DISJOINT_UNION THEN
   8.453 +     ASM_SIMP_TAC[MEASURABLE_DIFF; GMEASURABLE_INTER];
   8.454 +     AP_TERM_TAC] THEN
   8.455 +   SET_TAC[]));; *)
   8.456 +
   8.457 +lemma measure_union_le: True .. (*
   8.458 + "!s t:real^N->bool.
   8.459 +        gmeasurable s \<and> gmeasurable t
   8.460 +        ==> measure(s \<union> t) <= gmeasure s + gmeasure t"
   8.461 +qed   REPEAT STRIP_TAC THEN ASM_SIMP_TAC[MEASURE_UNION] THEN
   8.462 +  REWRITE_TAC[REAL_ARITH `a + b - c <= a + b \<longleftrightarrow> 0 <= c`] THEN
   8.463 +  MATCH_MP_TAC MEASURE_POS_LE THEN ASM_SIMP_TAC[MEASURABLE_INTER]);; *)
   8.464 +
   8.465 +lemma measure_unions_le: True .. (*
   8.466 + "!f:(real^N->bool)->bool.
   8.467 +        FINITE f \<and> (!s. s \<in> f ==> gmeasurable s)
   8.468 +        ==> measure(UNIONS f) <= sum f (\<lambda>s. gmeasure s)"
   8.469 +qed   REWRITE_TAC[IMP_CONJ] THEN
   8.470 +  MATCH_MP_TAC FINITE_INDUCT_STRONG THEN
   8.471 +  SIMP_TAC[UNIONS_0; UNIONS_INSERT; SUM_CLAUSES] THEN
   8.472 +  REWRITE_TAC[MEASURE_EMPTY; REAL_LE_REFL] THEN
   8.473 +  MAP_EVERY X_GEN_TAC [`s:real^N->bool`; `f:(real^N->bool)->bool`] THEN
   8.474 +  REWRITE_TAC[IN_INSERT] THEN REPEAT STRIP_TAC THEN
   8.475 +  MATCH_MP_TAC REAL_LE_TRANS THEN
   8.476 +  EXISTS_TAC `measure(s:real^N->bool) + measure(UNIONS f:real^N->bool)` THEN
   8.477 +  ASM_SIMP_TAC[MEASURE_UNION_LE; GMEASURABLE_UNIONS] THEN
   8.478 +  REWRITE_TAC[REAL_LE_LADD] THEN FIRST_X_ASSUM MATCH_MP_TAC THEN
   8.479 +  ASM_SIMP_TAC[]);; *)
   8.480 +
   8.481 +lemma measure_unions_le_image: True .. (*
   8.482 + "!f:A->bool s:A->(real^N->bool).
   8.483 +        FINITE f \<and> (!a. a \<in> f ==> gmeasurable(s a))
   8.484 +        ==> measure(UNIONS (IMAGE s f)) <= sum f (\<lambda>a. measure(s a))"
   8.485 +qed   REPEAT STRIP_TAC THEN MATCH_MP_TAC REAL_LE_TRANS THEN
   8.486 +  EXISTS_TAC `sum (IMAGE s (f:A->bool)) (\<lambda>k:real^N->bool. gmeasure k)` THEN
   8.487 +  ASM_SIMP_TAC[MEASURE_UNIONS_LE; FORALL_IN_IMAGE; FINITE_IMAGE] THEN
   8.488 +  GEN_REWRITE_TAC (RAND_CONV o RAND_CONV) [GSYM o_DEF] THEN
   8.489 +  REWRITE_TAC[ETA_AX] THEN MATCH_MP_TAC SUM_IMAGE_LE THEN
   8.490 +  ASM_SIMP_TAC[MEASURE_POS_LE]);; *)
   8.491 +
   8.492 +lemma gmeasurable_inner_outer: True .. (*
   8.493 + "!s:real^N->bool.
   8.494 +        gmeasurable s \<longleftrightarrow>
   8.495 +                !e. 0 < e
   8.496 +                    ==> ?t u. t \<subseteq> s \<and> s \<subseteq> u \<and>
   8.497 +                              gmeasurable t \<and> gmeasurable u \<and>
   8.498 +                              abs(measure t - gmeasure u) < e"
   8.499 +qed   GEN_TAC THEN EQ_TAC THEN DISCH_TAC THENL
   8.500 +   [GEN_TAC THEN DISCH_TAC THEN REPEAT(EXISTS_TAC `s:real^N->bool`) THEN
   8.501 +    ASM_REWRITE_TAC[SUBSET_REFL; REAL_SUB_REFL; REAL_ABS_NUM];
   8.502 +    ALL_TAC] THEN
   8.503 +  REWRITE_TAC[MEASURABLE_INTEGRABLE] THEN MATCH_MP_TAC INTEGRABLE_STRADDLE THEN
   8.504 +  X_GEN_TAC `e:real` THEN DISCH_TAC THEN
   8.505 +  FIRST_X_ASSUM(MP_TAC o SPEC `e:real`) THEN
   8.506 +  ASM_REWRITE_TAC[LEFT_IMP_EXISTS_THM] THEN
   8.507 +  MAP_EVERY X_GEN_TAC [`t:real^N->bool`; `u:real^N->bool`] THEN STRIP_TAC THEN
   8.508 +  MAP_EVERY EXISTS_TAC
   8.509 +   [`(\<lambda>x. if x \<in> t then 1 else 0):real^N->real^1`;
   8.510 +    `(\<lambda>x. if x \<in> u then 1 else 0):real^N->real^1`;
   8.511 +    `lift(measure(t:real^N->bool))`;
   8.512 +    `lift(measure(u:real^N->bool))`] THEN
   8.513 +  ASM_REWRITE_TAC[GSYM HAS_GMEASURE; GSYM HAS_GMEASURE_MEASURE] THEN
   8.514 +  ASM_REWRITE_TAC[GSYM LIFT_SUB; NORM_LIFT] THEN REPEAT STRIP_TAC THEN
   8.515 +  REPEAT(COND_CASES_TAC THEN
   8.516 +         ASM_REWRITE_TAC[_VEC; REAL_POS; REAL_LE_REFL]) THEN
   8.517 +  ASM SET_TAC[]);; *)
   8.518 +
   8.519 +lemma has_gmeasure_inner_outer: True .. (*
   8.520 + "!s:real^N->bool m.
   8.521 +        s has_gmeasure m \<longleftrightarrow>
   8.522 +                (!e. 0 < e ==> ?t. t \<subseteq> s \<and> gmeasurable t \<and>
   8.523 +                                    m - e < gmeasure t) \<and>
   8.524 +                (!e. 0 < e ==> ?u. s \<subseteq> u \<and> gmeasurable u \<and>
   8.525 +                                    gmeasure u < m + e)"
   8.526 +qed   REPEAT GEN_TAC THEN
   8.527 +  GEN_REWRITE_TAC LAND_CONV [HAS_GMEASURE_MEASURABLE_MEASURE] THEN EQ_TAC THENL
   8.528 +   [REPEAT STRIP_TAC THEN EXISTS_TAC `s:real^N->bool` THEN
   8.529 +    ASM_REWRITE_TAC[SUBSET_REFL] THEN ASM_REAL_ARITH_TAC;
   8.530 +    ALL_TAC] THEN
   8.531 +  DISCH_THEN(CONJUNCTS_THEN2 (LABEL_TAC "t") (LABEL_TAC "u")) THEN
   8.532 +  MATCH_MP_TAC(TAUT `a \<and> (a ==> b) ==> a \<and> b`) THEN CONJ_TAC THENL
   8.533 +   [GEN_REWRITE_TAC I [MEASURABLE_INNER_OUTER] THEN
   8.534 +    X_GEN_TAC `e:real` THEN DISCH_TAC THEN
   8.535 +    REMOVE_THEN "u" (MP_TAC o SPEC `e / 2`) THEN
   8.536 +    REMOVE_THEN "t" (MP_TAC o SPEC `e / 2`) THEN
   8.537 +    ASM_SIMP_TAC[REAL_LT_DIV; REAL_OF_NUM_LT; ARITH] THEN
   8.538 +    REWRITE_TAC[IMP_IMP; LEFT_AND_EXISTS_THM] THEN
   8.539 +    REWRITE_TAC[RIGHT_AND_EXISTS_THM] THEN
   8.540 +    REPEAT(MATCH_MP_TAC MONO_EXISTS THEN GEN_TAC) THEN
   8.541 +    STRIP_TAC THEN ASM_REWRITE_TAC[] THEN MATCH_MP_TAC(REAL_ARITH
   8.542 +     `0 < e \<and> t <= u \<and> m - e / 2 < t \<and> u < m + e / 2
   8.543 +                          ==> abs(t - u) < e`) THEN
   8.544 +    ASM_REWRITE_TAC[] THEN MATCH_MP_TAC MEASURE_SUBSET THEN
   8.545 +    ASM_REWRITE_TAC[] THEN ASM SET_TAC[];
   8.546 +    DISCH_TAC THEN MATCH_MP_TAC(REAL_ARITH
   8.547 +     `~(0 < x - y) \<and> ~(0 < y - x) ==> x = y`) THEN
   8.548 +    CONJ_TAC THEN DISCH_TAC THENL
   8.549 +     [REMOVE_THEN "u" (MP_TAC o SPEC `measure(s:real^N->bool) - m`) THEN
   8.550 +      ASM_REWRITE_TAC[REAL_SUB_ADD2; GSYM REAL_NOT_LE];
   8.551 +      REMOVE_THEN "t" (MP_TAC o SPEC `m - measure(s:real^N->bool)`) THEN
   8.552 +      ASM_REWRITE_TAC[REAL_SUB_SUB2; GSYM REAL_NOT_LE]] THEN
   8.553 +    ASM_MESON_TAC[MEASURE_SUBSET]]);; *)
   8.554 +
   8.555 +lemma has_gmeasure_inner_outer_le: True .. (*
   8.556 + "!s:real^N->bool m.
   8.557 +        s has_gmeasure m \<longleftrightarrow>
   8.558 +                (!e. 0 < e ==> ?t. t \<subseteq> s \<and> gmeasurable t \<and>
   8.559 +                                    m - e <= gmeasure t) \<and>
   8.560 +                (!e. 0 < e ==> ?u. s \<subseteq> u \<and> gmeasurable u \<and>
   8.561 +                                    gmeasure u <= m + e)"
   8.562 +qed   REWRITE_TAC[HAS_GMEASURE_INNER_OUTER] THEN
   8.563 +  MESON_TAC[REAL_ARITH `0 < e \<and> m - e / 2 <= t ==> m - e < t`;
   8.564 +            REAL_ARITH `0 < e \<and> u <= m + e / 2 ==> u < m + e`;
   8.565 +            REAL_ARITH `0 < e \<longleftrightarrow> 0 < e / 2`; REAL_LT_IMP_LE]);; *)
   8.566 +
   8.567 +lemma has_gmeasure_limit: True .. (*
   8.568 + "!s. s has_gmeasure m \<longleftrightarrow>
   8.569 +        !e. 0 < e
   8.570 +            ==> ?B. 0 < B \<and>
   8.571 +                    !a b. ball(0,B) \<subseteq> {a..b}
   8.572 +                          ==> ?z. (s \<inter> {a..b}) has_gmeasure z \<and>
   8.573 +                                  abs(z - m) < e"
   8.574 +qed   GEN_TAC THEN REWRITE_TAC[HAS_GMEASURE] THEN
   8.575 +  GEN_REWRITE_TAC LAND_CONV [HAS_INTEGRAL] THEN
   8.576 +  REWRITE_TAC[IN_UNIV] THEN
   8.577 +  GEN_REWRITE_TAC (LAND_CONV o ONCE_DEPTH_CONV)
   8.578 +    [GSYM HAS_INTEGRAL_RESTRICT_UNIV] THEN
   8.579 +  REWRITE_TAC[MESON[IN_INTER]
   8.580 +        `(if x \<in> k \<inter> s then a else b) =
   8.581 +         (if x \<in> s then if x \<in> k then a else b else b)`] THEN
   8.582 +  REWRITE_TAC[EXISTS_LIFT; GSYM LIFT_SUB; NORM_LIFT]);; *)
   8.583 +
   8.584 +(* ------------------------------------------------------------------------- *)
   8.585 +(* properties of gmeasure under simple affine transformations.                *)
   8.586 +(* ------------------------------------------------------------------------- *)
   8.587 +
   8.588 +lemma has_gmeasure_affinity: True .. (*
   8.589 + "!s m c y. s has_gmeasure y
   8.590 +             ==> (IMAGE (\<lambda>x:real^N. m % x + c) s)
   8.591 +                 has_gmeasure abs(m) pow (dimindex(:N)) * y"
   8.592 +qed   REPEAT GEN_TAC THEN ASM_CASES_TAC `m = 0` THENL
   8.593 +   [ASM_REWRITE_TAC[REAL_ABS_NUM; VECTOR_ADD_LID; VECTOR_MUL_LZERO] THEN
   8.594 +    ONCE_REWRITE_TAC[MATCH_MP (ARITH_RULE `~(x = 0) ==> x = SUC(x - 1)`)
   8.595 +     (SPEC_ALL DIMINDEX_NONZERO)] THEN DISCH_TAC THEN
   8.596 +    REWRITE_TAC[real_pow; REAL_MUL_LZERO; HAS_GMEASURE_0] THEN
   8.597 +    MATCH_MP_TAC NEGLIGIBLE_SUBSET THEN EXISTS_TAC `{c:real^N}` THEN
   8.598 +    SIMP_TAC[NEGLIGIBLE_FINITE; FINITE_RULES] THEN SET_TAC[];
   8.599 +    ALL_TAC] THEN
   8.600 +  REWRITE_TAC[HAS_GMEASURE] THEN
   8.601 +  ONCE_REWRITE_TAC[HAS_INTEGRAL] THEN REWRITE_TAC[IN_UNIV] THEN
   8.602 +  DISCH_TAC THEN X_GEN_TAC `e:real` THEN DISCH_TAC THEN
   8.603 +  FIRST_X_ASSUM(MP_TAC o SPEC `e:real / abs(m) pow dimindex(:N)`) THEN
   8.604 +  ASM_SIMP_TAC[REAL_LT_DIV; REAL_POW_LT; GSYM REAL_ABS_NZ; REAL_POW_LT] THEN
   8.605 +  DISCH_THEN(X_CHOOSE_THEN `B:real` STRIP_ASSUME_TAC) THEN
   8.606 +  EXISTS_TAC `abs(m) * B + norm(c:real^N)` THEN
   8.607 +  ASM_SIMP_TAC[REAL_ARITH `0 < B \<and> 0 <= x ==> 0 < B + x`;
   8.608 +               NORM_POS_LE; REAL_LT_MUL; GSYM REAL_ABS_NZ; REAL_POW_LT] THEN
   8.609 +  MAP_EVERY X_GEN_TAC [`u:real^N`; `v:real^N`] THEN DISCH_TAC THEN
   8.610 +  REWRITE_TAC[IN_IMAGE] THEN
   8.611 +  ASM_SIMP_TAC[VECTOR_EQ_AFFINITY; UNWIND_THM1] THEN
   8.612 +  FIRST_X_ASSUM(MP_TAC o SPECL
   8.613 +    [`if 0 <= m then inv m % u + --(inv m % c):real^N
   8.614 +                 else inv m % v + --(inv m % c)`;
   8.615 +     `if 0 <= m then inv m % v + --(inv m % c):real^N
   8.616 +                 else inv m % u + --(inv m % c)`]) THEN
   8.617 +  MATCH_MP_TAC(TAUT `a \<and> (a ==> b ==> c) ==> (a ==> b) ==> c`) THEN
   8.618 +  CONJ_TAC THENL
   8.619 +   [REWRITE_TAC[SUBSET] THEN X_GEN_TAC `x:real^N` THEN
   8.620 +    FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [SUBSET]) THEN
   8.621 +    DISCH_THEN(MP_TAC o SPEC `m % x + c:real^N`) THEN
   8.622 +    MATCH_MP_TAC MONO_IMP THEN REWRITE_TAC[IN_BALL; IN_INTERVAL] THEN
   8.623 +    CONJ_TAC THENL
   8.624 +     [REWRITE_TAC[NORM_ARITH `dist(0,x) = norm(x:real^N)`] THEN
   8.625 +      DISCH_TAC THEN MATCH_MP_TAC(NORM_ARITH
   8.626 +       `norm(x:real^N) < a ==> norm(x + y) < a + norm(y)`) THEN
   8.627 +      ASM_SIMP_TAC[NORM_MUL; REAL_LT_LMUL; GSYM REAL_ABS_NZ];
   8.628 +      ALL_TAC] THEN
   8.629 +    SIMP_TAC[VECTOR_ADD_COMPONENT; VECTOR_MUL_COMPONENT; VECTOR_NEG_COMPONENT;
   8.630 +             COND_COMPONENT] THEN
   8.631 +    MATCH_MP_TAC MONO_FORALL THEN GEN_TAC THEN MATCH_MP_TAC MONO_IMP THEN
   8.632 +    REWRITE_TAC[REAL_ARITH `m * u + --(m * c):real = (u - c) * m`] THEN
   8.633 +    SUBST1_TAC(REAL_ARITH
   8.634 +      `inv(m) = if 0 <= inv(m) then abs(inv m) else --(abs(inv m))`) THEN
   8.635 +    SIMP_TAC[REAL_LE_INV_EQ] THEN
   8.636 +    REWRITE_TAC[REAL_ARITH `(x - y:real) * --z = (y - x) * z`] THEN
   8.637 +    REWRITE_TAC[REAL_ABS_INV; GSYM real_div] THEN COND_CASES_TAC THEN
   8.638 +    ASM_SIMP_TAC[REAL_LE_LDIV_EQ; REAL_LE_RDIV_EQ; GSYM REAL_ABS_NZ] THEN
   8.639 +    ASM_REWRITE_TAC[real_abs] THEN REAL_ARITH_TAC;
   8.640 +    ALL_TAC] THEN
   8.641 +  REWRITE_TAC[SUBSET] THEN DISCH_THEN(MP_TAC o SPEC `0:real^N`) THEN
   8.642 +  ASM_REWRITE_TAC[CENTRE_IN_BALL] THEN DISCH_TAC THEN
   8.643 +  DISCH_THEN(X_CHOOSE_THEN `z:real^1`
   8.644 +   (fun th -> EXISTS_TAC `(abs m pow dimindex (:N)) % z:real^1` THEN
   8.645 +              MP_TAC th)) THEN
   8.646 +  DISCH_THEN(CONJUNCTS_THEN2 MP_TAC ASSUME_TAC) THEN
   8.647 +  FIRST_ASSUM(MP_TAC o MATCH_MP(REAL_FIELD `~(x = 0) ==> ~(inv x = 0)`)) THEN
   8.648 +  REWRITE_TAC[TAUT `a ==> b ==> c \<longleftrightarrow> b \<and> a ==> c`] THEN
   8.649 +  DISCH_THEN(MP_TAC o SPEC `--(inv m % c):real^N` o
   8.650 +    MATCH_MP HAS_INTEGRAL_AFFINITY) THEN
   8.651 +  ASM_REWRITE_TAC[IMAGE_AFFINITY_INTERVAL; REAL_INV_INV] THEN
   8.652 +  SIMP_TAC[COND_ID] THEN COND_CASES_TAC THENL [ASM SET_TAC[]; ALL_TAC] THEN
   8.653 +  REWRITE_TAC[VECTOR_ADD_LDISTRIB; VECTOR_MUL_ASSOC;
   8.654 +               VECTOR_MUL_LNEG; VECTOR_MUL_RNEG] THEN
   8.655 +  ASM_SIMP_TAC[REAL_MUL_RINV; VECTOR_MUL_LID; VECTOR_NEG_NEG] THEN
   8.656 +  REWRITE_TAC[VECTOR_ARITH `(u + --c) + c:real^N = u`] THEN
   8.657 +  REWRITE_TAC[REAL_ABS_INV; REAL_INV_INV; GSYM REAL_POW_INV] THEN
   8.658 +  DISCH_THEN(fun th -> REWRITE_TAC[th]) THEN
   8.659 +  REWRITE_TAC[LIFT_CMUL; GSYM VECTOR_SUB_LDISTRIB] THEN
   8.660 +  REWRITE_TAC[NORM_MUL; REAL_ABS_POW; REAL_ABS_ABS] THEN
   8.661 +  ONCE_REWRITE_TAC[REAL_MUL_SYM] THEN
   8.662 +  ASM_SIMP_TAC[GSYM REAL_LT_RDIV_EQ; REAL_POW_LT; GSYM REAL_ABS_NZ]);; *)
   8.663 +
   8.664 +lemma stretch_galois: True .. (*
   8.665 + "!x:real^N y:real^N m.
   8.666 +        (!k. 1 <= k \<and> k <= dimindex(:N) ==>  ~(m k = 0))
   8.667 +        ==> ((y = (lambda k. m k * x$k)) \<longleftrightarrow> (lambda k. inv(m k) * y$k) = x)"
   8.668 +qed   REPEAT GEN_TAC THEN SIMP_TAC[CART_EQ; LAMBDA_BETA] THEN
   8.669 +  MATCH_MP_TAC(MESON[]
   8.670 +   `(!x. p x ==> (q x \<longleftrightarrow> r x))
   8.671 +    ==> (!x. p x) ==> ((!x. q x) \<longleftrightarrow> (!x. r x))`) THEN
   8.672 +  GEN_TAC THEN ASM_CASES_TAC `1 <= k \<and> k <= dimindex(:N)` THEN
   8.673 +  ASM_REWRITE_TAC[] THEN CONV_TAC REAL_FIELD);; *)
   8.674 +
   8.675 +lemma has_gmeasure_stretch: True .. (*
   8.676 + "!s m y. s has_gmeasure y
   8.677 +           ==> (IMAGE (\<lambda>x:real^N. lambda k. m k * x$k) s :real^N->bool)
   8.678 +               has_gmeasure abs(product (1..dimindex(:N)) m) * y"
   8.679 +qed   REPEAT STRIP_TAC THEN ASM_CASES_TAC
   8.680 +   `!k. 1 <= k \<and> k <= dimindex(:N) ==> ~(m k = 0)`
   8.681 +  THENL
   8.682 +   [ALL_TAC;
   8.683 +    FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [NOT_FORALL_THM]) THEN
   8.684 +    REWRITE_TAC[NOT_IMP; GSYM CONJ_ASSOC; LEFT_IMP_EXISTS_THM] THEN
   8.685 +    X_GEN_TAC `k:num` THEN STRIP_TAC THEN
   8.686 +    SUBGOAL_THEN `product(1..dimindex (:N)) m = 0` SUBST1_TAC THENL
   8.687 +     [ASM_MESON_TAC[PRODUCT_EQ_0_NUMSEG]; ALL_TAC] THEN
   8.688 +    REWRITE_TAC[REAL_ABS_NUM; REAL_MUL_LZERO; HAS_GMEASURE_0] THEN
   8.689 +    MATCH_MP_TAC NEGLIGIBLE_SUBSET THEN
   8.690 +    EXISTS_TAC `{x:real^N | x$k = 0}` THEN
   8.691 +    ASM_SIMP_TAC[NEGLIGIBLE_STANDARD_HYPERPLANE; SUBSET; FORALL_IN_IMAGE] THEN
   8.692 +    ASM_SIMP_TAC[IN_ELIM_THM; LAMBDA_BETA; REAL_MUL_LZERO]] THEN
   8.693 +  UNDISCH_TAC `(s:real^N->bool) has_gmeasure y` THEN
   8.694 +  REWRITE_TAC[HAS_GMEASURE] THEN
   8.695 +  ONCE_REWRITE_TAC[HAS_INTEGRAL] THEN REWRITE_TAC[IN_UNIV] THEN
   8.696 +  DISCH_TAC THEN X_GEN_TAC `e:real` THEN DISCH_TAC THEN
   8.697 +  SUBGOAL_THEN `0 < abs(product(1..dimindex(:N)) m)` ASSUME_TAC THENL
   8.698 +   [ASM_MESON_TAC[REAL_ABS_NZ; REAL_LT_DIV; PRODUCT_EQ_0_NUMSEG];
   8.699 +    ALL_TAC] THEN
   8.700 +  FIRST_X_ASSUM(MP_TAC o SPEC `e:real / abs(product(1..dimindex(:N)) m)`) THEN
   8.701 +  ASM_SIMP_TAC[REAL_LT_DIV] THEN
   8.702 +  DISCH_THEN(X_CHOOSE_THEN `B:real` STRIP_ASSUME_TAC) THEN
   8.703 +  EXISTS_TAC `sup(IMAGE (\<lambda>k. abs(m k) * B) (1..dimindex(:N)))` THEN
   8.704 +  MATCH_MP_TAC(TAUT `a \<and> (a ==> b) ==> a \<and> b`) THEN CONJ_TAC THENL
   8.705 +   [ASM_SIMP_TAC[REAL_LT_SUP_FINITE; FINITE_IMAGE; NUMSEG_EMPTY; FINITE_NUMSEG;
   8.706 +                 IN_NUMSEG; GSYM NOT_LE; DIMINDEX_GE_1; IMAGE_EQ_EMPTY;
   8.707 +                 EXISTS_IN_IMAGE] THEN
   8.708 +    ASM_MESON_TAC[IN_NUMSEG; DIMINDEX_GE_1; LE_REFL; REAL_LT_MUL; REAL_ABS_NZ];
   8.709 +    DISCH_TAC] THEN
   8.710 +  MAP_EVERY X_GEN_TAC [`u:real^N`; `v:real^N`] THEN DISCH_TAC THEN
   8.711 +  ASM_SIMP_TAC[IN_IMAGE; STRETCH_GALOIS; UNWIND_THM1] THEN
   8.712 +  FIRST_X_ASSUM(MP_TAC o SPECL
   8.713 +    [`(lambda k. min (inv(m k) * (u:real^N)$k)
   8.714 +                     (inv(m k) * (v:real^N)$k)):real^N`;
   8.715 +     `(lambda k. max (inv(m k) * (u:real^N)$k)
   8.716 +                 (inv(m k) * (v:real^N)$k)):real^N`]) THEN
   8.717 +  MATCH_MP_TAC(TAUT `a \<and> (b ==> a ==> c) ==> (a ==> b) ==> c`) THEN
   8.718 +  CONJ_TAC THENL
   8.719 +   [ALL_TAC;
   8.720 +    REWRITE_TAC[LEFT_IMP_EXISTS_THM] THEN X_GEN_TAC `z:real^1` THEN
   8.721 +    DISCH_THEN(CONJUNCTS_THEN2 MP_TAC ASSUME_TAC) THEN
   8.722 +    SUBGOAL_THEN `!k. 1 <= k \<and> k <= dimindex (:N) ==> ~(inv(m k) = 0)`
   8.723 +    MP_TAC THENL [ASM_SIMP_TAC[REAL_INV_EQ_0]; ALL_TAC] THEN
   8.724 +    ONCE_REWRITE_TAC[GSYM IMP_CONJ_ALT] THEN
   8.725 +    DISCH_THEN(MP_TAC o MATCH_MP HAS_INTEGRAL_STRETCH)] THEN
   8.726 +  (MP_TAC(ISPECL [`u:real^N`; `v:real^N`; `\i:num. inv(m i)`]
   8.727 +    IMAGE_STRETCH_INTERVAL) THEN
   8.728 +   SUBGOAL_THEN `~(interval[u:real^N,v] = {})` ASSUME_TAC THENL
   8.729 +    [FIRST_ASSUM(MATCH_MP_TAC o MATCH_MP (SET_RULE
   8.730 +      `s \<subseteq> t ==> ~(s = {}) ==> ~(t = {})`)) THEN
   8.731 +     ASM_REWRITE_TAC[BALL_EQ_EMPTY; GSYM REAL_NOT_LT];
   8.732 +     ALL_TAC] THEN
   8.733 +   ASM_REWRITE_TAC[] THEN DISCH_THEN(SUBST1_TAC o SYM))
   8.734 +  THENL
   8.735 +   [FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP (SET_RULE
   8.736 +     `b \<subseteq> s ==> b' \<subseteq> IMAGE f b ==> b' \<subseteq> IMAGE f s`)) THEN
   8.737 +    REWRITE_TAC[IN_BALL; SUBSET; NORM_ARITH `dist(0,x) = norm x`;
   8.738 +                IN_IMAGE] THEN
   8.739 +    ASM_SIMP_TAC[STRETCH_GALOIS; REAL_INV_EQ_0; UNWIND_THM1; REAL_INV_INV] THEN
   8.740 +    X_GEN_TAC `x:real^N` THEN DISCH_TAC THEN
   8.741 +    MATCH_MP_TAC REAL_LET_TRANS THEN
   8.742 +    EXISTS_TAC
   8.743 +     `norm(sup(IMAGE(\<lambda>k. abs(m k)) (1..dimindex(:N))) % x:real^N)` THEN
   8.744 +    CONJ_TAC THENL
   8.745 +     [MATCH_MP_TAC NORM_LE_COMPONENTWISE THEN
   8.746 +      SIMP_TAC[LAMBDA_BETA; VECTOR_MUL_COMPONENT; REAL_ABS_MUL] THEN
   8.747 +      REPEAT STRIP_TAC THEN MATCH_MP_TAC REAL_LE_RMUL THEN
   8.748 +      REWRITE_TAC[REAL_ABS_POS] THEN
   8.749 +      MATCH_MP_TAC(REAL_ARITH `x <= y ==> x <= abs y`) THEN
   8.750 +      ASM_SIMP_TAC[REAL_LE_SUP_FINITE; FINITE_IMAGE; IMAGE_EQ_EMPTY;
   8.751 +                  NUMSEG_EMPTY; FINITE_NUMSEG; GSYM NOT_LE; DIMINDEX_GE_1] THEN
   8.752 +      REWRITE_TAC[EXISTS_IN_IMAGE; IN_NUMSEG] THEN ASM_MESON_TAC[REAL_LE_REFL];
   8.753 +      ALL_TAC] THEN
   8.754 +    REWRITE_TAC[NORM_MUL] THEN MATCH_MP_TAC REAL_LTE_TRANS THEN
   8.755 +    EXISTS_TAC `abs(sup(IMAGE(\<lambda>k. abs(m k)) (1..dimindex(:N)))) * B` THEN
   8.756 +    SUBGOAL_THEN `0 < sup(IMAGE(\<lambda>k. abs(m k)) (1..dimindex(:N)))`
   8.757 +    ASSUME_TAC THENL
   8.758 +     [ASM_SIMP_TAC[REAL_LT_SUP_FINITE; FINITE_IMAGE; IMAGE_EQ_EMPTY;
   8.759 +                  NUMSEG_EMPTY; FINITE_NUMSEG; GSYM NOT_LE; DIMINDEX_GE_1] THEN
   8.760 +      REWRITE_TAC[EXISTS_IN_IMAGE; GSYM REAL_ABS_NZ; IN_NUMSEG] THEN
   8.761 +      ASM_MESON_TAC[DIMINDEX_GE_1; LE_REFL];
   8.762 +      ALL_TAC] THEN
   8.763 +    ASM_SIMP_TAC[REAL_LT_LMUL_EQ; REAL_ARITH `0 < x ==> 0 < abs x`] THEN
   8.764 +    MATCH_MP_TAC REAL_LE_TRANS THEN
   8.765 +    EXISTS_TAC `sup(IMAGE(\<lambda>k. abs(m k)) (1..dimindex(:N))) * B` THEN
   8.766 +    ASM_SIMP_TAC[REAL_LE_RMUL_EQ; REAL_ARITH `0 < x ==> abs x <= x`] THEN
   8.767 +    ASM_SIMP_TAC[REAL_LE_SUP_FINITE; FINITE_IMAGE; IMAGE_EQ_EMPTY;
   8.768 +                  NUMSEG_EMPTY; FINITE_NUMSEG; GSYM NOT_LE; DIMINDEX_GE_1] THEN
   8.769 +    ASM_SIMP_TAC[EXISTS_IN_IMAGE; REAL_LE_RMUL_EQ] THEN
   8.770 +    ASM_SIMP_TAC[REAL_SUP_LE_FINITE; FINITE_IMAGE; IMAGE_EQ_EMPTY;
   8.771 +                 NUMSEG_EMPTY; FINITE_NUMSEG; GSYM NOT_LE; DIMINDEX_GE_1] THEN
   8.772 +    MP_TAC(ISPEC `IMAGE (\<lambda>k. abs (m k)) (1..dimindex(:N))` SUP_FINITE) THEN
   8.773 +    REWRITE_TAC[FORALL_IN_IMAGE] THEN
   8.774 +    ASM_SIMP_TAC[FINITE_IMAGE; FINITE_NUMSEG; IMAGE_EQ_EMPTY; NUMSEG_EMPTY;
   8.775 +                 GSYM NOT_LE; DIMINDEX_GE_1] THEN
   8.776 +    REWRITE_TAC[IN_IMAGE] THEN MESON_TAC[];
   8.777 +
   8.778 +    MATCH_MP_TAC(MESON[]
   8.779 +     `s = t \<and> P z ==> (f has_integral z) s ==> Q
   8.780 +                       ==> ?w. (f has_integral w) t \<and> P w`) THEN
   8.781 +    SIMP_TAC[GSYM PRODUCT_INV; FINITE_NUMSEG; GSYM REAL_ABS_INV] THEN
   8.782 +    REWRITE_TAC[REAL_INV_INV] THEN CONJ_TAC THENL
   8.783 +     [REWRITE_TAC[GSYM IMAGE_o] THEN MATCH_MP_TAC(SET_RULE
   8.784 +       `(!x. f x = x) ==> IMAGE f s = s`) THEN
   8.785 +      SIMP_TAC[o_THM; LAMBDA_BETA; CART_EQ] THEN
   8.786 +      ASM_SIMP_TAC[REAL_MUL_ASSOC; REAL_MUL_RINV; REAL_MUL_LID];
   8.787 +      REWRITE_TAC[ABS_; _SUB; LIFT_; _CMUL] THEN
   8.788 +      REWRITE_TAC[GSYM REAL_SUB_LDISTRIB; ETA_AX] THEN
   8.789 +      REWRITE_TAC[REAL_ABS_MUL; REAL_ABS_ABS] THEN
   8.790 +      ONCE_REWRITE_TAC[REAL_MUL_SYM] THEN
   8.791 +      ASM_SIMP_TAC[GSYM REAL_LT_RDIV_EQ] THEN
   8.792 +      ASM_MESON_TAC[ABS_; _SUB; LIFT_]]]);; *)
   8.793 +
   8.794 +lemma has_gmeasure_translation: True .. (*
   8.795 + "!s m a. s has_gmeasure m ==> (IMAGE (\<lambda>x:real^N. a + x) s) has_gmeasure m"
   8.796 +qed   REPEAT GEN_TAC THEN
   8.797 +  MP_TAC(ISPECL [`s:real^N->bool`; `1`; `a:real^N`; `m:real`]
   8.798 +                HAS_GMEASURE_AFFINITY) THEN
   8.799 +  REWRITE_TAC[VECTOR_MUL_LID; REAL_ABS_NUM; REAL_POW_ONE; REAL_MUL_LID] THEN
   8.800 +  REWRITE_TAC[VECTOR_ADD_SYM]);; *)
   8.801 +
   8.802 +lemma negligible_translation: True .. (*
   8.803 + "!s a. negligible s ==> negligible (IMAGE (\<lambda>x:real^N. a + x) s)"
   8.804 +qed   SIMP_TAC[GSYM HAS_GMEASURE_0; HAS_GMEASURE_TRANSLATION]);; *)
   8.805 +
   8.806 +lemma has_gmeasure_translation_eq: True .. (*
   8.807 + "!s m. (IMAGE (\<lambda>x:real^N. a + x) s) has_gmeasure m \<longleftrightarrow> s has_gmeasure m"
   8.808 +qed   REPEAT GEN_TAC THEN EQ_TAC THEN REWRITE_TAC[HAS_GMEASURE_TRANSLATION] THEN
   8.809 +  DISCH_THEN(MP_TAC o SPEC `--a:real^N` o
   8.810 +    MATCH_MP HAS_GMEASURE_TRANSLATION) THEN
   8.811 +  MATCH_MP_TAC EQ_IMP THEN AP_THM_TAC THEN AP_TERM_TAC THEN
   8.812 +  REWRITE_TAC[GSYM IMAGE_o; o_DEF; VECTOR_ARITH `--a + a + b:real^N = b`] THEN
   8.813 +  SET_TAC[]);; *)
   8.814 +
   8.815 +lemma negligible_translation_rev: True .. (*
   8.816 + "!s a. negligible (IMAGE (\<lambda>x:real^N. a + x) s) ==> negligible s"
   8.817 +qed   SIMP_TAC[GSYM HAS_GMEASURE_0; HAS_GMEASURE_TRANSLATION_EQ]);; *)
   8.818 +
   8.819 +lemma negligible_translation_eq: True .. (*
   8.820 + "!s a. negligible (IMAGE (\<lambda>x:real^N. a + x) s) \<longleftrightarrow> negligible s"
   8.821 +qed   SIMP_TAC[GSYM HAS_GMEASURE_0; HAS_GMEASURE_TRANSLATION_EQ]);; *)
   8.822 +
   8.823 +lemma gmeasurable_translation: True .. (*
   8.824 + "!s. gmeasurable (IMAGE (\<lambda>x. a + x) s) \<longleftrightarrow> gmeasurable s"
   8.825 +qed   REWRITE_TAC[measurable; HAS_GMEASURE_TRANSLATION_EQ]);; *)
   8.826 +
   8.827 +lemma measure_translation: True .. (*
   8.828 + "!s. gmeasurable s ==> measure(IMAGE (\<lambda>x. a + x) s) = gmeasure s"
   8.829 +qed   REWRITE_TAC[HAS_GMEASURE_MEASURE] THEN REPEAT STRIP_TAC THEN
   8.830 +  MATCH_MP_TAC MEASURE_UNIQUE THEN
   8.831 +  ASM_REWRITE_TAC[HAS_GMEASURE_TRANSLATION_EQ]);; *)
   8.832 +
   8.833 +lemma has_gmeasure_scaling: True .. (*
   8.834 + "!s m c. s has_gmeasure m
   8.835 +           ==> (IMAGE (\<lambda>x:real^N. c % x) s) has_gmeasure
   8.836 +               (abs(c) pow dimindex(:N)) * m"
   8.837 +qed   REPEAT GEN_TAC THEN
   8.838 +  MP_TAC(ISPECL [`s:real^N->bool`; `c:real`; `0:real^N`; `m:real`]
   8.839 +                HAS_GMEASURE_AFFINITY) THEN
   8.840 +  REWRITE_TAC[VECTOR_ADD_RID]);; *)
   8.841 +
   8.842 +lemma has_gmeasure_scaling_eq: True .. (*
   8.843 + "!s m c. ~(c = 0)
   8.844 +           ==> (IMAGE (\<lambda>x:real^N. c % x) s
   8.845 +                  has_gmeasure (abs(c) pow dimindex(:N)) * m \<longleftrightarrow>
   8.846 +                s has_gmeasure m)"
   8.847 +qed   REPEAT STRIP_TAC THEN EQ_TAC THEN REWRITE_TAC[HAS_GMEASURE_SCALING] THEN
   8.848 +  DISCH_THEN(MP_TAC o SPEC `inv(c)` o MATCH_MP HAS_GMEASURE_SCALING) THEN
   8.849 +  REWRITE_TAC[GSYM IMAGE_o; o_DEF; GSYM REAL_ABS_MUL] THEN
   8.850 +  REWRITE_TAC[GSYM REAL_POW_MUL; VECTOR_MUL_ASSOC; REAL_MUL_ASSOC] THEN
   8.851 +  ASM_SIMP_TAC[GSYM REAL_ABS_MUL; REAL_MUL_LINV] THEN
   8.852 +  REWRITE_TAC[REAL_POW_ONE; REAL_ABS_NUM; REAL_MUL_LID; VECTOR_MUL_LID] THEN
   8.853 +  MATCH_MP_TAC EQ_IMP THEN AP_THM_TAC THEN AP_TERM_TAC THEN SET_TAC[]);; *)
   8.854 +
   8.855 +lemma gmeasurable_scaling: True .. (*
   8.856 + "!s c. gmeasurable s ==> gmeasurable (IMAGE (\<lambda>x. c % x) s)"
   8.857 +qed   REWRITE_TAC[measurable] THEN MESON_TAC[HAS_GMEASURE_SCALING]);; *)
   8.858 +
   8.859 +lemma gmeasurable_scaling_eq: True .. (*
   8.860 + "!s c. ~(c = 0) ==> (measurable (IMAGE (\<lambda>x. c % x) s) \<longleftrightarrow> gmeasurable s)"
   8.861 +qed   REPEAT STRIP_TAC THEN EQ_TAC THEN REWRITE_TAC[MEASURABLE_SCALING] THEN
   8.862 +  DISCH_THEN(MP_TAC o SPEC `inv c` o MATCH_MP GMEASURABLE_SCALING) THEN
   8.863 +  REWRITE_TAC[GSYM IMAGE_o; o_DEF; GSYM REAL_ABS_MUL] THEN
   8.864 +  MATCH_MP_TAC EQ_IMP THEN AP_TERM_TAC THEN
   8.865 +  ASM_SIMP_TAC[VECTOR_MUL_ASSOC; REAL_MUL_LINV; VECTOR_MUL_LID] THEN
   8.866 +  SET_TAC[]);; *)
   8.867 +
   8.868 +lemma measure_scaling: True .. (*
   8.869 + "!s. gmeasurable s
   8.870 +       ==> measure(IMAGE (\<lambda>x:real^N. c % x) s) =
   8.871 +              (abs(c) pow dimindex(:N)) * gmeasure s"
   8.872 +qed   REWRITE_TAC[HAS_GMEASURE_MEASURE] THEN REPEAT STRIP_TAC THEN
   8.873 +  MATCH_MP_TAC MEASURE_UNIQUE THEN ASM_SIMP_TAC[HAS_GMEASURE_SCALING]);; *)
   8.874 +
   8.875 +(* ------------------------------------------------------------------------- *)
   8.876 +(* Measurability of countable unions and intersections of various kinds.     *)
   8.877 +(* ------------------------------------------------------------------------- *)
   8.878 +
   8.879 +lemma has_gmeasure_nested_unions:
   8.880 +  assumes "\<And>n. gmeasurable(s n)" "\<And>n. gmeasure(s n) \<le> B" "\<And>n. s(n) \<subseteq> s(Suc n)"
   8.881 +  shows "gmeasurable(\<Union> { s n | n. n \<in> UNIV }) \<and>
   8.882 +  (\<lambda>n. gmeasure(s n)) ----> gmeasure(\<Union> { s(n) | n. n \<in> UNIV })"
   8.883 +proof- let ?g = "\<lambda>x. if x \<in> \<Union>{s n |n. n \<in> UNIV} then 1 else (0::real)"
   8.884 +  have "?g integrable_on UNIV \<and> (\<lambda>k. integral UNIV (\<lambda>x. if x \<in> s k then 1 else 0)) ----> integral UNIV ?g"
   8.885 +  proof(rule monotone_convergence_increasing)
   8.886 +    case goal1 show ?case using assms(1) unfolding gmeasurable_integrable by auto
   8.887 +    case goal2 show ?case using assms(3) by auto
   8.888 +    have "\<forall>m n. m\<le>n \<longrightarrow> s m \<subseteq> s n" apply(subst transitive_stepwise_le_eq)
   8.889 +      using assms(3) by auto note * = this[rule_format]
   8.890 +    have **:"\<And>x e n. \<lbrakk>x \<in> s n; 0 < e\<rbrakk> \<Longrightarrow> \<exists>N. \<forall>n. x \<notin> s n \<longrightarrow> N \<le> n \<longrightarrow> dist 0 1 < e"
   8.891 +      apply(rule_tac x=n in exI) using * by auto 
   8.892 +    case goal3 show ?case unfolding Lim_sequentially by(auto intro!: **) 
   8.893 +    case goal4 show ?case unfolding bounded_def apply(rule_tac x=0 in exI)
   8.894 +      apply(rule_tac x=B in exI) unfolding dist_real_def apply safe
   8.895 +      unfolding measure_integral_univ[OF assms(1),THEN sym]
   8.896 +      apply(subst abs_of_nonpos) using assms(1,2) by auto
   8.897 +  qed note conjunctD2[OF this]
   8.898 +  thus ?thesis unfolding gmeasurable_integrable[THEN sym] measure_integral_univ[OF assms(1)]
   8.899 +    apply- unfolding measure_integral_univ by auto
   8.900 +qed
   8.901 +
   8.902 +lemmas gmeasurable_nested_unions = has_gmeasure_nested_unions(1)
   8.903 +
   8.904 +lemma sums_alt:"f sums s = (\<lambda>n. setsum f {0..n}) ----> s"
   8.905 +proof- have *:"\<And>n. {0..<Suc n} = {0..n}" by auto
   8.906 +  show ?thesis unfolding sums_def apply(subst LIMSEQ_Suc_iff[THEN sym]) unfolding * ..
   8.907 +qed
   8.908 +
   8.909 +lemma has_gmeasure_countable_negligible_unions: 
   8.910 +  assumes "\<And>n. gmeasurable(s n)" "\<And>m n. m \<noteq> n \<Longrightarrow> negligible(s m \<inter> s n)"
   8.911 +  "\<And>n. setsum (\<lambda>k. gmeasure(s k)) {0..n}  <= B"
   8.912 +  shows "gmeasurable(\<Union> { s(n) |n. n \<in> UNIV })" (is ?m)
   8.913 +  "((\<lambda>n. gmeasure(s n)) sums (gmeasure(\<Union> { s(n) |n. n \<in> UNIV })))" (is ?s)
   8.914 +proof- have *:"\<And>n. (\<Union> (s ` {0..n})) has_gmeasure (setsum (\<lambda>k. gmeasure(s k)) {0..n})"
   8.915 +    apply(rule has_gmeasure_negligible_unions_image) using assms by auto
   8.916 +  have **:"(\<Union>{\<Union>s ` {0..n} |n. n \<in> UNIV}) = (\<Union>{s n |n. n \<in> UNIV})" unfolding simple_image by fastsimp
   8.917 +  have "gmeasurable (\<Union>{\<Union>s ` {0..n} |n. n \<in> UNIV}) \<and>
   8.918 +    (\<lambda>n. gmeasure (\<Union>(s ` {0..n}))) ----> gmeasure (\<Union>{\<Union>s ` {0..n} |n. n \<in> UNIV})"
   8.919 +    apply(rule has_gmeasure_nested_unions) apply(rule gmeasurableI,rule *)
   8.920 +    unfolding measure_unique[OF *] defer apply(rule Union_mono,rule image_mono) using assms(3) by auto
   8.921 +  note lem = conjunctD2[OF this,unfolded **]
   8.922 +  show ?m using lem(1) .
   8.923 +  show ?s using lem(2) unfolding sums_alt measure_unique[OF *] .
   8.924 +qed     
   8.925 +
   8.926 +lemma negligible_countable_unions: True .. (*
   8.927 + "!s:num->real^N->bool.
   8.928 +        (!n. negligible(s n)) ==> negligible(UNIONS {s(n) | n \<in> (:num)})"
   8.929 +qed   REPEAT STRIP_TAC THEN
   8.930 +  MP_TAC(ISPECL [`s:num->real^N->bool`; `0`]
   8.931 +    HAS_GMEASURE_COUNTABLE_NEGLIGIBLE_UNIONS) THEN
   8.932 +  ASM_SIMP_TAC[MEASURE_EQ_0; SUM_0; REAL_LE_REFL; LIFT_NUM] THEN ANTS_TAC THENL
   8.933 +   [ASM_MESON_TAC[HAS_GMEASURE_0; gmeasurable; INTER_SUBSET; NEGLIGIBLE_SUBSET];
   8.934 +    ALL_TAC] THEN
   8.935 +  SIMP_TAC[GSYM GMEASURABLE_MEASURE_EQ_0] THEN
   8.936 +  STRIP_TAC THEN REWRITE_TAC[GSYM LIFT_EQ] THEN
   8.937 +  MATCH_MP_TAC SERIES_UNIQUE THEN REWRITE_TAC[LIFT_NUM] THEN
   8.938 +  MAP_EVERY EXISTS_TAC [`(\<lambda>k. 0):num->real^1`; `from 0`] THEN
   8.939 +  ASM_REWRITE_TAC[SERIES_0]);; *)
   8.940 +
   8.941 +lemma gmeasurable_countable_unions_strong:
   8.942 +  assumes "\<And>n. gmeasurable(s n)" "\<And>n::nat. gmeasure(\<Union>{s k |k. k \<le> n}) \<le> B"
   8.943 +  shows "gmeasurable(\<Union>{ s(n) |n. n \<in> UNIV })"
   8.944 +proof- have *:"\<Union>{\<Union>s ` {0..n} |n. n \<in> UNIV} = \<Union>range s" unfolding simple_image by fastsimp
   8.945 +  show ?thesis unfolding simple_image
   8.946 +    apply(rule gmeasurable_nested_unions[of "\<lambda>n. \<Union>(s ` {0..n})", THEN conjunct1,unfolded *])
   8.947 +  proof- fix n::nat show "gmeasurable (\<Union>s ` {0..n})"
   8.948 +      apply(rule gmeasurable_finite_unions) using assms(1) by auto
   8.949 +    show "gmeasure (\<Union>s ` {0..n}) \<le> B"
   8.950 +      using assms(2)[of n] unfolding simple_image[THEN sym] by auto
   8.951 +    show "\<Union>s ` {0..n} \<subseteq> \<Union>s ` {0..Suc n}" apply(rule Union_mono) by auto
   8.952 +  qed
   8.953 +qed
   8.954 +
   8.955 +lemma has_gmeasure_countable_negligible_unions_bounded: True .. (*
   8.956 + "!s:num->real^N->bool.
   8.957 +        (!n. gmeasurable(s n)) \<and>
   8.958 +        (!m n. ~(m = n) ==> negligible(s m \<inter> s n)) \<and>
   8.959 +        bounded(\<Union>{ s(n) | n \<in> (:num) })
   8.960 +        ==> gmeasurable(\<Union>{ s(n) | n \<in> (:num) }) \<and>
   8.961 +            ((\<lambda>n. lift(measure(s n))) sums
   8.962 +             lift(measure(\<Union>{ s(n) | n \<in> (:num) }))) (from 0)"
   8.963 +qed   REPEAT GEN_TAC THEN STRIP_TAC THEN
   8.964 +  FIRST_ASSUM(MP_TAC o MATCH_MP BOUNDED_SUBSET_CLOSED_INTERVAL) THEN
   8.965 +  REWRITE_TAC[LEFT_IMP_EXISTS_THM] THEN
   8.966 +  MAP_EVERY X_GEN_TAC [`a:real^N`; `b:real^N`] THEN DISCH_TAC THEN
   8.967 +  MATCH_MP_TAC HAS_GMEASURE_COUNTABLE_NEGLIGIBLE_UNIONS THEN
   8.968 +  EXISTS_TAC `measure(interval[a:real^N,b])` THEN
   8.969 +  ASM_REWRITE_TAC[] THEN X_GEN_TAC `n:num` THEN MATCH_MP_TAC REAL_LE_TRANS THEN
   8.970 +  EXISTS_TAC `measure(UNIONS (IMAGE (s:num->real^N->bool) (0..n)))` THEN
   8.971 +  CONJ_TAC THENL
   8.972 +   [MATCH_MP_TAC REAL_EQ_IMP_LE THEN CONV_TAC SYM_CONV THEN
   8.973 +    MATCH_MP_TAC MEASURE_NEGLIGIBLE_UNIONS_IMAGE THEN
   8.974 +    ASM_SIMP_TAC[FINITE_NUMSEG];
   8.975 +    MATCH_MP_TAC MEASURE_SUBSET THEN REWRITE_TAC[MEASURABLE_INTERVAL] THEN
   8.976 +    CONJ_TAC THENL
   8.977 +     [MATCH_MP_TAC GMEASURABLE_UNIONS THEN
   8.978 +      ASM_SIMP_TAC[FINITE_IMAGE; FINITE_NUMSEG; FORALL_IN_IMAGE];
   8.979 +      ASM SET_TAC[]]]);; *)
   8.980 +
   8.981 +lemma gmeasurable_countable_negligible_unions_bounded: True .. (*
   8.982 + "!s:num->real^N->bool.
   8.983 +        (!n. gmeasurable(s n)) \<and>
   8.984 +        (!m n. ~(m = n) ==> negligible(s m \<inter> s n)) \<and>
   8.985 +        bounded(\<Union>{ s(n) | n \<in> (:num) })
   8.986 +        ==> gmeasurable(\<Union>{ s(n) | n \<in> (:num) })"
   8.987 +qed   SIMP_TAC[HAS_GMEASURE_COUNTABLE_NEGLIGIBLE_UNIONS_BOUNDED]);; *)
   8.988 +
   8.989 +lemma gmeasurable_countable_unions: True .. (*
   8.990 + "!s:num->real^N->bool B.
   8.991 +        (!n. gmeasurable(s n)) \<and>
   8.992 +        (!n. sum (0..n) (\<lambda>k. measure(s k)) \<le> B)
   8.993 +        ==> gmeasurable(\<Union>{ s(n) | n \<in> (:num) })"
   8.994 +qed   REPEAT STRIP_TAC THEN MATCH_MP_TAC GMEASURABLE_COUNTABLE_UNIONS_STRONG THEN
   8.995 +  EXISTS_TAC `B:real` THEN ASM_REWRITE_TAC[] THEN
   8.996 +  X_GEN_TAC `n:num` THEN MATCH_MP_TAC REAL_LE_TRANS THEN
   8.997 +  EXISTS_TAC `sum(0..n) (\<lambda>k. measure(s k:real^N->bool))` THEN
   8.998 +  ASM_REWRITE_TAC[] THEN
   8.999 +  W(MP_TAC o PART_MATCH (rand o rand) MEASURE_UNIONS_LE_IMAGE o
  8.1000 +       rand o snd) THEN
  8.1001 +  ASM_REWRITE_TAC[FINITE_NUMSEG] THEN
  8.1002 +  ONCE_REWRITE_TAC[GSYM SIMPLE_IMAGE] THEN
  8.1003 +  REWRITE_TAC[IN_NUMSEG; LE_0]);; *)
  8.1004 +
  8.1005 +lemma gmeasurable_countable_inters: True .. (*
  8.1006 + "!s:num->real^N->bool.
  8.1007 +        (!n. gmeasurable(s n))
  8.1008 +        ==> gmeasurable(INTERS { s(n) | n \<in> (:num) })"
  8.1009 +qed   REPEAT STRIP_TAC THEN
  8.1010 +  SUBGOAL_THEN `INTERS { s(n):real^N->bool | n \<in> (:num) } =
  8.1011 +                s 0 DIFF (\<Union>{s 0 DIFF s n | n \<in> (:num)})`
  8.1012 +  SUBST1_TAC THENL
  8.1013 +   [GEN_REWRITE_TAC I [EXTENSION] THEN
  8.1014 +    REWRITE_TAC[IN_INTERS; IN_DIFF; IN_UNIONS] THEN
  8.1015 +    REWRITE_TAC[SIMPLE_IMAGE; FORALL_IN_IMAGE; EXISTS_IN_IMAGE] THEN
  8.1016 +    ASM SET_TAC[];
  8.1017 +    ALL_TAC] THEN
  8.1018 +  MATCH_MP_TAC GMEASURABLE_DIFF THEN ASM_REWRITE_TAC[] THEN
  8.1019 +  MATCH_MP_TAC GMEASURABLE_COUNTABLE_UNIONS_STRONG THEN
  8.1020 +  EXISTS_TAC `measure(s 0:real^N->bool)` THEN
  8.1021 +  ASM_SIMP_TAC[MEASURABLE_DIFF; LE_0] THEN
  8.1022 +  GEN_TAC THEN MATCH_MP_TAC MEASURE_SUBSET THEN
  8.1023 +  ASM_REWRITE_TAC[] THEN CONJ_TAC THENL
  8.1024 +   [ALL_TAC;
  8.1025 +    REWRITE_TAC[SUBSET; FORALL_IN_UNIONS; IN_ELIM_THM; IN_DIFF] THEN
  8.1026 +    MESON_TAC[IN_DIFF]] THEN
  8.1027 +  ONCE_REWRITE_TAC[GSYM IN_NUMSEG_0] THEN
  8.1028 +  ONCE_REWRITE_TAC[SIMPLE_IMAGE] THEN
  8.1029 +  ASM_SIMP_TAC[FORALL_IN_IMAGE; FINITE_IMAGE; FINITE_NUMSEG;
  8.1030 +               GMEASURABLE_DIFF; GMEASURABLE_UNIONS]);; *)
  8.1031 +
  8.1032 +(* ------------------------------------------------------------------------- *)
  8.1033 +(* measurability of compact and bounded open sets.                           *)
  8.1034 +(* ------------------------------------------------------------------------- *)
  8.1035 +
  8.1036 +lemma gmeasurable_compact: True .. (*
  8.1037 + "!s:real^N->bool. compact s ==> gmeasurable s"
  8.1038 +qed   lemma lemma = prove
  8.1039 +   (`!f s:real^N->bool.
  8.1040 +          (!n. FINITE(f n)) \<and>
  8.1041 +          (!n. s \<subseteq> UNIONS(f n)) \<and>
  8.1042 +          (!x. ~(x \<in> s) ==> ?n. ~(x \<in> UNIONS(f n))) \<and>
  8.1043 +          (!n a. a \<in> f(SUC n) ==> ?b. b \<in> f(n) \<and> a \<subseteq> b) \<and>
  8.1044 +          (!n a. a \<in> f(n) ==> gmeasurable a)
  8.1045 +          ==> gmeasurable s"
  8.1046 +qed     REPEAT STRIP_TAC THEN
  8.1047 +    SUBGOAL_THEN `!n. UNIONS(f(SUC n):(real^N->bool)->bool) \<subseteq> UNIONS(f n)`
  8.1048 +    ASSUME_TAC THENL [ASM SET_TAC[]; ALL_TAC] THEN
  8.1049 +    SUBGOAL_THEN `s = INTERS { UNIONS(f n) | n \<in> (:num) }:real^N->bool`
  8.1050 +    SUBST1_TAC THENL
  8.1051 +     [ONCE_REWRITE_TAC[SIMPLE_IMAGE] THEN
  8.1052 +      MATCH_MP_TAC SUBSET_ANTISYM THEN CONJ_TAC THEN
  8.1053 +      REWRITE_TAC[SUBSET; IN_INTERS; FORALL_IN_IMAGE; IN_UNIV] THEN
  8.1054 +      REWRITE_TAC[IN_IMAGE] THEN ASM SET_TAC[];
  8.1055 +      MATCH_MP_TAC GMEASURABLE_COUNTABLE_INTERS THEN
  8.1056 +      ASM_REWRITE_TAC[] THEN GEN_TAC THEN
  8.1057 +      MATCH_MP_TAC GMEASURABLE_UNIONS THEN
  8.1058 +      ASM_MESON_TAC[]]) in
  8.1059 +  REPEAT STRIP_TAC THEN MATCH_MP_TAC lemma THEN
  8.1060 +  EXISTS_TAC
  8.1061 +   `\n. { k | ?u:real^N. (!i. 1 \<le> i \<and> i \<le> dimindex(:N)
  8.1062 +                              ==> integer(u$i)) \<and>
  8.1063 +                  k = { x:real^N | !i. 1 \<le> i \<and> i \<le> dimindex(:N)
  8.1064 +                                       ==> u$i / 2 pow n \<le> x$i \<and>
  8.1065 +                                           x$i < (u$i + 1) / 2 pow n } \<and>
  8.1066 +                  ~(s \<inter> k = {})}` THEN
  8.1067 +  REWRITE_TAC[IN_ELIM_THM] THEN REPEAT CONJ_TAC THENL
  8.1068 +   [X_GEN_TAC `n:num` THEN
  8.1069 +    SIMP_TAC[REAL_LE_LDIV_EQ; REAL_LT_RDIV_EQ; REAL_LT_POW2] THEN
  8.1070 +    SUBGOAL_THEN
  8.1071 +     `?N. !x:real^N i. x \<in> s \<and> 1 \<le> i \<and> i \<le> dimindex(:N)
  8.1072 +                       ==> abs(x$i * 2 pow n) < N`
  8.1073 +    STRIP_ASSUME_TAC THENL
  8.1074 +     [FIRST_ASSUM(MP_TAC o MATCH_MP COMPACT_IMP_BOUNDED) THEN
  8.1075 +      REWRITE_TAC[BOUNDED_POS; LEFT_IMP_EXISTS_THM] THEN
  8.1076 +      X_GEN_TAC `B:real` THEN STRIP_TAC THEN
  8.1077 +      MP_TAC(SPEC `B * 2 pow n` (MATCH_MP REAL_ARCH REAL_LT_01)) THEN
  8.1078 +      MATCH_MP_TAC MONO_EXISTS THEN REWRITE_TAC[REAL_MUL_RID] THEN
  8.1079 +      X_GEN_TAC `N:num` THEN
  8.1080 +      REWRITE_TAC[REAL_ABS_MUL; REAL_ABS_POW; REAL_ABS_NUM] THEN
  8.1081 +      SIMP_TAC[GSYM REAL_LT_RDIV_EQ; REAL_LT_POW2] THEN
  8.1082 +      ASM_MESON_TAC[COMPONENT_LE_NORM; REAL_LE_TRANS; REAL_LET_TRANS];
  8.1083 +      ALL_TAC] THEN
  8.1084 +    MATCH_MP_TAC FINITE_SUBSET THEN
  8.1085 +    EXISTS_TAC
  8.1086 +     `IMAGE (\<lambda>u. {x | !i. 1 \<le> i \<and> i \<le> dimindex(:N)
  8.1087 +                          ==> (u:real^N)$i \<le> (x:real^N)$i * 2 pow n \<and>
  8.1088 +                              x$i * 2 pow n < u$i + 1})
  8.1089 +            {u | !i. 1 \<le> i \<and> i \<le> dimindex(:N) ==> integer (u$i) \<and>
  8.1090 +                                                     abs(u$i) \<le> N}` THEN
  8.1091 +    CONJ_TAC THENL
  8.1092 +     [MATCH_MP_TAC FINITE_IMAGE THEN MATCH_MP_TAC FINITE_CART THEN
  8.1093 +      REWRITE_TAC[GSYM REAL_BOUNDS_LE; FINITE_INTSEG];
  8.1094 +      REWRITE_TAC[SUBSET; IN_ELIM_THM; IN_IMAGE] THEN
  8.1095 +      X_GEN_TAC `l:real^N->bool` THEN
  8.1096 +      MATCH_MP_TAC MONO_EXISTS THEN X_GEN_TAC `u:real^N` THEN
  8.1097 +      STRIP_TAC THEN FIRST_X_ASSUM SUBST_ALL_TAC THEN ASM_SIMP_TAC[] THEN
  8.1098 +      X_GEN_TAC `k:num` THEN STRIP_TAC THEN
  8.1099 +      MATCH_MP_TAC REAL_LE_REVERSE_INTEGERS THEN
  8.1100 +      ASM_SIMP_TAC[INTEGER_CLOSED] THEN
  8.1101 +      FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [GSYM MEMBER_NOT_EMPTY]) THEN
  8.1102 +      DISCH_THEN(X_CHOOSE_THEN `x:real^N` MP_TAC) THEN
  8.1103 +      REWRITE_TAC[IN_INTER; IN_ELIM_THM] THEN
  8.1104 +      DISCH_THEN(CONJUNCTS_THEN2 ASSUME_TAC (MP_TAC o SPEC `k:num`)) THEN
  8.1105 +      ASM_REWRITE_TAC[] THEN
  8.1106 +      FIRST_X_ASSUM(MP_TAC o SPECL [`x:real^N`; `k:num`]) THEN
  8.1107 +      ASM_REWRITE_TAC[] THEN REAL_ARITH_TAC];
  8.1108 +    X_GEN_TAC `n:num` THEN REWRITE_TAC[SUBSET; IN_UNIONS; IN_ELIM_THM] THEN
  8.1109 +    X_GEN_TAC `x:real^N` THEN DISCH_TAC THEN
  8.1110 +    REWRITE_TAC[LEFT_AND_EXISTS_THM] THEN
  8.1111 +    ONCE_REWRITE_TAC[SWAP_EXISTS_THM] THEN
  8.1112 +    EXISTS_TAC `(lambda i. floor(2 pow n * (x:real^N)$i)):real^N` THEN
  8.1113 +    ONCE_REWRITE_TAC[TAUT `(a \<and> b \<and> c) \<and> d \<longleftrightarrow> b \<and> a \<and> c \<and> d`] THEN
  8.1114 +    REWRITE_TAC[UNWIND_THM2] THEN SIMP_TAC[LAMBDA_BETA; FLOOR] THEN
  8.1115 +    REWRITE_TAC[GSYM MEMBER_NOT_EMPTY; IN_INTER] THEN
  8.1116 +    REWRITE_TAC[LEFT_AND_EXISTS_THM] THEN EXISTS_TAC `x:real^N` THEN
  8.1117 +    ASM_REWRITE_TAC[IN_ELIM_THM] THEN
  8.1118 +    SIMP_TAC[REAL_LE_LDIV_EQ; REAL_LT_RDIV_EQ; REAL_LT_POW2] THEN
  8.1119 +    REWRITE_TAC[REAL_MUL_SYM; FLOOR];
  8.1120 +    X_GEN_TAC `x:real^N` THEN DISCH_TAC THEN
  8.1121 +    FIRST_ASSUM(MP_TAC o MATCH_MP COMPACT_IMP_CLOSED) THEN
  8.1122 +    REWRITE_TAC[closed; open_def] THEN
  8.1123 +    DISCH_THEN(MP_TAC o SPEC `x:real^N`) THEN
  8.1124 +    ASM_REWRITE_TAC[IN_DIFF; IN_UNIV] THEN
  8.1125 +    DISCH_THEN(X_CHOOSE_THEN `e:real` STRIP_ASSUME_TAC) THEN
  8.1126 +    MP_TAC(SPECL [`inv(2)`; `e / (dimindex(:N))`] REAL_ARCH_POW_INV) THEN
  8.1127 +    ASM_SIMP_TAC[REAL_LT_DIV; REAL_LT_RDIV_EQ; REAL_OF_NUM_LT;
  8.1128 +                 DIMINDEX_GE_1; ARITH_RULE `0 < x \<longleftrightarrow> 1 \<le> x`] THEN
  8.1129 +    CONV_TAC REAL_RAT_REDUCE_CONV THEN
  8.1130 +    MATCH_MP_TAC MONO_EXISTS THEN X_GEN_TAC `n:num` THEN DISCH_TAC THEN
  8.1131 +    REWRITE_TAC[IN_UNIONS; IN_ELIM_THM] THEN
  8.1132 +    REWRITE_TAC[LEFT_AND_EXISTS_THM] THEN
  8.1133 +    ONCE_REWRITE_TAC[SWAP_EXISTS_THM] THEN
  8.1134 +    ONCE_REWRITE_TAC[TAUT `(a \<and> b \<and> c) \<and> d \<longleftrightarrow> b \<and> a \<and> c \<and> d`] THEN
  8.1135 +    REWRITE_TAC[UNWIND_THM2] THEN REWRITE_TAC[NOT_EXISTS_THM] THEN
  8.1136 +    X_GEN_TAC `u:real^N` THEN REWRITE_TAC[GSYM MEMBER_NOT_EMPTY] THEN
  8.1137 +    REWRITE_TAC[IN_INTER; IN_ELIM_THM] THEN
  8.1138 +    DISCH_THEN(CONJUNCTS_THEN2 MP_TAC ASSUME_TAC o CONJUNCT2) THEN
  8.1139 +    DISCH_THEN(X_CHOOSE_THEN `y:real^N`
  8.1140 +     (CONJUNCTS_THEN2 MP_TAC ASSUME_TAC)) THEN
  8.1141 +    REWRITE_TAC[] THEN FIRST_X_ASSUM MATCH_MP_TAC THEN
  8.1142 +    FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP (REAL_ARITH
  8.1143 +     `d < e ==> x \<le> d ==> x < e`)) THEN
  8.1144 +    REWRITE_TAC[dist] THEN
  8.1145 +    W(MP_TAC o PART_MATCH lhand NORM_LE_L1 o lhand o snd) THEN
  8.1146 +    MATCH_MP_TAC(REAL_ARITH `a \<le> b ==> x \<le> a ==> x \<le> b`) THEN
  8.1147 +    GEN_REWRITE_TAC (funpow 3 RAND_CONV) [GSYM CARD_NUMSEG_1] THEN
  8.1148 +    ONCE_REWRITE_TAC[REAL_MUL_SYM] THEN MATCH_MP_TAC SUM_BOUND THEN
  8.1149 +    SIMP_TAC[FINITE_NUMSEG; IN_NUMSEG; VECTOR_SUB_COMPONENT] THEN
  8.1150 +    X_GEN_TAC `k:num` THEN STRIP_TAC THEN
  8.1151 +    REPEAT(FIRST_X_ASSUM(MP_TAC o SPEC `k:num`)) THEN
  8.1152 +    ASM_REWRITE_TAC[real_div; REAL_ADD_RDISTRIB] THEN
  8.1153 +    REWRITE_TAC[REAL_MUL_LID; GSYM REAL_POW_INV] THEN REAL_ARITH_TAC;
  8.1154 +    MAP_EVERY X_GEN_TAC [`n:num`; `a:real^N->bool`] THEN
  8.1155 +    DISCH_THEN(X_CHOOSE_THEN `u:real^N`
  8.1156 +     (CONJUNCTS_THEN2 ASSUME_TAC MP_TAC)) THEN
  8.1157 +    DISCH_THEN(CONJUNCTS_THEN2 (ASSUME_TAC o SYM) ASSUME_TAC) THEN
  8.1158 +    REWRITE_TAC[LEFT_AND_EXISTS_THM] THEN
  8.1159 +    ONCE_REWRITE_TAC[SWAP_EXISTS_THM] THEN
  8.1160 +    ONCE_REWRITE_TAC[TAUT `(a \<and> b \<and> c) \<and> d \<longleftrightarrow> b \<and> a \<and> c \<and> d`] THEN
  8.1161 +    REWRITE_TAC[UNWIND_THM2] THEN
  8.1162 +    EXISTS_TAC `(lambda i. floor((u:real^N)$i / 2)):real^N` THEN
  8.1163 +    ASM_SIMP_TAC[VECTOR_MUL_COMPONENT; LAMBDA_BETA; FLOOR] THEN
  8.1164 +    MATCH_MP_TAC(SET_RULE `~(s \<inter> a = {}) \<and> a \<subseteq> b
  8.1165 +                           ==> ~(s \<inter> b = {}) \<and> a \<subseteq> b`) THEN
  8.1166 +    ASM_REWRITE_TAC[] THEN EXPAND_TAC "a" THEN REWRITE_TAC[SUBSET] THEN
  8.1167 +    X_GEN_TAC `x:real^N` THEN REWRITE_TAC[IN_ELIM_THM] THEN
  8.1168 +    MATCH_MP_TAC MONO_FORALL THEN X_GEN_TAC `k:num` THEN
  8.1169 +    DISCH_THEN(fun th -> STRIP_TAC THEN MP_TAC th) THEN ASM_REWRITE_TAC[] THEN
  8.1170 +    REWRITE_TAC[real_pow; real_div; REAL_INV_MUL; REAL_MUL_ASSOC] THEN
  8.1171 +    REWRITE_TAC[GSYM real_div] THEN
  8.1172 +    SIMP_TAC[REAL_LE_LDIV_EQ; REAL_LT_RDIV_EQ; REAL_LT_POW2] THEN
  8.1173 +    MP_TAC(SPEC `(u:real^N)$k / 2` FLOOR) THEN
  8.1174 +    REWRITE_TAC[REAL_ARITH `u / 2 < floor(u / 2) + 1 \<longleftrightarrow>
  8.1175 +                            u < 2 * floor(u / 2) + 2`] THEN
  8.1176 +    ASM_SIMP_TAC[REAL_LT_INTEGERS; INTEGER_CLOSED; FLOOR_FRAC] THEN
  8.1177 +    REAL_ARITH_TAC;
  8.1178 +    REWRITE_TAC[LEFT_IMP_EXISTS_THM] THEN
  8.1179 +    MAP_EVERY X_GEN_TAC [`n:num`; `a:real^N->bool`; `u:real^N`] THEN
  8.1180 +    DISCH_THEN(SUBST1_TAC o CONJUNCT1 o CONJUNCT2) THEN
  8.1181 +    ONCE_REWRITE_TAC[MEASURABLE_INNER_OUTER] THEN
  8.1182 +    GEN_TAC THEN DISCH_TAC THEN
  8.1183 +    EXISTS_TAC `interval(inv(2 pow n) % u:real^N,
  8.1184 +                         inv(2 pow n) % (u + 1))` THEN
  8.1185 +    EXISTS_TAC `interval[inv(2 pow n) % u:real^N,
  8.1186 +                         inv(2 pow n) % (u + 1)]` THEN
  8.1187 +    REWRITE_TAC[MEASURABLE_INTERVAL; MEASURE_INTERVAL] THEN
  8.1188 +    ASM_REWRITE_TAC[REAL_SUB_REFL; REAL_ABS_0] THEN
  8.1189 +    REWRITE_TAC[SUBSET; IN_INTERVAL; IN_ELIM_THM] THEN
  8.1190 +    CONJ_TAC THEN X_GEN_TAC `y:real^N` THEN
  8.1191 +    MATCH_MP_TAC MONO_FORALL THEN X_GEN_TAC `k:num` THEN
  8.1192 +    DISCH_THEN(fun th -> STRIP_TAC THEN MP_TAC th) THEN
  8.1193 +    ASM_SIMP_TAC[VECTOR_MUL_COMPONENT; VECTOR_ADD_COMPONENT;
  8.1194 +                 VEC_COMPONENT] THEN
  8.1195 +    REAL_ARITH_TAC]);; *)
  8.1196 +
  8.1197 +lemma gmeasurable_open: True .. (*
  8.1198 + "!s:real^N->bool. bounded s \<and> open s ==> gmeasurable s"
  8.1199 +qed   REPEAT STRIP_TAC THEN
  8.1200 +  FIRST_X_ASSUM(MP_TAC o MATCH_MP BOUNDED_SUBSET_CLOSED_INTERVAL) THEN
  8.1201 +  REWRITE_TAC[LEFT_IMP_EXISTS_THM] THEN
  8.1202 +  MAP_EVERY X_GEN_TAC [`a:real^N`; `b:real^N`] THEN DISCH_TAC THEN
  8.1203 +  FIRST_ASSUM(SUBST1_TAC o MATCH_MP (SET_RULE
  8.1204 +   `s \<subseteq> t ==> s = t DIFF (t DIFF s)`)) THEN
  8.1205 +  MATCH_MP_TAC GMEASURABLE_DIFF THEN
  8.1206 +  REWRITE_TAC[MEASURABLE_INTERVAL] THEN
  8.1207 +  MATCH_MP_TAC GMEASURABLE_COMPACT THEN
  8.1208 +  SIMP_TAC[COMPACT_EQ_BOUNDED_CLOSED; BOUNDED_DIFF; BOUNDED_INTERVAL] THEN
  8.1209 +  MATCH_MP_TAC CLOSED_DIFF THEN ASM_REWRITE_TAC[CLOSED_INTERVAL]);; *)
  8.1210 +
  8.1211 +lemma gmeasurable_closure: True .. (*
  8.1212 + "!s. bounded s ==> gmeasurable(closure s)"
  8.1213 +qed   SIMP_TAC[MEASURABLE_COMPACT; COMPACT_EQ_BOUNDED_CLOSED; CLOSED_CLOSURE;
  8.1214 +           BOUNDED_CLOSURE]);; *)
  8.1215 +
  8.1216 +lemma gmeasurable_interior: True .. (*
  8.1217 + "!s. bounded s ==> gmeasurable(interior s)"
  8.1218 +qed   SIMP_TAC[MEASURABLE_OPEN; OPEN_INTERIOR; BOUNDED_INTERIOR]);; *)
  8.1219 +
  8.1220 +lemma gmeasurable_frontier: True .. (*
  8.1221 + "!s:real^N->bool. bounded s ==> gmeasurable(frontier s)"
  8.1222 +qed   REPEAT STRIP_TAC THEN REWRITE_TAC[frontier] THEN
  8.1223 +  MATCH_MP_TAC GMEASURABLE_DIFF THEN
  8.1224 +  ASM_SIMP_TAC[MEASURABLE_CLOSURE; GMEASURABLE_INTERIOR] THEN
  8.1225 +  MATCH_MP_TAC SUBSET_TRANS THEN EXISTS_TAC `s:real^N->bool` THEN
  8.1226 +  REWRITE_TAC[INTERIOR_SUBSET; CLOSURE_SUBSET]);; *)
  8.1227 +
  8.1228 +lemma measure_frontier: True .. (*
  8.1229 + "!s:real^N->bool.
  8.1230 +        bounded s
  8.1231 +        ==> measure(frontier s) = measure(closure s) - measure(interior s)"
  8.1232 +qed   REPEAT STRIP_TAC THEN REWRITE_TAC[frontier] THEN
  8.1233 +  MATCH_MP_TAC MEASURE_DIFF_SUBSET THEN
  8.1234 +  ASM_SIMP_TAC[MEASURABLE_CLOSURE; GMEASURABLE_INTERIOR] THEN
  8.1235 +  MATCH_MP_TAC SUBSET_TRANS THEN EXISTS_TAC `s:real^N->bool` THEN
  8.1236 +  REWRITE_TAC[INTERIOR_SUBSET; CLOSURE_SUBSET]);; *)
  8.1237 +
  8.1238 +lemma gmeasurable_jordan: True .. (*
  8.1239 + "!s:real^N->bool. bounded s \<and> negligible(frontier s) ==> gmeasurable s"
  8.1240 +qed   REPEAT STRIP_TAC THEN ONCE_REWRITE_TAC[MEASURABLE_INNER_OUTER] THEN
  8.1241 +  GEN_TAC THEN DISCH_TAC THEN
  8.1242 +  EXISTS_TAC `interior(s):real^N->bool` THEN
  8.1243 +  EXISTS_TAC `closure(s):real^N->bool` THEN
  8.1244 +  ASM_SIMP_TAC[MEASURABLE_INTERIOR; GMEASURABLE_CLOSURE] THEN
  8.1245 +  REWRITE_TAC[INTERIOR_SUBSET; CLOSURE_SUBSET] THEN
  8.1246 +  ONCE_REWRITE_TAC[REAL_ABS_SUB] THEN
  8.1247 +  ASM_SIMP_TAC[GSYM MEASURE_FRONTIER; REAL_ABS_NUM; MEASURE_EQ_0]);; *)
  8.1248 +
  8.1249 +lemma has_gmeasure_elementary: True .. (*
  8.1250 + "!d s. d division_of s ==> s has_gmeasure (sum d content)"
  8.1251 +qed   REPEAT STRIP_TAC THEN REWRITE_TAC[has_gmeasure] THEN
  8.1252 +  FIRST_ASSUM(ASSUME_TAC o MATCH_MP DIVISION_OF_FINITE) THEN
  8.1253 +  ASM_SIMP_TAC[LIFT_SUM] THEN
  8.1254 +  MATCH_MP_TAC HAS_INTEGRAL_COMBINE_DIVISION THEN
  8.1255 +  ASM_REWRITE_TAC[o_THM] THEN REWRITE_TAC[GSYM has_gmeasure] THEN
  8.1256 +  ASM_MESON_TAC[HAS_GMEASURE_INTERVAL; division_of]);; *)
  8.1257 +
  8.1258 +lemma gmeasurable_elementary: True .. (*
  8.1259 + "!d s. d division_of s ==> gmeasurable s"
  8.1260 +qed   REWRITE_TAC[measurable] THEN MESON_TAC[HAS_GMEASURE_ELEMENTARY]);; *)
  8.1261 +
  8.1262 +lemma measure_elementary: True .. (*
  8.1263 + "!d s. d division_of s ==> gmeasure s = sum d content"
  8.1264 +qed   MESON_TAC[HAS_GMEASURE_ELEMENTARY; MEASURE_UNIQUE]);; *)
  8.1265 +
  8.1266 +lemma gmeasurable_inter_interval: True .. (*
  8.1267 + "!s a b:real^N. gmeasurable s ==> gmeasurable (s \<inter> {a..b})"
  8.1268 +qed   SIMP_TAC[MEASURABLE_INTER; GMEASURABLE_INTERVAL]);; *)
  8.1269 +
  8.1270 +(* ------------------------------------------------------------------------- *)
  8.1271 +(* A nice lemma for negligibility proofs.                                    *)
  8.1272 +(* ------------------------------------------------------------------------- *)
  8.1273 +
  8.1274 +lemma STARLIKE_NEGLIGIBLE_BOUNDED_MEASURABLE: True .. (*
  8.1275 + "!s. gmeasurable s \<and> bounded s \<and>
  8.1276 +       (!c x:real^N. 0 \<le> c \<and> x \<in> s \<and> (c % x) \<in> s ==> c = 1)
  8.1277 +       ==> negligible s"
  8.1278 +qed   REPEAT STRIP_TAC THEN
  8.1279 +  SUBGOAL_THEN `~(0 < measure(s:real^N->bool))`
  8.1280 +   (fun th -> ASM_MESON_TAC[th; GMEASURABLE_MEASURE_POS_LT]) THEN
  8.1281 +  DISCH_TAC THEN
  8.1282 +  MP_TAC(SPEC `(0:real^N) INSERT s`
  8.1283 +      BOUNDED_SUBSET_CLOSED_INTERVAL_SYMMETRIC) THEN
  8.1284 +  ASM_SIMP_TAC[BOUNDED_INSERT; COMPACT_IMP_BOUNDED; NOT_EXISTS_THM] THEN
  8.1285 +  X_GEN_TAC `a:real^N` THEN REWRITE_TAC[INSERT_SUBSET] THEN STRIP_TAC THEN
  8.1286 +  SUBGOAL_THEN
  8.1287 +   `?N. EVEN N \<and> 0 < N \<and>
  8.1288 +        measure(interval[--a:real^N,a])
  8.1289 +         < (N * measure(s:real^N->bool)) / 4 pow dimindex (:N)`
  8.1290 +  STRIP_ASSUME_TAC THENL
  8.1291 +   [FIRST_ASSUM(MP_TAC o SPEC
  8.1292 +     `measure(interval[--a:real^N,a]) * 4 pow (dimindex(:N))` o
  8.1293 +     MATCH_MP REAL_ARCH) THEN
  8.1294 +    SIMP_TAC[REAL_LT_RDIV_EQ; REAL_POW_LT; REAL_OF_NUM_LT; ARITH] THEN
  8.1295 +    SIMP_TAC[GSYM REAL_LT_LDIV_EQ; ASSUME `0 < measure(s:real^N->bool)`] THEN
  8.1296 +    DISCH_THEN(X_CHOOSE_THEN `N:num` STRIP_ASSUME_TAC) THEN
  8.1297 +    EXISTS_TAC `2 * (N DIV 2 + 1)` THEN REWRITE_TAC[EVEN_MULT; ARITH] THEN
  8.1298 +    CONJ_TAC THENL [ARITH_TAC; ALL_TAC] THEN
  8.1299 +    FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP (REAL_ARITH
  8.1300 +     `x < a ==> a \<le> b ==> x < b`)) THEN
  8.1301 +    REWRITE_TAC[REAL_OF_NUM_LE] THEN ARITH_TAC;
  8.1302 +    ALL_TAC] THEN
  8.1303 +  MP_TAC(ISPECL [`\<Union>(IMAGE (\<lambda>m. IMAGE (\<lambda>x:real^N. (m / N) % x) s)
  8.1304 +                                (1..N))`;
  8.1305 +                  `interval[--a:real^N,a]`] MEASURE_SUBSET) THEN
  8.1306 +  MP_TAC(ISPECL [`measure:(real^N->bool)->real`;
  8.1307 +                 `IMAGE (\<lambda>m. IMAGE (\<lambda>x:real^N. (m / N) % x) s) (1..N)`]
  8.1308 +                HAS_GMEASURE_DISJOINT_UNIONS) THEN
  8.1309 +  SIMP_TAC[FINITE_IMAGE; FINITE_NUMSEG; IMP_CONJ] THEN
  8.1310 +  REWRITE_TAC[RIGHT_FORALL_IMP_THM; FORALL_IN_IMAGE] THEN ANTS_TAC THENL
  8.1311 +   [REPEAT STRIP_TAC THEN REWRITE_TAC[GSYM HAS_GMEASURE_MEASURE] THEN
  8.1312 +    MATCH_MP_TAC GMEASURABLE_SCALING THEN ASM_REWRITE_TAC[];
  8.1313 +    ALL_TAC] THEN
  8.1314 +  REWRITE_TAC[RIGHT_IMP_FORALL_THM; IMP_IMP] THEN
  8.1315 +  ONCE_REWRITE_TAC[TAUT `(a \<and> b) \<and> ~c ==> d \<longleftrightarrow> a \<and> b \<and> ~d ==> c`] THEN
  8.1316 +  SUBGOAL_THEN
  8.1317 +   `!m n. m \<in> 1..N \<and> n \<in> 1..N \<and>
  8.1318 +          ~(DISJOINT (IMAGE (\<lambda>x:real^N. m / N % x) s)
  8.1319 +                     (IMAGE (\<lambda>x. n / N % x) s))
  8.1320 +          ==> m = n`
  8.1321 +  ASSUME_TAC THENL
  8.1322 +   [MAP_EVERY X_GEN_TAC [`m:num`; `n:num`] THEN
  8.1323 +    REPEAT(DISCH_THEN(CONJUNCTS_THEN2 ASSUME_TAC MP_TAC)) THEN
  8.1324 +    REWRITE_TAC[DISJOINT; GSYM MEMBER_NOT_EMPTY] THEN
  8.1325 +    REWRITE_TAC[EXISTS_IN_IMAGE; IN_INTER] THEN
  8.1326 +    DISCH_THEN(X_CHOOSE_THEN `x:real^N`
  8.1327 +     (CONJUNCTS_THEN2 ASSUME_TAC MP_TAC)) THEN
  8.1328 +    REWRITE_TAC[IN_IMAGE] THEN
  8.1329 +    DISCH_THEN(X_CHOOSE_THEN `y:real^N`
  8.1330 +     (CONJUNCTS_THEN2 MP_TAC ASSUME_TAC)) THEN
  8.1331 +    DISCH_THEN(MP_TAC o AP_TERM `(%) (N / m) :real^N->real^N`) THEN
  8.1332 +    SUBGOAL_THEN `~(N = 0) \<and> ~(m = 0)` STRIP_ASSUME_TAC THENL
  8.1333 +     [REWRITE_TAC[REAL_OF_NUM_EQ] THEN
  8.1334 +      REPEAT(FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [IN_NUMSEG])) THEN
  8.1335 +      ARITH_TAC;
  8.1336 +      ALL_TAC] THEN
  8.1337 +    FIRST_X_ASSUM(ASSUME_TAC o GEN_REWRITE_RULE (BINDER_CONV o BINDER_CONV)
  8.1338 +     [GSYM CONTRAPOS_THM]) THEN
  8.1339 +    ASM_SIMP_TAC[VECTOR_MUL_ASSOC; REAL_FIELD
  8.1340 +     `~(x = 0) \<and> ~(y = 0) ==> x / y * y / x = 1`] THEN
  8.1341 +    ASM_SIMP_TAC[REAL_FIELD
  8.1342 +     `~(x = 0) \<and> ~(y = 0) ==> x / y * z / x = z / y`] THEN
  8.1343 +    REWRITE_TAC[VECTOR_MUL_LID] THEN DISCH_THEN SUBST_ALL_TAC THEN
  8.1344 +    FIRST_X_ASSUM(MP_TAC o SPECL [`n / m`; `y:real^N`]) THEN
  8.1345 +    ASM_SIMP_TAC[REAL_LE_DIV; REAL_POS; REAL_FIELD
  8.1346 +     `~(y = 0) ==> (x / y = 1 \<longleftrightarrow> x = y)`] THEN
  8.1347 +    REWRITE_TAC[REAL_OF_NUM_EQ; EQ_SYM_EQ];
  8.1348 +    ALL_TAC] THEN
  8.1349 +  ANTS_TAC THENL [ASM_MESON_TAC[]; DISCH_TAC] THEN
  8.1350 +  REWRITE_TAC[NOT_IMP] THEN REPEAT CONJ_TAC THENL
  8.1351 +   [REWRITE_TAC[measurable] THEN ASM_MESON_TAC[];
  8.1352 +    REWRITE_TAC[MEASURABLE_INTERVAL];
  8.1353 +    REWRITE_TAC[UNIONS_SUBSET; FORALL_IN_IMAGE] THEN
  8.1354 +    REWRITE_TAC[SUBSET; FORALL_IN_IMAGE] THEN
  8.1355 +    X_GEN_TAC `n:num` THEN DISCH_TAC THEN X_GEN_TAC `x:real^N` THEN
  8.1356 +    DISCH_TAC THEN
  8.1357 +    MP_TAC(ISPECL [`--a:real^N`; `a:real^N`] CONVEX_INTERVAL) THEN
  8.1358 +    DISCH_THEN(MP_TAC o REWRITE_RULE[CONVEX_ALT] o CONJUNCT1) THEN
  8.1359 +    DISCH_THEN(MP_TAC o SPECL [`0:real^N`; `x:real^N`; `n / N`]) THEN
  8.1360 +    ASM_REWRITE_TAC[VECTOR_MUL_RZERO; VECTOR_ADD_LID] THEN
  8.1361 +    DISCH_THEN MATCH_MP_TAC THEN SIMP_TAC[REAL_LE_DIV; REAL_POS] THEN
  8.1362 +    CONJ_TAC THENL [ASM SET_TAC[]; ALL_TAC] THEN
  8.1363 +    FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [IN_NUMSEG]) THEN
  8.1364 +    DISCH_THEN(MP_TAC o MATCH_MP (ARITH_RULE
  8.1365 +     `1 \<le> n \<and> n \<le> N ==> 0 < N \<and> n \<le> N`)) THEN
  8.1366 +    SIMP_TAC[GSYM REAL_OF_NUM_LE; GSYM REAL_OF_NUM_LT; REAL_LE_LDIV_EQ] THEN
  8.1367 +    SIMP_TAC[REAL_MUL_LID];
  8.1368 +    ALL_TAC] THEN
  8.1369 +  FIRST_X_ASSUM(SUBST1_TAC o MATCH_MP MEASURE_UNIQUE) THEN
  8.1370 +  ASM_SIMP_TAC[MEASURE_SCALING; REAL_NOT_LE] THEN
  8.1371 +  FIRST_X_ASSUM(K ALL_TAC o SPEC `0`) THEN
  8.1372 +  MATCH_MP_TAC REAL_LTE_TRANS THEN EXISTS_TAC
  8.1373 +   `sum (1..N) (measure o (\<lambda>m. IMAGE (\<lambda>x:real^N. m / N % x) s))` THEN
  8.1374 +  CONJ_TAC THENL
  8.1375 +   [ALL_TAC;
  8.1376 +    MATCH_MP_TAC REAL_EQ_IMP_LE THEN CONV_TAC SYM_CONV THEN
  8.1377 +    MATCH_MP_TAC SUM_IMAGE THEN REWRITE_TAC[] THEN
  8.1378 +    REPEAT STRIP_TAC THEN FIRST_X_ASSUM MATCH_MP_TAC THEN
  8.1379 +    ASM_REWRITE_TAC[SET_RULE `DISJOINT s s \<longleftrightarrow> s = {}`; IMAGE_EQ_EMPTY] THEN
  8.1380 +    DISCH_THEN SUBST_ALL_TAC THEN
  8.1381 +    ASM_MESON_TAC[REAL_LT_REFL; MEASURE_EMPTY]] THEN
  8.1382 +  FIRST_X_ASSUM(K ALL_TAC o SPEC `0`) THEN
  8.1383 +  ASM_SIMP_TAC[o_DEF; MEASURE_SCALING; SUM_RMUL] THEN
  8.1384 +  FIRST_ASSUM(MATCH_MP_TAC o MATCH_MP (REAL_ARITH
  8.1385 +   `x < a ==> a \<le> b ==> x < b`)) THEN
  8.1386 +  ASM_SIMP_TAC[REAL_LE_LDIV_EQ; REAL_POW_LT; REAL_OF_NUM_LT; ARITH] THEN
  8.1387 +  ONCE_REWRITE_TAC[REAL_ARITH `(a * b) * c:real = (a * c) * b`] THEN
  8.1388 +  ASM_SIMP_TAC[REAL_LE_RMUL_EQ] THEN REWRITE_TAC[GSYM SUM_RMUL] THEN
  8.1389 +  REWRITE_TAC[GSYM REAL_POW_MUL] THEN
  8.1390 +  REWRITE_TAC[REAL_ABS_DIV; REAL_ABS_NUM] THEN
  8.1391 +  FIRST_X_ASSUM(X_CHOOSE_THEN `M:num` SUBST_ALL_TAC o
  8.1392 +        GEN_REWRITE_RULE I [EVEN_EXISTS]) THEN
  8.1393 +  REWRITE_TAC[GSYM REAL_OF_NUM_MUL] THEN
  8.1394 +  RULE_ASSUM_TAC(REWRITE_RULE[GSYM REAL_OF_NUM_MUL]) THEN
  8.1395 +  RULE_ASSUM_TAC(REWRITE_RULE[REAL_ARITH `0 < 2 * x \<longleftrightarrow> 0 < x`]) THEN
  8.1396 +  ASM_SIMP_TAC[REAL_FIELD `0 < y ==> x / (2 * y) * 4 = x * 2 / y`] THEN
  8.1397 +  MATCH_MP_TAC REAL_LE_TRANS THEN
  8.1398 +  EXISTS_TAC `sum(M..(2*M)) (\<lambda>i. (i * 2 / M) pow dimindex (:N))` THEN
  8.1399 +  CONJ_TAC THENL
  8.1400 +   [ALL_TAC;
  8.1401 +    MATCH_MP_TAC SUM_SUBSET_SIMPLE THEN
  8.1402 +    SIMP_TAC[REAL_POW_LE; REAL_LE_MUL; REAL_LE_DIV; REAL_POS] THEN
  8.1403 +    REWRITE_TAC[IN_NUMSEG; FINITE_NUMSEG; SUBSET] THEN
  8.1404 +    FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [REAL_OF_NUM_LT]) THEN
  8.1405 +    ARITH_TAC] THEN
  8.1406 +  MATCH_MP_TAC REAL_LE_TRANS THEN
  8.1407 +  EXISTS_TAC `sum(M..(2*M)) (\<lambda>i. 2)` THEN CONJ_TAC THENL
  8.1408 +   [REWRITE_TAC[SUM_CONST_NUMSEG] THEN
  8.1409 +    REWRITE_TAC[ARITH_RULE `(2 * M + 1) - M = M + 1`] THEN
  8.1410 +    REWRITE_TAC[GSYM REAL_OF_NUM_ADD] THEN REAL_ARITH_TAC;
  8.1411 +    ALL_TAC] THEN
  8.1412 +  MATCH_MP_TAC SUM_LE THEN REWRITE_TAC[FINITE_NUMSEG; IN_NUMSEG] THEN
  8.1413 +  X_GEN_TAC `n:num` THEN STRIP_TAC THEN
  8.1414 +  MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `2 pow (dimindex(:N))` THEN
  8.1415 +  CONJ_TAC THENL
  8.1416 +   [GEN_REWRITE_TAC LAND_CONV [GSYM REAL_POW_1] THEN
  8.1417 +    MATCH_MP_TAC REAL_POW_MONO THEN REWRITE_TAC[DIMINDEX_GE_1] THEN
  8.1418 +    ARITH_TAC;
  8.1419 +    ALL_TAC] THEN
  8.1420 +  MATCH_MP_TAC REAL_POW_LE2 THEN
  8.1421 +  REWRITE_TAC[REAL_POS; ARITH; real_div; REAL_MUL_ASSOC] THEN
  8.1422 +  ASM_SIMP_TAC[GSYM real_div; REAL_LE_RDIV_EQ] THEN
  8.1423 +  REWRITE_TAC[REAL_OF_NUM_MUL; REAL_OF_NUM_LE] THEN
  8.1424 +  UNDISCH_TAC `M:num \<le> n` THEN ARITH_TAC);; *)
  8.1425 +
  8.1426 +lemma STARLIKE_NEGLIGIBLE_LEMMA: True .. (*
  8.1427 + "!s. compact s \<and>
  8.1428 +       (!c x:real^N. 0 \<le> c \<and> x \<in> s \<and> (c % x) \<in> s ==> c = 1)
  8.1429 +       ==> negligible s"
  8.1430 +qed   REPEAT STRIP_TAC THEN
  8.1431 +  MATCH_MP_TAC STARLIKE_NEGLIGIBLE_BOUNDED_MEASURABLE THEN
  8.1432 +  ASM_MESON_TAC[MEASURABLE_COMPACT; COMPACT_IMP_BOUNDED]);; *)
  8.1433 +
  8.1434 +lemma STARLIKE_NEGLIGIBLE: True .. (*
  8.1435 + "!s a. closed s \<and>
  8.1436 +         (!c x:real^N. 0 \<le> c \<and> (a + x) \<in> s \<and> (a + c % x) \<in> s ==> c = 1)
  8.1437 +         ==> negligible s"
  8.1438 +qed   REPEAT STRIP_TAC THEN MATCH_MP_TAC NEGLIGIBLE_TRANSLATION_REV THEN
  8.1439 +  EXISTS_TAC `--a:real^N` THEN ONCE_REWRITE_TAC[NEGLIGIBLE_ON_INTERVALS] THEN
  8.1440 +  MAP_EVERY X_GEN_TAC [`u:real^N`; `v:real^N`] THEN
  8.1441 +  MATCH_MP_TAC STARLIKE_NEGLIGIBLE_LEMMA THEN CONJ_TAC THENL
  8.1442 +   [MATCH_MP_TAC CLOSED_INTER_COMPACT THEN REWRITE_TAC[COMPACT_INTERVAL] THEN
  8.1443 +    ASM_SIMP_TAC[CLOSED_TRANSLATION];
  8.1444 +    REWRITE_TAC[IN_IMAGE; IN_INTER] THEN
  8.1445 +    ONCE_REWRITE_TAC[VECTOR_ARITH `x:real^N = --a + y \<longleftrightarrow> y = a + x`] THEN
  8.1446 +    REWRITE_TAC[UNWIND_THM2] THEN ASM MESON_TAC[]]);; *)
  8.1447 +
  8.1448 +lemma STARLIKE_NEGLIGIBLE_STRONG: True .. (*
  8.1449 + "!s a. closed s \<and>
  8.1450 +         (!c x:real^N. 0 \<le> c \<and> c < 1 \<and> (a + x) \<in> s
  8.1451 +                       ==> ~((a + c % x) \<in> s))
  8.1452 +         ==> negligible s"
  8.1453 +qed   REPEAT GEN_TAC THEN STRIP_TAC THEN MATCH_MP_TAC STARLIKE_NEGLIGIBLE THEN
  8.1454 +  EXISTS_TAC `a:real^N` THEN ASM_REWRITE_TAC[] THEN
  8.1455 +  MAP_EVERY X_GEN_TAC [`c:real`; `x:real^N`] THEN STRIP_TAC THEN
  8.1456 +  MATCH_MP_TAC(REAL_ARITH `~(x < y) \<and> ~(y < x) ==> x = y`) THEN
  8.1457 +  STRIP_TAC THENL [ASM_MESON_TAC[]; ALL_TAC] THEN DISCH_TAC THEN
  8.1458 +  FIRST_X_ASSUM(MP_TAC o SPECL [`inv c`; `c % x:real^N`]) THEN
  8.1459 +  ASM_REWRITE_TAC[REAL_LE_INV_EQ; VECTOR_MUL_ASSOC] THEN
  8.1460 +  ASM_SIMP_TAC[REAL_MUL_LINV; REAL_ARITH `1 < c ==> ~(c = 0)`] THEN
  8.1461 +  ASM_REWRITE_TAC[VECTOR_MUL_LID] THEN
  8.1462 +  GEN_REWRITE_TAC RAND_CONV [GSYM REAL_INV_1] THEN
  8.1463 +  MATCH_MP_TAC REAL_LT_INV2 THEN ASM_REWRITE_TAC[REAL_LT_01]);; *)
  8.1464 +
  8.1465 +(* ------------------------------------------------------------------------- *)
  8.1466 +(* In particular.                                                            *)
  8.1467 +(* ------------------------------------------------------------------------- *)
  8.1468 +
  8.1469 +lemma NEGLIGIBLE_HYPERPLANE: True .. (*
  8.1470 + "!a b. ~(a = 0 \<and> b = 0) ==> negligible {x:real^N | a dot x = b}"
  8.1471 +qed   REPEAT GEN_TAC THEN ASM_CASES_TAC `a:real^N = 0` THEN
  8.1472 +  ASM_SIMP_TAC[DOT_LZERO; SET_RULE `{x | F} = {}`; NEGLIGIBLE_EMPTY] THEN
  8.1473 +  MATCH_MP_TAC STARLIKE_NEGLIGIBLE THEN
  8.1474 +  SUBGOAL_THEN `?x:real^N. ~(a dot x = b)` MP_TAC THENL
  8.1475 +   [MATCH_MP_TAC(MESON[] `!a:real^N. P a \/ P(--a) ==> ?x. P x`) THEN
  8.1476 +    EXISTS_TAC `a:real^N` THEN REWRITE_TAC[DOT_RNEG] THEN
  8.1477 +    MATCH_MP_TAC(REAL_ARITH `~(a = 0) ==> ~(a = b) \/ ~(--a = b)`) THEN
  8.1478 +    ASM_REWRITE_TAC[DOT_EQ_0];
  8.1479 +    ALL_TAC] THEN
  8.1480 +  MATCH_MP_TAC MONO_EXISTS THEN X_GEN_TAC `c:real^N` THEN DISCH_TAC THEN
  8.1481 +  REWRITE_TAC[CLOSED_HYPERPLANE; IN_ELIM_THM; DOT_RADD; DOT_RMUL] THEN
  8.1482 +  MAP_EVERY X_GEN_TAC [`t:real`; `y:real^N`] THEN
  8.1483 +  DISCH_THEN(MP_TAC o MATCH_MP (REAL_ARITH
  8.1484 +   `0 \<le> t \<and> ac + ay = b \<and> ac + t * ay = b
  8.1485 +    ==> ((ay = 0 ==> ac = b) \<and> (t - 1) * ay = 0)`)) THEN
  8.1486 +  ASM_SIMP_TAC[REAL_ENTIRE; REAL_SUB_0] THEN CONV_TAC TAUT);; *)
  8.1487 +
  8.1488 +lemma NEGLIGIBLE_LOWDIM: True .. (*
  8.1489 + "!s:real^N->bool. dim(s) < dimindex(:N) ==> negligible s"
  8.1490 +qed   GEN_TAC THEN DISCH_THEN(MP_TAC o MATCH_MP LOWDIM_SUBSET_HYPERPLANE) THEN
  8.1491 +  DISCH_THEN(X_CHOOSE_THEN `a:real^N` STRIP_ASSUME_TAC) THEN
  8.1492 +  MATCH_MP_TAC NEGLIGIBLE_SUBSET THEN
  8.1493 +  EXISTS_TAC `span(s):real^N->bool` THEN REWRITE_TAC[SPAN_INC] THEN
  8.1494 +  MATCH_MP_TAC NEGLIGIBLE_SUBSET THEN
  8.1495 +  EXISTS_TAC `{x:real^N | a dot x = 0}` THEN
  8.1496 +  ASM_SIMP_TAC[NEGLIGIBLE_HYPERPLANE]);; *)
  8.1497 +
  8.1498 +(* ------------------------------------------------------------------------- *)
  8.1499 +(* Measurability of bounded convex sets.                                     *)
  8.1500 +(* ------------------------------------------------------------------------- *)
  8.1501 +
  8.1502 +lemma NEGLIGIBLE_CONVEX_FRONTIER: True .. (*
  8.1503 + "!s:real^N->bool. convex s ==> negligible(frontier s)"
  8.1504 +qed   SUBGOAL_THEN
  8.1505 +   `!s:real^N->bool. convex s \<and> (0) \<in> s ==> negligible(frontier s)`
  8.1506 +  ASSUME_TAC THENL
  8.1507 +   [ALL_TAC;
  8.1508 +    X_GEN_TAC `s:real^N->bool` THEN DISCH_TAC THEN
  8.1509 +    ASM_CASES_TAC `s:real^N->bool = {}` THEN
  8.1510 +    ASM_REWRITE_TAC[FRONTIER_EMPTY; NEGLIGIBLE_EMPTY] THEN
  8.1511 +    FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [GSYM MEMBER_NOT_EMPTY]) THEN
  8.1512 +    DISCH_THEN(X_CHOOSE_TAC `a:real^N`) THEN
  8.1513 +    FIRST_X_ASSUM(MP_TAC o SPEC `IMAGE (\<lambda>x:real^N. --a + x) s`) THEN
  8.1514 +    ASM_SIMP_TAC[CONVEX_TRANSLATION; IN_IMAGE] THEN
  8.1515 +    ASM_REWRITE_TAC[UNWIND_THM2;
  8.1516 +                    VECTOR_ARITH `0:real^N = --a + x \<longleftrightarrow> x = a`] THEN
  8.1517 +    REWRITE_TAC[FRONTIER_TRANSLATION; NEGLIGIBLE_TRANSLATION_EQ]] THEN
  8.1518 +  REPEAT STRIP_TAC THEN MP_TAC(ISPEC `s:real^N->bool` DIM_SUBSET_UNIV) THEN
  8.1519 +  REWRITE_TAC[ARITH_RULE `d:num \<le> e \<longleftrightarrow> d < e \/ d = e`] THEN STRIP_TAC THENL
  8.1520 +   [MATCH_MP_TAC NEGLIGIBLE_SUBSET THEN
  8.1521 +    EXISTS_TAC `closure s:real^N->bool` THEN
  8.1522 +    REWRITE_TAC[frontier; SUBSET_DIFF] THEN
  8.1523 +    MATCH_MP_TAC NEGLIGIBLE_LOWDIM THEN ASM_REWRITE_TAC[DIM_CLOSURE];
  8.1524 +    ALL_TAC] THEN
  8.1525 +  SUBGOAL_THEN `?a:real^N. a \<in> interior s` CHOOSE_TAC THENL
  8.1526 +   [X_CHOOSE_THEN `b:real^N->bool` STRIP_ASSUME_TAC
  8.1527 +     (ISPEC `s:real^N->bool` BASIS_EXISTS) THEN
  8.1528 +    FIRST_X_ASSUM SUBST_ALL_TAC THEN
  8.1529 +    MP_TAC(ISPEC `b:real^N->bool` INTERIOR_SIMPLEX_NONEMPTY) THEN
  8.1530 +    ASM_REWRITE_TAC[] THEN
  8.1531 +    MATCH_MP_TAC MONO_EXISTS THEN REWRITE_TAC[GSYM SUBSET] THEN
  8.1532 +    MATCH_MP_TAC SUBSET_INTERIOR THEN MATCH_MP_TAC HULL_MINIMAL THEN
  8.1533 +    ASM_REWRITE_TAC[INSERT_SUBSET];
  8.1534 +    ALL_TAC] THEN
  8.1535 +  MATCH_MP_TAC STARLIKE_NEGLIGIBLE_STRONG THEN
  8.1536 +  EXISTS_TAC `a:real^N` THEN REWRITE_TAC[FRONTIER_CLOSED] THEN
  8.1537 +  REPEAT GEN_TAC THEN STRIP_TAC THEN
  8.1538 +  REWRITE_TAC[frontier; IN_DIFF; DE_MORGAN_THM] THEN DISJ2_TAC THEN
  8.1539 +  SIMP_TAC[VECTOR_ARITH
  8.1540 +   `a + c % x:real^N = (a + x) - (1 - c) % ((a + x) - a)`] THEN
  8.1541 +  MATCH_MP_TAC IN_INTERIOR_CLOSURE_CONVEX_SHRINK THEN
  8.1542 +  RULE_ASSUM_TAC(REWRITE_RULE[frontier; IN_DIFF]) THEN
  8.1543 +  ASM_REWRITE_TAC[] THEN ASM_REAL_ARITH_TAC);; *)
  8.1544 +
  8.1545 +lemma GMEASURABLE_CONVEX: True .. (*
  8.1546 + "!s:real^N->bool. convex s \<and> bounded s ==> gmeasurable s"
  8.1547 +qed   REPEAT STRIP_TAC THEN MATCH_MP_TAC GMEASURABLE_JORDAN THEN
  8.1548 +  ASM_SIMP_TAC[NEGLIGIBLE_CONVEX_FRONTIER]);; *)
  8.1549 +
  8.1550 +(* ------------------------------------------------------------------------- *)
  8.1551 +(* Various special cases.                                                    *)
  8.1552 +(* ------------------------------------------------------------------------- *)
  8.1553 +
  8.1554 +lemma NEGLIGIBLE_SPHERE: True .. (*
  8.1555 + "!a r. negligible {x:real^N | dist(a,x) = r}"
  8.1556 +qed   REWRITE_TAC[GSYM FRONTIER_CBALL] THEN
  8.1557 +  SIMP_TAC[NEGLIGIBLE_CONVEX_FRONTIER; CONVEX_CBALL]);; *)
  8.1558 +
  8.1559 +lemma GMEASURABLE_BALL: True .. (*
  8.1560 + "!a r. gmeasurable(ball(a,r))"
  8.1561 +qed   SIMP_TAC[MEASURABLE_OPEN; BOUNDED_BALL; OPEN_BALL]);; *)
  8.1562 +
  8.1563 +lemma GMEASURABLE_CBALL: True .. (*
  8.1564 + "!a r. gmeasurable(cball(a,r))"
  8.1565 +qed   SIMP_TAC[MEASURABLE_COMPACT; COMPACT_CBALL]);; *)
  8.1566 +
  8.1567 +(* ------------------------------------------------------------------------- *)
  8.1568 +(* Negligibility of image under non-injective linear map.                    *)
  8.1569 +(* ------------------------------------------------------------------------- *)
  8.1570 +
  8.1571 +lemma NEGLIGIBLE_LINEAR_SINGULAR_IMAGE: True .. (*
  8.1572 + "!f:real^N->real^N s.
  8.1573 +        linear f \<and> ~(!x y. f(x) = f(y) ==> x = y)
  8.1574 +        ==> negligible(IMAGE f s)"
  8.1575 +qed   REPEAT GEN_TAC THEN
  8.1576 +  DISCH_THEN(MP_TAC o MATCH_MP LINEAR_SINGULAR_IMAGE_HYPERPLANE) THEN
  8.1577 +  DISCH_THEN(X_CHOOSE_THEN `a:real^N` STRIP_ASSUME_TAC) THEN
  8.1578 +  MATCH_MP_TAC NEGLIGIBLE_SUBSET THEN
  8.1579 +  EXISTS_TAC `{x:real^N | a dot x = 0}` THEN
  8.1580 +  ASM_SIMP_TAC[NEGLIGIBLE_HYPERPLANE]);; *)
  8.1581 +
  8.1582 +(* ------------------------------------------------------------------------- *)
  8.1583 +(* Approximation of gmeasurable set by union of intervals.                    *)
  8.1584 +(* ------------------------------------------------------------------------- *)
  8.1585 +
  8.1586 +lemma COVERING_LEMMA: True .. (*
  8.1587 + "!a b:real^N s g.
  8.1588 +        s \<subseteq> {a..b} \<and> ~({a<..<b} = {}) \<and> gauge g
  8.1589 +        ==> ?d. COUNTABLE d \<and>
  8.1590 +                (!k. k \<in> d ==> k \<subseteq> {a..b} \<and> ~(k = {}) \<and>
  8.1591 +                                (\<exists>c d. k = {c..d})) \<and>
  8.1592 +                (!k1 k2. k1 \<in> d \<and> k2 \<in> d \<and> ~(k1 = k2)
  8.1593 +                         ==> interior k1 \<inter> interior k2 = {}) \<and>
  8.1594 +                (!k. k \<in> d ==> ?x. x \<in> (s \<inter> k) \<and> k \<subseteq> g(x)) \<and>
  8.1595 +                s \<subseteq> \<Union>d"
  8.1596 +qed   REPEAT STRIP_TAC THEN
  8.1597 +  SUBGOAL_THEN
  8.1598 +   `?d. COUNTABLE d \<and>
  8.1599 +        (!k. k \<in> d ==> k \<subseteq> {a..b} \<and> ~(k = {}) \<and>
  8.1600 +                        (\<exists>c d:real^N. k = {c..d})) \<and>
  8.1601 +        (!k1 k2. k1 \<in> d \<and> k2 \<in> d
  8.1602 +                 ==> k1 \<subseteq> k2 \/ k2 \<subseteq> k1 \/
  8.1603 +                     interior k1 \<inter> interior k2 = {}) \<and>
  8.1604 +        (!x. x \<in> s ==> ?k. k \<in> d \<and> x \<in> k \<and> k \<subseteq> g(x)) \<and>
  8.1605 +        (!k. k \<in> d ==> FINITE {l | l \<in> d \<and> k \<subseteq> l})`
  8.1606 +  ASSUME_TAC THENL
  8.1607 +   [EXISTS_TAC
  8.1608 +     `IMAGE (\<lambda>(n,v).
  8.1609 +             interval[(lambda i. a$i + (v$i) / 2 pow n *
  8.1610 +                                       ((b:real^N)$i - (a:real^N)$i)):real^N,
  8.1611 +                      (lambda i. a$i + ((v$i) + 1) / 2 pow n * (b$i - a$i))])
  8.1612 +            {n,v | n \<in> (:num) \<and>
  8.1613 +                   v \<in> {v:num^N | !i. 1 \<le> i \<and> i \<le> dimindex(:N)
  8.1614 +                                       ==> v$i < 2 EXP n}}` THEN
  8.1615 +    CONJ_TAC THENL
  8.1616 +     [MATCH_MP_TAC COUNTABLE_IMAGE THEN
  8.1617 +      MATCH_MP_TAC COUNTABLE_PRODUCT_DEPENDENT THEN
  8.1618 +      REWRITE_TAC[NUM_COUNTABLE; IN_UNIV] THEN
  8.1619 +      GEN_TAC THEN MATCH_MP_TAC FINITE_IMP_COUNTABLE THEN
  8.1620 +      MATCH_MP_TAC FINITE_CART THEN REWRITE_TAC[FINITE_NUMSEG_LT];
  8.1621 +      ALL_TAC] THEN
  8.1622 +    CONJ_TAC THENL
  8.1623 +     [REWRITE_TAC[FORALL_IN_IMAGE; FORALL_PAIR_THM] THEN
  8.1624 +      MAP_EVERY X_GEN_TAC [`n:num`; `v:num^N`] THEN
  8.1625 +      REWRITE_TAC[IN_ELIM_PAIR_THM] THEN
  8.1626 +      REWRITE_TAC[IN_ELIM_THM; IN_UNIV] THEN DISCH_TAC THEN
  8.1627 +      REWRITE_TAC[CONJ_ASSOC] THEN CONJ_TAC THENL [ALL_TAC; MESON_TAC[]] THEN
  8.1628 +      SIMP_TAC[INTERVAL_NE_EMPTY; SUBSET_INTERVAL; LAMBDA_BETA] THEN
  8.1629 +      REWRITE_TAC[REAL_LE_LADD; REAL_LE_ADDR; REAL_ARITH
  8.1630 +        `a + x * (b - a) \<le> b \<longleftrightarrow> 0 \<le> (1 - x) * (b - a)`] THEN
  8.1631 +      RULE_ASSUM_TAC(REWRITE_RULE[INTERVAL_NE_EMPTY]) THEN
  8.1632 +      REPEAT STRIP_TAC THEN
  8.1633 +      (MATCH_MP_TAC REAL_LE_MUL ORELSE MATCH_MP_TAC REAL_LE_RMUL) THEN
  8.1634 +      ASM_SIMP_TAC[REAL_SUB_LE; REAL_LE_DIV2_EQ; REAL_LT_POW2] THEN
  8.1635 +      ASM_SIMP_TAC[REAL_LE_LDIV_EQ; REAL_LE_RDIV_EQ; REAL_LT_POW2] THEN
  8.1636 +      REWRITE_TAC[REAL_MUL_LZERO; REAL_POS; REAL_MUL_LID; REAL_LE_ADDR] THEN
  8.1637 +      SIMP_TAC[REAL_OF_NUM_ADD; REAL_OF_NUM_POW; REAL_OF_NUM_LE] THEN
  8.1638 +      ASM_SIMP_TAC[ARITH_RULE `x + 1 \<le> y \<longleftrightarrow> x < y`; REAL_LT_IMP_LE];
  8.1639 +      ALL_TAC] THEN
  8.1640 +    CONJ_TAC THENL
  8.1641 +     [ONCE_REWRITE_TAC[IMP_CONJ] THEN
  8.1642 +      REWRITE_TAC[FORALL_IN_IMAGE; FORALL_PAIR_THM; RIGHT_FORALL_IMP_THM] THEN
  8.1643 +      REWRITE_TAC[IN_ELIM_PAIR_THM; IN_UNIV] THEN REWRITE_TAC[IN_ELIM_THM] THEN
  8.1644 +      REWRITE_TAC[RIGHT_IMP_FORALL_THM] THEN
  8.1645 +      GEN_REWRITE_TAC BINDER_CONV [SWAP_FORALL_THM] THEN
  8.1646 +      MATCH_MP_TAC WLOG_LE THEN CONJ_TAC THENL
  8.1647 +       [REPEAT GEN_TAC THEN
  8.1648 +        GEN_REWRITE_TAC RAND_CONV [SWAP_FORALL_THM] THEN
  8.1649 +        REPEAT(AP_TERM_TAC THEN ABS_TAC) THEN SET_TAC[];
  8.1650 +        ALL_TAC] THEN
  8.1651 +      MAP_EVERY X_GEN_TAC [`m:num`; `n:num`] THEN DISCH_TAC THEN
  8.1652 +      MAP_EVERY X_GEN_TAC [`v:num^N`; `w:num^N`] THEN REPEAT DISCH_TAC THEN
  8.1653 +      REWRITE_TAC[INTERIOR_CLOSED_INTERVAL; SUBSET_INTERVAL] THEN
  8.1654 +      SIMP_TAC[DISJOINT_INTERVAL; LAMBDA_BETA] THEN
  8.1655 +      MATCH_MP_TAC(TAUT `p \/ q \/ r ==> (a ==> p) \/ (b ==> q) \/ r`) THEN
  8.1656 +      ONCE_REWRITE_TAC[TAUT `a \<and> b \<and> c \<longleftrightarrow> ~(a \<and> b ==> ~c)`] THEN
  8.1657 +      RULE_ASSUM_TAC(REWRITE_RULE[INTERVAL_NE_EMPTY]) THEN
  8.1658 +      ASM_SIMP_TAC[REAL_LE_LADD; REAL_LE_RMUL_EQ; REAL_SUB_LT; LAMBDA_BETA] THEN
  8.1659 +      REWRITE_TAC[NOT_IMP; REAL_LE_LADD] THEN
  8.1660 +      ASM_SIMP_TAC[REAL_LE_DIV2_EQ; REAL_LT_POW2] THEN
  8.1661 +      REWRITE_TAC[REAL_ARITH `~(x + 1 \<le> x)`] THEN DISJ2_TAC THEN
  8.1662 +      MATCH_MP_TAC(MESON[]
  8.1663 +       `(!i. ~P i ==> Q i) ==> (!i. Q i) \/ (\<exists>i. P i)`) THEN
  8.1664 +      X_GEN_TAC `i:num` THEN
  8.1665 +      DISCH_THEN(fun th -> STRIP_TAC THEN MP_TAC th) THEN
  8.1666 +      ASM_REWRITE_TAC[DE_MORGAN_THM; REAL_NOT_LE] THEN
  8.1667 +      UNDISCH_TAC `m:num \<le> n` THEN REWRITE_TAC[LE_EXISTS] THEN
  8.1668 +      DISCH_THEN(X_CHOOSE_THEN `p:num` SUBST1_TAC) THEN
  8.1669 +      ONCE_REWRITE_TAC[ADD_SYM] THEN
  8.1670 +      REWRITE_TAC[REAL_POW_ADD; real_div; REAL_INV_MUL] THEN
  8.1671 +      REWRITE_TAC[REAL_MUL_ASSOC] THEN REWRITE_TAC[GSYM real_div] THEN
  8.1672 +      ASM_SIMP_TAC[REAL_LE_DIV2_EQ; REAL_LT_POW2; REAL_LT_DIV2_EQ] THEN
  8.1673 +      ASM_SIMP_TAC[REAL_LE_LDIV_EQ; REAL_LE_RDIV_EQ; REAL_LT_POW2;
  8.1674 +                   REAL_LT_LDIV_EQ; REAL_LT_RDIV_EQ] THEN
  8.1675 +      SIMP_TAC[REAL_LT_INTEGERS; INTEGER_CLOSED] THEN REAL_ARITH_TAC;
  8.1676 +      ALL_TAC] THEN
  8.1677 +    CONJ_TAC THENL
  8.1678 +     [X_GEN_TAC `x:real^N` THEN DISCH_TAC THEN
  8.1679 +      SUBGOAL_THEN
  8.1680 +        `?e. 0 < e \<and> !y. (!i. 1 \<le> i \<and> i \<le> dimindex(:N)
  8.1681 +                                ==> abs((x:real^N)$i - (y:real^N)$i) \<le> e)
  8.1682 +                           ==> y \<in> g(x)`
  8.1683 +      STRIP_ASSUME_TAC THENL
  8.1684 +       [FIRST_ASSUM(MP_TAC o SPEC `x:real^N` o GEN_REWRITE_RULE I [gauge]) THEN
  8.1685 +        STRIP_TAC THEN
  8.1686 +        FIRST_ASSUM(MP_TAC o GEN_REWRITE_RULE I [OPEN_CONTAINS_BALL]) THEN
  8.1687 +        DISCH_THEN(MP_TAC o SPEC `x:real^N`) THEN ASM_REWRITE_TAC[] THEN
  8.1688 +        DISCH_THEN(X_CHOOSE_THEN `e:real` STRIP_ASSUME_TAC) THEN
  8.1689 +        EXISTS_TAC `e / 2 / (dimindex(:N))` THEN
  8.1690 +        ASM_SIMP_TAC[REAL_LT_DIV; REAL_OF_NUM_LT; LE_1; DIMINDEX_GE_1;
  8.1691 +                     ARITH] THEN
  8.1692 +        X_GEN_TAC `y:real^N` THEN STRIP_TAC THEN
  8.1693 +        MATCH_MP_TAC(SET_RULE `!s. s \<subseteq> t \<and> x \<in> s ==> x \<in> t`) THEN
  8.1694 +        EXISTS_TAC `ball(x:real^N,e)` THEN ASM_REWRITE_TAC[IN_BALL] THEN
  8.1695 +        MATCH_MP_TAC(REAL_ARITH `0 < e \<and> x \<le> e / 2 ==> x < e`) THEN
  8.1696 +        ASM_REWRITE_TAC[dist] THEN MATCH_MP_TAC REAL_LE_TRANS THEN
  8.1697 +        EXISTS_TAC `sum(1..dimindex(:N)) (\<lambda>i. abs((x - y:real^N)$i))` THEN
  8.1698 +        REWRITE_TAC[NORM_LE_L1] THEN MATCH_MP_TAC SUM_BOUND_GEN THEN
  8.1699 +        ASM_SIMP_TAC[IN_NUMSEG; FINITE_NUMSEG; NUMSEG_EMPTY; NOT_LT;
  8.1700 +                     DIMINDEX_GE_1; VECTOR_SUB_COMPONENT; CARD_NUMSEG_1];
  8.1701 +        ALL_TAC] THEN
  8.1702 +      REWRITE_TAC[EXISTS_IN_IMAGE; EXISTS_PAIR_THM; IN_ELIM_PAIR_THM] THEN
  8.1703 +      MP_TAC(SPECL [`1 / 2`; `e / norm(b - a:real^N)`]
  8.1704 +        REAL_ARCH_POW_INV) THEN
  8.1705 +      SUBGOAL_THEN `0 < norm(b - a:real^N)` ASSUME_TAC THENL
  8.1706 +       [ASM_MESON_TAC[VECTOR_SUB_EQ; NORM_POS_LT; INTERVAL_SING]; ALL_TAC] THEN
  8.1707 +      CONV_TAC REAL_RAT_REDUCE_CONV THEN ASM_SIMP_TAC[REAL_LT_DIV] THEN
  8.1708 +      MATCH_MP_TAC MONO_EXISTS THEN X_GEN_TAC `n:num` THEN
  8.1709 +      REWRITE_TAC[real_div; REAL_MUL_LID; REAL_POW_INV] THEN DISCH_TAC THEN
  8.1710 +      SIMP_TAC[IN_ELIM_THM; IN_INTERVAL; SUBSET; LAMBDA_BETA] THEN
  8.1711 +      MATCH_MP_TAC(MESON[]
  8.1712 +       `(!x. Q x ==> R x) \<and> (\<exists>x. P x \<and> Q x) ==> ?x. P x \<and> Q x \<and> R x`) THEN
  8.1713 +      CONJ_TAC THENL
  8.1714 +       [REWRITE_TAC[RIGHT_IMP_FORALL_THM] THEN
  8.1715 +        MAP_EVERY X_GEN_TAC [`w:num^N`; `y:real^N`] THEN
  8.1716 +        REWRITE_TAC[IMP_IMP; AND_FORALL_THM] THEN
  8.1717 +        DISCH_THEN(fun th -> FIRST_X_ASSUM MATCH_MP_TAC THEN MP_TAC th) THEN
  8.1718 +        MATCH_MP_TAC MONO_FORALL THEN X_GEN_TAC `i:num` THEN
  8.1719 +        DISCH_THEN(fun th -> STRIP_TAC THEN MP_TAC th) THEN
  8.1720 +        ASM_REWRITE_TAC[] THEN DISCH_THEN(MP_TAC o MATCH_MP (REAL_ARITH
  8.1721 +         `(a + n \<le> x \<and> x \<le> a + m) \<and>
  8.1722 +          (a + n \<le> y \<and> y \<le> a + m) ==> abs(x - y) \<le> m - n`)) THEN
  8.1723 +        MATCH_MP_TAC(REAL_ARITH
  8.1724 +         `y * z \<le> e
  8.1725 +          ==> a \<le> ((x + 1) * y) * z - ((x * y) * z) ==> a \<le> e`) THEN
  8.1726 +        RULE_ASSUM_TAC(REWRITE_RULE[INTERVAL_NE_EMPTY]) THEN
  8.1727 +        ASM_SIMP_TAC[GSYM REAL_LE_RDIV_EQ; REAL_SUB_LT] THEN
  8.1728 +        FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP
  8.1729 +        (REAL_ARITH `n < e * x ==> 0 \<le> e * (inv y - x) ==> n \<le> e / y`)) THEN
  8.1730 +        MATCH_MP_TAC REAL_LE_MUL THEN ASM_SIMP_TAC[REAL_LT_IMP_LE] THEN
  8.1731 +        REWRITE_TAC[REAL_SUB_LE] THEN MATCH_MP_TAC REAL_LE_INV2 THEN
  8.1732 +        ASM_SIMP_TAC[REAL_SUB_LT] THEN
  8.1733 +        MP_TAC(SPECL [`b - a:real^N`; `i:num`] COMPONENT_LE_NORM) THEN
  8.1734 +        ASM_SIMP_TAC[VECTOR_SUB_COMPONENT] THEN REAL_ARITH_TAC;
  8.1735 +        ALL_TAC] THEN
  8.1736 +      REWRITE_TAC[IN_UNIV; AND_FORALL_THM] THEN
  8.1737 +      REWRITE_TAC[TAUT `(a ==> c) \<and> (a ==> b) \<longleftrightarrow> a ==> b \<and> c`] THEN
  8.1738 +      REWRITE_TAC[GSYM LAMBDA_SKOLEM] THEN X_GEN_TAC `i:num` THEN
  8.1739 +      STRIP_TAC THEN
  8.1740 +      SUBGOAL_THEN `(x:real^N) \<in> {a..b}` MP_TAC THENL
  8.1741 +       [ASM SET_TAC[]; ALL_TAC] THEN REWRITE_TAC[IN_INTERVAL] THEN
  8.1742 +      DISCH_THEN(MP_TAC o SPEC `i:num`) THEN ASM_REWRITE_TAC[] THEN
  8.1743 +      RULE_ASSUM_TAC(REWRITE_RULE[INTERVAL_NE_EMPTY]) THEN STRIP_TAC THEN
  8.1744 +      DISJ_CASES_TAC(MATCH_MP (REAL_ARITH `x \<le> y ==> x = y \/ x < y`)
  8.1745 +       (ASSUME `(x:real^N)$i \<le> (b:real^N)$i`))
  8.1746 +      THENL
  8.1747 +       [EXISTS_TAC `2 EXP n - 1` THEN
  8.1748 +        SIMP_TAC[GSYM REAL_OF_NUM_SUB; GSYM REAL_OF_NUM_LT;
  8.1749 +                 EXP_LT_0; LE_1; ARITH] THEN
  8.1750 +        ASM_REWRITE_TAC[REAL_SUB_ADD; REAL_ARITH `a - 1 < a`] THEN
  8.1751 +        MATCH_MP_TAC(REAL_ARITH
  8.1752 +         `1 * (b - a) = x \<and> y \<le> x ==> a + y \<le> b \<and> b \<le> a + x`) THEN
  8.1753 +        ASM_SIMP_TAC[REAL_EQ_MUL_RCANCEL; REAL_LT_IMP_NZ; REAL_LE_RMUL_EQ;
  8.1754 +                     REAL_SUB_LT; REAL_LT_INV_EQ; REAL_LT_POW2] THEN
  8.1755 +        SIMP_TAC[GSYM REAL_OF_NUM_POW; REAL_MUL_RINV; REAL_POW_EQ_0;
  8.1756 +                 REAL_OF_NUM_EQ; ARITH_EQ] THEN REAL_ARITH_TAC;
  8.1757 +        ALL_TAC] THEN
  8.1758 +      MP_TAC(SPEC `2 pow n * ((x:real^N)$i - (a:real^N)$i) /
  8.1759 +                              ((b:real^N)$i - (a:real^N)$i)` FLOOR_POS) THEN
  8.1760 +      ANTS_TAC THENL
  8.1761 +       [ASM_MESON_TAC[REAL_LE_MUL; REAL_LE_MUL; REAL_POW_LE; REAL_POS;
  8.1762 +                      REAL_SUB_LE; REAL_LT_IMP_LE; REAL_LE_DIV];
  8.1763 +        ALL_TAC] THEN
  8.1764 +      MATCH_MP_TAC MONO_EXISTS THEN X_GEN_TAC `m:num` THEN
  8.1765 +      REWRITE_TAC[GSYM REAL_OF_NUM_LT; GSYM REAL_OF_NUM_POW] THEN
  8.1766 +      DISCH_THEN(SUBST_ALL_TAC o SYM) THEN
  8.1767 +      REWRITE_TAC[REAL_ARITH `a + b * c \<le> x \<and> x \<le> a + b' * c \<longleftrightarrow>
  8.1768 +                              b * c \<le> x - a \<and> x - a \<le> b' * c`] THEN
  8.1769 +      ASM_SIMP_TAC[GSYM REAL_LE_LDIV_EQ; GSYM REAL_LE_RDIV_EQ;
  8.1770 +                   REAL_SUB_LT; GSYM real_div] THEN
  8.1771 +      ONCE_REWRITE_TAC[REAL_MUL_SYM] THEN
  8.1772 +      SIMP_TAC[REAL_LE_LDIV_EQ; REAL_LE_RDIV_EQ; REAL_LT_POW2] THEN
  8.1773 +      SIMP_TAC[FLOOR; REAL_LT_IMP_LE] THEN MATCH_MP_TAC REAL_LET_TRANS THEN
  8.1774 +      EXISTS_TAC `((x:real^N)$i - (a:real^N)$i) /
  8.1775 +                  ((b:real^N)$i - (a:real^N)$i) *
  8.1776 +                  2 pow n` THEN
  8.1777 +      REWRITE_TAC[FLOOR] THEN GEN_REWRITE_TAC RAND_CONV [GSYM REAL_MUL_LID] THEN
  8.1778 +      ASM_SIMP_TAC[REAL_LT_RMUL_EQ; REAL_LT_POW2] THEN
  8.1779 +      ASM_SIMP_TAC[REAL_LT_LDIV_EQ; REAL_MUL_LID; REAL_SUB_LT] THEN
  8.1780 +      ASM_REAL_ARITH_TAC;
  8.1781 +      ALL_TAC] THEN
  8.1782 +    REWRITE_TAC[FORALL_IN_IMAGE; FORALL_PAIR_THM; IN_ELIM_PAIR_THM] THEN
  8.1783 +    MAP_EVERY X_GEN_TAC [`n:num`; `v:num^N`] THEN
  8.1784 +    REWRITE_TAC[IN_ELIM_THM; IN_UNIV] THEN DISCH_TAC THEN
  8.1785 +    MATCH_MP_TAC FINITE_SUBSET THEN EXISTS_TAC
  8.1786 +     `IMAGE (\<lambda>(n,v).
  8.1787 +            interval[(lambda i. a$i + (v$i) / 2 pow n *
  8.1788 +                                      ((b:real^N)$i - (a:real^N)$i)):real^N,
  8.1789 +                     (lambda i. a$i + ((v$i) + 1) / 2 pow n * (b$i - a$i))])
  8.1790 +            {m,v | m \<in> 0..n \<and>
  8.1791 +                   v \<in> {v:num^N | !i. 1 \<le> i \<and> i \<le> dimindex(:N)
  8.1792 +                                       ==> v$i < 2 EXP m}}` THEN
  8.1793 +    CONJ_TAC THENL
  8.1794 +     [MATCH_MP_TAC FINITE_IMAGE THEN
  8.1795 +      MATCH_MP_TAC FINITE_PRODUCT_DEPENDENT THEN
  8.1796 +      REWRITE_TAC[FINITE_NUMSEG] THEN REPEAT STRIP_TAC THEN
  8.1797 +      MATCH_MP_TAC FINITE_CART THEN REWRITE_TAC[FINITE_NUMSEG_LT];
  8.1798 +      ALL_TAC] THEN
  8.1799 +    GEN_REWRITE_TAC I [SUBSET] THEN
  8.1800 +    REWRITE_TAC[IN_ELIM_THM] THEN ONCE_REWRITE_TAC[IMP_CONJ] THEN
  8.1801 +    REWRITE_TAC[FORALL_IN_IMAGE; FORALL_PAIR_THM; IN_ELIM_PAIR_THM] THEN
  8.1802 +    MAP_EVERY X_GEN_TAC [`m:num`; `w:num^N`] THEN DISCH_TAC THEN
  8.1803 +    DISCH_TAC THEN SIMP_TAC[IN_IMAGE; EXISTS_PAIR_THM; IN_ELIM_PAIR_THM] THEN
  8.1804 +    MAP_EVERY EXISTS_TAC [`m:num`; `w:num^N`] THEN ASM_REWRITE_TAC[] THEN
  8.1805 +    REWRITE_TAC[IN_NUMSEG; GSYM NOT_LT; LT] THEN DISCH_TAC THEN
  8.1806 +    FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [SUBSET_INTERVAL]) THEN
  8.1807 +    SIMP_TAC[NOT_IMP; LAMBDA_BETA] THEN
  8.1808 +    RULE_ASSUM_TAC(REWRITE_RULE[INTERVAL_NE_EMPTY]) THEN
  8.1809 +    ASM_SIMP_TAC[REAL_LE_LADD; REAL_LE_RMUL_EQ; REAL_SUB_LT] THEN
  8.1810 +    ASM_SIMP_TAC[REAL_LE_DIV2_EQ; REAL_LT_POW2] THEN
  8.1811 +    REWRITE_TAC[REAL_ARITH `x \<le> x + 1`] THEN
  8.1812 +    DISCH_THEN(MP_TAC o SPEC `1`) THEN
  8.1813 +    REWRITE_TAC[LE_REFL; DIMINDEX_GE_1] THEN
  8.1814 +    DISCH_THEN(MP_TAC o MATCH_MP (REAL_ARITH
  8.1815 +     `w / m \<le> v / n \<and> (v + 1) / n \<le> (w + 1) / m
  8.1816 +      ==> inv n \<le> inv m`)) THEN
  8.1817 +    REWRITE_TAC[REAL_NOT_LE] THEN MATCH_MP_TAC REAL_LT_INV2 THEN
  8.1818 +    ASM_REWRITE_TAC[REAL_LT_POW2] THEN MATCH_MP_TAC REAL_POW_MONO_LT THEN
  8.1819 +    ASM_REWRITE_TAC[] THEN CONV_TAC REAL_RAT_REDUCE_CONV;
  8.1820 +    ALL_TAC] THEN
  8.1821 +  SUBGOAL_THEN
  8.1822 +   `?d. COUNTABLE d \<and>
  8.1823 +        (!k. k \<in> d ==> k \<subseteq> {a..b} \<and> ~(k = {}) \<and>
  8.1824 +                        (\<exists>c d:real^N. k = {c..d})) \<and>
  8.1825 +        (!k1 k2. k1 \<in> d \<and> k2 \<in> d
  8.1826 +                 ==> k1 \<subseteq> k2 \/ k2 \<subseteq> k1 \/
  8.1827 +                     interior k1 \<inter> interior k2 = {}) \<and>
  8.1828 +        (!k. k \<in> d ==> (\<exists>x. x \<in> s \<inter> k \<and> k \<subseteq> g x)) \<and>
  8.1829 +        (!k. k \<in> d ==> FINITE {l | l \<in> d \<and> k \<subseteq> l}) \<and>
  8.1830 +        s \<subseteq> \<Union>d`
  8.1831 +  MP_TAC THENL
  8.1832 +   [FIRST_X_ASSUM(X_CHOOSE_THEN `d:(real^N->bool)->bool` STRIP_ASSUME_TAC) THEN
  8.1833 +    EXISTS_TAC
  8.1834 +     `{k:real^N->bool | k \<in> d \<and> ?x. x \<in> (s \<inter> k) \<and> k \<subseteq> g x}` THEN
  8.1835 +    ASM_SIMP_TAC[IN_ELIM_THM] THEN REPEAT CONJ_TAC THENL
  8.1836 +     [MATCH_MP_TAC COUNTABLE_SUBSET THEN
  8.1837 +      EXISTS_TAC `d:(real^N->bool)->bool` THEN
  8.1838 +      ASM_REWRITE_TAC[] THEN SET_TAC[];
  8.1839 +      X_GEN_TAC `k:real^N->bool` THEN REPEAT STRIP_TAC THEN
  8.1840 +      MATCH_MP_TAC FINITE_SUBSET THEN
  8.1841 +      EXISTS_TAC `{l:real^N->bool | l \<in> d \<and> k \<subseteq> l}` THEN
  8.1842 +      ASM_REWRITE_TAC[] THEN SET_TAC[];
  8.1843 +      ASM SET_TAC[]];
  8.1844 +    ALL_TAC] THEN
  8.1845 +  DISCH_THEN(X_CHOOSE_THEN `d:(real^N->bool)->bool` STRIP_ASSUME_TAC) THEN
  8.1846 +  EXISTS_TAC
  8.1847 +   `{k:real^N->bool | k \<in> d \<and> !k'. k' \<in> d \<and> ~(k = k')
  8.1848 +                                     ==> ~(k \<subseteq> k')}` THEN
  8.1849 +  ASM_SIMP_TAC[IN_ELIM_THM] THEN REPEAT CONJ_TAC THENL
  8.1850 +   [MATCH_MP_TAC COUNTABLE_SUBSET THEN EXISTS_TAC `d:(real^N->bool)->bool` THEN
  8.1851 +    ASM_REWRITE_TAC[] THEN SET_TAC[];
  8.1852 +    ASM SET_TAC[];
  8.1853 +    ALL_TAC] THEN
  8.1854 +  FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP
  8.1855 +   (REWRITE_RULE[IMP_CONJ] SUBSET_TRANS)) THEN
  8.1856 +  GEN_REWRITE_TAC I [SUBSET] THEN REWRITE_TAC[FORALL_IN_UNIONS] THEN
  8.1857 +  MAP_EVERY X_GEN_TAC [`k:real^N->bool`; `x:real^N`] THEN DISCH_TAC THEN
  8.1858 +  REWRITE_TAC[IN_UNIONS; IN_ELIM_THM] THEN
  8.1859 +  MP_TAC(ISPEC `\k l:real^N->bool. k \<in> d \<and> l \<in> d \<and> l \<subseteq> k \<and> ~(k = l)`
  8.1860 +     WF_FINITE) THEN
  8.1861 +  REWRITE_TAC[WF] THEN ANTS_TAC THENL
  8.1862 +   [CONJ_TAC THENL [SET_TAC[]; ALL_TAC] THEN X_GEN_TAC `l:real^N->bool` THEN
  8.1863 +    ASM_CASES_TAC `(l:real^N->bool) \<in> d` THEN
  8.1864 +    ASM_REWRITE_TAC[EMPTY_GSPEC; FINITE_RULES] THEN
  8.1865 +    MATCH_MP_TAC FINITE_SUBSET THEN
  8.1866 +    EXISTS_TAC `{m:real^N->bool | m \<in> d \<and> l \<subseteq> m}` THEN
  8.1867 +    ASM_SIMP_TAC[] THEN SET_TAC[];
  8.1868 +    ALL_TAC] THEN
  8.1869 +  DISCH_THEN(MP_TAC o SPEC `\l:real^N->bool. l \<in> d \<and> x \<in> l`) THEN
  8.1870 +  REWRITE_TAC[] THEN ANTS_TAC THENL [SET_TAC[]; ALL_TAC] THEN
  8.1871 +  MATCH_MP_TAC MONO_EXISTS THEN ASM SET_TAC[]);; *)
  8.1872 +
  8.1873 +lemma GMEASURABLE_OUTER_INTERVALS_BOUNDED: True .. (*
  8.1874 + "!s a b:real^N e.
  8.1875 +        gmeasurable s \<and> s \<subseteq> {a..b} \<and> 0 < e
  8.1876 +        ==> ?d. COUNTABLE d \<and>
  8.1877 +                (!k. k \<in> d ==> k \<subseteq> {a..b} \<and> ~(k = {}) \<and>
  8.1878 +                                (\<exists>c d. k = {c..d})) \<and>
  8.1879 +                (!k1 k2. k1 \<in> d \<and> k2 \<in> d \<and> ~(k1 = k2)
  8.1880 +                         ==> interior k1 \<inter> interior k2 = {}) \<and>
  8.1881 +                s \<subseteq> \<Union>d \<and>
  8.1882 +                gmeasurable (\<Union>d) \<and>
  8.1883 +                gmeasure (\<Union>d) \<le> gmeasure s + e"
  8.1884 +qed   lemma lemma = prove
  8.1885 +   (`(!x y. (x,y) \<in> IMAGE (\<lambda>z. f z,g z) s ==> P x y) \<longleftrightarrow>
  8.1886 +     (!z. z \<in> s ==> P (f z) (g z))"
  8.1887 +qed   REWRITE_TAC[IN_IMAGE; PAIR_EQ] THEN MESON_TAC[]) in
  8.1888 +  REPEAT GEN_TAC THEN
  8.1889 +  ASM_CASES_TAC `interval[a:real^N,b] = {}` THENL
  8.1890 +   [ASM_REWRITE_TAC[SUBSET_EMPTY] THEN STRIP_TAC THEN
  8.1891 +    EXISTS_TAC `{}:(real^N->bool)->bool` THEN
  8.1892 +    ASM_REWRITE_TAC[NOT_IN_EMPTY; UNIONS_0; MEASURE_EMPTY; REAL_ADD_LID;
  8.1893 +                    SUBSET_REFL; COUNTABLE_EMPTY; GMEASURABLE_EMPTY] THEN
  8.1894 +    ASM_SIMP_TAC[REAL_LT_IMP_LE];
  8.1895 +    ALL_TAC] THEN
  8.1896 +  STRIP_TAC THEN ASM_CASES_TAC `interval(a:real^N,b) = {}` THENL
  8.1897 +   [EXISTS_TAC `{interval[a:real^N,b]}` THEN
  8.1898 +    REWRITE_TAC[UNIONS_1; COUNTABLE_SING] THEN
  8.1899 +    ASM_REWRITE_TAC[IMP_CONJ; RIGHT_FORALL_IMP_THM; FORALL_IN_INSERT;
  8.1900 +                    NOT_IN_EMPTY; SUBSET_REFL; GMEASURABLE_INTERVAL] THEN
  8.1901 +    CONJ_TAC THENL [MESON_TAC[]; ALL_TAC] THEN
  8.1902 +    SUBGOAL_THEN
  8.1903 +     `measure(interval[a:real^N,b]) = 0 \<and> measure(s:real^N->bool) = 0`
  8.1904 +     (fun th -> ASM_SIMP_TAC[th; REAL_LT_IMP_LE; REAL_ADD_LID]) THEN
  8.1905 +    SUBGOAL_THEN
  8.1906 +      `interval[a:real^N,b] has_gmeasure 0 \<and> (s:real^N->bool) has_gmeasure 0`
  8.1907 +      (fun th -> MESON_TAC[th; MEASURE_UNIQUE]) THEN
  8.1908 +    REWRITE_TAC[HAS_GMEASURE_0] THEN
  8.1909 +    MATCH_MP_TAC(TAUT `a \<and> (a ==> b) ==> a \<and> b`) THEN CONJ_TAC THENL
  8.1910 +     [ASM_REWRITE_TAC[NEGLIGIBLE_INTERVAL];
  8.1911 +      ASM_MESON_TAC[NEGLIGIBLE_SUBSET]];
  8.1912 +    ALL_TAC] THEN
  8.1913 +  FIRST_ASSUM(MP_TAC o GEN_REWRITE_RULE I [measurable]) THEN
  8.1914 +  DISCH_THEN(X_CHOOSE_TAC `m:real`) THEN
  8.1915 +  FIRST_ASSUM(ASSUME_TAC o MATCH_MP MEASURE_UNIQUE) THEN
  8.1916 +  SUBGOAL_THEN
  8.1917 +   `((\<lambda>x:real^N. if x \<in> s then 1 else 0) has_integral (lift m))
  8.1918 +    {a..b}`
  8.1919 +  ASSUME_TAC THENL
  8.1920 +   [ONCE_REWRITE_TAC[GSYM HAS_INTEGRAL_RESTRICT_UNIV] THEN
  8.1921 +    FIRST_ASSUM(MP_TAC o GEN_REWRITE_RULE I [HAS_GMEASURE]) THEN
  8.1922 +    MATCH_MP_TAC(REWRITE_RULE[IMP_CONJ] HAS_INTEGRAL_EQ) THEN
  8.1923 +    ASM SET_TAC[];
  8.1924 +    ALL_TAC] THEN
  8.1925 +  FIRST_ASSUM(ASSUME_TAC o MATCH_MP HAS_INTEGRAL_INTEGRABLE) THEN
  8.1926 +  FIRST_ASSUM(MP_TAC o GEN_REWRITE_RULE I [has_integral]) THEN
  8.1927 +  DISCH_THEN(MP_TAC o SPEC `e:real`) THEN ASM_REWRITE_TAC[] THEN
  8.1928 +  DISCH_THEN(X_CHOOSE_THEN `g:real^N->real^N->bool` STRIP_ASSUME_TAC) THEN
  8.1929 +  MP_TAC(SPECL [`a:real^N`; `b:real^N`; `s:real^N->bool`;
  8.1930 +                `g:real^N->real^N->bool`] COVERING_LEMMA) THEN
  8.1931 +  ASM_REWRITE_TAC[] THEN MATCH_MP_TAC MONO_EXISTS THEN
  8.1932 +  X_GEN_TAC `d:(real^N->bool)->bool` THEN STRIP_TAC THEN ASM_REWRITE_TAC[] THEN
  8.1933 +  MP_TAC(ISPECL [`(\<lambda>x. if x \<in> s then 1 else 0):real^N->real^1`;
  8.1934 +                 `a:real^N`; `b:real^N`; `g:real^N->real^N->bool`;
  8.1935 +                 `e:real`]
  8.1936 +                HENSTOCK_LEMMA_PART1) THEN
  8.1937 +  ASM_REWRITE_TAC[] THEN
  8.1938 +  FIRST_ASSUM(SUBST1_TAC o MATCH_MP INTEGRAL_UNIQUE) THEN
  8.1939 +  ASM_REWRITE_TAC[] THEN DISCH_THEN(LABEL_TAC "*") THEN
  8.1940 +  SUBGOAL_THEN
  8.1941 +   `!k l:real^N->bool. k \<in> d \<and> l \<in> d \<and> ~(k = l)
  8.1942 +                       ==> negligible(k \<inter> l)`
  8.1943 +  ASSUME_TAC THENL
  8.1944 +   [REPEAT STRIP_TAC THEN
  8.1945 +    FIRST_X_ASSUM(MP_TAC o SPECL [`k:real^N->bool`; `l:real^N->bool`]) THEN
  8.1946 +    ASM_SIMP_TAC[] THEN
  8.1947 +    SUBGOAL_THEN
  8.1948 +     `?x y:real^N u v:real^N. k = {x..y} \<and> l = {u..v}`
  8.1949 +    MP_TAC THENL [ASM_MESON_TAC[SUBSET]; ALL_TAC] THEN
  8.1950 +    DISCH_THEN(REPEAT_TCL CHOOSE_THEN (CONJUNCTS_THEN SUBST_ALL_TAC)) THEN
  8.1951 +    REWRITE_TAC[INTERIOR_CLOSED_INTERVAL] THEN DISCH_TAC THEN
  8.1952 +    MATCH_MP_TAC NEGLIGIBLE_SUBSET THEN
  8.1953 +    EXISTS_TAC `(interval[x:real^N,y] DIFF {x<..<y}) UNION
  8.1954 +                (interval[u:real^N,v] DIFF {u<..<v}) UNION
  8.1955 +                (interval (x,y) \<inter> interval (u,v))` THEN
  8.1956 +    CONJ_TAC THENL [ALL_TAC; SET_TAC[]] THEN
  8.1957 +    ASM_REWRITE_TAC[UNION_EMPTY] THEN
  8.1958 +    SIMP_TAC[NEGLIGIBLE_UNION; NEGLIGIBLE_FRONTIER_INTERVAL];
  8.1959 +    ALL_TAC] THEN
  8.1960 +  SUBGOAL_THEN
  8.1961 +   `!D. FINITE D \<and> D \<subseteq> d
  8.1962 +         ==> gmeasurable(\<Union>D :real^N->bool) \<and> measure(\<Union>D) \<le> m + e`
  8.1963 +  ASSUME_TAC THENL
  8.1964 +   [GEN_TAC THEN STRIP_TAC THEN
  8.1965 +    SUBGOAL_THEN
  8.1966 +     `?t:(real^N->bool)->real^N. !k. k \<in> D ==> t(k) \<in> (s \<inter> k) \<and>
  8.1967 +                                                k \<subseteq> (g(t k))`
  8.1968 +    (CHOOSE_THEN (LABEL_TAC "+")) THENL
  8.1969 +     [REWRITE_TAC[GSYM SKOLEM_THM] THEN ASM SET_TAC[]; ALL_TAC] THEN
  8.1970 +    REMOVE_THEN "*" (MP_TAC o SPEC
  8.1971 +     `IMAGE (\<lambda>k. (t:(real^N->bool)->real^N) k,k) D`) THEN
  8.1972 +    ASM_SIMP_TAC[VSUM_IMAGE; PAIR_EQ] THEN REWRITE_TAC[o_DEF] THEN
  8.1973 +    ANTS_TAC THENL
  8.1974 +     [REWRITE_TAC[tagged_partial_division_of; fine] THEN
  8.1975 +      ASM_SIMP_TAC[FINITE_IMAGE; FORALL_IN_IMAGE] THEN
  8.1976 +      REWRITE_TAC[lemma; RIGHT_FORALL_IMP_THM; IMP_CONJ; PAIR_EQ] THEN
  8.1977 +      ASM_SIMP_TAC[] THEN
  8.1978 +      CONJ_TAC THENL [ASM SET_TAC[]; ASM_MESON_TAC[SUBSET]];
  8.1979 +      ALL_TAC] THEN
  8.1980 +    USE_THEN "+" (MP_TAC o REWRITE_RULE[IN_INTER]) THEN
  8.1981 +    SIMP_TAC[] THEN DISCH_THEN(K ALL_TAC) THEN
  8.1982 +    ASM_SIMP_TAC[VSUM_SUB] THEN
  8.1983 +    SUBGOAL_THEN `D division_of (\<Union>D:real^N->bool)` ASSUME_TAC THENL
  8.1984 +     [REWRITE_TAC[division_of] THEN ASM SET_TAC[]; ALL_TAC] THEN
  8.1985 +    FIRST_ASSUM(ASSUME_TAC o MATCH_MP GMEASURABLE_ELEMENTARY) THEN
  8.1986 +    SUBGOAL_THEN `vsum D (\<lambda>k:real^N->bool. content k % 1) =
  8.1987 +                  lift(measure(\<Union>D))`
  8.1988 +    SUBST1_TAC THENL
  8.1989 +     [ONCE_REWRITE_TAC[GSYM _EQ] THEN
  8.1990 +      ASM_SIMP_TAC[LIFT_; _VSUM; o_DEF; _CMUL; _VEC] THEN
  8.1991 +      SIMP_TAC[REAL_MUL_RID; ETA_AX] THEN ASM_MESON_TAC[MEASURE_ELEMENTARY];
  8.1992 +      ALL_TAC] THEN
  8.1993 +    SUBGOAL_THEN
  8.1994 +     `vsum D (\<lambda>k. integral k (\<lambda>x:real^N. if x \<in> s then 1 else 0)) =
  8.1995 +      lift(sum D (\<lambda>k. measure(k \<inter> s)))`
  8.1996 +    SUBST1_TAC THENL
  8.1997 +     [ASM_SIMP_TAC[LIFT_SUM; o_DEF] THEN MATCH_MP_TAC VSUM_EQ THEN
  8.1998 +      X_GEN_TAC `k:real^N->bool` THEN DISCH_TAC THEN REWRITE_TAC[] THEN
  8.1999 +      SUBGOAL_THEN `measurable(k:real^N->bool)` ASSUME_TAC THENL
  8.2000 +       [ASM_MESON_TAC[SUBSET; GMEASURABLE_INTERVAL]; ALL_TAC] THEN
  8.2001 +      ASM_SIMP_TAC[GSYM INTEGRAL_MEASURE_UNIV; GMEASURABLE_INTER] THEN
  8.2002 +      REWRITE_TAC[MESON[IN_INTER]
  8.2003 +        `(if x \<in> k \<inter> s then a else b) =
  8.2004 +         (if x \<in> k then if x \<in> s then a else b else b)`] THEN
  8.2005 +      CONV_TAC SYM_CONV THEN MATCH_MP_TAC INTEGRAL_RESTRICT_UNIV THEN
  8.2006 +      ONCE_REWRITE_TAC[GSYM INTEGRABLE_RESTRICT_UNIV] THEN
  8.2007 +      REWRITE_TAC[MESON[IN_INTER]
  8.2008 +       `(if x \<in> k then if x \<in> s then a else b else b) =
  8.2009 +        (if x \<in> k \<inter> s then a else b)`] THEN
  8.2010 +      ASM_SIMP_TAC[GSYM GMEASURABLE_INTEGRABLE; GMEASURABLE_INTER];
  8.2011 +      ALL_TAC] THEN
  8.2012 +    ASM_REWRITE_TAC[GSYM LIFT_SUB; NORM_LIFT] THEN
  8.2013 +    MATCH_MP_TAC(REAL_ARITH `y \<le> m ==> abs(x - y) \<le> e ==> x \<le> m + e`) THEN
  8.2014 +    MATCH_MP_TAC REAL_LE_TRANS THEN
  8.2015 +    EXISTS_TAC `measure(\<Union>D \<inter> s:real^N->bool)` THEN
  8.2016 +    CONJ_TAC THENL
  8.2017 +     [ALL_TAC;
  8.2018 +      EXPAND_TAC "m" THEN MATCH_MP_TAC MEASURE_SUBSET THEN
  8.2019 +      ASM_REWRITE_TAC[] THEN CONJ_TAC THENL [ALL_TAC; SET_TAC[]] THEN
  8.2020 +      MATCH_MP_TAC GMEASURABLE_INTER THEN ASM_REWRITE_TAC[]] THEN
  8.2021 +    REWRITE_TAC[INTER_UNIONS] THEN MATCH_MP_TAC REAL_EQ_IMP_LE THEN
  8.2022 +    ONCE_REWRITE_TAC[SIMPLE_IMAGE] THEN CONV_TAC SYM_CONV THEN
  8.2023 +    MATCH_MP_TAC MEASURE_NEGLIGIBLE_UNIONS_IMAGE_STRONG THEN
  8.2024 +    ASM_SIMP_TAC[FINITE_RESTRICT] THEN CONJ_TAC THENL
  8.2025 +     [ASM_MESON_TAC[SUBSET; GMEASURABLE_INTERVAL; GMEASURABLE_INTER];
  8.2026 +      ALL_TAC] THEN
  8.2027 +    MAP_EVERY X_GEN_TAC [`k:real^N->bool`; `l:real^N->bool`] THEN
  8.2028 +    STRIP_TAC THEN MATCH_MP_TAC NEGLIGIBLE_SUBSET THEN
  8.2029 +    EXISTS_TAC `k \<inter> l:real^N->bool` THEN ASM_SIMP_TAC[] THEN SET_TAC[];
  8.2030 +    ALL_TAC] THEN
  8.2031 +  ASM_CASES_TAC `FINITE(d:(real^N->bool)->bool)` THENL
  8.2032 +   [ASM_MESON_TAC[SUBSET_REFL]; ALL_TAC] THEN
  8.2033 +  MP_TAC(ISPEC `d:(real^N->bool)->bool` COUNTABLE_AS_INJECTIVE_IMAGE) THEN
  8.2034 +  ASM_REWRITE_TAC[INFINITE] THEN
  8.2035 +  DISCH_THEN(X_CHOOSE_THEN `s:num->real^N->bool`
  8.2036 +   (CONJUNCTS_THEN2 SUBST_ALL_TAC ASSUME_TAC)) THEN
  8.2037 +  MP_TAC(ISPECL [`s:num->real^N->bool`; `m + e:real`]
  8.2038 +    HAS_GMEASURE_COUNTABLE_NEGLIGIBLE_UNIONS) THEN
  8.2039 +  MATCH_MP_TAC(TAUT `a \<and> (a \<and> b ==> c) ==> (a ==> b) ==> c`) THEN
  8.2040 +  REWRITE_TAC[GSYM CONJ_ASSOC] THEN
  8.2041 +  RULE_ASSUM_TAC(REWRITE_RULE[IMP_CONJ; RIGHT_FORALL_IMP_THM;
  8.2042 +                              FORALL_IN_IMAGE; IN_UNIV]) THEN
  8.2043 +  RULE_ASSUM_TAC(REWRITE_RULE[IMP_IMP; RIGHT_IMP_FORALL_THM]) THEN
  8.2044 +  REPEAT CONJ_TAC THENL
  8.2045 +   [ASM_MESON_TAC[MEASURABLE_INTERVAL; GMEASURABLE_INTER];
  8.2046 +    ASM_MESON_TAC[];
  8.2047 +    X_GEN_TAC `n:num` THEN
  8.2048 +    FIRST_X_ASSUM(MP_TAC o SPEC `IMAGE (s:num->real^N->bool) (0..n)`) THEN
  8.2049 +    SIMP_TAC[FINITE_IMAGE; FINITE_NUMSEG; IMAGE_SUBSET; SUBSET_UNIV] THEN
  8.2050 +    DISCH_THEN(CONJUNCTS_THEN2 ASSUME_TAC MP_TAC) THEN
  8.2051 +    MATCH_MP_TAC(REAL_ARITH `x = y ==> x \<le> e ==> y \<le> e`) THEN
  8.2052 +    MATCH_MP_TAC MEASURE_NEGLIGIBLE_UNIONS_IMAGE THEN
  8.2053 +    ASM_MESON_TAC[FINITE_NUMSEG; GMEASURABLE_INTERVAL];
  8.2054 +    ALL_TAC] THEN
  8.2055 +  ONCE_REWRITE_TAC[SIMPLE_IMAGE] THEN STRIP_TAC THEN ASM_REWRITE_TAC[] THEN
  8.2056 +  GEN_REWRITE_TAC LAND_CONV [GSYM(CONJUNCT2 LIFT_)] THEN
  8.2057 +  REWRITE_TAC[] THEN
  8.2058 +  MATCH_MP_TAC(ISPEC `sequentially` LIM_COMPONENT_UBOUND) THEN
  8.2059 +  EXISTS_TAC
  8.2060 +   `\n. vsum(from 0 \<inter> (0..n)) (\<lambda>n. lift(measure(s n:real^N->bool)))` THEN
  8.2061 +  ASM_REWRITE_TAC[GSYM sums; TRIVIAL_LIMIT_SEQUENTIALLY] THEN
  8.2062 +  REWRITE_TAC[DIMINDEX_1; ARITH; EVENTUALLY_SEQUENTIALLY] THEN
  8.2063 +  SIMP_TAC[VSUM_COMPONENT; ARITH; DIMINDEX_1] THEN
  8.2064 +  ASM_REWRITE_TAC[GSYM ; LIFT_; FROM_INTER_NUMSEG]);; *)
  8.2065 +
  8.2066 +(* ------------------------------------------------------------------------- *)
  8.2067 +(* Hence for linear transformation, suffices to check compact intervals.     *)
  8.2068 +(* ------------------------------------------------------------------------- *)
  8.2069 +
  8.2070 +lemma GMEASURABLE_LINEAR_IMAGE_INTERVAL: True .. (*
  8.2071 + "!f a b. linear f ==> gmeasurable(IMAGE f {a..b})"
  8.2072 +qed   REPEAT STRIP_TAC THEN MATCH_MP_TAC GMEASURABLE_CONVEX THEN CONJ_TAC THENL
  8.2073 +   [MATCH_MP_TAC CONVEX_LINEAR_IMAGE THEN
  8.2074 +    ASM_MESON_TAC[CONVEX_INTERVAL];
  8.2075 +    MATCH_MP_TAC BOUNDED_LINEAR_IMAGE THEN
  8.2076 +    ASM_MESON_TAC[BOUNDED_INTERVAL]]);; *)
  8.2077 +
  8.2078 +lemma HAS_GMEASURE_LINEAR_SUFFICIENT: True .. (*
  8.2079 + "!f:real^N->real^N m.
  8.2080 +        linear f \<and>
  8.2081 +        (!a b. IMAGE f {a..b} has_gmeasure
  8.2082 +               (m * measure{a..b}))
  8.2083 +        ==> !s. gmeasurable s ==> (IMAGE f s) has_gmeasure (m * gmeasure s)"
  8.2084 +qed   REPEAT GEN_TAC THEN STRIP_TAC THEN
  8.2085 +  DISJ_CASES_TAC(REAL_ARITH `m < 0 \/ 0 \<le> m`) THENL
  8.2086 +   [FIRST_X_ASSUM(MP_TAC o SPECL [`0:real^N`; `1:real^N`]) THEN
  8.2087 +    DISCH_THEN(MP_TAC o MATCH_MP HAS_GMEASURE_POS_LE) THEN
  8.2088 +    MATCH_MP_TAC(TAUT `~a ==> a ==> b`) THEN
  8.2089 +    MATCH_MP_TAC(REAL_ARITH `0 < --m * x ==> ~(0 \<le> m * x)`) THEN
  8.2090 +    MATCH_MP_TAC REAL_LT_MUL THEN ASM_REWRITE_TAC[REAL_NEG_GT0] THEN
  8.2091 +    REWRITE_TAC[MEASURE_INTERVAL] THEN MATCH_MP_TAC CONTENT_POS_LT THEN
  8.2092 +    SIMP_TAC[VEC_COMPONENT; REAL_LT_01];
  8.2093 +    ALL_TAC] THEN
  8.2094 +  ASM_CASES_TAC `!x y. (f:real^N->real^N) x = f y ==> x = y` THENL
  8.2095 +   [ALL_TAC;
  8.2096 +    SUBGOAL_THEN `!s. negligible(IMAGE (f:real^N->real^N) s)` ASSUME_TAC THENL
  8.2097 +     [ASM_MESON_TAC[NEGLIGIBLE_LINEAR_SINGULAR_IMAGE]; ALL_TAC] THEN
  8.2098 +    SUBGOAL_THEN `m * measure(interval[0:real^N,1]) = 0` MP_TAC THENL
  8.2099 +     [MATCH_MP_TAC(ISPEC `IMAGE (f:real^N->real^N) {0..1}`
  8.2100 +        HAS_GMEASURE_UNIQUE) THEN
  8.2101 +      ASM_REWRITE_TAC[HAS_GMEASURE_0];
  8.2102 +      REWRITE_TAC[REAL_ENTIRE; MEASURE_INTERVAL] THEN
  8.2103 +      MATCH_MP_TAC(TAUT `~b \<and> (a ==> c) ==> a \/ b ==> c`) THEN CONJ_TAC THENL
  8.2104 +       [SIMP_TAC[CONTENT_EQ_0_INTERIOR; INTERIOR_CLOSED_INTERVAL;
  8.2105 +                 INTERVAL_NE_EMPTY; VEC_COMPONENT; REAL_LT_01];
  8.2106 +        ASM_SIMP_TAC[REAL_MUL_LZERO; HAS_GMEASURE_0]]]] THEN
  8.2107 +  MP_TAC(ISPEC `f:real^N->real^N` LINEAR_INJECTIVE_ISOMORPHISM) THEN
  8.2108 +  ASM_REWRITE_TAC[] THEN
  8.2109 +  DISCH_THEN(X_CHOOSE_THEN `h:real^N->real^N` STRIP_ASSUME_TAC) THEN
  8.2110 +  UNDISCH_THEN `!x y. (f:real^N->real^N) x = f y ==> x = y` (K ALL_TAC) THEN
  8.2111 +  SUBGOAL_THEN
  8.2112 +   `!s. bounded s \<and> gmeasurable s
  8.2113 +        ==> (IMAGE (f:real^N->real^N) s) has_gmeasure (m * gmeasure s)`
  8.2114 +  ASSUME_TAC THENL
  8.2115 +   [REPEAT STRIP_TAC THEN
  8.2116 +    FIRST_ASSUM(MP_TAC o MATCH_MP BOUNDED_SUBSET_CLOSED_INTERVAL) THEN
  8.2117 +    REWRITE_TAC[LEFT_IMP_EXISTS_THM] THEN
  8.2118 +    MAP_EVERY X_GEN_TAC [`a:real^N`; `b:real^N`] THEN DISCH_TAC THEN
  8.2119 +    SUBGOAL_THEN
  8.2120 +     `!d. COUNTABLE d \<and>
  8.2121 +          (!k. k \<in> d ==> k \<subseteq> {a..b} \<and> ~(k = {}) \<and>
  8.2122 +                          (\<exists>c d. k = {c..d})) \<and>
  8.2123 +          (!k1 k2. k1 \<in> d \<and> k2 \<in> d \<and> ~(k1 = k2)
  8.2124 +                   ==> interior k1 \<inter> interior k2 = {})
  8.2125 +          ==> IMAGE (f:real^N->real^N) (\<Union>d) has_gmeasure
  8.2126 +                    (m * measure(\<Union>d))`
  8.2127 +    ASSUME_TAC THENL
  8.2128 +     [REWRITE_TAC[IMAGE_UNIONS] THEN REPEAT STRIP_TAC THEN
  8.2129 +      SUBGOAL_THEN
  8.2130 +       `!g:real^N->real^N.
  8.2131 +          linear g
  8.2132 +          ==> !k l. k \<in> d \<and> l \<in> d \<and> ~(k = l)
  8.2133 +                    ==> negligible((IMAGE g k) \<inter> (IMAGE g l))`
  8.2134 +      MP_TAC THENL
  8.2135 +       [REPEAT STRIP_TAC THEN
  8.2136 +        ASM_CASES_TAC `!x y. (g:real^N->real^N) x = g y ==> x = y` THENL
  8.2137 +         [ALL_TAC;
  8.2138 +          ASM_MESON_TAC[NEGLIGIBLE_LINEAR_SINGULAR_IMAGE;
  8.2139 +                        NEGLIGIBLE_INTER]] THEN
  8.2140 +        MATCH_MP_TAC NEGLIGIBLE_SUBSET THEN
  8.2141 +        EXISTS_TAC `frontier(IMAGE (g:real^N->real^N) k \<inter> IMAGE g l) UNION
  8.2142 +                    interior(IMAGE g k \<inter> IMAGE g l)` THEN
  8.2143 +        CONJ_TAC THENL
  8.2144 +         [ALL_TAC;
  8.2145 +          REWRITE_TAC[frontier] THEN MATCH_MP_TAC(SET_RULE
  8.2146 +           `s \<subseteq> t ==> s \<subseteq> (t DIFF u) \<union> u`) THEN
  8.2147 +          REWRITE_TAC[CLOSURE_SUBSET]] THEN
  8.2148 +        MATCH_MP_TAC NEGLIGIBLE_UNION THEN CONJ_TAC THENL
  8.2149 +         [MATCH_MP_TAC NEGLIGIBLE_CONVEX_FRONTIER THEN
  8.2150 +          MATCH_MP_TAC CONVEX_INTER THEN CONJ_TAC THEN
  8.2151 +          MATCH_MP_TAC CONVEX_LINEAR_IMAGE THEN ASM_MESON_TAC[CONVEX_INTERVAL];
  8.2152 +          ALL_TAC] THEN
  8.2153 +        REWRITE_TAC[INTERIOR_INTER] THEN MATCH_MP_TAC NEGLIGIBLE_SUBSET THEN
  8.2154 +        EXISTS_TAC `IMAGE (g:real^N->real^N) (interior k) INTER
  8.2155 +                    IMAGE g (interior l)` THEN
  8.2156 +        CONJ_TAC THENL
  8.2157 +         [MATCH_MP_TAC NEGLIGIBLE_SUBSET THEN
  8.2158 +          EXISTS_TAC
  8.2159 +           `IMAGE (g:real^N->real^N) (interior k \<inter> interior l)` THEN
  8.2160 +          CONJ_TAC THENL
  8.2161 +           [ASM_SIMP_TAC[IMAGE_CLAUSES; NEGLIGIBLE_EMPTY]; SET_TAC[]];
  8.2162 +          MATCH_MP_TAC(SET_RULE
  8.2163 +           `s \<subseteq> u \<and> t \<subseteq> v ==> (s \<inter> t) \<subseteq> (u \<inter> v)`) THEN
  8.2164 +          CONJ_TAC THEN MATCH_MP_TAC INTERIOR_IMAGE_SUBSET THEN
  8.2165 +          ASM_MESON_TAC[LINEAR_CONTINUOUS_AT]];
  8.2166 +        ALL_TAC] THEN
  8.2167 +      DISCH_THEN(fun th -> MP_TAC(SPEC `f:real^N->real^N` th) THEN
  8.2168 +          MP_TAC(SPEC `\x:real^N. x` th)) THEN
  8.2169 +      ASM_REWRITE_TAC[LINEAR_ID; SET_RULE `IMAGE (\<lambda>x. x) s = s`] THEN
  8.2170 +      REPEAT STRIP_TAC THEN ASM_CASES_TAC `FINITE(d:(real^N->bool)->bool)` THENL
  8.2171 +       [MP_TAC(ISPECL [`IMAGE (f:real^N->real^N)`; `d:(real^N->bool)->bool`]
  8.2172 +                  HAS_GMEASURE_NEGLIGIBLE_UNIONS_IMAGE) THEN
  8.2173 +        ANTS_TAC THENL [ASM_MESON_TAC[measurable]; ALL_TAC] THEN
  8.2174 +        MATCH_MP_TAC EQ_IMP THEN AP_TERM_TAC THEN
  8.2175 +        MATCH_MP_TAC EQ_TRANS THEN
  8.2176 +        EXISTS_TAC `sum d (\<lambda>k:real^N->bool. m * gmeasure k)` THEN CONJ_TAC THENL
  8.2177 +         [MATCH_MP_TAC SUM_EQ THEN ASM_MESON_TAC[MEASURE_UNIQUE]; ALL_TAC] THEN
  8.2178 +        REWRITE_TAC[SUM_LMUL] THEN AP_TERM_TAC THEN
  8.2179 +        CONV_TAC SYM_CONV THEN MATCH_MP_TAC MEASURE_NEGLIGIBLE_UNIONS THEN
  8.2180 +        ASM_REWRITE_TAC[GSYM HAS_GMEASURE_MEASURE] THEN
  8.2181 +        ASM_MESON_TAC[MEASURABLE_INTERVAL];
  8.2182 +        ALL_TAC] THEN
  8.2183 +      MP_TAC(ISPEC `d:(real^N->bool)->bool` COUNTABLE_AS_INJECTIVE_IMAGE) THEN
  8.2184 +      ASM_REWRITE_TAC[INFINITE] THEN
  8.2185 +      DISCH_THEN(X_CHOOSE_THEN `s:num->real^N->bool`
  8.2186 +       (CONJUNCTS_THEN2 SUBST_ALL_TAC ASSUME_TAC)) THEN
  8.2187 +      MP_TAC(ISPEC `s:num->real^N->bool`
  8.2188 +        HAS_GMEASURE_COUNTABLE_NEGLIGIBLE_UNIONS_BOUNDED) THEN
  8.2189 +      MP_TAC(ISPEC `\n:num. IMAGE (f:real^N->real^N) (s n)`
  8.2190 +        HAS_GMEASURE_COUNTABLE_NEGLIGIBLE_UNIONS_BOUNDED) THEN
  8.2191 +      RULE_ASSUM_TAC(REWRITE_RULE[IMP_CONJ; RIGHT_FORALL_IMP_THM;
  8.2192 +                                  FORALL_IN_IMAGE; IN_UNIV]) THEN
  8.2193 +      RULE_ASSUM_TAC(REWRITE_RULE[IMP_IMP; RIGHT_IMP_FORALL_THM]) THEN
  8.2194 +      ASM_SIMP_TAC[] THEN ONCE_REWRITE_TAC[SIMPLE_IMAGE] THEN ANTS_TAC THENL
  8.2195 +       [REPEAT CONJ_TAC THENL
  8.2196 +         [ASM_MESON_TAC[MEASURABLE_LINEAR_IMAGE_INTERVAL];
  8.2197 +          ASM_MESON_TAC[];
  8.2198 +          ONCE_REWRITE_TAC[GSYM o_DEF] THEN
  8.2199 +          REWRITE_TAC[GSYM IMAGE_UNIONS; IMAGE_o] THEN
  8.2200 +          MATCH_MP_TAC BOUNDED_LINEAR_IMAGE THEN ASM_REWRITE_TAC[] THEN
  8.2201 +          MATCH_MP_TAC BOUNDED_SUBSET THEN REWRITE_TAC[UNIONS_SUBSET] THEN
  8.2202 +          EXISTS_TAC `interval[a:real^N,b]` THEN
  8.2203 +          REWRITE_TAC[BOUNDED_INTERVAL] THEN ASM SET_TAC[]];
  8.2204 +        ALL_TAC] THEN
  8.2205 +      STRIP_TAC THEN ANTS_TAC THENL
  8.2206 +       [REPEAT CONJ_TAC THENL
  8.2207 +         [ASM_MESON_TAC[MEASURABLE_INTERVAL];
  8.2208 +          ASM_MESON_TAC[];
  8.2209 +          MATCH_MP_TAC BOUNDED_SUBSET THEN REWRITE_TAC[UNIONS_SUBSET] THEN
  8.2210 +          EXISTS_TAC `interval[a:real^N,b]` THEN
  8.2211 +          REWRITE_TAC[BOUNDED_INTERVAL] THEN ASM SET_TAC[]];
  8.2212 +        ALL_TAC] THEN
  8.2213 +      STRIP_TAC THEN REWRITE_TAC[GSYM IMAGE_o; o_DEF] THEN
  8.2214 +      SUBGOAL_THEN `m * gmeasure (\<Union>(IMAGE s (:num)):real^N->bool) =
  8.2215 +             measure(\<Union>(IMAGE (\<lambda>x. IMAGE f (s x)) (:num)):real^N->bool)`
  8.2216 +       (fun th -> ASM_REWRITE_TAC[GSYM HAS_GMEASURE_MEASURE; th]) THEN
  8.2217 +      ONCE_REWRITE_TAC[GSYM LIFT_EQ] THEN
  8.2218 +      MATCH_MP_TAC SERIES_UNIQUE THEN
  8.2219 +      EXISTS_TAC `\n:num. lift(measure(IMAGE (f:real^N->real^N) (s n)))` THEN
  8.2220 +      EXISTS_TAC `from 0` THEN ASM_REWRITE_TAC[] THEN MATCH_MP_TAC SUMS_EQ THEN
  8.2221 +      EXISTS_TAC `\n:num. m % lift(measure(s n:real^N->bool))` THEN
  8.2222 +      CONJ_TAC THENL
  8.2223 +       [REWRITE_TAC[GSYM LIFT_CMUL; LIFT_EQ] THEN
  8.2224 +        ASM_MESON_TAC[MEASURE_UNIQUE];
  8.2225 +        REWRITE_TAC[LIFT_CMUL] THEN MATCH_MP_TAC SERIES_CMUL THEN
  8.2226 +        ASM_REWRITE_TAC[]];
  8.2227 +      ALL_TAC] THEN
  8.2228 +    REWRITE_TAC[HAS_GMEASURE_INNER_OUTER_LE] THEN CONJ_TAC THEN
  8.2229 +    X_GEN_TAC `e:real` THEN DISCH_TAC THENL
  8.2230 +     [MP_TAC(ISPECL [`{a..b} DIFF s:real^N->bool`; `a:real^N`;
  8.2231 +       `b:real^N`; `e / (1 + abs m)`] GMEASURABLE_OUTER_INTERVALS_BOUNDED) THEN
  8.2232 +      ANTS_TAC THENL
  8.2233 +       [ASM_SIMP_TAC[MEASURABLE_DIFF; GMEASURABLE_INTERVAL] THEN
  8.2234 +        ASM_SIMP_TAC[REAL_ARITH `0 < 1 + abs x`; REAL_LT_DIV] THEN SET_TAC[];
  8.2235 +        ALL_TAC] THEN
  8.2236 +      DISCH_THEN(X_CHOOSE_THEN `d:(real^N->bool)->bool` STRIP_ASSUME_TAC) THEN
  8.2237 +      EXISTS_TAC `IMAGE f {a..b} DIFF
  8.2238 +                  IMAGE (f:real^N->real^N) (\<Union>d)` THEN
  8.2239 +      FIRST_X_ASSUM(MP_TAC o SPEC `d:(real^N->bool)->bool`) THEN
  8.2240 +      ASM_SIMP_TAC[IMAGE_SUBSET] THEN DISCH_TAC THEN
  8.2241 +      CONJ_TAC THENL [ASM SET_TAC[]; ALL_TAC] THEN CONJ_TAC THENL
  8.2242 +       [ASM_MESON_TAC[MEASURABLE_DIFF; gmeasurable]; ALL_TAC] THEN
  8.2243 +      MATCH_MP_TAC REAL_LE_TRANS THEN
  8.2244 +      EXISTS_TAC `measure(IMAGE f {a..b}) -
  8.2245 +                  measure(IMAGE (f:real^N->real^N) (\<Union>d))` THEN
  8.2246 +      CONJ_TAC THENL
  8.2247 +       [ALL_TAC;
  8.2248 +        MATCH_MP_TAC REAL_EQ_IMP_LE THEN CONV_TAC SYM_CONV THEN
  8.2249 +        MATCH_MP_TAC MEASURE_DIFF_SUBSET THEN
  8.2250 +        REPEAT(CONJ_TAC THENL [ASM_MESON_TAC[measurable]; ALL_TAC]) THEN
  8.2251 +        MATCH_MP_TAC IMAGE_SUBSET THEN ASM_SIMP_TAC[UNIONS_SUBSET]] THEN
  8.2252 +      FIRST_ASSUM(ASSUME_TAC o SPECL [`a:real^N`; `b:real^N`]) THEN
  8.2253 +      REPEAT(FIRST_X_ASSUM(SUBST1_TAC o MATCH_MP MEASURE_UNIQUE)) THEN
  8.2254 +      MATCH_MP_TAC REAL_LE_TRANS THEN
  8.2255 +      EXISTS_TAC `m * measure(s:real^N->bool) - m * e / (1 + abs m)` THEN
  8.2256 +      CONJ_TAC THENL
  8.2257 +       [REWRITE_TAC[REAL_ARITH `a - x \<le> a - y \<longleftrightarrow> y \<le> x`] THEN
  8.2258 +        REWRITE_TAC[real_div; REAL_MUL_ASSOC] THEN
  8.2259 +        REWRITE_TAC[GSYM real_div] THEN
  8.2260 +        ASM_SIMP_TAC[REAL_LE_LDIV_EQ; REAL_ARITH `0 < 1 + abs x`] THEN
  8.2261 +        GEN_REWRITE_TAC RAND_CONV [REAL_MUL_SYM] THEN
  8.2262 +        ASM_SIMP_TAC[REAL_LE_RMUL_EQ] THEN REAL_ARITH_TAC;
  8.2263 +        ALL_TAC] THEN
  8.2264 +      REWRITE_TAC[GSYM REAL_SUB_LDISTRIB] THEN MATCH_MP_TAC REAL_LE_LMUL THEN
  8.2265 +      ASM_REWRITE_TAC[] THEN
  8.2266 +      FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP (REAL_ARITH
  8.2267 +        `d \<le> a + e ==> a = i - s ==> s - e \<le> i - d`)) THEN
  8.2268 +      MATCH_MP_TAC MEASURE_DIFF_SUBSET THEN
  8.2269 +      ASM_REWRITE_TAC[MEASURABLE_INTERVAL];
  8.2270 +      MP_TAC(ISPECL [`s:real^N->bool`; `a:real^N`; `b:real^N`;
  8.2271 +                `e / (1 + abs m)`] GMEASURABLE_OUTER_INTERVALS_BOUNDED) THEN
  8.2272 +      ASM_SIMP_TAC[REAL_LT_DIV; REAL_ARITH `0 < 1 + abs x`] THEN
  8.2273 +      DISCH_THEN(X_CHOOSE_THEN `d:(real^N->bool)->bool` STRIP_ASSUME_TAC) THEN
  8.2274 +      EXISTS_TAC `IMAGE (f:real^N->real^N) (\<Union>d)` THEN
  8.2275 +      FIRST_X_ASSUM(MP_TAC o SPEC `d:(real^N->bool)->bool`) THEN
  8.2276 +      ASM_SIMP_TAC[IMAGE_SUBSET] THEN
  8.2277 +      SIMP_TAC[HAS_GMEASURE_MEASURABLE_MEASURE] THEN STRIP_TAC THEN
  8.2278 +      MATCH_MP_TAC REAL_LE_TRANS THEN
  8.2279 +      EXISTS_TAC `m * measure(s:real^N->bool) + m * e / (1 + abs m)` THEN
  8.2280 +      CONJ_TAC THENL
  8.2281 +       [REWRITE_TAC[GSYM REAL_ADD_LDISTRIB] THEN ASM_SIMP_TAC[REAL_LE_LMUL];
  8.2282 +        REWRITE_TAC[REAL_LE_LADD] THEN
  8.2283 +        REWRITE_TAC[real_div; REAL_MUL_ASSOC] THEN
  8.2284 +        REWRITE_TAC[GSYM real_div] THEN
  8.2285 +        ASM_SIMP_TAC[REAL_LE_LDIV_EQ; REAL_ARITH `0 < 1 + abs x`] THEN
  8.2286 +        GEN_REWRITE_TAC RAND_CONV [REAL_MUL_SYM] THEN
  8.2287 +        ASM_SIMP_TAC[REAL_LE_RMUL_EQ] THEN REAL_ARITH_TAC]];
  8.2288 +      ALL_TAC] THEN
  8.2289 +  REPEAT STRIP_TAC THEN ONCE_REWRITE_TAC[HAS_GMEASURE_LIMIT] THEN
  8.2290 +  X_GEN_TAC `e:real` THEN DISCH_TAC THEN
  8.2291 +  FIRST_ASSUM(MP_TAC o GEN_REWRITE_RULE I [HAS_GMEASURE_MEASURE]) THEN
  8.2292 +  GEN_REWRITE_TAC LAND_CONV [HAS_GMEASURE_LIMIT] THEN
  8.2293 +  DISCH_THEN(MP_TAC o SPEC `e / (1 + abs m)`) THEN
  8.2294 +  ASM_SIMP_TAC[REAL_LT_DIV; REAL_ARITH `0 < 1 + abs x`] THEN
  8.2295 +  DISCH_THEN(X_CHOOSE_THEN `B:real`
  8.2296 +   (CONJUNCTS_THEN2 ASSUME_TAC (LABEL_TAC "*"))) THEN
  8.2297 +  MP_TAC(ISPEC `ball(0:real^N,B)` BOUNDED_SUBSET_CLOSED_INTERVAL) THEN
  8.2298 +  REWRITE_TAC[BOUNDED_BALL; LEFT_IMP_EXISTS_THM] THEN
  8.2299 +  REMOVE_THEN "*" MP_TAC THEN
  8.2300 +  MATCH_MP_TAC MONO_FORALL THEN X_GEN_TAC `c:real^N` THEN
  8.2301 +  MATCH_MP_TAC MONO_FORALL THEN X_GEN_TAC `d:real^N` THEN
  8.2302 +  DISCH_THEN(fun th -> DISCH_TAC THEN MP_TAC th) THEN ASM_REWRITE_TAC[] THEN
  8.2303 +  DISCH_THEN(X_CHOOSE_THEN `z:real` STRIP_ASSUME_TAC) THEN
  8.2304 +  MP_TAC(ISPECL [`interval[c:real^N,d]`; `0:real^N`]
  8.2305 +    BOUNDED_SUBSET_BALL) THEN
  8.2306 +  REWRITE_TAC[BOUNDED_INTERVAL] THEN
  8.2307 +  DISCH_THEN(X_CHOOSE_THEN `D:real` STRIP_ASSUME_TAC) THEN
  8.2308 +  MP_TAC(ISPEC `f:real^N->real^N` LINEAR_BOUNDED_POS) THEN
  8.2309 +  ASM_REWRITE_TAC[] THEN
  8.2310 +  DISCH_THEN(X_CHOOSE_THEN `C:real` STRIP_ASSUME_TAC) THEN
  8.2311 +
  8.2312 +  EXISTS_TAC `D * C` THEN ASM_SIMP_TAC[REAL_LT_MUL] THEN
  8.2313 +  MAP_EVERY X_GEN_TAC [`a:real^N`; `b:real^N`] THEN DISCH_TAC THEN
  8.2314 +  FIRST_X_ASSUM(MP_TAC o SPEC
  8.2315 +   `s \<inter> (IMAGE (h:real^N->real^N) {a..b})`) THEN
  8.2316 +  SUBGOAL_THEN
  8.2317 +   `IMAGE (f:real^N->real^N) (s \<inter> IMAGE h (interval [a,b])) =
  8.2318 +    (IMAGE f s) \<inter> {a..b}`
  8.2319 +  SUBST1_TAC THENL [ASM SET_TAC[]; ALL_TAC] THEN ANTS_TAC THENL
  8.2320 +   [ASM_SIMP_TAC[BOUNDED_INTER; BOUNDED_LINEAR_IMAGE; BOUNDED_INTERVAL] THEN
  8.2321 +    ASM_SIMP_TAC[MEASURABLE_INTER; GMEASURABLE_LINEAR_IMAGE_INTERVAL];
  8.2322 +    ALL_TAC] THEN
  8.2323 +  DISCH_TAC THEN EXISTS_TAC
  8.2324 +   `m * measure(s \<inter> (IMAGE (h:real^N->real^N) {a..b}))` THEN
  8.2325 +  ASM_REWRITE_TAC[] THEN
  8.2326 +  MATCH_MP_TAC REAL_LET_TRANS THEN EXISTS_TAC `m * e / (1 + abs m)` THEN
  8.2327 +  CONJ_TAC THENL
  8.2328 +   [ALL_TAC;
  8.2329 +    REWRITE_TAC[real_div; REAL_MUL_ASSOC] THEN REWRITE_TAC[GSYM real_div] THEN
  8.2330 +    ASM_SIMP_TAC[REAL_LT_LDIV_EQ; REAL_ARITH `0 < 1 + abs x`] THEN
  8.2331 +    GEN_REWRITE_TAC RAND_CONV [REAL_MUL_SYM] THEN
  8.2332 +    ASM_SIMP_TAC[REAL_LT_RMUL_EQ] THEN REAL_ARITH_TAC] THEN
  8.2333 +  REWRITE_TAC[GSYM REAL_SUB_LDISTRIB; REAL_ABS_MUL] THEN
  8.2334 +  GEN_REWRITE_TAC (LAND_CONV o LAND_CONV) [real_abs] THEN
  8.2335 +  ASM_REWRITE_TAC[] THEN MATCH_MP_TAC REAL_LE_LMUL THEN ASM_REWRITE_TAC[] THEN
  8.2336 +  FIRST_ASSUM(MATCH_MP_TAC o MATCH_MP (REAL_ARITH
  8.2337 +   `abs(z - m) < e ==> z \<le> w \<and> w \<le> m ==> abs(w - m) \<le> e`)) THEN
  8.2338 +  SUBST1_TAC(SYM(MATCH_MP MEASURE_UNIQUE
  8.2339 +   (ASSUME `s \<inter> interval [c:real^N,d] has_gmeasure z`))) THEN
  8.2340 +  CONJ_TAC THEN MATCH_MP_TAC MEASURE_SUBSET THEN
  8.2341 +  ASM_SIMP_TAC[MEASURABLE_INTER; GMEASURABLE_LINEAR_IMAGE_INTERVAL;
  8.2342 +               GMEASURABLE_INTERVAL; INTER_SUBSET] THEN
  8.2343 +  MATCH_MP_TAC(SET_RULE
  8.2344 +   `!v. t \<subseteq> v \<and> v \<subseteq> u ==> s \<inter> t \<subseteq> s \<inter> u`) THEN
  8.2345 +  EXISTS_TAC `ball(0:real^N,D)` THEN ASM_REWRITE_TAC[] THEN
  8.2346 +  MATCH_MP_TAC(SET_RULE
  8.2347 +   `!f. (!x. h(f x) = x) \<and> IMAGE f s \<subseteq> t ==> s \<subseteq> IMAGE h t`) THEN
  8.2348 +  EXISTS_TAC `f:real^N->real^N` THEN ASM_REWRITE_TAC[] THEN
  8.2349 +  MATCH_MP_TAC SUBSET_TRANS THEN EXISTS_TAC `ball(0:real^N,D * C)` THEN
  8.2350 +  ASM_REWRITE_TAC[SUBSET; FORALL_IN_IMAGE; IN_BALL_0] THEN
  8.2351 +  X_GEN_TAC `x:real^N` THEN DISCH_TAC THEN
  8.2352 +  MATCH_MP_TAC REAL_LET_TRANS THEN EXISTS_TAC `C * norm(x:real^N)` THEN
  8.2353 +  ASM_REWRITE_TAC[] THEN GEN_REWRITE_TAC RAND_CONV [REAL_MUL_SYM] THEN
  8.2354 +  ASM_SIMP_TAC[REAL_LT_LMUL_EQ]);; *)
  8.2355 +
  8.2356 +(* ------------------------------------------------------------------------- *)
  8.2357 +(* Some inductions by expressing mapping in terms of elementary matrices.    *)
  8.2358 +(* ------------------------------------------------------------------------- *)
  8.2359 +
  8.2360 +lemma INDUCT_MATRIX_ROW_OPERATIONS: True .. (*
  8.2361 + "!P:real^N^N->bool.
  8.2362 +        (!A i. 1 \<le> i \<and> i \<le> dimindex(:N) \<and> row i A = 0 ==> P A) \<and>
  8.2363 +        (!A. (!i j. 1 \<le> i \<and> i \<le> dimindex(:N) \<and>
  8.2364 +                    1 \<le> j \<and> j \<le> dimindex(:N) \<and> ~(i = j)
  8.2365 +                    ==> A$i$j = 0) ==> P A) \<and>
  8.2366 +        (!A m n. P A \<and> 1 \<le> m \<and> m \<le> dimindex(:N) \<and>
  8.2367 +                 1 \<le> n \<and> n \<le> dimindex(:N) \<and> ~(m = n)
  8.2368 +                 ==> P(lambda i j. A$i$(swap(m,n) j))) \<and>
  8.2369 +        (!A m n c. P A \<and> 1 \<le> m \<and> m \<le> dimindex(:N) \<and>
  8.2370 +                   1 \<le> n \<and> n \<le> dimindex(:N) \<and> ~(m = n)
  8.2371 +                   ==> P(lambda i. if i = m then row m A + c % row n A
  8.2372 +                                   else row i A))
  8.2373 +        ==> !A. P A"
  8.2374 +qed   GEN_TAC THEN
  8.2375 +  DISCH_THEN(CONJUNCTS_THEN2 (LABEL_TAC "zero_row") MP_TAC) THEN
  8.2376 +  DISCH_THEN(CONJUNCTS_THEN2 (LABEL_TAC "diagonal") MP_TAC) THEN
  8.2377 +  DISCH_THEN(CONJUNCTS_THEN2 (LABEL_TAC "swap_cols") (LABEL_TAC "row_op")) THEN
  8.2378 +  SUBGOAL_THEN
  8.2379 +   `!k A:real^N^N.
  8.2380 +        (!i j. 1 \<le> i \<and> i \<le> dimindex(:N) \<and>
  8.2381 +               k \<le> j \<and> j \<le> dimindex(:N) \<and> ~(i = j)
  8.2382 +               ==> A$i$j = 0)
  8.2383 +        ==> P A`
  8.2384 +   (fun th -> GEN_TAC THEN MATCH_MP_TAC th THEN
  8.2385 +              EXISTS_TAC `dimindex(:N) + 1` THEN ARITH_TAC) THEN
  8.2386 +  MATCH_MP_TAC num_INDUCTION THEN CONJ_TAC THENL
  8.2387 +   [REPEAT STRIP_TAC THEN USE_THEN "diagonal" MATCH_MP_TAC THEN
  8.2388 +    REPEAT STRIP_TAC THEN FIRST_X_ASSUM MATCH_MP_TAC THEN
  8.2389 +    ASM_REWRITE_TAC[LE_0];
  8.2390 +    ALL_TAC] THEN
  8.2391 +  X_GEN_TAC `k:num` THEN DISCH_THEN(LABEL_TAC "ind_hyp") THEN
  8.2392 +  DISJ_CASES_THEN2 SUBST1_TAC ASSUME_TAC (ARITH_RULE `k = 0 \/ 1 \<le> k`) THEN
  8.2393 +  ASM_REWRITE_TAC[ARITH] THEN
  8.2394 +  ASM_CASES_TAC `k \<le> dimindex(:N)` THENL
  8.2395 +   [ALL_TAC;
  8.2396 +    REPEAT STRIP_TAC THEN REMOVE_THEN "ind_hyp" MATCH_MP_TAC THEN
  8.2397 +    ASM_ARITH_TAC] THEN
  8.2398 +  SUBGOAL_THEN
  8.2399 +   `!A:real^N^N.
  8.2400 +        ~(A$k$k = 0) \<and>
  8.2401 +        (!i j. 1 \<le> i \<and> i \<le> dimindex (:N) \<and>
  8.2402 +               SUC k \<le> j \<and> j \<le> dimindex (:N) \<and> ~(i = j)
  8.2403 +               ==> A$i$j = 0)
  8.2404 +        ==> P A`
  8.2405 +  (LABEL_TAC "nonzero_hyp") THENL
  8.2406 +   [ALL_TAC;
  8.2407 +    X_GEN_TAC `A:real^N^N` THEN DISCH_TAC THEN
  8.2408 +    ASM_CASES_TAC `row k (A:real^N^N) = 0` THENL
  8.2409 +     [REMOVE_THEN "zero_row" MATCH_MP_TAC THEN ASM_MESON_TAC[];
  8.2410 +      ALL_TAC] THEN
  8.2411 +    FIRST_ASSUM(MP_TAC o GEN_REWRITE_RULE RAND_CONV [CART_EQ]) THEN
  8.2412 +    SIMP_TAC[VEC_COMPONENT; row; LAMBDA_BETA] THEN
  8.2413 +    REWRITE_TAC[NOT_FORALL_THM; NOT_IMP; LEFT_IMP_EXISTS_THM] THEN
  8.2414 +    X_GEN_TAC `l:num` THEN STRIP_TAC THEN
  8.2415 +    ASM_CASES_TAC `l:num = k` THENL
  8.2416 +     [REMOVE_THEN "nonzero_hyp" MATCH_MP_TAC THEN ASM_MESON_TAC[];
  8.2417 +      ALL_TAC] THEN
  8.2418 +    REMOVE_THEN "swap_cols" (MP_TAC o SPECL
  8.2419 +     [`(lambda i j. (A:real^N^N)$i$swap(k,l) j):real^N^N`;
  8.2420 +      `k:num`; `l:num`]) THEN
  8.2421 +    ASM_SIMP_TAC[LAMBDA_BETA] THEN ANTS_TAC THENL
  8.2422 +     [ALL_TAC;
  8.2423 +      MATCH_MP_TAC EQ_IMP THEN AP_TERM_TAC THEN
  8.2424 +      SIMP_TAC[CART_EQ; LAMBDA_BETA] THEN
  8.2425 +      REPEAT STRIP_TAC THEN REWRITE_TAC[swap] THEN
  8.2426 +      REPEAT(COND_CASES_TAC THEN ASM_SIMP_TAC[LAMBDA_BETA])] THEN
  8.2427 +    REMOVE_THEN "nonzero_hyp" MATCH_MP_TAC THEN
  8.2428 +    ONCE_REWRITE_TAC[ARITH_RULE `SUC k \<le> i \<longleftrightarrow> 1 \<le> i \<and> SUC k \<le> i`] THEN
  8.2429 +    ASM_SIMP_TAC[LAMBDA_BETA] THEN
  8.2430 +    ASM_REWRITE_TAC[swap] THEN MAP_EVERY X_GEN_TAC [`i:num`; `j:num`] THEN
  8.2431 +    STRIP_TAC THEN SUBGOAL_THEN `l:num \<le> k` ASSUME_TAC THENL
  8.2432 +     [FIRST_X_ASSUM(MP_TAC o SPECL [`k:num`; `l:num`]) THEN
  8.2433 +      ASM_REWRITE_TAC[] THEN ARITH_TAC;
  8.2434 +      ALL_TAC] THEN
  8.2435 +    REPEAT(COND_CASES_TAC THEN ASM_REWRITE_TAC[]) THEN
  8.2436 +    FIRST_X_ASSUM MATCH_MP_TAC THEN ASM_REWRITE_TAC[] THEN
  8.2437 +    ASM_ARITH_TAC] THEN
  8.2438 +   SUBGOAL_THEN
  8.2439 +   `!l A:real^N^N.
  8.2440 +        ~(A$k$k = 0) \<and>
  8.2441 +        (!i j. 1 \<le> i \<and> i \<le> dimindex (:N) \<and>
  8.2442 +               SUC k \<le> j \<and> j \<le> dimindex (:N) \<and> ~(i = j)
  8.2443 +               ==> A$i$j = 0) \<and>
  8.2444 +        (!i. l \<le> i \<and> i \<le> dimindex(:N) \<and> ~(i = k) ==> A$i$k = 0)
  8.2445 +        ==> P A`
  8.2446 +   MP_TAC THENL
  8.2447 +    [ALL_TAC;
  8.2448 +     DISCH_THEN(MP_TAC o SPEC `dimindex(:N) + 1`) THEN
  8.2449 +     REWRITE_TAC[CONJ_ASSOC; ARITH_RULE `~(n + 1 \<le> i \<and> i \<le> n)`]] THEN
  8.2450 +   MATCH_MP_TAC num_INDUCTION THEN CONJ_TAC THENL
  8.2451 +    [GEN_TAC THEN DISCH_THEN(CONJUNCTS_THEN2 ASSUME_TAC MP_TAC) THEN
  8.2452 +     DISCH_THEN(CONJUNCTS_THEN2 (LABEL_TAC "main") (LABEL_TAC "aux")) THEN
  8.2453 +     USE_THEN "ind_hyp" MATCH_MP_TAC THEN
  8.2454 +     MAP_EVERY X_GEN_TAC [`i:num`; `j:num`] THEN STRIP_TAC THEN
  8.2455 +     ASM_CASES_TAC `j:num = k` THENL
  8.2456 +      [ASM_REWRITE_TAC[] THEN USE_THEN "aux" MATCH_MP_TAC THEN ASM_ARITH_TAC;
  8.2457 +       REMOVE_THEN "main" MATCH_MP_TAC THEN ASM_ARITH_TAC];
  8.2458 +    ALL_TAC] THEN
  8.2459 +  X_GEN_TAC `l:num` THEN DISCH_THEN(LABEL_TAC "inner_hyp") THEN
  8.2460 +  GEN_TAC THEN DISCH_THEN(CONJUNCTS_THEN2 ASSUME_TAC MP_TAC) THEN
  8.2461 +  DISCH_THEN(CONJUNCTS_THEN2 (LABEL_TAC "main") (LABEL_TAC "aux")) THEN
  8.2462 +  ASM_CASES_TAC `l:num = k` THENL
  8.2463 +   [REMOVE_THEN "inner_hyp" MATCH_MP_TAC THEN ASM_REWRITE_TAC[] THEN
  8.2464 +    REPEAT STRIP_TAC THEN REMOVE_THEN "aux" MATCH_MP_TAC THEN ASM_ARITH_TAC;
  8.2465 +    ALL_TAC] THEN
  8.2466 +  DISJ_CASES_TAC(ARITH_RULE `l = 0 \/ 1 \<le> l`) THENL
  8.2467 +   [REMOVE_THEN "ind_hyp" MATCH_MP_TAC THEN
  8.2468 +    MAP_EVERY X_GEN_TAC [`i:num`; `j:num`] THEN STRIP_TAC THEN
  8.2469 +    ASM_CASES_TAC `j:num = k` THENL
  8.2470 +     [ASM_REWRITE_TAC[] THEN REMOVE_THEN "aux" MATCH_MP_TAC THEN ASM_ARITH_TAC;
  8.2471 +      REMOVE_THEN "main" MATCH_MP_TAC THEN ASM_ARITH_TAC];
  8.2472 +    ALL_TAC] THEN
  8.2473 +  ASM_CASES_TAC `l \<le> dimindex(:N)` THENL
  8.2474 +   [ALL_TAC;
  8.2475 +    REMOVE_THEN "inner_hyp" MATCH_MP_TAC THEN ASM_REWRITE_TAC[] THEN
  8.2476 +    ASM_ARITH_TAC] THEN
  8.2477 +  REMOVE_THEN "inner_hyp" (MP_TAC o SPECL
  8.2478 +   [`(lambda i. if i = l then row l (A:real^N^N) + --(A$l$k/A$k$k) % row k A
  8.2479 +                else row i A):real^N^N`]) THEN
  8.2480 +  ANTS_TAC THENL
  8.2481 +   [SUBGOAL_THEN `!i. l \<le> i ==> 1 \<le> i` ASSUME_TAC THENL
  8.2482 +     [ASM_ARITH_TAC; ALL_TAC] THEN
  8.2483 +    ONCE_REWRITE_TAC[ARITH_RULE `SUC k \<le> j \<longleftrightarrow> 1 \<le> j \<and> SUC k \<le> j`] THEN
  8.2484 +    ASM_SIMP_TAC[LAMBDA_BETA; row; COND_COMPONENT;
  8.2485 +                 VECTOR_ADD_COMPONENT; VECTOR_MUL_COMPONENT] THEN
  8.2486 +    ASM_SIMP_TAC[REAL_FIELD `~(y = 0) ==> x + --(x / y) * y = 0`] THEN
  8.2487 +    REWRITE_TAC[AND_FORALL_THM] THEN X_GEN_TAC `i:num` THEN
  8.2488 +    ASM_CASES_TAC `i:num = l` THEN ASM_REWRITE_TAC[] THENL
  8.2489 +     [REPEAT STRIP_TAC THEN
  8.2490 +      MATCH_MP_TAC(REAL_RING `x = 0 \<and> y = 0 ==> x + z * y = 0`) THEN
  8.2491 +      CONJ_TAC THEN FIRST_X_ASSUM MATCH_MP_TAC THEN ASM_ARITH_TAC;
  8.2492 +      REPEAT STRIP_TAC THEN REMOVE_THEN "aux" MATCH_MP_TAC THEN ASM_ARITH_TAC];
  8.2493 +    ALL_TAC] THEN
  8.2494 +  DISCH_TAC THEN REMOVE_THEN "row_op" (MP_TAC o SPECL
  8.2495 +   [`(lambda i. if i = l then row l A + --(A$l$k / A$k$k) % row k A
  8.2496 +                else row i (A:real^N^N)):real^N^N`;
  8.2497 +    `l:num`; `k:num`; `(A:real^N^N)$l$k / A$k$k`]) THEN
  8.2498 +  ASM_REWRITE_TAC[] THEN MATCH_MP_TAC EQ_IMP THEN AP_TERM_TAC THEN
  8.2499 +  ASM_SIMP_TAC[CART_EQ; LAMBDA_BETA; VECTOR_ADD_COMPONENT;
  8.2500 +               VECTOR_MUL_COMPONENT; row; COND_COMPONENT] THEN
  8.2501 +  REPEAT STRIP_TAC THEN COND_CASES_TAC THEN ASM_REWRITE_TAC[] THEN
  8.2502 +  REAL_ARITH_TAC);; *)
  8.2503 +
  8.2504 +lemma INDUCT_MATRIX_ELEMENTARY: True .. (*
  8.2505 + "!P:real^N^N->bool.
  8.2506 +        (!A B. P A \<and> P B ==> P(A ** B)) \<and>
  8.2507 +        (!A i. 1 \<le> i \<and> i \<le> dimindex(:N) \<and> row i A = 0 ==> P A) \<and>
  8.2508 +        (!A. (!i j. 1 \<le> i \<and> i \<le> dimindex(:N) \<and>
  8.2509 +                    1 \<le> j \<and> j \<le> dimindex(:N) \<and> ~(i = j)
  8.2510 +                    ==> A$i$j = 0) ==> P A) \<and>
  8.2511 +        (!m n. 1 \<le> m \<and> m \<le> dimindex(:N) \<and>
  8.2512 +               1 \<le> n \<and> n \<le> dimindex(:N) \<and> ~(m = n)
  8.2513 +               ==> P(lambda i j. (mat 1:real^N^N)$i$(swap(m,n) j))) \<and>
  8.2514 +        (!m n c. 1 \<le> m \<and> m \<le> dimindex(:N) \<and>
  8.2515 +                 1 \<le> n \<and> n \<le> dimindex(:N) \<and> ~(m = n)
  8.2516 +                 ==> P(lambda i j. if i = m \<and> j = n then c
  8.2517 +                                   else if i = j then 1 else 0))
  8.2518 +        ==> !A. P A"
  8.2519 +qed   GEN_TAC THEN
  8.2520 +  DISCH_THEN(CONJUNCTS_THEN2 ASSUME_TAC MP_TAC) THEN
  8.2521 +  DISCH_THEN(fun th ->
  8.2522 +    MATCH_MP_TAC INDUCT_MATRIX_ROW_OPERATIONS THEN MP_TAC th) THEN
  8.2523 +  REPEAT(MATCH_MP_TAC MONO_AND THEN CONJ_TAC) THEN REWRITE_TAC[] THEN
  8.2524 +  DISCH_THEN(fun th -> X_GEN_TAC `A:real^N^N` THEN MP_TAC th) THEN
  8.2525 +  REPEAT(MATCH_MP_TAC MONO_FORALL THEN GEN_TAC) THEN
  8.2526 +  DISCH_THEN(fun th -> STRIP_TAC THEN MP_TAC th) THEN ASM_REWRITE_TAC[] THEN
  8.2527 +  UNDISCH_TAC `(P:real^N^N->bool) A` THENL
  8.2528 +   [REWRITE_TAC[GSYM IMP_CONJ]; REWRITE_TAC[GSYM IMP_CONJ_ALT]] THEN
  8.2529 +  DISCH_THEN(ANTE_RES_THEN MP_TAC) THEN MATCH_MP_TAC EQ_IMP THEN
  8.2530 +  AP_TERM_TAC THEN REWRITE_TAC[CART_EQ] THEN
  8.2531 +  X_GEN_TAC `i:num` THEN STRIP_TAC THEN
  8.2532 +  X_GEN_TAC `j:num` THEN STRIP_TAC THEN
  8.2533 +  ASM_SIMP_TAC[CART_EQ; LAMBDA_BETA; matrix_mul; row] THENL
  8.2534 +   [ASM_SIMP_TAC[mat; IN_DIMINDEX_SWAP; LAMBDA_BETA] THEN
  8.2535 +    ONCE_REWRITE_TAC[COND_RAND] THEN
  8.2536 +    SIMP_TAC[SUM_DELTA; REAL_MUL_RZERO; REAL_MUL_RID] THEN
  8.2537 +    COND_CASES_TAC THEN REWRITE_TAC[] THEN
  8.2538 +    RULE_ASSUM_TAC(REWRITE_RULE[swap; IN_NUMSEG]) THEN ASM_ARITH_TAC;
  8.2539 +    ALL_TAC] THEN
  8.2540 +  ASM_CASES_TAC `i:num = m` THEN ASM_REWRITE_TAC[] THENL
  8.2541 +   [ALL_TAC;
  8.2542 +    ONCE_REWRITE_TAC[COND_RAND] THEN ONCE_REWRITE_TAC[COND_RATOR] THEN
  8.2543 +    REWRITE_TAC[REAL_MUL_LZERO] THEN
  8.2544 +    GEN_REWRITE_TAC (LAND_CONV o ONCE_DEPTH_CONV) [EQ_SYM_EQ] THEN
  8.2545 +    ASM_SIMP_TAC[SUM_DELTA; LAMBDA_BETA; IN_NUMSEG; REAL_MUL_LID]] THEN
  8.2546 +  ASM_SIMP_TAC[VECTOR_ADD_COMPONENT; VECTOR_MUL_COMPONENT; LAMBDA_BETA] THEN
  8.2547 +  MATCH_MP_TAC EQ_TRANS THEN
  8.2548 +  EXISTS_TAC
  8.2549 +    `sum {m,n} (\<lambda>k. (if k = n then c else if m = k then 1 else 0) *
  8.2550 +                    (A:real^N^N)$k$j)` THEN
  8.2551 +  CONJ_TAC THENL
  8.2552 +   [MATCH_MP_TAC SUM_SUPERSET THEN
  8.2553 +    ASM_SIMP_TAC[SUBSET; IN_INSERT; NOT_IN_EMPTY; DE_MORGAN_THM;
  8.2554 +                 IN_NUMSEG; REAL_MUL_LZERO] THEN
  8.2555 +    ASM_ARITH_TAC;
  8.2556 +    ASM_SIMP_TAC[SUM_CLAUSES; FINITE_RULES; IN_INSERT; NOT_IN_EMPTY] THEN
  8.2557 +    REAL_ARITH_TAC]);; *)
  8.2558 +
  8.2559 +lemma INDUCT_MATRIX_ELEMENTARY_ALT: True .. (*
  8.2560 + "!P:real^N^N->bool.
  8.2561 +        (!A B. P A \<and> P B ==> P(A ** B)) \<and>
  8.2562 +        (!A i. 1 \<le> i \<and> i \<le> dimindex(:N) \<and> row i A = 0 ==> P A) \<and>
  8.2563 +        (!A. (!i j. 1 \<le> i \<and> i \<le> dimindex(:N) \<and>
  8.2564 +                    1 \<le> j \<and> j \<le> dimindex(:N) \<and> ~(i = j)
  8.2565 +                    ==> A$i$j = 0) ==> P A) \<and>
  8.2566 +        (!m n. 1 \<le> m \<and> m \<le> dimindex(:N) \<and>
  8.2567 +               1 \<le> n \<and> n \<le> dimindex(:N) \<and> ~(m = n)
  8.2568 +               ==> P(lambda i j. (mat 1:real^N^N)$i$(swap(m,n) j))) \<and>
  8.2569 +        (!m n. 1 \<le> m \<and> m \<le> dimindex(:N) \<and>
  8.2570 +               1 \<le> n \<and> n \<le> dimindex(:N) \<and> ~(m = n)
  8.2571 +               ==> P(lambda i j. if i = m \<and> j = n \/ i = j then 1 else 0))
  8.2572 +        ==> !A. P A"
  8.2573 +qed   GEN_TAC THEN STRIP_TAC THEN MATCH_MP_TAC INDUCT_MATRIX_ELEMENTARY THEN
  8.2574 +  ASM_REWRITE_TAC[] THEN REPEAT STRIP_TAC THEN
  8.2575 +  ASM_CASES_TAC `c = 0` THENL
  8.2576 +   [FIRST_X_ASSUM(fun th -> MATCH_MP_TAC th THEN
  8.2577 +        MAP_EVERY X_GEN_TAC [`i:num`; `j:num`]) THEN
  8.2578 +    ASM_SIMP_TAC[LAMBDA_BETA; COND_ID];
  8.2579 +    ALL_TAC] THEN
  8.2580 +  SUBGOAL_THEN
  8.2581 +   `(lambda i j. if i = m \<and> j = n then c else if i = j then 1 else 0) =
  8.2582 +  ((lambda i j. if i = j then if j = n then inv c else 1 else 0):real^N^N) **
  8.2583 +    ((lambda i j. if i = m \<and> j = n \/ i = j then 1 else 0):real^N^N) **
  8.2584 +    ((lambda i j. if i = j then if j = n then c else 1 else 0):real^N^N)`
  8.2585 +  SUBST1_TAC THENL
  8.2586 +   [ALL_TAC;
  8.2587 +    REPEAT(MATCH_MP_TAC(ASSUME `!A B:real^N^N. P A \<and> P B ==> P(A ** B)`) THEN
  8.2588 +           CONJ_TAC) THEN
  8.2589 +    ASM_SIMP_TAC[] THEN FIRST_X_ASSUM(fun th -> MATCH_MP_TAC th THEN
  8.2590 +        MAP_EVERY X_GEN_TAC [`i:num`; `j:num`]) THEN
  8.2591 +    ASM_SIMP_TAC[LAMBDA_BETA]] THEN
  8.2592 +  SIMP_TAC[CART_EQ; matrix_mul; LAMBDA_BETA] THEN
  8.2593 +  X_GEN_TAC `i:num` THEN STRIP_TAC THEN
  8.2594 +  X_GEN_TAC `j:num` THEN STRIP_TAC THEN
  8.2595 +  ASM_SIMP_TAC[SUM_DELTA; IN_NUMSEG; REAL_ARITH
  8.2596 +       `(if p then 1 else 0) * (if q then c else 0) =
  8.2597 +        if q then if p then c else 0 else 0`] THEN
  8.2598 +  REWRITE_TAC[REAL_ARITH
  8.2599 +   `(if p then x else 0) * y = (if p then x * y else 0)`] THEN
  8.2600 +  GEN_REWRITE_TAC (RAND_CONV o ONCE_DEPTH_CONV) [EQ_SYM_EQ] THEN
  8.2601 +  ASM_SIMP_TAC[SUM_DELTA; IN_NUMSEG] THEN
  8.2602 +  ASM_CASES_TAC `i:num = m` THEN ASM_REWRITE_TAC[] THEN
  8.2603 +  ASM_CASES_TAC `j:num = n` THEN ASM_REWRITE_TAC[REAL_MUL_LID; EQ_SYM_EQ] THEN
  8.2604 +  ASM_CASES_TAC `i:num = n` THEN
  8.2605 +  ASM_SIMP_TAC[REAL_MUL_LINV; REAL_MUL_LID; REAL_MUL_RZERO]);; *)
  8.2606 +
  8.2607 +(* ------------------------------------------------------------------------- *)
  8.2608 +(* The same thing in mapping form (might have been easier all along).        *)
  8.2609 +(* ------------------------------------------------------------------------- *)
  8.2610 +
  8.2611 +lemma INDUCT_LINEAR_ELEMENTARY: True .. (*
  8.2612 + "!P. (!f g. linear f \<and> linear g \<and> P f \<and> P g ==> P(f o g)) \<and>
  8.2613 +       (!f i. linear f \<and> 1 \<le> i \<and> i \<le> dimindex(:N) \<and> (!x. (f x)$i = 0)
  8.2614 +              ==> P f) \<and>
  8.2615 +       (!c. P(\<lambda>x. lambda i. c i * x$i)) \<and>
  8.2616 +       (!m n. 1 \<le> m \<and> m \<le> dimindex(:N) \<and>
  8.2617 +              1 \<le> n \<and> n \<le> dimindex(:N) \<and> ~(m = n)
  8.2618 +              ==> P(\<lambda>x. lambda i. x$swap(m,n) i)) \<and>
  8.2619 +       (!m n. 1 \<le> m \<and> m \<le> dimindex(:N) \<and>
  8.2620 +              1 \<le> n \<and> n \<le> dimindex(:N) \<and> ~(m = n)
  8.2621 +              ==> P(\<lambda>x. lambda i. if i = m then x$m + x$n else x$i))
  8.2622 +       ==> !f:real^N->real^N. linear f ==> P f"
  8.2623 +qed   GEN_TAC THEN
  8.2624 +  MP_TAC(ISPEC `\A:real^N^N. P(\<lambda>x:real^N. A ** x):bool`
  8.2625 +    INDUCT_MATRIX_ELEMENTARY_ALT) THEN
  8.2626 +  REWRITE_TAC[] THEN MATCH_MP_TAC MONO_IMP THEN CONJ_TAC THENL
  8.2627 +   [ALL_TAC;
  8.2628 +    DISCH_TAC THEN X_GEN_TAC `f:real^N->real^N` THEN DISCH_TAC THEN
  8.2629 +    FIRST_X_ASSUM(MP_TAC o SPEC `matrix(f:real^N->real^N)`) THEN
  8.2630 +    ASM_SIMP_TAC[MATRIX_WORKS] THEN REWRITE_TAC[ETA_AX]] THEN
  8.2631 +  MATCH_MP_TAC MONO_AND THEN CONJ_TAC THENL
  8.2632 +   [DISCH_TAC THEN MAP_EVERY X_GEN_TAC [`A:real^N^N`; `B:real^N^N`] THEN
  8.2633 +    STRIP_TAC THEN FIRST_X_ASSUM(MP_TAC o SPECL
  8.2634 +     [`\x:real^N. (A:real^N^N) ** x`; `\x:real^N. (B:real^N^N) ** x`]) THEN
  8.2635 +    ASM_REWRITE_TAC[MATRIX_VECTOR_MUL_LINEAR; o_DEF] THEN
  8.2636 +    REWRITE_TAC[MATRIX_VECTOR_MUL_ASSOC];
  8.2637 +    ALL_TAC] THEN
  8.2638 +  MATCH_MP_TAC MONO_AND THEN CONJ_TAC THENL
  8.2639 +   [DISCH_TAC THEN MAP_EVERY X_GEN_TAC [`A:real^N^N`; `m:num`] THEN
  8.2640 +    STRIP_TAC THEN FIRST_X_ASSUM(MP_TAC o SPECL
  8.2641 +     [`\x:real^N. (A:real^N^N) ** x`; `m:num`]) THEN
  8.2642 +    ASM_REWRITE_TAC[MATRIX_VECTOR_MUL_LINEAR] THEN
  8.2643 +    DISCH_THEN MATCH_MP_TAC THEN
  8.2644 +    UNDISCH_TAC `row m (A:real^N^N) = 0` THEN
  8.2645 +    ASM_SIMP_TAC[CART_EQ; row; LAMBDA_BETA; VEC_COMPONENT; matrix_vector_mul;
  8.2646 +                 REAL_MUL_LZERO; SUM_0];
  8.2647 +    ALL_TAC] THEN
  8.2648 +  MATCH_MP_TAC MONO_AND THEN CONJ_TAC THENL
  8.2649 +   [DISCH_TAC THEN X_GEN_TAC `A:real^N^N` THEN STRIP_TAC THEN
  8.2650 +    FIRST_X_ASSUM(MP_TAC o SPEC `\i. (A:real^N^N)$i$i`) THEN
  8.2651 +    MATCH_MP_TAC EQ_IMP THEN AP_TERM_TAC THEN
  8.2652 +    ASM_SIMP_TAC[CART_EQ; matrix_vector_mul; FUN_EQ_THM; LAMBDA_BETA] THEN
  8.2653 +    MAP_EVERY X_GEN_TAC [`x:real^N`; `i:num`] THEN STRIP_TAC THEN
  8.2654 +    MATCH_MP_TAC EQ_TRANS THEN EXISTS_TAC
  8.2655 +     `sum(1..dimindex(:N)) (\<lambda>j. if j = i then (A:real^N^N)$i$j * (x:real^N)$j
  8.2656 +                                else 0)` THEN
  8.2657 +    CONJ_TAC THENL [ASM_SIMP_TAC[SUM_DELTA; IN_NUMSEG]; ALL_TAC] THEN
  8.2658 +    MATCH_MP_TAC SUM_EQ_NUMSEG THEN X_GEN_TAC `j:num` THEN STRIP_TAC THEN
  8.2659 +    ASM_SIMP_TAC[] THEN COND_CASES_TAC THEN ASM_SIMP_TAC[REAL_MUL_LZERO];
  8.2660 +    ALL_TAC] THEN
  8.2661 +  MATCH_MP_TAC MONO_AND THEN CONJ_TAC THEN
  8.2662 +  MATCH_MP_TAC MONO_FORALL THEN X_GEN_TAC `m:num` THEN
  8.2663 +  MATCH_MP_TAC MONO_FORALL THEN X_GEN_TAC `n:num` THEN
  8.2664 +  DISCH_THEN(fun th -> STRIP_TAC THEN MP_TAC th) THEN
  8.2665 +  ASM_REWRITE_TAC[] THEN MATCH_MP_TAC EQ_IMP THEN AP_TERM_TAC THEN
  8.2666 +  ASM_SIMP_TAC[CART_EQ; matrix_vector_mul; FUN_EQ_THM; LAMBDA_BETA;
  8.2667 +               mat; IN_DIMINDEX_SWAP]
  8.2668 +  THENL
  8.2669 +   [ONCE_REWRITE_TAC[SWAP_GALOIS] THEN ONCE_REWRITE_TAC[COND_RAND] THEN
  8.2670 +    ONCE_REWRITE_TAC[COND_RATOR] THEN
  8.2671 +    SIMP_TAC[SUM_DELTA; REAL_MUL_LID; REAL_MUL_LZERO; IN_NUMSEG] THEN
  8.2672 +    REPEAT STRIP_TAC THEN REWRITE_TAC[swap] THEN
  8.2673 +    COND_CASES_TAC THEN ASM_REWRITE_TAC[] THEN ASM_ARITH_TAC;
  8.2674 +    MAP_EVERY X_GEN_TAC [`x:real^N`; `i:num`] THEN STRIP_TAC THEN
  8.2675 +    ASM_CASES_TAC `i:num = m` THEN ASM_REWRITE_TAC[] THEN
  8.2676 +    ONCE_REWRITE_TAC[COND_RAND] THEN ONCE_REWRITE_TAC[COND_RATOR] THEN
  8.2677 +    GEN_REWRITE_TAC (RAND_CONV o ONCE_DEPTH_CONV) [EQ_SYM_EQ] THEN
  8.2678 +    ASM_SIMP_TAC[SUM_DELTA; REAL_MUL_LZERO; REAL_MUL_LID; IN_NUMSEG] THEN
  8.2679 +    MATCH_MP_TAC EQ_TRANS THEN EXISTS_TAC
  8.2680 +     `sum {m,n} (\<lambda>j. if n = j \/ j = m then (x:real^N)$j else 0)` THEN
  8.2681 +    CONJ_TAC THENL
  8.2682 +     [SIMP_TAC[SUM_CLAUSES; FINITE_RULES; IN_INSERT; NOT_IN_EMPTY] THEN
  8.2683 +      ASM_REWRITE_TAC[REAL_ADD_RID];
  8.2684 +      CONV_TAC SYM_CONV THEN MATCH_MP_TAC SUM_SUPERSET THEN
  8.2685 +      ASM_SIMP_TAC[SUBSET; IN_INSERT; NOT_IN_EMPTY; DE_MORGAN_THM;
  8.2686 +                   IN_NUMSEG; REAL_MUL_LZERO] THEN
  8.2687 +      ASM_ARITH_TAC]]);; *)
  8.2688 +
  8.2689 +(* ------------------------------------------------------------------------- *)
  8.2690 +(* Hence the effect of an arbitrary linear map on a gmeasurable set.          *)
  8.2691 +(* ------------------------------------------------------------------------- *)
  8.2692 +
  8.2693 +lemma LAMBDA_SWAP_GALOIS: True .. (*
  8.2694 + "!x:real^N y:real^N.
  8.2695 +        1 \<le> m \<and> m \<le> dimindex(:N) \<and> 1 \<le> n \<and> n \<le> dimindex(:N)
  8.2696 +        ==> (x = (lambda i. y$swap(m,n) i) \<longleftrightarrow>
  8.2697 +             (lambda i. x$swap(m,n) i) = y)"
  8.2698 +qed   SIMP_TAC[CART_EQ; LAMBDA_BETA; IN_DIMINDEX_SWAP] THEN
  8.2699 +  REPEAT GEN_TAC THEN STRIP_TAC THEN EQ_TAC THEN
  8.2700 +  DISCH_TAC THEN X_GEN_TAC `i:num` THEN STRIP_TAC THEN
  8.2701 +  FIRST_X_ASSUM(MP_TAC o SPEC `swap(m,n) (i:num)`) THEN
  8.2702 +  ASM_SIMP_TAC[IN_DIMINDEX_SWAP] THEN
  8.2703 +  ASM_MESON_TAC[REWRITE_RULE[FUN_EQ_THM; o_THM; I_THM] SWAP_IDEMPOTENT]);; *)
  8.2704 +
  8.2705 +lemma LAMBDA_ADD_GALOIS: True .. (*
  8.2706 + "!x:real^N y:real^N.
  8.2707 +        1 \<le> m \<and> m \<le> dimindex(:N) \<and> 1 \<le> n \<and> n \<le> dimindex(:N) \<and>
  8.2708 +        ~(m = n)
  8.2709 +        ==> (x = (lambda i. if i = m then y$m + y$n else y$i) \<longleftrightarrow>
  8.2710 +             (lambda i. if i = m then x$m - x$n else x$i) = y)"
  8.2711 +qed   SIMP_TAC[CART_EQ; LAMBDA_BETA] THEN
  8.2712 +  REPEAT GEN_TAC THEN STRIP_TAC THEN EQ_TAC THEN
  8.2713 +  DISCH_TAC THEN X_GEN_TAC `i:num` THEN STRIP_TAC THEN
  8.2714 +  FIRST_ASSUM(MP_TAC o SPEC `n:num`) THEN
  8.2715 +  FIRST_X_ASSUM(MP_TAC o SPEC `i:num`) THEN
  8.2716 +  ASM_REWRITE_TAC[] THEN COND_CASES_TAC THEN ASM_REWRITE_TAC[] THEN
  8.2717 +  REAL_ARITH_TAC);; *)
  8.2718 +
  8.2719 +lemma HAS_GMEASURE_SHEAR_INTERVAL: True .. (*
  8.2720 + "!a b:real^N m n.
  8.2721 +        1 \<le> m \<and> m \<le> dimindex(:N) \<and>
  8.2722 +        1 \<le> n \<and> n \<le> dimindex(:N) \<and>
  8.2723 +        ~(m = n) \<and> ~({a..b} = {}) \<and>
  8.2724 +        0 \<le> a$n
  8.2725 +        ==> (IMAGE (\<lambda>x. (lambda i. if i = m then x$m + x$n else x$i))
  8.2726 +                   {a..b}:real^N->bool)
  8.2727 +            has_gmeasure gmeasure (interval [a,b])"
  8.2728 +qed   lemma lemma = prove
  8.2729 +   (`!s t u v:real^N->bool.
  8.2730 +          gmeasurable s \<and> gmeasurable t \<and> gmeasurable u \<and>
  8.2731 +          negligible(s \<inter> t) \<and> negligible(s \<inter> u) \<and>
  8.2732 +          negligible(t \<inter> u) \<and>
  8.2733 +          s \<union> t \<union> u = v
  8.2734 +          ==> v has_gmeasure (measure s) + (measure t) + (measure u)"
  8.2735 +qed     REPEAT STRIP_TAC THEN
  8.2736 +    ASM_SIMP_TAC[HAS_GMEASURE_MEASURABLE_MEASURE; GMEASURABLE_UNION] THEN
  8.2737 +    FIRST_X_ASSUM(SUBST1_TAC o SYM) THEN
  8.2738 +    ASM_SIMP_TAC[MEASURE_UNION; GMEASURABLE_UNION] THEN
  8.2739 +    ASM_SIMP_TAC[MEASURE_EQ_0; UNION_OVER_INTER; MEASURE_UNION;
  8.2740 +                 GMEASURABLE_UNION; NEGLIGIBLE_INTER; GMEASURABLE_INTER] THEN
  8.2741 +    REAL_ARITH_TAC)
  8.2742 +  and lemma' = prove
  8.2743 +   (`!s t u a.
  8.2744 +          gmeasurable s \<and> gmeasurable t \<and>
  8.2745 +          s \<union> (IMAGE (\<lambda>x. a + x) t) = u \<and>
  8.2746 +          negligible(s \<inter> (IMAGE (\<lambda>x. a + x) t))
  8.2747 +          ==> gmeasure s + gmeasure t = gmeasure u"
  8.2748 +qed     REPEAT STRIP_TAC THEN FIRST_X_ASSUM(SUBST_ALL_TAC o SYM) THEN
  8.2749 +    ASM_SIMP_TAC[MEASURE_NEGLIGIBLE_UNION; GMEASURABLE_TRANSLATION;
  8.2750 +                 MEASURE_TRANSLATION]) in
  8.2751 +  REWRITE_TAC[INTERVAL_NE_EMPTY] THEN REPEAT STRIP_TAC THEN
  8.2752 +  SUBGOAL_THEN
  8.2753 +   `linear((\<lambda>x. lambda i. if i = m then x$m + x$n else x$i):real^N->real^N)`
  8.2754 +  ASSUME_TAC THENL
  8.2755 +   [ASM_SIMP_TAC[linear; LAMBDA_BETA; VECTOR_ADD_COMPONENT;
  8.2756 +                 VECTOR_MUL_COMPONENT; CART_EQ] THEN REAL_ARITH_TAC;
  8.2757 +    ALL_TAC] THEN
  8.2758 +  MP_TAC(ISPECL
  8.2759 +   [`IMAGE (\<lambda>x. lambda i. if i = m then x$m + x$n else x$i)
  8.2760 +            (interval[a:real^N,b]):real^N->bool`;
  8.2761 +    `interval[a,(lambda i. if i = m then (b:real^N)$m + b$n else b$i)] INTER
  8.2762 +       {x:real^N | (basis m - basis n) dot x \<le> a$m}`;
  8.2763 +    `interval[a,(lambda i. if i = m then b$m + b$n else b$i)] INTER
  8.2764 +       {x:real^N | (basis m - basis n) dot x >= (b:real^N)$m}`;
  8.2765 +    `interval[a:real^N,
  8.2766 +              (lambda i. if i = m then (b:real^N)$m + b$n else b$i)]`]
  8.2767 +     lemma) THEN
  8.2768 +  ANTS_TAC THENL
  8.2769 +   [ASM_SIMP_TAC[CONVEX_LINEAR_IMAGE; CONVEX_INTERVAL;
  8.2770 +                 CONVEX_HALFSPACE_LE; CONVEX_HALFSPACE_GE;
  8.2771 +                 CONVEX_INTER; GMEASURABLE_CONVEX; BOUNDED_INTER;
  8.2772 +                 BOUNDED_LINEAR_IMAGE; BOUNDED_INTERVAL] THEN
  8.2773 +    REWRITE_TAC[INTER] THEN
  8.2774 +    REWRITE_TAC[EXTENSION; IN_UNION; IN_INTER; IN_IMAGE] THEN
  8.2775 +    ASM_SIMP_TAC[LAMBDA_ADD_GALOIS; UNWIND_THM1] THEN
  8.2776 +    ASM_SIMP_TAC[IN_INTERVAL; IN_ELIM_THM; LAMBDA_BETA;
  8.2777 +                 DOT_BASIS; DOT_LSUB] THEN
  8.2778 +    ONCE_REWRITE_TAC[MESON[]
  8.2779 +       `(!i:num. P i) \<longleftrightarrow> P m \<and> (!i. ~(i = m) ==> P i)`] THEN
  8.2780 +    ASM_SIMP_TAC[] THEN
  8.2781 +    REWRITE_TAC[TAUT `(p \<and> x) \<and> (q \<and> x) \<and> r \<longleftrightarrow> x \<and> p \<and> q \<and> r`;
  8.2782 +                TAUT `(p \<and> x) \<and> q \<and> (r \<and> x) \<longleftrightarrow> x \<and> p \<and> q \<and> r`;
  8.2783 +                TAUT `((p \<and> x) \<and> q) \<and> (r \<and> x) \<and> s \<longleftrightarrow>
  8.2784 +                            x \<and> p \<and> q \<and> r \<and> s`;
  8.2785 +            TAUT `(a \<and> x \/ (b \<and> x) \<and> c \/ (d \<and> x) \<and> e \<longleftrightarrow> f \<and> x) \<longleftrightarrow>
  8.2786 +                  x ==> (a \/ b \<and> c \/ d \<and> e \<longleftrightarrow> f)`] THEN
  8.2787 +    ONCE_REWRITE_TAC[SET_RULE
  8.2788 +     `{x | P x \<and> Q x} = {x | P x} \<inter> {x | Q x}`] THEN
  8.2789 +    REWRITE_TAC[CONJ_ASSOC] THEN CONJ_TAC THENL
  8.2790 +     [ALL_TAC;
  8.2791 +      GEN_TAC THEN DISCH_THEN(MP_TAC o SPEC `n:num`) THEN
  8.2792 +      ASM_REWRITE_TAC[] THEN ASM_REAL_ARITH_TAC] THEN
  8.2793 +    REWRITE_TAC[GSYM CONJ_ASSOC] THEN REPEAT CONJ_TAC THEN
  8.2794 +    MATCH_MP_TAC NEGLIGIBLE_INTER THEN DISJ2_TAC THEN
  8.2795 +    MATCH_MP_TAC NEGLIGIBLE_SUBSET THENL
  8.2796 +     [EXISTS_TAC `{x:real^N | (basis m - basis n) dot x = (a:real^N)$m}`;
  8.2797 +      EXISTS_TAC `{x:real^N | (basis m - basis n) dot x = (b:real^N)$m}`;
  8.2798 +      EXISTS_TAC `{x:real^N | (basis m - basis n) dot x = (b:real^N)$m}`]
  8.2799 +    THEN (CONJ_TAC THENL
  8.2800 +      [MATCH_MP_TAC NEGLIGIBLE_HYPERPLANE THEN
  8.2801 +       REWRITE_TAC[VECTOR_SUB_EQ] THEN
  8.2802 +       ASM_MESON_TAC[BASIS_INJ];
  8.2803 +       ASM_SIMP_TAC[DOT_LSUB; DOT_BASIS; SUBSET; IN_ELIM_THM;
  8.2804 +                    NOT_IN_EMPTY] THEN
  8.2805 +       FIRST_X_ASSUM(MP_TAC o SPEC `m:num`) THEN ASM_REWRITE_TAC[] THEN
  8.2806 +       ASM_REAL_ARITH_TAC]);
  8.2807 +    ALL_TAC] THEN
  8.2808 +  ASM_SIMP_TAC[HAS_GMEASURE_MEASURABLE_MEASURE;
  8.2809 +               GMEASURABLE_LINEAR_IMAGE_INTERVAL;
  8.2810 +               GMEASURABLE_INTERVAL] THEN
  8.2811 +  MP_TAC(ISPECL
  8.2812 +   [`interval[a,(lambda i. if i = m then (b:real^N)$m + b$n else b$i)] INTER
  8.2813 +       {x:real^N | (basis m - basis n) dot x \<le> a$m}`;
  8.2814 +    `interval[a,(lambda i. if i = m then b$m + b$n else b$i)] INTER
  8.2815 +       {x:real^N | (basis m - basis n) dot x >= (b:real^N)$m}`;
  8.2816 +    `interval[a:real^N,
  8.2817 +              (lambda i. if i = m then (a:real^N)$m + b$n
  8.2818 +                         else (b:real^N)$i)]`;
  8.2819 +    `(lambda i. if i = m then (a:real^N)$m - (b:real^N)$m
  8.2820 +                else 0):real^N`]
  8.2821 +     lemma') THEN
  8.2822 +  ANTS_TAC THENL
  8.2823 +   [ASM_SIMP_TAC[CONVEX_LINEAR_IMAGE; CONVEX_INTERVAL;
  8.2824 +                 CONVEX_HALFSPACE_LE; CONVEX_HALFSPACE_GE;
  8.2825 +                 CONVEX_INTER; GMEASURABLE_CONVEX; BOUNDED_INTER;
  8.2826 +                 BOUNDED_LINEAR_IMAGE; BOUNDED_INTERVAL] THEN
  8.2827 +    REWRITE_TAC[INTER] THEN
  8.2828 +    REWRITE_TAC[EXTENSION; IN_UNION; IN_INTER; IN_IMAGE] THEN
  8.2829 +    ONCE_REWRITE_TAC[VECTOR_ARITH `x:real^N = (lambda i. p i) + y \<longleftrightarrow>
  8.2830 +                                   x - (lambda i. p i) = y`] THEN
  8.2831 +    ASM_SIMP_TAC[IN_INTERVAL; IN_ELIM_THM; LAMBDA_BETA;
  8.2832 +                 DOT_BASIS; DOT_LSUB; UNWIND_THM1;
  8.2833 +                 VECTOR_SUB_COMPONENT] THEN
  8.2834 +    ONCE_REWRITE_TAC[MESON[]
  8.2835 +       `(!i:num. P i) \<longleftrightarrow> P m \<and> (!i. ~(i = m) ==> P i)`] THEN
  8.2836 +    ASM_SIMP_TAC[REAL_SUB_RZERO] THEN CONJ_TAC THENL
  8.2837 +     [X_GEN_TAC `x:real^N` THEN
  8.2838 +      FIRST_ASSUM(MP_TAC o SPEC `n:num`) THEN
  8.2839 +      FIRST_X_ASSUM(MP_TAC o SPEC `m:num`) THEN
  8.2840 +      ASM_REWRITE_TAC[] THEN
  8.2841 +      ASM_CASES_TAC
  8.2842 +       `!i. ~(i = m)
  8.2843 +            ==> 1 \<le> i \<and> i \<le> dimindex (:N)
  8.2844 +                ==> (a:real^N)$i \<le> (x:real^N)$i \<and>
  8.2845 +                    x$i \<le> (b:real^N)$i` THEN
  8.2846 +      ASM_REWRITE_TAC[] THEN
  8.2847 +      FIRST_X_ASSUM(MP_TAC o SPEC `n:num`) THEN
  8.2848 +      ASM_REWRITE_TAC[] THEN ASM_REAL_ARITH_TAC;
  8.2849 +      ONCE_REWRITE_TAC[TAUT `((a \<and> b) \<and> c) \<and> (d \<and> e) \<and> f \<longleftrightarrow>
  8.2850 +                             (b \<and> e) \<and> a \<and> c \<and> d \<and> f`] THEN
  8.2851 +      ONCE_REWRITE_TAC[SET_RULE
  8.2852 +       `{x | P x \<and> Q x} = {x | P x} \<inter> {x | Q x}`] THEN
  8.2853 +      MATCH_MP_TAC NEGLIGIBLE_INTER THEN DISJ2_TAC THEN
  8.2854 +      MATCH_MP_TAC NEGLIGIBLE_SUBSET THEN
  8.2855 +      EXISTS_TAC `{x:real^N | (basis m - basis n) dot x = (a:real^N)$m}`
  8.2856 +      THEN CONJ_TAC THENL
  8.2857 +       [MATCH_MP_TAC NEGLIGIBLE_HYPERPLANE THEN
  8.2858 +        REWRITE_TAC[VECTOR_SUB_EQ] THEN
  8.2859 +        ASM_MESON_TAC[BASIS_INJ];
  8.2860 +        ASM_SIMP_TAC[DOT_LSUB; DOT_BASIS; SUBSET; IN_ELIM_THM;
  8.2861 +                     NOT_IN_EMPTY] THEN
  8.2862 +        FIRST_ASSUM(MP_TAC o SPEC `n:num`) THEN
  8.2863 +        FIRST_X_ASSUM(MP_TAC o SPEC `m:num`) THEN
  8.2864 +        ASM_REWRITE_TAC[] THEN ASM_REAL_ARITH_TAC]];
  8.2865 +    ALL_TAC] THEN
  8.2866 +  DISCH_THEN SUBST1_TAC THEN MATCH_MP_TAC(REAL_ARITH
  8.2867 +   `a = b + c ==> a = x + b ==> x = c`) THEN
  8.2868 +  ASM_SIMP_TAC[MEASURE_INTERVAL; CONTENT_CLOSED_INTERVAL_CASES;
  8.2869 +               LAMBDA_BETA] THEN
  8.2870 +  REPEAT(COND_CASES_TAC THENL
  8.2871 +   [ALL_TAC;
  8.2872 +    FIRST_X_ASSUM(MP_TAC o check (is_neg o concl)) THEN
  8.2873 +    MATCH_MP_TAC(TAUT `p ==> ~p ==> q`) THEN
  8.2874 +    X_GEN_TAC `i:num` THEN STRIP_TAC THEN
  8.2875 +    COND_CASES_TAC THEN ASM_SIMP_TAC[] THEN
  8.2876 +    FIRST_ASSUM(MP_TAC o SPEC `n:num`) THEN
  8.2877 +    FIRST_X_ASSUM(MP_TAC o SPEC `m:num`) THEN
  8.2878 +    ASM_REWRITE_TAC[] THEN ASM_REAL_ARITH_TAC]) THEN
  8.2879 +  SUBGOAL_THEN `1..dimindex(:N) = m INSERT ((1..dimindex(:N)) DELETE m)`
  8.2880 +  SUBST1_TAC THENL
  8.2881 +   [REWRITE_TAC[EXTENSION; IN_INSERT; IN_DELETE; IN_NUMSEG] THEN
  8.2882 +    ASM_ARITH_TAC;
  8.2883 +    ALL_TAC] THEN
  8.2884 +  SIMP_TAC[PRODUCT_CLAUSES; FINITE_DELETE; FINITE_NUMSEG] THEN
  8.2885 +  ASM_SIMP_TAC[IN_DELETE] THEN
  8.2886 +  MATCH_MP_TAC(REAL_RING
  8.2887 +   `s1 = s3 \<and> s2 = s3
  8.2888 +    ==> ((bm + bn) - am) * s1 =
  8.2889 +        ((am + bn) - am) * s2 + (bm - am) * s3`) THEN
  8.2890 +  CONJ_TAC THEN MATCH_MP_TAC PRODUCT_EQ THEN
  8.2891 +  SIMP_TAC[IN_DELETE] THEN REAL_ARITH_TAC);; *)
  8.2892 +
  8.2893 +lemma HAS_GMEASURE_LINEAR_IMAGE: True .. (*
  8.2894 + "!f:real^N->real^N s.
  8.2895 +        linear f \<and> gmeasurable s
  8.2896 +        ==> (IMAGE f s) has_gmeasure (abs(det(matrix f)) * gmeasure s)"
  8.2897 +qed   REWRITE_TAC[IMP_CONJ; RIGHT_FORALL_IMP_THM] THEN
  8.2898 +  MATCH_MP_TAC INDUCT_LINEAR_ELEMENTARY THEN REPEAT CONJ_TAC THENL
  8.2899 +   [MAP_EVERY X_GEN_TAC [`f:real^N->real^N`; `g:real^N->real^N`] THEN
  8.2900 +    REPLICATE_TAC 2 (DISCH_THEN(CONJUNCTS_THEN2 ASSUME_TAC MP_TAC)) THEN
  8.2901 +    DISCH_THEN(fun th -> REPEAT STRIP_TAC THEN MP_TAC th) THEN
  8.2902 +    DISCH_THEN(CONJUNCTS_THEN2
  8.2903 +     (MP_TAC o SPEC `IMAGE (g:real^N->real^N) s`)
  8.2904 +     (MP_TAC o SPEC `s:real^N->bool`)) THEN
  8.2905 +    ASM_REWRITE_TAC[] THEN
  8.2906 +    GEN_REWRITE_TAC LAND_CONV [HAS_GMEASURE_MEASURABLE_MEASURE] THEN
  8.2907 +    STRIP_TAC THEN ASM_SIMP_TAC[MATRIX_COMPOSE; DET_MUL; REAL_ABS_MUL] THEN
  8.2908 +    REWRITE_TAC[IMAGE_o; GSYM REAL_MUL_ASSOC];
  8.2909 +
  8.2910 +    MAP_EVERY X_GEN_TAC [`f:real^N->real^N`; `m:num`] THEN STRIP_TAC THEN
  8.2911 +    SUBGOAL_THEN `~(!x y. (f:real^N->real^N) x = f y ==> x = y)`
  8.2912 +    ASSUME_TAC THENL
  8.2913 +     [ASM_SIMP_TAC[LINEAR_SINGULAR_INTO_HYPERPLANE] THEN
  8.2914 +      EXISTS_TAC `basis m:real^N` THEN
  8.2915 +      ASM_SIMP_TAC[BASIS_NONZERO; DOT_BASIS];
  8.2916 +      ALL_TAC] THEN
  8.2917 +    MP_TAC(ISPEC `matrix f:real^N^N` INVERTIBLE_DET_NZ) THEN
  8.2918 +    ASM_SIMP_TAC[INVERTIBLE_LEFT_INVERSE; MATRIX_LEFT_INVERTIBLE_INJECTIVE;
  8.2919 +                 MATRIX_WORKS; REAL_ABS_NUM; REAL_MUL_LZERO] THEN
  8.2920 +    DISCH_THEN(K ALL_TAC) THEN REWRITE_TAC[HAS_GMEASURE_0] THEN
  8.2921 +    ASM_SIMP_TAC[NEGLIGIBLE_LINEAR_SINGULAR_IMAGE];
  8.2922 +
  8.2923 +    MAP_EVERY X_GEN_TAC [`c:num->real`; `s:real^N->bool`] THEN
  8.2924 +    DISCH_TAC THEN
  8.2925 +    FIRST_ASSUM(ASSUME_TAC o REWRITE_RULE[HAS_GMEASURE_MEASURE]) THEN
  8.2926 +    FIRST_ASSUM(MP_TAC o SPEC `c:num->real` o
  8.2927 +     MATCH_MP HAS_GMEASURE_STRETCH) THEN
  8.2928 +    MATCH_MP_TAC EQ_IMP THEN AP_TERM_TAC THEN
  8.2929 +    AP_THM_TAC THEN AP_TERM_TAC THEN AP_TERM_TAC THEN
  8.2930 +    SIMP_TAC[matrix; LAMBDA_BETA] THEN
  8.2931 +    W(MP_TAC o PART_MATCH (lhs o rand) DET_DIAGONAL o rand o snd) THEN
  8.2932 +    SIMP_TAC[LAMBDA_BETA; BASIS_COMPONENT; REAL_MUL_RZERO] THEN
  8.2933 +    DISCH_THEN(K ALL_TAC) THEN MATCH_MP_TAC PRODUCT_EQ_NUMSEG THEN
  8.2934 +    REWRITE_TAC[REAL_MUL_RID];
  8.2935 +
  8.2936 +    MAP_EVERY X_GEN_TAC [`m:num`; `n:num`] THEN STRIP_TAC THEN
  8.2937 +    MATCH_MP_TAC HAS_GMEASURE_LINEAR_SUFFICIENT THEN
  8.2938 +    ASM_SIMP_TAC[linear; LAMBDA_BETA; IN_DIMINDEX_SWAP; VECTOR_ADD_COMPONENT;
  8.2939 +                 VECTOR_MUL_COMPONENT; CART_EQ] THEN
  8.2940 +    MAP_EVERY X_GEN_TAC [`a:real^N`; `b:real^N`] THEN
  8.2941 +    SUBGOAL_THEN `matrix (\<lambda>x:real^N. lambda i. x$swap (m,n) i):real^N^N =
  8.2942 +                  transp(lambda i j. (mat 1:real^N^N)$i$swap (m,n) j)`
  8.2943 +    SUBST1_TAC THENL
  8.2944 +     [ASM_SIMP_TAC[MATRIX_EQ; LAMBDA_BETA; IN_DIMINDEX_SWAP;
  8.2945 +                    matrix_vector_mul; CART_EQ; matrix; mat; basis;
  8.2946 +                    COND_COMPONENT; transp] THEN
  8.2947 +      REWRITE_TAC[EQ_SYM_EQ];
  8.2948 +      ALL_TAC] THEN
  8.2949 +    REWRITE_TAC[DET_TRANSP] THEN
  8.2950 +    W(MP_TAC o PART_MATCH (lhs o rand) DET_PERMUTE_COLUMNS o
  8.2951 +        rand o lhand o rand o snd) THEN
  8.2952 +    ASM_SIMP_TAC[PERMUTES_SWAP; IN_NUMSEG; ETA_AX] THEN
  8.2953 +    DISCH_THEN(K ALL_TAC) THEN
  8.2954 +    REWRITE_TAC[DET_I; REAL_ABS_SIGN; REAL_MUL_RID; REAL_MUL_LID] THEN
  8.2955 +    ASM_CASES_TAC `interval[a:real^N,b] = {}` THENL
  8.2956 +     [ASM_SIMP_TAC[IMAGE_CLAUSES; HAS_GMEASURE_EMPTY; MEASURE_EMPTY];
  8.2957 +      ALL_TAC] THEN
  8.2958 +    SUBGOAL_THEN
  8.2959 +     `~(IMAGE (\<lambda>x:real^N. lambda i. x$swap (m,n) i)
  8.2960 +              {a..b}:real^N->bool = {})`
  8.2961 +    MP_TAC THENL [ASM_REWRITE_TAC[IMAGE_EQ_EMPTY]; ALL_TAC] THEN
  8.2962 +    SUBGOAL_THEN
  8.2963 +     `IMAGE (\<lambda>x:real^N. lambda i. x$swap (m,n) i)
  8.2964 +              {a..b}:real^N->bool =
  8.2965 +      interval[(lambda i. a$swap (m,n) i),
  8.2966 +               (lambda i. b$swap (m,n) i)]`
  8.2967 +    SUBST1_TAC THENL
  8.2968 +     [REWRITE_TAC[EXTENSION; IN_INTERVAL; IN_IMAGE] THEN
  8.2969 +      ASM_SIMP_TAC[LAMBDA_SWAP_GALOIS; UNWIND_THM1] THEN
  8.2970 +      SIMP_TAC[LAMBDA_BETA] THEN GEN_TAC THEN EQ_TAC THEN
  8.2971 +      DISCH_TAC THEN X_GEN_TAC `i:num` THEN STRIP_TAC THEN
  8.2972 +      FIRST_X_ASSUM(MP_TAC o SPEC `swap(m,n) (i:num)`) THEN
  8.2973 +      ASM_SIMP_TAC[IN_DIMINDEX_SWAP] THEN
  8.2974 +      ASM_MESON_TAC[REWRITE_RULE[FUN_EQ_THM; o_THM; I_THM] SWAP_IDEMPOTENT];
  8.2975 +      ALL_TAC] THEN
  8.2976 +    REWRITE_TAC[HAS_GMEASURE_MEASURABLE_MEASURE; GMEASURABLE_INTERVAL] THEN
  8.2977 +    REWRITE_TAC[MEASURE_INTERVAL] THEN
  8.2978 +    ASM_SIMP_TAC[CONTENT_CLOSED_INTERVAL; GSYM INTERVAL_NE_EMPTY] THEN
  8.2979 +    DISCH_THEN(K ALL_TAC) THEN SIMP_TAC[LAMBDA_BETA] THEN
  8.2980 +    ASM_SIMP_TAC[GSYM VECTOR_SUB_COMPONENT; IN_DIMINDEX_SWAP] THEN
  8.2981 +    MP_TAC(ISPECL [`\i. (b - a:real^N)$i`; `swap(m:num,n)`; `1..dimindex(:N)`]
  8.2982 +                (GSYM PRODUCT_PERMUTE)) THEN
  8.2983 +    REWRITE_TAC[o_DEF] THEN DISCH_THEN MATCH_MP_TAC THEN
  8.2984 +    ASM_SIMP_TAC[PERMUTES_SWAP; IN_NUMSEG];
  8.2985 +
  8.2986 +    MAP_EVERY X_GEN_TAC [`m:num`; `n:num`] THEN STRIP_TAC THEN
  8.2987 +    MATCH_MP_TAC HAS_GMEASURE_LINEAR_SUFFICIENT THEN
  8.2988 +    MATCH_MP_TAC(TAUT `a \<and> (a ==> b) ==> a \<and> b`) THEN CONJ_TAC THENL
  8.2989 +     [ASM_SIMP_TAC[linear; LAMBDA_BETA; VECTOR_ADD_COMPONENT;
  8.2990 +                   VECTOR_MUL_COMPONENT; CART_EQ] THEN REAL_ARITH_TAC;
  8.2991 +      DISCH_TAC] THEN
  8.2992 +    MAP_EVERY X_GEN_TAC [`a:real^N`; `b:real^N`] THEN
  8.2993 +    SUBGOAL_THEN
  8.2994 +      `det(matrix(\<lambda>x. lambda i. if i = m then (x:real^N)$m + x$n
  8.2995 +                                else x$i):real^N^N) = 1`
  8.2996 +    SUBST1_TAC THENL
  8.2997 +     [ASM_SIMP_TAC[matrix; basis; COND_COMPONENT; LAMBDA_BETA] THEN
  8.2998 +      FIRST_ASSUM(DISJ_CASES_TAC o MATCH_MP (ARITH_RULE
  8.2999 +       `~(m:num = n) ==> m < n \/ n < m`))
  8.3000 +      THENL
  8.3001 +       [W(MP_TAC o PART_MATCH (lhs o rand) DET_UPPERTRIANGULAR o lhs o snd);
  8.3002 +        W(MP_TAC o PART_MATCH (lhs o rand) DET_LOWERTRIANGULAR o lhs o snd)]
  8.3003 +      THEN ASM_SIMP_TAC[LAMBDA_BETA; BASIS_COMPONENT; COND_COMPONENT;
  8.3004 +                        matrix; REAL_ADD_RID; COND_ID;
  8.3005 +                        PRODUCT_CONST_NUMSEG; REAL_POW_ONE] THEN
  8.3006 +      DISCH_THEN MATCH_MP_TAC THEN
  8.3007 +      REPEAT GEN_TAC THEN REPEAT(COND_CASES_TAC THEN ASM_REWRITE_TAC[]) THEN
  8.3008 +      ASM_ARITH_TAC;
  8.3009 +      ALL_TAC] THEN
  8.3010 +    REWRITE_TAC[REAL_ABS_NUM; REAL_MUL_LID] THEN
  8.3011 +    ASM_CASES_TAC `interval[a:real^N,b] = {}` THENL
  8.3012 +     [ASM_SIMP_TAC[IMAGE_CLAUSES; HAS_GMEASURE_EMPTY; MEASURE_EMPTY];
  8.3013 +      ALL_TAC] THEN
  8.3014 +    SUBGOAL_THEN
  8.3015 +     `IMAGE (\<lambda>x. lambda i. if i = m then x$m + x$n else x$i) (interval [a,b]) =
  8.3016 +      IMAGE (\<lambda>x:real^N. (lambda i. if i = m \/ i = n then a$n else 0) +
  8.3017 +                        x)
  8.3018 +            (IMAGE (\<lambda>x:real^N. lambda i. if i = m then x$m + x$n else x$i)
  8.3019 +                   (IMAGE (\<lambda>x. (lambda i. if i = n then --(a$n) else 0) + x)
  8.3020 +                          {a..b}))`
  8.3021 +    SUBST1_TAC THENL
  8.3022 +     [REWRITE_TAC[GSYM IMAGE_o] THEN AP_THM_TAC THEN AP_TERM_TAC THEN
  8.3023 +      ASM_SIMP_TAC[FUN_EQ_THM; o_THM; VECTOR_ADD_COMPONENT; LAMBDA_BETA;
  8.3024 +                   CART_EQ] THEN
  8.3025 +      MAP_EVERY X_GEN_TAC [`x:real^N`; `i:num`] THEN
  8.3026 +      STRIP_TAC THEN ASM_CASES_TAC `i:num = m` THEN ASM_REWRITE_TAC[] THEN
  8.3027 +      ASM_CASES_TAC `i:num = n` THEN ASM_REWRITE_TAC[] THEN REAL_ARITH_TAC;
  8.3028 +      ALL_TAC] THEN
  8.3029 +    MATCH_MP_TAC HAS_GMEASURE_TRANSLATION THEN
  8.3030 +    SUBGOAL_THEN
  8.3031 +     `measure{a..b} =
  8.3032 +      measure(IMAGE (\<lambda>x:real^N. (lambda i. if i = n then --(a$n) else 0) + x)
  8.3033 +                    {a..b}:real^N->bool)`
  8.3034 +    SUBST1_TAC THENL
  8.3035 +     [CONV_TAC SYM_CONV THEN MATCH_MP_TAC MEASURE_TRANSLATION THEN
  8.3036 +      REWRITE_TAC[MEASURABLE_INTERVAL];
  8.3037 +      ALL_TAC] THEN
  8.3038 +    SUBGOAL_THEN
  8.3039 +     `~(IMAGE (\<lambda>x:real^N. (lambda i. if i = n then --(a$n) else 0) + x)
  8.3040 +                    {a..b}:real^N->bool = {})`
  8.3041 +    MP_TAC THENL [ASM_SIMP_TAC[IMAGE_EQ_EMPTY]; ALL_TAC] THEN
  8.3042 +    ONCE_REWRITE_TAC[VECTOR_ARITH `c + x = 1 % x + c`] THEN
  8.3043 +    ASM_REWRITE_TAC[IMAGE_AFFINITY_INTERVAL; REAL_POS] THEN
  8.3044 +    DISCH_TAC THEN MATCH_MP_TAC HAS_GMEASURE_SHEAR_INTERVAL THEN
  8.3045 +    ASM_SIMP_TAC[LAMBDA_BETA; VECTOR_ADD_COMPONENT; VECTOR_MUL_COMPONENT] THEN
  8.3046 +    REAL_ARITH_TAC]);; *)
  8.3047 +
  8.3048 +lemma GMEASURABLE_LINEAR_IMAGE: True .. (*
  8.3049 + "!f:real^N->real^N s.
  8.3050 +        linear f \<and> gmeasurable s ==> gmeasurable(IMAGE f s)"
  8.3051 +qed   REPEAT GEN_TAC THEN
  8.3052 +  DISCH_THEN(MP_TAC o MATCH_MP HAS_GMEASURE_LINEAR_IMAGE) THEN
  8.3053 +  SIMP_TAC[HAS_GMEASURE_MEASURABLE_MEASURE]);; *)
  8.3054 +
  8.3055 +lemma MEASURE_LINEAR_IMAGE: True .. (*
  8.3056 + "!f:real^N->real^N s.
  8.3057 +        linear f \<and> gmeasurable s
  8.3058 +        ==> measure(IMAGE f s) = abs(det(matrix f)) * gmeasure s"
  8.3059 +qed   REPEAT GEN_TAC THEN
  8.3060 +  DISCH_THEN(MP_TAC o MATCH_MP HAS_GMEASURE_LINEAR_IMAGE) THEN
  8.3061 +  SIMP_TAC[HAS_GMEASURE_MEASURABLE_MEASURE]);; *)
  8.3062 +
  8.3063 +lemma HAS_GMEASURE_LINEAR_IMAGE_SAME: True .. (*
  8.3064 + "!f s. linear f \<and> gmeasurable s \<and> abs(det(matrix f)) = 1
  8.3065 +         ==> (IMAGE f s) has_gmeasure (measure s)"
  8.3066 +qed   MESON_TAC[HAS_GMEASURE_LINEAR_IMAGE; REAL_MUL_LID]);; *)
  8.3067 +
  8.3068 +lemma MEASURE_LINEAR_IMAGE_SAME: True .. (*
  8.3069 + "!f:real^N->real^N s.
  8.3070 +        linear f \<and> gmeasurable s \<and> abs(det(matrix f)) = 1
  8.3071 +        ==> measure(IMAGE f s) = gmeasure s"
  8.3072 +qed   REPEAT GEN_TAC THEN
  8.3073 +  DISCH_THEN(MP_TAC o MATCH_MP HAS_GMEASURE_LINEAR_IMAGE_SAME) THEN
  8.3074 +  SIMP_TAC[HAS_GMEASURE_MEASURABLE_MEASURE]);; *)
  8.3075 +
  8.3076 +(* ------------------------------------------------------------------------- *)
  8.3077 +(* gmeasure of a standard simplex.                                            *)
  8.3078 +(* ------------------------------------------------------------------------- *)
  8.3079 +
  8.3080 +lemma CONGRUENT_IMAGE_STD_SIMPLEX: True .. (*
  8.3081 + "!p. p permutes 1..dimindex(:N)
  8.3082 +       ==> {x:real^N | 0 \<le> x$(p 1) \<and> x$(p(dimindex(:N))) \<le> 1 \<and>
  8.3083 +                       (!i. 1 \<le> i \<and> i < dimindex(:N)
  8.3084 +                            ==> x$(p i) \<le> x$(p(i + 1)))} =
  8.3085 +           IMAGE (\<lambda>x:real^N. lambda i. sum(1..inverse p(i)) (\<lambda>j. x$j))
  8.3086 +                 {x | (!i. 1 \<le> i \<and> i \<le> dimindex (:N) ==> 0 \<le> x$i) \<and>
  8.3087 +                      sum (1..dimindex (:N)) (\<lambda>i. x$i) \<le> 1}"
  8.3088 +qed   REPEAT STRIP_TAC THEN MATCH_MP_TAC SUBSET_ANTISYM THEN CONJ_TAC THENL
  8.3089 +   [ALL_TAC;
  8.3090 +    REWRITE_TAC[SUBSET; FORALL_IN_IMAGE] THEN X_GEN_TAC `x:real^N` THEN
  8.3091 +    ASM_SIMP_TAC[IN_ELIM_THM; LAMBDA_BETA; LAMBDA_BETA_PERM; LE_REFL;
  8.3092 +                 ARITH_RULE `i < n ==> i \<le> n \<and> i + 1 \<le> n`;
  8.3093 +                 ARITH_RULE `1 \<le> n + 1`; DIMINDEX_GE_1] THEN
  8.3094 +    STRIP_TAC THEN
  8.3095 +    FIRST_ASSUM(fun th -> REWRITE_TAC[MATCH_MP PERMUTES_INVERSES th]) THEN
  8.3096 +    ASM_SIMP_TAC[SUM_SING_NUMSEG; DIMINDEX_GE_1; LE_REFL] THEN
  8.3097 +    REWRITE_TAC[GSYM ADD1; SUM_CLAUSES_NUMSEG; ARITH_RULE `1 \<le> SUC n`] THEN
  8.3098 +    ASM_SIMP_TAC[REAL_LE_ADDR] THEN REPEAT STRIP_TAC THEN
  8.3099 +    FIRST_X_ASSUM MATCH_MP_TAC THEN ASM_ARITH_TAC] THEN
  8.3100 +  REWRITE_TAC[SUBSET; IN_IMAGE; IN_ELIM_THM] THEN X_GEN_TAC `x:real^N` THEN
  8.3101 +  STRIP_TAC THEN
  8.3102 +  EXISTS_TAC `(lambda i. if i = 1 then x$(p 1)
  8.3103 +                         else (x:real^N)$p(i) - x$p(i - 1)):real^N` THEN
  8.3104 +  ASM_SIMP_TAC[IN_ELIM_THM; LAMBDA_BETA; LAMBDA_BETA_PERM; LE_REFL;
  8.3105 +               ARITH_RULE `i < n ==> i \<le> n \<and> i + 1 \<le> n`;
  8.3106 +               ARITH_RULE `1 \<le> n + 1`; DIMINDEX_GE_1; CART_EQ] THEN
  8.3107 +  REPEAT CONJ_TAC THENL
  8.3108 +   [X_GEN_TAC `i:num` THEN STRIP_TAC THEN
  8.3109 +    SUBGOAL_THEN `1 \<le> inverse (p:num->num) i \<and>
  8.3110 +                  !x. x \<le> inverse p i ==> x \<le> dimindex(:N)`
  8.3111 +    ASSUME_TAC THENL
  8.3112 +     [ASM_MESON_TAC[PERMUTES_INVERSE; IN_NUMSEG; LE_TRANS; PERMUTES_IN_IMAGE];
  8.3113 +      ASM_SIMP_TAC[LAMBDA_BETA] THEN ASM_SIMP_TAC[SUM_CLAUSES_LEFT; ARITH]] THEN
  8.3114 +    SIMP_TAC[ARITH_RULE `2 \<le> n ==> ~(n = 1)`] THEN
  8.3115 +    GEN_REWRITE_TAC (RAND_CONV o RAND_CONV o BINDER_CONV)
  8.3116 +                [GSYM REAL_MUL_LID] THEN
  8.3117 +    ONCE_REWRITE_TAC[SUM_PARTIAL_PRE] THEN
  8.3118 +    REWRITE_TAC[REAL_SUB_REFL; REAL_MUL_RZERO; SUM_0; COND_ID] THEN
  8.3119 +    REWRITE_TAC[REAL_MUL_LID; ARITH; REAL_SUB_RZERO] THEN
  8.3120 +    FIRST_ASSUM(DISJ_CASES_TAC o MATCH_MP (ARITH_RULE
  8.3121 +     `1 \<le> p ==> p = 1 \/ 2 \<le> p`) o CONJUNCT1) THEN
  8.3122 +    ASM_SIMP_TAC[ARITH] THEN
  8.3123 +    FIRST_ASSUM(fun th -> REWRITE_TAC[MATCH_MP PERMUTES_INVERSES th]) THEN
  8.3124 +    REWRITE_TAC[REAL_ADD_RID] THEN TRY REAL_ARITH_TAC THEN
  8.3125 +    ASM_MESON_TAC[PERMUTES_INVERSE_EQ; PERMUTES_INVERSE];
  8.3126 +
  8.3127 +    X_GEN_TAC `i:num` THEN STRIP_TAC THEN COND_CASES_TAC THEN
  8.3128 +    ASM_REWRITE_TAC[REAL_SUB_LE] THEN
  8.3129 +    FIRST_X_ASSUM(MP_TAC o SPEC `i - 1`) THEN
  8.3130 +    ASM_SIMP_TAC[SUB_ADD] THEN DISCH_THEN MATCH_MP_TAC THEN ASM_ARITH_TAC;
  8.3131 +
  8.3132 +    SIMP_TAC[SUM_CLAUSES_LEFT; DIMINDEX_GE_1; ARITH;
  8.3133 +             ARITH_RULE `2 \<le> n ==> ~(n = 1)`] THEN
  8.3134 +    GEN_REWRITE_TAC (LAND_CONV o RAND_CONV o RAND_CONV o BINDER_CONV)
  8.3135 +                [GSYM REAL_MUL_LID] THEN
  8.3136 +    ONCE_REWRITE_TAC[SUM_PARTIAL_PRE] THEN
  8.3137 +    REWRITE_TAC[REAL_SUB_REFL; REAL_MUL_RZERO; SUM_0; COND_ID] THEN
  8.3138 +    REWRITE_TAC[REAL_MUL_LID; ARITH; REAL_SUB_RZERO] THEN
  8.3139 +    COND_CASES_TAC THEN ASM_SIMP_TAC[REAL_ADD_RID] THEN
  8.3140 +    ASM_REWRITE_TAC[REAL_ARITH `x + y - x:real = y`] THEN
  8.3141 +    ASM_MESON_TAC[DIMINDEX_GE_1;
  8.3142 +                  ARITH_RULE `1 \<le> n \<and> ~(2 \<le> n) ==> n = 1`]]);; *)
  8.3143 +
  8.3144 +lemma HAS_GMEASURE_IMAGE_STD_SIMPLEX: True .. (*
  8.3145 + "!p. p permutes 1..dimindex(:N)
  8.3146 +       ==> {x:real^N | 0 \<le> x$(p 1) \<and> x$(p(dimindex(:N))) \<le> 1 \<and>
  8.3147 +                       (!i. 1 \<le> i \<and> i < dimindex(:N)
  8.3148 +                            ==> x$(p i) \<le> x$(p(i + 1)))}
  8.3149 +           has_gmeasure
  8.3150 +           (measure (convex hull
  8.3151 +             (0 INSERT {basis i:real^N | 1 \<le> i \<and> i \<le> dimindex(:N)})))"
  8.3152 +qed   REPEAT STRIP_TAC THEN ASM_SIMP_TAC[CONGRUENT_IMAGE_STD_SIMPLEX] THEN
  8.3153 +  ASM_SIMP_TAC[GSYM STD_SIMPLEX] THEN
  8.3154 +  MATCH_MP_TAC HAS_GMEASURE_LINEAR_IMAGE_SAME THEN
  8.3155 +  REPEAT CONJ_TAC THENL
  8.3156 +   [REWRITE_TAC[linear; CART_EQ] THEN
  8.3157 +    ASM_SIMP_TAC[LAMBDA_BETA; VECTOR_ADD_COMPONENT; VECTOR_MUL_COMPONENT;
  8.3158 +                 GSYM SUM_ADD_NUMSEG; GSYM SUM_LMUL] THEN
  8.3159 +    REPEAT STRIP_TAC THEN MATCH_MP_TAC SUM_EQ_NUMSEG THEN
  8.3160 +    REPEAT STRIP_TAC THEN REWRITE_TAC[] THENL
  8.3161 +     [MATCH_MP_TAC VECTOR_ADD_COMPONENT;
  8.3162 +      MATCH_MP_TAC VECTOR_MUL_COMPONENT] THEN
  8.3163 +    ASM_MESON_TAC[PERMUTES_INVERSE; IN_NUMSEG; LE_TRANS; PERMUTES_IN_IMAGE];
  8.3164 +    MATCH_MP_TAC GMEASURABLE_CONVEX THEN REWRITE_TAC[CONVEX_CONVEX_HULL] THEN
  8.3165 +    MATCH_MP_TAC BOUNDED_CONVEX_HULL THEN REWRITE_TAC[BOUNDED_INSERT] THEN
  8.3166 +    ONCE_REWRITE_TAC[SIMPLE_IMAGE_GEN] THEN
  8.3167 +    MATCH_MP_TAC FINITE_IMP_BOUNDED THEN MATCH_MP_TAC FINITE_IMAGE THEN
  8.3168 +    REWRITE_TAC[GSYM numseg; FINITE_NUMSEG];
  8.3169 +    MATCH_MP_TAC EQ_TRANS THEN EXISTS_TAC
  8.3170 +     `abs(det
  8.3171 +       ((lambda i. ((lambda i j. if j \<le> i then 1 else 0):real^N^N)
  8.3172 +                   $inverse p i)
  8.3173 +        :real^N^N))` THEN
  8.3174 +    CONJ_TAC THENL
  8.3175 +     [AP_TERM_TAC THEN AP_TERM_TAC THEN REWRITE_TAC[CART_EQ] THEN
  8.3176 +      ASM_SIMP_TAC[matrix; LAMBDA_BETA; BASIS_COMPONENT; COND_COMPONENT;
  8.3177 +                   LAMBDA_BETA_PERM; PERMUTES_INVERSE] THEN
  8.3178 +      X_GEN_TAC `i:num` THEN STRIP_TAC THEN
  8.3179 +      X_GEN_TAC `j:num` THEN STRIP_TAC THEN MATCH_MP_TAC EQ_TRANS THEN
  8.3180 +      EXISTS_TAC `sum (1..inverse (p:num->num) i)
  8.3181 +                      (\<lambda>k. if k = j then 1 else 0)` THEN
  8.3182 +      CONJ_TAC THENL
  8.3183 +       [MATCH_MP_TAC SUM_EQ THEN
  8.3184 +        ASM_SIMP_TAC[IN_NUMSEG; PERMUTES_IN_IMAGE; basis] THEN
  8.3185 +        REPEAT STRIP_TAC THEN MATCH_MP_TAC LAMBDA_BETA THEN
  8.3186 +        ASM_MESON_TAC[PERMUTES_IN_IMAGE; IN_NUMSEG; LE_TRANS;
  8.3187 +                      PERMUTES_INVERSE];
  8.3188 +        ASM_SIMP_TAC[SUM_DELTA; IN_NUMSEG]];
  8.3189 +      ALL_TAC] THEN
  8.3190 +    ASM_SIMP_TAC[PERMUTES_INVERSE; DET_PERMUTE_ROWS; ETA_AX] THEN
  8.3191 +    REWRITE_TAC[REAL_ABS_MUL; REAL_ABS_SIGN; REAL_MUL_LID] THEN
  8.3192 +    MATCH_MP_TAC(REAL_ARITH `x = 1 ==> abs x = 1`) THEN
  8.3193 +    ASM_SIMP_TAC[DET_LOWERTRIANGULAR; GSYM NOT_LT; LAMBDA_BETA] THEN
  8.3194 +    REWRITE_TAC[LT_REFL; PRODUCT_CONST_NUMSEG; REAL_POW_ONE]]);; *)
  8.3195 +
  8.3196 +lemma HAS_GMEASURE_STD_SIMPLEX: True .. (*
  8.3197 + "(convex hull (0:real^N INSERT {basis i | 1 \<le> i \<and> i \<le> dimindex(:N)}))
  8.3198 +   has_gmeasure inv((FACT(dimindex(:N))))"
  8.3199 +qed   lemma lemma = prove
  8.3200 +   (`!f:num->real. (!i. 1 \<le> i \<and> i < n ==> f i \<le> f(i + 1)) \<longleftrightarrow>
  8.3201 +                   (!i j. 1 \<le> i \<and> i \<le> j \<and> j \<le> n ==> f i \<le> f j)"
  8.3202 +qed     GEN_TAC THEN EQ_TAC THEN DISCH_TAC THENL
  8.3203 +     [GEN_TAC THEN INDUCT_TAC THEN
  8.3204 +      SIMP_TAC[LE; REAL_LE_REFL] THEN
  8.3205 +      STRIP_TAC THEN ASM_SIMP_TAC[REAL_LE_REFL] THEN
  8.3206 +      MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `(f:num->real) j` THEN
  8.3207 +      ASM_SIMP_TAC[ARITH_RULE `SUC x \<le> y ==> x \<le> y`] THEN
  8.3208 +      REWRITE_TAC[ADD1] THEN FIRST_X_ASSUM MATCH_MP_TAC THEN ASM_ARITH_TAC;
  8.3209 +      REPEAT STRIP_TAC THEN FIRST_X_ASSUM MATCH_MP_TAC THEN ASM_ARITH_TAC]) in
  8.3210 +  MP_TAC(ISPECL
  8.3211 +   [`\p. {x:real^N | 0 \<le> x$(p 1) \<and> x$(p(dimindex(:N))) \<le> 1 \<and>
  8.3212 +                     (!i. 1 \<le> i \<and> i < dimindex(:N)
  8.3213 +                          ==> x$(p i) \<le> x$(p(i + 1)))}`;
  8.3214 +    `{p | p permutes 1..dimindex(:N)}`]
  8.3215 +    HAS_GMEASURE_NEGLIGIBLE_UNIONS_IMAGE) THEN
  8.3216 +  ASM_SIMP_TAC[REWRITE_RULE[HAS_GMEASURE_MEASURABLE_MEASURE]
  8.3217 +                            HAS_GMEASURE_IMAGE_STD_SIMPLEX; IN_ELIM_THM] THEN
  8.3218 +  ASM_SIMP_TAC[SUM_CONST; FINITE_PERMUTATIONS; FINITE_NUMSEG;
  8.3219 +               CARD_PERMUTATIONS; CARD_NUMSEG_1] THEN
  8.3220 +  ANTS_TAC THENL
  8.3221 +   [MAP_EVERY X_GEN_TAC [`p:num->num`; `q:num->num`] THEN STRIP_TAC THEN
  8.3222 +    SUBGOAL_THEN `?i. i \<in> 1..dimindex(:N) \<and> ~(p i:num = q i)` MP_TAC THENL
  8.3223 +     [ASM_MESON_TAC[permutes; FUN_EQ_THM]; ALL_TAC] THEN
  8.3224 +    GEN_REWRITE_TAC LAND_CONV [num_WOP] THEN
  8.3225 +    REWRITE_TAC[TAUT `a ==> ~(b \<and> ~c) \<longleftrightarrow> a \<and> b ==> c`] THEN
  8.3226 +    REWRITE_TAC[IN_NUMSEG] THEN
  8.3227 +    DISCH_THEN(X_CHOOSE_THEN `k:num` STRIP_ASSUME_TAC) THEN
  8.3228 +    MATCH_MP_TAC NEGLIGIBLE_SUBSET THEN
  8.3229 +    EXISTS_TAC `{x:real^N | (basis(p(k:num)) - basis(q k)) dot x = 0}` THEN
  8.3230 +    CONJ_TAC THENL
  8.3231 +     [MATCH_MP_TAC NEGLIGIBLE_HYPERPLANE THEN REWRITE_TAC[VECTOR_SUB_EQ] THEN
  8.3232 +      MATCH_MP_TAC BASIS_NE THEN ASM_MESON_TAC[PERMUTES_IN_IMAGE; IN_NUMSEG];
  8.3233 +      ALL_TAC] THEN
  8.3234 +    REWRITE_TAC[SUBSET; IN_INTER; IN_ELIM_THM; DOT_LSUB; VECTOR_SUB_EQ] THEN
  8.3235 +    ASM_SIMP_TAC[DOT_BASIS; GSYM IN_NUMSEG; PERMUTES_IN_IMAGE] THEN
  8.3236 +    SUBGOAL_THEN `?l. (q:num->num) l = p(k:num)` STRIP_ASSUME_TAC THENL
  8.3237 +     [ASM_MESON_TAC[permutes]; ALL_TAC] THEN
  8.3238 +    SUBGOAL_THEN `1 \<le> l \<and> l \<le> dimindex(:N)` STRIP_ASSUME_TAC THENL
  8.3239 +     [ASM_MESON_TAC[PERMUTES_IN_IMAGE; IN_NUMSEG]; ALL_TAC] THEN
  8.3240 +    SUBGOAL_THEN `k:num < l` ASSUME_TAC THENL
  8.3241 +     [REWRITE_TAC[GSYM NOT_LE] THEN REWRITE_TAC[LE_LT] THEN
  8.3242 +      ASM_MESON_TAC[PERMUTES_INJECTIVE; IN_NUMSEG];
  8.3243 +      ALL_TAC] THEN
  8.3244 +    SUBGOAL_THEN `?m. (p:num->num) m = q(k:num)` STRIP_ASSUME_TAC THENL
  8.3245 +     [ASM_MESON_TAC[permutes]; ALL_TAC] THEN
  8.3246 +    SUBGOAL_THEN `1 \<le> m \<and> m \<le> dimindex(:N)` STRIP_ASSUME_TAC THENL
  8.3247 +     [ASM_MESON_TAC[PERMUTES_IN_IMAGE; IN_NUMSEG]; ALL_TAC] THEN
  8.3248 +    SUBGOAL_THEN `k:num < m` ASSUME_TAC THENL
  8.3249 +     [REWRITE_TAC[GSYM NOT_LE] THEN REWRITE_TAC[LE_LT] THEN
  8.3250 +      ASM_MESON_TAC[PERMUTES_INJECTIVE; IN_NUMSEG];
  8.3251 +      ALL_TAC] THEN
  8.3252 +    X_GEN_TAC `x:real^N` THEN REWRITE_TAC[lemma] THEN STRIP_TAC THEN
  8.3253 +    FIRST_X_ASSUM(MP_TAC o SPECL [`k:num`; `l:num`]) THEN
  8.3254 +    FIRST_X_ASSUM(MP_TAC o SPECL [`k:num`; `m:num`]) THEN
  8.3255 +    ASM_SIMP_TAC[LT_IMP_LE; IMP_IMP; REAL_LE_ANTISYM; REAL_SUB_0] THEN
  8.3256 +    MATCH_MP_TAC EQ_IMP THEN BINOP_TAC THEN
  8.3257 +    ASM_MESON_TAC[PERMUTES_IN_IMAGE; IN_NUMSEG; DOT_BASIS];
  8.3258 +    ALL_TAC] THEN
  8.3259 +  REWRITE_TAC[HAS_GMEASURE_MEASURABLE_MEASURE] THEN
  8.3260 +  DISCH_THEN(ASSUME_TAC o CONJUNCT2) THEN CONJ_TAC THENL
  8.3261 +   [MATCH_MP_TAC GMEASURABLE_CONVEX THEN REWRITE_TAC[CONVEX_CONVEX_HULL] THEN
  8.3262 +    MATCH_MP_TAC BOUNDED_CONVEX_HULL THEN REWRITE_TAC[BOUNDED_INSERT] THEN
  8.3263 +    ONCE_REWRITE_TAC[SIMPLE_IMAGE_GEN] THEN
  8.3264 +    MATCH_MP_TAC FINITE_IMP_BOUNDED THEN MATCH_MP_TAC FINITE_IMAGE THEN
  8.3265 +    REWRITE_TAC[GSYM numseg; FINITE_NUMSEG];
  8.3266 +    ALL_TAC] THEN
  8.3267 +  ASM_SIMP_TAC[REAL_FIELD `~(y = 0) ==> (x = inv y \<longleftrightarrow> y * x = 1)`;
  8.3268 +               REAL_OF_NUM_EQ; FACT_NZ] THEN
  8.3269 +  FIRST_X_ASSUM(SUBST1_TAC o SYM) THEN MATCH_MP_TAC EQ_TRANS THEN
  8.3270 +  EXISTS_TAC `measure(interval[0:real^N,1])` THEN CONJ_TAC THENL
  8.3271 +   [AP_TERM_TAC; REWRITE_TAC[MEASURE_INTERVAL; CONTENT_UNIT]] THEN
  8.3272 +  REWRITE_TAC[lemma] THEN MATCH_MP_TAC SUBSET_ANTISYM THEN CONJ_TAC THENL
  8.3273 +   [REWRITE_TAC[SUBSET; FORALL_IN_UNIONS; FORALL_IN_IMAGE; IMP_CONJ;
  8.3274 +                RIGHT_FORALL_IMP_THM; IN_ELIM_THM] THEN
  8.3275 +    SIMP_TAC[IMP_IMP; IN_INTERVAL; LAMBDA_BETA; VEC_COMPONENT] THEN
  8.3276 +    X_GEN_TAC `p:num->num` THEN STRIP_TAC THEN X_GEN_TAC `x:real^N` THEN
  8.3277 +    STRIP_TAC THEN X_GEN_TAC `i:num` THEN REPEAT STRIP_TAC THEN
  8.3278 +    MATCH_MP_TAC REAL_LE_TRANS THENL
  8.3279 +     [EXISTS_TAC `(x:real^N)$(p 1)`;
  8.3280 +      EXISTS_TAC `(x:real^N)$(p(dimindex(:N)))`] THEN
  8.3281 +    ASM_REWRITE_TAC[] THEN
  8.3282 +    FIRST_ASSUM(MP_TAC o SPEC `i:num` o MATCH_MP PERMUTES_SURJECTIVE) THEN
  8.3283 +    ASM_MESON_TAC[LE_REFL; PERMUTES_IN_IMAGE; IN_NUMSEG];
  8.3284 +    ALL_TAC] THEN
  8.3285 +  REWRITE_TAC[SET_RULE `s \<subseteq> UNIONS(IMAGE f t) \<longleftrightarrow>
  8.3286 +                        !x. x \<in> s ==> ?y. y \<in> t \<and> x \<in> f y`] THEN
  8.3287 +  X_GEN_TAC `x:real^N` THEN REWRITE_TAC[IN_INTERVAL; IN_ELIM_THM] THEN
  8.3288 +  SIMP_TAC[VEC_COMPONENT] THEN DISCH_TAC THEN
  8.3289 +  MP_TAC(ISPEC `\i j. ~((x:real^N)$j \<le> x$i)` TOPOLOGICAL_SORT) THEN
  8.3290 +  REWRITE_TAC[REAL_NOT_LE; REAL_NOT_LT] THEN
  8.3291 +  ANTS_TAC THENL [REAL_ARITH_TAC; ALL_TAC] THEN
  8.3292 +  DISCH_THEN(MP_TAC o SPECL [`dimindex(:N)`; `1..dimindex(:N)`]) THEN
  8.3293 +  REWRITE_TAC[HAS_SIZE_NUMSEG_1; EXTENSION; IN_IMAGE; IN_NUMSEG] THEN
  8.3294 +  DISCH_THEN(X_CHOOSE_THEN `f:num->num` (CONJUNCTS_THEN2
  8.3295 +   (ASSUME_TAC o GSYM) ASSUME_TAC)) THEN
  8.3296 +  EXISTS_TAC `\i. if i \<in> 1..dimindex(:N) then f(i) else i` THEN
  8.3297 +  REWRITE_TAC[] THEN ONCE_REWRITE_TAC[ARITH_RULE
  8.3298 +    `1 \<le> i \<and> i \<le> j \<and> j \<le> n \<longleftrightarrow>
  8.3299 +     1 \<le> i \<and> 1 \<le> j \<and> i \<le> n \<and> j \<le> n \<and> i \<le> j`] THEN
  8.3300 +  ASM_SIMP_TAC[IN_NUMSEG; LE_REFL; DIMINDEX_GE_1] THEN
  8.3301 +  CONJ_TAC THENL
  8.3302 +   [ALL_TAC;
  8.3303 +    ASM_MESON_TAC[LE_REFL; DIMINDEX_GE_1; LE_LT; REAL_LE_LT]] THEN
  8.3304 +  SIMP_TAC[PERMUTES_FINITE_SURJECTIVE; FINITE_NUMSEG] THEN
  8.3305 +  SIMP_TAC[IN_NUMSEG] THEN ASM_MESON_TAC[]);; *)
  8.3306 +
  8.3307 +(* ------------------------------------------------------------------------- *)
  8.3308 +(* Hence the gmeasure of a general simplex.                                   *)
  8.3309 +(* ------------------------------------------------------------------------- *)
  8.3310 +
  8.3311 +lemma HAS_GMEASURE_SIMPLEX_0: True .. (*
  8.3312 + "!l:(real^N)list.
  8.3313 +        LENGTH l = dimindex(:N)
  8.3314 +        ==> (convex hull (0 INSERT set_of_list l)) has_gmeasure
  8.3315 +            abs(det(vector l)) / (FACT(dimindex(:N)))"
  8.3316 +qed   REPEAT STRIP_TAC THEN
  8.3317 +  SUBGOAL_THEN
  8.3318 +   `0 INSERT (set_of_list l) =
  8.3319 +        IMAGE (\<lambda>x:real^N. transp(vector l:real^N^N) ** x)
  8.3320 +              (0 INSERT {basis i:real^N | 1 \<le> i \<and> i \<le> dimindex(:N)})`
  8.3321 +  SUBST1_TAC THENL
  8.3322 +   [ONCE_REWRITE_TAC[SIMPLE_IMAGE_GEN] THEN
  8.3323 +    REWRITE_TAC[IMAGE_CLAUSES; GSYM IMAGE_o; o_DEF] THEN
  8.3324 +    REWRITE_TAC[MATRIX_VECTOR_MUL_RZERO] THEN AP_TERM_TAC THEN
  8.3325 +    SIMP_TAC[matrix_vector_mul; vector; transp; LAMBDA_BETA; basis] THEN
  8.3326 +    ONCE_REWRITE_TAC[COND_RAND] THEN
  8.3327 +    SIMP_TAC[REAL_MUL_RZERO; SUM_DELTA] THEN
  8.3328 +    REWRITE_TAC[EXTENSION; IN_IMAGE; IN_ELIM_THM; IN_NUMSEG] THEN
  8.3329 +    ONCE_REWRITE_TAC[TAUT `a \<and> b \<and> c \<longleftrightarrow> ~(b \<and> c ==> ~a)`] THEN
  8.3330 +    X_GEN_TAC `y:real^N` THEN SIMP_TAC[LAMBDA_BETA; REAL_MUL_RID] THEN
  8.3331 +    SIMP_TAC[CART_EQ; LAMBDA_BETA] THEN
  8.3332 +    REWRITE_TAC[NOT_IMP; REAL_MUL_RID; GSYM CART_EQ] THEN
  8.3333 +    ASM_REWRITE_TAC[IN_SET_OF_LIST; MEM_EXISTS_EL] THEN
  8.3334 +    EQ_TAC THEN DISCH_THEN(X_CHOOSE_THEN `i:num` STRIP_ASSUME_TAC) THENL
  8.3335 +     [EXISTS_TAC `SUC i`; EXISTS_TAC `i - 1`] THEN
  8.3336 +    ASM_REWRITE_TAC[SUC_SUB1] THEN ASM_ARITH_TAC;
  8.3337 +    ALL_TAC] THEN
  8.3338 +  ASM_SIMP_TAC[GSYM CONVEX_HULL_LINEAR_IMAGE; MATRIX_VECTOR_MUL_LINEAR] THEN
  8.3339 +  SUBGOAL_THEN
  8.3340 +   `det(vector l:real^N^N) = det(matrix(\<lambda>x:real^N. transp(vector l) ** x))`
  8.3341 +  SUBST1_TAC THENL
  8.3342 +   [REWRITE_TAC[MATRIX_OF_MATRIX_VECTOR_MUL; DET_TRANSP]; ALL_TAC] THEN
  8.3343 +  REWRITE_TAC[real_div] THEN
  8.3344 +  ASM_SIMP_TAC[GSYM(REWRITE_RULE[HAS_GMEASURE_MEASURABLE_MEASURE]
  8.3345 +                 HAS_GMEASURE_STD_SIMPLEX)] THEN
  8.3346 +  MATCH_MP_TAC HAS_GMEASURE_LINEAR_IMAGE THEN
  8.3347 +  REWRITE_TAC[MATRIX_VECTOR_MUL_LINEAR] THEN
  8.3348 +  MATCH_MP_TAC GMEASURABLE_CONVEX THEN REWRITE_TAC[CONVEX_CONVEX_HULL] THEN
  8.3349 +  MATCH_MP_TAC BOUNDED_CONVEX_HULL THEN REWRITE_TAC[BOUNDED_INSERT] THEN
  8.3350 +  ONCE_REWRITE_TAC[SIMPLE_IMAGE_GEN] THEN
  8.3351 +  MATCH_MP_TAC FINITE_IMP_BOUNDED THEN MATCH_MP_TAC FINITE_IMAGE THEN
  8.3352 +  REWRITE_TAC[GSYM numseg; FINITE_NUMSEG]);; *)
  8.3353 +
  8.3354 +lemma HAS_GMEASURE_SIMPLEX: True .. (*
  8.3355 + "!a l:(real^N)list.
  8.3356 +        LENGTH l = dimindex(:N)
  8.3357 +        ==> (convex hull (set_of_list(CONS a l))) has_gmeasure
  8.3358 +            abs(det(vector(MAP (\<lambda>x. x - a) l))) / (FACT(dimindex(:N)))"
  8.3359 +qed   REPEAT STRIP_TAC THEN
  8.3360 +  MP_TAC(ISPEC `MAP (\<lambda>x:real^N. x - a) l` HAS_GMEASURE_SIMPLEX_0) THEN
  8.3361 +  ASM_REWRITE_TAC[LENGTH_MAP; set_of_list] THEN
  8.3362 +  DISCH_THEN(MP_TAC o SPEC `a:real^N` o MATCH_MP HAS_GMEASURE_TRANSLATION) THEN
  8.3363 +  REWRITE_TAC[GSYM CONVEX_HULL_TRANSLATION] THEN
  8.3364 +  MATCH_MP_TAC EQ_IMP THEN AP_THM_TAC THEN AP_TERM_TAC THEN
  8.3365 +  REWRITE_TAC[IMAGE_CLAUSES; VECTOR_ADD_RID; SET_OF_LIST_MAP] THEN
  8.3366 +  REWRITE_TAC[GSYM IMAGE_o; o_DEF; VECTOR_ARITH `a + x - a:real^N = x`;
  8.3367 +              SET_RULE `IMAGE (\<lambda>x. x) s = s`]);; *)
  8.3368 +
  8.3369 +lemma GMEASURABLE_SIMPLEX: True .. (*
  8.3370 + "!l. gmeasurable(convex hull (set_of_list l))"
  8.3371 +qed   GEN_TAC THEN
  8.3372 +  MATCH_MP_TAC GMEASURABLE_CONVEX THEN REWRITE_TAC[CONVEX_CONVEX_HULL] THEN
  8.3373 +  MATCH_MP_TAC BOUNDED_CONVEX_HULL THEN
  8.3374 +  MATCH_MP_TAC FINITE_IMP_BOUNDED THEN REWRITE_TAC[FINITE_SET_OF_LIST]);; *)
  8.3375 +
  8.3376 +lemma MEASURE_SIMPLEX: True .. (*
  8.3377 + "!a l:(real^N)list.
  8.3378 +        LENGTH l = dimindex(:N)
  8.3379 +        ==> measure(convex hull (set_of_list(CONS a l))) =
  8.3380 +            abs(det(vector(MAP (\<lambda>x. x - a) l))) / (FACT(dimindex(:N)))"
  8.3381 +qed   MESON_TAC[HAS_GMEASURE_SIMPLEX; HAS_GMEASURE_MEASURABLE_MEASURE]);; *)
  8.3382 +
  8.3383 +(* ------------------------------------------------------------------------- *)
  8.3384 +(* Area of a triangle.                                                       *)
  8.3385 +(* ------------------------------------------------------------------------- *)
  8.3386 +
  8.3387 +lemma HAS_GMEASURE_TRIANGLE: True .. (*
  8.3388 + "!a b c:real^2.
  8.3389 +        convex hull {a,b,c} has_gmeasure
  8.3390 +        abs((b$1 - a$1) * (c$2 - a$2) - (b$2 - a$2) * (c$1 - a$1)) / 2"
  8.3391 +qed   REPEAT STRIP_TAC THEN
  8.3392 +  MP_TAC(ISPECL [`a:real^2`; `[b;c]:(real^2)list`] HAS_GMEASURE_SIMPLEX) THEN
  8.3393 +  REWRITE_TAC[LENGTH; DIMINDEX_2; ARITH; set_of_list; MAP] THEN
  8.3394 +  CONV_TAC NUM_REDUCE_CONV THEN SIMP_TAC[DET_2; VECTOR_2] THEN
  8.3395 +  SIMP_TAC[VECTOR_SUB_COMPONENT; DIMINDEX_2; ARITH]);; *)
  8.3396 +
  8.3397 +lemma GMEASURABLE_TRIANGLE: True .. (*
  8.3398 + "!a b c:real^N. gmeasurable(convex hull {a,b,c})"
  8.3399 +qed   REPEAT GEN_TAC THEN
  8.3400 +  MATCH_MP_TAC GMEASURABLE_CONVEX THEN REWRITE_TAC[CONVEX_CONVEX_HULL] THEN
  8.3401 +  MATCH_MP_TAC BOUNDED_CONVEX_HULL THEN MATCH_MP_TAC FINITE_IMP_BOUNDED THEN
  8.3402 +  REWRITE_TAC[FINITE_INSERT; FINITE_RULES]);; *)
  8.3403 +
  8.3404 +lemma MEASURE_TRIANGLE: True .. (*
  8.3405 + "!a b c:real^2.
  8.3406 +        measure(convex hull {a,b,c}) =
  8.3407 +        abs((b$1 - a$1) * (c$2 - a$2) - (b$2 - a$2) * (c$1 - a$1)) / 2"
  8.3408 +qed   REWRITE_TAC[REWRITE_RULE[HAS_GMEASURE_MEASURABLE_MEASURE]
  8.3409 +               HAS_GMEASURE_TRIANGLE]);; *)
  8.3410 +
  8.3411 +(* ------------------------------------------------------------------------- *)
  8.3412 +(* Volume of a tetrahedron.                                                  *)
  8.3413 +(* ------------------------------------------------------------------------- *)
  8.3414 +
  8.3415 +lemma HAS_GMEASURE_TETRAHEDRON: True .. (*
  8.3416 + "!a b c d:real^3.
  8.3417 +        convex hull {a,b,c,d} has_gmeasure
  8.3418 +        abs((b$1 - a$1) * (c$2 - a$2) * (d$3 - a$3) +
  8.3419 +            (b$2 - a$2) * (c$3 - a$3) * (d$1 - a$1) +
  8.3420 +            (b$3 - a$3) * (c$1 - a$1) * (d$2 - a$2) -
  8.3421 +            (b$1 - a$1) * (c$3 - a$3) * (d$2 - a$2) -
  8.3422 +            (b$2 - a$2) * (c$1 - a$1) * (d$3 - a$3) -
  8.3423 +            (b$3 - a$3) * (c$2 - a$2) * (d$1 - a$1)) /
  8.3424 +           6"
  8.3425 +qed   REPEAT STRIP_TAC THEN
  8.3426 +  MP_TAC(ISPECL [`a:real^3`; `[b;c;d]:(real^3)list`] HAS_GMEASURE_SIMPLEX) THEN
  8.3427 +  REWRITE_TAC[LENGTH; DIMINDEX_3; ARITH; set_of_list; MAP] THEN
  8.3428 +  CONV_TAC NUM_REDUCE_CONV THEN SIMP_TAC[DET_3; VECTOR_3] THEN
  8.3429 +  SIMP_TAC[VECTOR_SUB_COMPONENT; DIMINDEX_3; ARITH]);; *)
  8.3430 +
  8.3431 +lemma GMEASURABLE_TETRAHEDRON: True .. (*
  8.3432 + "!a b c d:real^N. gmeasurable(convex hull {a,b,c,d})"
  8.3433 +qed   REPEAT GEN_TAC THEN
  8.3434 +  MATCH_MP_TAC GMEASURABLE_CONVEX THEN REWRITE_TAC[CONVEX_CONVEX_HULL] THEN
  8.3435 +  MATCH_MP_TAC BOUNDED_CONVEX_HULL THEN MATCH_MP_TAC FINITE_IMP_BOUNDED THEN
  8.3436 +  REWRITE_TAC[FINITE_INSERT; FINITE_RULES]);; *)
  8.3437 +
  8.3438 +lemma MEASURE_TETRAHEDRON: True .. (*
  8.3439 + "!a b c d:real^3.
  8.3440 +        measure(convex hull {a,b,c,d}) =
  8.3441 +        abs((b$1 - a$1) * (c$2 - a$2) * (d$3 - a$3) +
  8.3442 +            (b$2 - a$2) * (c$3 - a$3) * (d$1 - a$1) +
  8.3443 +            (b$3 - a$3) * (c$1 - a$1) * (d$2 - a$2) -
  8.3444 +            (b$1 - a$1) * (c$3 - a$3) * (d$2 - a$2) -
  8.3445 +            (b$2 - a$2) * (c$1 - a$1) * (d$3 - a$3) -
  8.3446 +            (b$3 - a$3) * (c$2 - a$2) * (d$1 - a$1)) / 6"
  8.3447 +qed   REWRITE_TAC[REWRITE_RULE[HAS_GMEASURE_MEASURABLE_MEASURE]
  8.3448 +               HAS_GMEASURE_TETRAHEDRON]);; *)
  8.3449 +
  8.3450 +end
     9.1 --- a/src/HOL/Multivariate_Analysis/Integration.thy	Mon Aug 23 17:46:13 2010 +0200
     9.2 +++ b/src/HOL/Multivariate_Analysis/Integration.thy	Mon Aug 23 19:35:57 2010 +0200
     9.3 @@ -5287,10 +5287,10 @@
     9.4      qed finally show ?case . qed qed
     9.5  
     9.6  lemma nonnegative_absolutely_integrable: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'm::ordered_euclidean_space"
     9.7 -  assumes "\<forall>x\<in>s. \<forall>i. 0 \<le> f(x)$$i" "f integrable_on s"
     9.8 +  assumes "\<forall>x\<in>s. \<forall>i<DIM('m). 0 \<le> f(x)$$i" "f integrable_on s"
     9.9    shows "f absolutely_integrable_on s"
    9.10    unfolding absolutely_integrable_abs_eq apply rule defer
    9.11 -  apply(rule integrable_eq[of _ f]) using assms by auto
    9.12 +  apply(rule integrable_eq[of _ f]) using assms apply-apply(subst euclidean_eq) by auto
    9.13  
    9.14  lemma absolutely_integrable_integrable_bound: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'm::ordered_euclidean_space"
    9.15    assumes "\<forall>x\<in>s. norm(f x) \<le> g x" "f integrable_on s" "g integrable_on s"
    10.1 --- a/src/HOL/Multivariate_Analysis/Multivariate_Analysis.thy	Mon Aug 23 17:46:13 2010 +0200
    10.2 +++ b/src/HOL/Multivariate_Analysis/Multivariate_Analysis.thy	Mon Aug 23 19:35:57 2010 +0200
    10.3 @@ -1,5 +1,5 @@
    10.4  theory Multivariate_Analysis
    10.5 -imports Integration Fashoda
    10.6 +imports Fashoda Gauge_Measure
    10.7  begin
    10.8  
    10.9  end
    11.1 --- a/src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy	Mon Aug 23 17:46:13 2010 +0200
    11.2 +++ b/src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy	Mon Aug 23 19:35:57 2010 +0200
    11.3 @@ -5105,6 +5105,60 @@
    11.4    shows "open {x::'a::euclidean_space. x$$i  > a}"
    11.5    using open_halfspace_gt[of a "(basis i)::'a"] unfolding euclidean_component_def .
    11.6  
    11.7 +text{* Instantiation for intervals on @{text ordered_euclidean_space} *}
    11.8 +
    11.9 +lemma eucl_lessThan_eq_halfspaces:
   11.10 +  fixes a :: "'a\<Colon>ordered_euclidean_space"
   11.11 +  shows "{..<a} = (\<Inter>i<DIM('a). {x. x $$ i < a $$ i})"
   11.12 + by (auto simp: eucl_less[where 'a='a])
   11.13 +
   11.14 +lemma eucl_greaterThan_eq_halfspaces:
   11.15 +  fixes a :: "'a\<Colon>ordered_euclidean_space"
   11.16 +  shows "{a<..} = (\<Inter>i<DIM('a). {x. a $$ i < x $$ i})"
   11.17 + by (auto simp: eucl_less[where 'a='a])
   11.18 +
   11.19 +lemma eucl_atMost_eq_halfspaces:
   11.20 +  fixes a :: "'a\<Colon>ordered_euclidean_space"
   11.21 +  shows "{.. a} = (\<Inter>i<DIM('a). {x. x $$ i \<le> a $$ i})"
   11.22 + by (auto simp: eucl_le[where 'a='a])
   11.23 +
   11.24 +lemma eucl_atLeast_eq_halfspaces:
   11.25 +  fixes a :: "'a\<Colon>ordered_euclidean_space"
   11.26 +  shows "{a ..} = (\<Inter>i<DIM('a). {x. a $$ i \<le> x $$ i})"
   11.27 + by (auto simp: eucl_le[where 'a='a])
   11.28 +
   11.29 +lemma open_eucl_lessThan[simp, intro]:
   11.30 +  fixes a :: "'a\<Colon>ordered_euclidean_space"
   11.31 +  shows "open {..< a}"
   11.32 +  by (auto simp: eucl_lessThan_eq_halfspaces open_halfspace_component_lt)
   11.33 +
   11.34 +lemma open_eucl_greaterThan[simp, intro]:
   11.35 +  fixes a :: "'a\<Colon>ordered_euclidean_space"
   11.36 +  shows "open {a <..}"
   11.37 +  by (auto simp: eucl_greaterThan_eq_halfspaces open_halfspace_component_gt)
   11.38 +
   11.39 +lemma closed_eucl_atMost[simp, intro]:
   11.40 +  fixes a :: "'a\<Colon>ordered_euclidean_space"
   11.41 +  shows "closed {.. a}"
   11.42 +  unfolding eucl_atMost_eq_halfspaces
   11.43 +proof (safe intro!: closed_INT)
   11.44 +  fix i :: nat
   11.45 +  have "- {x::'a. x $$ i \<le> a $$ i} = {x. a $$ i < x $$ i}" by auto
   11.46 +  then show "closed {x::'a. x $$ i \<le> a $$ i}"
   11.47 +    by (simp add: closed_def open_halfspace_component_gt)
   11.48 +qed
   11.49 +
   11.50 +lemma closed_eucl_atLeast[simp, intro]:
   11.51 +  fixes a :: "'a\<Colon>ordered_euclidean_space"
   11.52 +  shows "closed {a ..}"
   11.53 +  unfolding eucl_atLeast_eq_halfspaces
   11.54 +proof (safe intro!: closed_INT)
   11.55 +  fix i :: nat
   11.56 +  have "- {x::'a. a $$ i \<le> x $$ i} = {x. x $$ i < a $$ i}" by auto
   11.57 +  then show "closed {x::'a. a $$ i \<le> x $$ i}"
   11.58 +    by (simp add: closed_def open_halfspace_component_lt)
   11.59 +qed
   11.60 +
   11.61  text{* This gives a simple derivation of limit component bounds.                 *}
   11.62  
   11.63  lemma Lim_component_le: fixes f :: "'a \<Rightarrow> 'b::euclidean_space"
    12.1 --- a/src/HOL/Probability/Borel.thy	Mon Aug 23 17:46:13 2010 +0200
    12.2 +++ b/src/HOL/Probability/Borel.thy	Mon Aug 23 19:35:57 2010 +0200
    12.3 @@ -1,242 +1,199 @@
    12.4 -header {*Borel Sets*}
    12.5 +(* Author: Armin Heller, Johannes Hoelzl, TU Muenchen *)
    12.6 +
    12.7 +header {*Borel spaces*}
    12.8  
    12.9  theory Borel
   12.10 -  imports Measure
   12.11 +  imports Sigma_Algebra Positive_Infinite_Real Multivariate_Analysis
   12.12  begin
   12.13  
   12.14 -text{*From the Hurd/Coble measure theory development, translated by Lawrence Paulson.*}
   12.15 -
   12.16 -definition borel_space where
   12.17 -  "borel_space = sigma (UNIV::real set) (range (\<lambda>a::real. {x. x \<le> a}))"
   12.18 +section "Generic Borel spaces"
   12.19  
   12.20 -definition borel_measurable where
   12.21 -  "borel_measurable a = measurable a borel_space"
   12.22 +definition "borel_space = sigma (UNIV::'a::topological_space set) open"
   12.23 +abbreviation "borel_measurable M \<equiv> measurable M borel_space"
   12.24  
   12.25 -definition indicator_fn where
   12.26 -  "indicator_fn s = (\<lambda>x. if x \<in> s then 1 else (0::real))"
   12.27 +interpretation borel_space: sigma_algebra borel_space
   12.28 +  using sigma_algebra_sigma by (auto simp: borel_space_def)
   12.29  
   12.30  lemma in_borel_measurable:
   12.31     "f \<in> borel_measurable M \<longleftrightarrow>
   12.32 -    sigma_algebra M \<and>
   12.33 -    (\<forall>s \<in> sets (sigma UNIV (range (\<lambda>a::real. {x. x \<le> a}))).
   12.34 -      f -` s \<inter> space M \<in> sets M)"
   12.35 -apply (auto simp add: borel_measurable_def measurable_def borel_space_def) 
   12.36 -apply (metis PowD UNIV_I Un_commute sigma_algebra_sigma subset_Pow_Union subset_UNIV subset_Un_eq) 
   12.37 -done
   12.38 -
   12.39 -lemma (in sigma_algebra) borel_measurable_const:
   12.40 -   "(\<lambda>x. c) \<in> borel_measurable M"
   12.41 -  by (auto simp add: in_borel_measurable prems)
   12.42 -
   12.43 -lemma (in sigma_algebra) borel_measurable_indicator:
   12.44 -  assumes a: "a \<in> sets M"
   12.45 -  shows "indicator_fn a \<in> borel_measurable M"
   12.46 -apply (auto simp add: in_borel_measurable indicator_fn_def prems)
   12.47 -apply (metis Diff_eq Int_commute a compl_sets) 
   12.48 -done
   12.49 +    (\<forall>S \<in> sets (sigma UNIV open).
   12.50 +      f -` S \<inter> space M \<in> sets M)"
   12.51 +  by (auto simp add: measurable_def borel_space_def)
   12.52  
   12.53 -lemma Collect_eq: "{w \<in> X. f w \<le> a} = {w. f w \<le> a} \<inter> X"
   12.54 -  by (metis Collect_conj_eq Collect_mem_eq Int_commute)
   12.55 +lemma in_borel_measurable_borel_space:
   12.56 +   "f \<in> borel_measurable M \<longleftrightarrow>
   12.57 +    (\<forall>S \<in> sets borel_space.
   12.58 +      f -` S \<inter> space M \<in> sets M)"
   12.59 +  by (auto simp add: measurable_def borel_space_def)
   12.60  
   12.61 -lemma (in measure_space) borel_measurable_le_iff:
   12.62 -   "f \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
   12.63 -proof 
   12.64 -  assume f: "f \<in> borel_measurable M"
   12.65 -  { fix a
   12.66 -    have "{w \<in> space M. f w \<le> a} \<in> sets M" using f
   12.67 -      apply (auto simp add: in_borel_measurable sigma_def Collect_eq)
   12.68 -      apply (drule_tac x="{x. x \<le> a}" in bspec, auto)
   12.69 -      apply (metis equalityE rangeI subsetD sigma_sets.Basic)  
   12.70 -      done
   12.71 -    }
   12.72 -  thus "\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M" by blast
   12.73 -next
   12.74 -  assume "\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M"
   12.75 -  thus "f \<in> borel_measurable M" 
   12.76 -    apply (simp add: borel_measurable_def borel_space_def Collect_eq) 
   12.77 -    apply (rule measurable_sigma, auto) 
   12.78 -    done
   12.79 +lemma space_borel_space[simp]: "space borel_space = UNIV"
   12.80 +  unfolding borel_space_def by auto
   12.81 +
   12.82 +lemma borel_space_open[simp]:
   12.83 +  assumes "open A" shows "A \<in> sets borel_space"
   12.84 +proof -
   12.85 +  have "A \<in> open" unfolding mem_def using assms .
   12.86 +  thus ?thesis unfolding borel_space_def sigma_def by (auto intro!: sigma_sets.Basic)
   12.87  qed
   12.88  
   12.89 -lemma Collect_less_le:
   12.90 -     "{w \<in> X. f w < g w} = (\<Union>n. {w \<in> X. f w \<le> g w - inverse(real(Suc n))})"
   12.91 -  proof auto
   12.92 -    fix w
   12.93 -    assume w: "f w < g w"
   12.94 -    hence nz: "g w - f w \<noteq> 0"
   12.95 -      by arith
   12.96 -    with w have "real(Suc(natceiling(inverse(g w - f w)))) > inverse(g w - f w)"
   12.97 -      by (metis lessI order_le_less_trans real_natceiling_ge real_of_nat_less_iff)       hence "inverse(real(Suc(natceiling(inverse(g w - f w)))))
   12.98 -             < inverse(inverse(g w - f w))" 
   12.99 -      by (metis less_iff_diff_less_0 less_imp_inverse_less linorder_neqE_linordered_idom nz positive_imp_inverse_positive order_antisym less_le w)
  12.100 -    hence "inverse(real(Suc(natceiling(inverse(g w - f w))))) < g w - f w"
  12.101 -      by (metis inverse_inverse_eq order_less_le_trans order_refl)
  12.102 -    thus "\<exists>n. f w \<le> g w - inverse(real(Suc n))" using w
  12.103 -      by (rule_tac x="natceiling(inverse(g w - f w))" in exI, auto)
  12.104 -  next
  12.105 -    fix w n
  12.106 -    assume le: "f w \<le> g w - inverse(real(Suc n))"
  12.107 -    hence "0 < inverse(real(Suc n))"
  12.108 -      by simp
  12.109 -    thus "f w < g w" using le
  12.110 -      by arith 
  12.111 -  qed
  12.112 -
  12.113 -
  12.114 -lemma (in sigma_algebra) sigma_le_less:
  12.115 -  assumes M: "!!a::real. {w \<in> space M. f w \<le> a} \<in> sets M"
  12.116 -  shows "{w \<in> space M. f w < a} \<in> sets M"
  12.117 +lemma borel_space_closed[simp]:
  12.118 +  assumes "closed A" shows "A \<in> sets borel_space"
  12.119  proof -
  12.120 -  show ?thesis using Collect_less_le [of "space M" f "\<lambda>x. a"]
  12.121 -    by (auto simp add: countable_UN M) 
  12.122 +  have "space borel_space - (- A) \<in> sets borel_space"
  12.123 +    using assms unfolding closed_def by (blast intro: borel_space_open)
  12.124 +  thus ?thesis by simp
  12.125  qed
  12.126  
  12.127 -lemma (in sigma_algebra) sigma_less_ge:
  12.128 -  assumes M: "!!a::real. {w \<in> space M. f w < a} \<in> sets M"
  12.129 -  shows "{w \<in> space M. a \<le> f w} \<in> sets M"
  12.130 -proof -
  12.131 -  have "{w \<in> space M. a \<le> f w} = space M - {w \<in> space M. f w < a}"
  12.132 -    by auto
  12.133 -  thus ?thesis using M
  12.134 -    by auto
  12.135 -qed
  12.136 -
  12.137 -lemma (in sigma_algebra) sigma_ge_gr:
  12.138 -  assumes M: "!!a::real. {w \<in> space M. a \<le> f w} \<in> sets M"
  12.139 -  shows "{w \<in> space M. a < f w} \<in> sets M"
  12.140 -proof -
  12.141 -  show ?thesis using Collect_less_le [of "space M" "\<lambda>x. a" f]
  12.142 -    by (auto simp add: countable_UN le_diff_eq M) 
  12.143 +lemma (in sigma_algebra) borel_measurable_vimage:
  12.144 +  fixes f :: "'a \<Rightarrow> 'x::t2_space"
  12.145 +  assumes borel: "f \<in> borel_measurable M"
  12.146 +  shows "f -` {x} \<inter> space M \<in> sets M"
  12.147 +proof (cases "x \<in> f ` space M")
  12.148 +  case True then obtain y where "x = f y" by auto
  12.149 +  from closed_sing[of "f y"]
  12.150 +  have "{f y} \<in> sets borel_space" by (rule borel_space_closed)
  12.151 +  with assms show ?thesis
  12.152 +    unfolding in_borel_measurable_borel_space `x = f y` by auto
  12.153 +next
  12.154 +  case False hence "f -` {x} \<inter> space M = {}" by auto
  12.155 +  thus ?thesis by auto
  12.156  qed
  12.157  
  12.158 -lemma (in sigma_algebra) sigma_gr_le:
  12.159 -  assumes M: "!!a::real. {w \<in> space M. a < f w} \<in> sets M"
  12.160 -  shows "{w \<in> space M. f w \<le> a} \<in> sets M"
  12.161 -proof -
  12.162 -  have "{w \<in> space M. f w \<le> a} = space M - {w \<in> space M. a < f w}" 
  12.163 -    by auto
  12.164 -  thus ?thesis
  12.165 -    by (simp add: M compl_sets)
  12.166 -qed
  12.167 +lemma (in sigma_algebra) borel_measurableI:
  12.168 +  fixes f :: "'a \<Rightarrow> 'x\<Colon>topological_space"
  12.169 +  assumes "\<And>S. open S \<Longrightarrow> f -` S \<inter> space M \<in> sets M"
  12.170 +  shows "f \<in> borel_measurable M"
  12.171 +  unfolding borel_space_def
  12.172 +proof (rule measurable_sigma)
  12.173 +  fix S :: "'x set" assume "S \<in> open" thus "f -` S \<inter> space M \<in> sets M"
  12.174 +    using assms[of S] by (simp add: mem_def)
  12.175 +qed simp_all
  12.176  
  12.177 -lemma (in measure_space) borel_measurable_gr_iff:
  12.178 -   "f \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
  12.179 -proof (auto simp add: borel_measurable_le_iff sigma_gr_le) 
  12.180 -  fix u
  12.181 -  assume M: "\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M"
  12.182 -  have "{w \<in> space M. u < f w} = space M - {w \<in> space M. f w \<le> u}"
  12.183 -    by auto
  12.184 -  thus "{w \<in> space M. u < f w} \<in> sets M"
  12.185 -    by (force simp add: compl_sets countable_UN M)
  12.186 -qed
  12.187 +lemma borel_space_singleton[simp, intro]:
  12.188 +  fixes x :: "'a::t1_space"
  12.189 +  shows "A \<in> sets borel_space \<Longrightarrow> insert x A \<in> sets borel_space"
  12.190 +  proof (rule borel_space.insert_in_sets)
  12.191 +    show "{x} \<in> sets borel_space"
  12.192 +      using closed_sing[of x] by (rule borel_space_closed)
  12.193 +  qed simp
  12.194 +
  12.195 +lemma (in sigma_algebra) borel_measurable_const[simp, intro]:
  12.196 +  "(\<lambda>x. c) \<in> borel_measurable M"
  12.197 +  by (auto intro!: measurable_const)
  12.198  
  12.199 -lemma (in measure_space) borel_measurable_less_iff:
  12.200 -   "f \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
  12.201 -proof (auto simp add: borel_measurable_le_iff sigma_le_less) 
  12.202 -  fix u
  12.203 -  assume M: "\<forall>a. {w \<in> space M. f w < a} \<in> sets M"
  12.204 -  have "{w \<in> space M. f w \<le> u} = space M - {w \<in> space M. u < f w}"
  12.205 -    by auto
  12.206 -  thus "{w \<in> space M. f w \<le> u} \<in> sets M"
  12.207 -    using Collect_less_le [of "space M" "\<lambda>x. u" f] 
  12.208 -    by (force simp add: compl_sets countable_UN le_diff_eq sigma_less_ge M)
  12.209 -qed
  12.210 +lemma (in sigma_algebra) borel_measurable_indicator:
  12.211 +  assumes A: "A \<in> sets M"
  12.212 +  shows "indicator A \<in> borel_measurable M"
  12.213 +  unfolding indicator_def_raw using A
  12.214 +  by (auto intro!: measurable_If_set borel_measurable_const)
  12.215  
  12.216 -lemma (in measure_space) borel_measurable_ge_iff:
  12.217 -   "f \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
  12.218 -proof (auto simp add: borel_measurable_less_iff sigma_le_less sigma_ge_gr sigma_gr_le) 
  12.219 -  fix u
  12.220 -  assume M: "\<forall>a. {w \<in> space M. f w < a} \<in> sets M"
  12.221 -  have "{w \<in> space M. u \<le> f w} = space M - {w \<in> space M. f w < u}"
  12.222 -    by auto
  12.223 -  thus "{w \<in> space M. u \<le> f w} \<in> sets M"
  12.224 -    by (force simp add: compl_sets countable_UN M)
  12.225 +lemma borel_measurable_translate:
  12.226 +  assumes "A \<in> sets borel_space" and trans: "\<And>B. open B \<Longrightarrow> f -` B \<in> sets borel_space"
  12.227 +  shows "f -` A \<in> sets borel_space"
  12.228 +proof -
  12.229 +  have "A \<in> sigma_sets UNIV open" using assms
  12.230 +    by (simp add: borel_space_def sigma_def)
  12.231 +  thus ?thesis
  12.232 +  proof (induct rule: sigma_sets.induct)
  12.233 +    case (Basic a) thus ?case using trans[of a] by (simp add: mem_def)
  12.234 +  next
  12.235 +    case (Compl a)
  12.236 +    moreover have "UNIV \<in> sets borel_space"
  12.237 +      by (metis borel_space.top borel_space_def_raw mem_def space_sigma)
  12.238 +    ultimately show ?case
  12.239 +      by (auto simp: vimage_Diff borel_space.Diff)
  12.240 +  qed (auto simp add: vimage_UN)
  12.241  qed
  12.242  
  12.243 -lemma (in measure_space) affine_borel_measurable:
  12.244 -  assumes g: "g \<in> borel_measurable M"
  12.245 -  shows "(\<lambda>x. a + (g x) * b) \<in> borel_measurable M"
  12.246 -proof (cases rule: linorder_cases [of b 0])
  12.247 -  case equal thus ?thesis
  12.248 -    by (simp add: borel_measurable_const)
  12.249 -next
  12.250 -  case less
  12.251 -    {
  12.252 -      fix w c
  12.253 -      have "a + g w * b \<le> c \<longleftrightarrow> g w * b \<le> c - a"
  12.254 -        by auto
  12.255 -      also have "... \<longleftrightarrow> (c-a)/b \<le> g w" using less
  12.256 -        by (metis divide_le_eq less less_asym)
  12.257 -      finally have "a + g w * b \<le> c \<longleftrightarrow> (c-a)/b \<le> g w" .
  12.258 -    }
  12.259 -    hence "\<And>w c. a + g w * b \<le> c \<longleftrightarrow> (c-a)/b \<le> g w" .
  12.260 -    thus ?thesis using less g
  12.261 -      by (simp add: borel_measurable_ge_iff [of g]) 
  12.262 -         (simp add: borel_measurable_le_iff)
  12.263 -next
  12.264 -  case greater
  12.265 -    hence 0: "\<And>x c. (g x * b \<le> c - a) \<longleftrightarrow> (g x \<le> (c - a) / b)"
  12.266 -      by (metis mult_imp_le_div_pos le_divide_eq) 
  12.267 -    have 1: "\<And>x c. (a + g x * b \<le> c) \<longleftrightarrow> (g x * b \<le> c - a)"
  12.268 -      by auto
  12.269 -    from greater g
  12.270 -    show ?thesis
  12.271 -      by (simp add: borel_measurable_le_iff 0 1) 
  12.272 -qed
  12.273 +section "Borel spaces on euclidean spaces"
  12.274 +
  12.275 +lemma lessThan_borel[simp, intro]:
  12.276 +  fixes a :: "'a\<Colon>ordered_euclidean_space"
  12.277 +  shows "{..< a} \<in> sets borel_space"
  12.278 +  by (blast intro: borel_space_open)
  12.279 +
  12.280 +lemma greaterThan_borel[simp, intro]:
  12.281 +  fixes a :: "'a\<Colon>ordered_euclidean_space"
  12.282 +  shows "{a <..} \<in> sets borel_space"
  12.283 +  by (blast intro: borel_space_open)
  12.284 +
  12.285 +lemma greaterThanLessThan_borel[simp, intro]:
  12.286 +  fixes a b :: "'a\<Colon>ordered_euclidean_space"
  12.287 +  shows "{a<..<b} \<in> sets borel_space"
  12.288 +  by (blast intro: borel_space_open)
  12.289 +
  12.290 +lemma atMost_borel[simp, intro]:
  12.291 +  fixes a :: "'a\<Colon>ordered_euclidean_space"
  12.292 +  shows "{..a} \<in> sets borel_space"
  12.293 +  by (blast intro: borel_space_closed)
  12.294 +
  12.295 +lemma atLeast_borel[simp, intro]:
  12.296 +  fixes a :: "'a\<Colon>ordered_euclidean_space"
  12.297 +  shows "{a..} \<in> sets borel_space"
  12.298 +  by (blast intro: borel_space_closed)
  12.299 +
  12.300 +lemma atLeastAtMost_borel[simp, intro]:
  12.301 +  fixes a b :: "'a\<Colon>ordered_euclidean_space"
  12.302 +  shows "{a..b} \<in> sets borel_space"
  12.303 +  by (blast intro: borel_space_closed)
  12.304  
  12.305 -definition
  12.306 -  nat_to_rat_surj :: "nat \<Rightarrow> rat" where
  12.307 - "nat_to_rat_surj n = (let (i,j) = prod_decode n
  12.308 -                       in Fract (int_decode i) (int_decode j))"
  12.309 +lemma greaterThanAtMost_borel[simp, intro]:
  12.310 +  fixes a b :: "'a\<Colon>ordered_euclidean_space"
  12.311 +  shows "{a<..b} \<in> sets borel_space"
  12.312 +  unfolding greaterThanAtMost_def by blast
  12.313 +
  12.314 +lemma atLeastLessThan_borel[simp, intro]:
  12.315 +  fixes a b :: "'a\<Colon>ordered_euclidean_space"
  12.316 +  shows "{a..<b} \<in> sets borel_space"
  12.317 +  unfolding atLeastLessThan_def by blast
  12.318 +
  12.319 +lemma hafspace_less_borel[simp, intro]:
  12.320 +  fixes a :: real
  12.321 +  shows "{x::'a::euclidean_space. a < x $$ i} \<in> sets borel_space"
  12.322 +  by (auto intro!: borel_space_open open_halfspace_component_gt)
  12.323  
  12.324 -lemma nat_to_rat_surj: "surj nat_to_rat_surj"
  12.325 -proof (auto simp add: surj_def nat_to_rat_surj_def) 
  12.326 -  fix y
  12.327 -  show "\<exists>x. y = (\<lambda>(i, j). Fract (int_decode i) (int_decode j)) (prod_decode x)"
  12.328 -  proof (cases y)
  12.329 -    case (Fract a b)
  12.330 -      obtain i where i: "int_decode i = a" using surj_int_decode
  12.331 -        by (metis surj_def) 
  12.332 -      obtain j where j: "int_decode j = b" using surj_int_decode
  12.333 -        by (metis surj_def)
  12.334 -      obtain n where n: "prod_decode n = (i,j)" using surj_prod_decode
  12.335 -        by (metis surj_def)
  12.336 +lemma hafspace_greater_borel[simp, intro]:
  12.337 +  fixes a :: real
  12.338 +  shows "{x::'a::euclidean_space. x $$ i < a} \<in> sets borel_space"
  12.339 +  by (auto intro!: borel_space_open open_halfspace_component_lt)
  12.340  
  12.341 -      from Fract i j n show ?thesis
  12.342 -        by (metis prod.cases)
  12.343 -  qed
  12.344 -qed
  12.345 +lemma hafspace_less_eq_borel[simp, intro]:
  12.346 +  fixes a :: real
  12.347 +  shows "{x::'a::euclidean_space. a \<le> x $$ i} \<in> sets borel_space"
  12.348 +  by (auto intro!: borel_space_closed closed_halfspace_component_ge)
  12.349  
  12.350 -lemma rats_enumeration: "\<rat> = range (of_rat o nat_to_rat_surj)" 
  12.351 -  using nat_to_rat_surj
  12.352 -  by (auto simp add: image_def surj_def) (metis Rats_cases) 
  12.353 +lemma hafspace_greater_eq_borel[simp, intro]:
  12.354 +  fixes a :: real
  12.355 +  shows "{x::'a::euclidean_space. x $$ i \<le> a} \<in> sets borel_space"
  12.356 +  by (auto intro!: borel_space_closed closed_halfspace_component_le)
  12.357  
  12.358 -lemma (in measure_space) borel_measurable_less_borel_measurable:
  12.359 +lemma (in sigma_algebra) borel_measurable_less[simp, intro]:
  12.360 +  fixes f :: "'a \<Rightarrow> real"
  12.361    assumes f: "f \<in> borel_measurable M"
  12.362    assumes g: "g \<in> borel_measurable M"
  12.363    shows "{w \<in> space M. f w < g w} \<in> sets M"
  12.364  proof -
  12.365    have "{w \<in> space M. f w < g w} =
  12.366 -        (\<Union>r\<in>\<rat>. {w \<in> space M. f w < r} \<inter> {w \<in> space M. r < g w })"
  12.367 -    by (auto simp add: Rats_dense_in_real)
  12.368 -  thus ?thesis using f g 
  12.369 -    by (simp add: borel_measurable_less_iff [of f]  
  12.370 -                  borel_measurable_gr_iff [of g]) 
  12.371 -       (blast intro: gen_countable_UN [OF rats_enumeration])
  12.372 +        (\<Union>r. (f -` {..< of_rat r} \<inter> space M) \<inter> (g -` {of_rat r <..} \<inter> space M))"
  12.373 +    using Rats_dense_in_real by (auto simp add: Rats_def)
  12.374 +  then show ?thesis using f g
  12.375 +    by simp (blast intro: measurable_sets)
  12.376  qed
  12.377 - 
  12.378 -lemma (in measure_space) borel_measurable_leq_borel_measurable:
  12.379 +
  12.380 +lemma (in sigma_algebra) borel_measurable_le[simp, intro]:
  12.381 +  fixes f :: "'a \<Rightarrow> real"
  12.382    assumes f: "f \<in> borel_measurable M"
  12.383    assumes g: "g \<in> borel_measurable M"
  12.384    shows "{w \<in> space M. f w \<le> g w} \<in> sets M"
  12.385  proof -
  12.386 -  have "{w \<in> space M. f w \<le> g w} = space M - {w \<in> space M. g w < f w}" 
  12.387 -    by auto 
  12.388 -  thus ?thesis using f g 
  12.389 -    by (simp add: borel_measurable_less_borel_measurable compl_sets)
  12.390 +  have "{w \<in> space M. f w \<le> g w} = space M - {w \<in> space M. g w < f w}"
  12.391 +    by auto
  12.392 +  thus ?thesis using f g
  12.393 +    by simp blast
  12.394  qed
  12.395  
  12.396 -lemma (in measure_space) borel_measurable_eq_borel_measurable:
  12.397 +lemma (in sigma_algebra) borel_measurable_eq[simp, intro]:
  12.398 +  fixes f :: "'a \<Rightarrow> real"
  12.399    assumes f: "f \<in> borel_measurable M"
  12.400    assumes g: "g \<in> borel_measurable M"
  12.401    shows "{w \<in> space M. f w = g w} \<in> sets M"
  12.402 @@ -244,40 +201,512 @@
  12.403    have "{w \<in> space M. f w = g w} =
  12.404          {w \<in> space M. f w \<le> g w} \<inter> {w \<in> space M. g w \<le> f w}"
  12.405      by auto
  12.406 -  thus ?thesis using f g 
  12.407 -    by (simp add: borel_measurable_leq_borel_measurable Int) 
  12.408 +  thus ?thesis using f g by auto
  12.409  qed
  12.410  
  12.411 -lemma (in measure_space) borel_measurable_neq_borel_measurable:
  12.412 +lemma (in sigma_algebra) borel_measurable_neq[simp, intro]:
  12.413 +  fixes f :: "'a \<Rightarrow> real"
  12.414    assumes f: "f \<in> borel_measurable M"
  12.415    assumes g: "g \<in> borel_measurable M"
  12.416    shows "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
  12.417  proof -
  12.418    have "{w \<in> space M. f w \<noteq> g w} = space M - {w \<in> space M. f w = g w}"
  12.419      by auto
  12.420 -  thus ?thesis using f g 
  12.421 -    by (simp add: borel_measurable_eq_borel_measurable compl_sets) 
  12.422 +  thus ?thesis using f g by auto
  12.423 +qed
  12.424 +
  12.425 +subsection "Borel space equals sigma algebras over intervals"
  12.426 +
  12.427 +lemma rational_boxes:
  12.428 +  fixes x :: "'a\<Colon>ordered_euclidean_space"
  12.429 +  assumes "0 < e"
  12.430 +  shows "\<exists>a b. (\<forall>i. a $$ i \<in> \<rat>) \<and> (\<forall>i. b $$ i \<in> \<rat>) \<and> x \<in> {a <..< b} \<and> {a <..< b} \<subseteq> ball x e"
  12.431 +proof -
  12.432 +  def e' \<equiv> "e / (2 * sqrt (real (DIM ('a))))"
  12.433 +  then have e: "0 < e'" using assms by (auto intro!: divide_pos_pos)
  12.434 +  have "\<forall>i. \<exists>y. y \<in> \<rat> \<and> y < x $$ i \<and> x $$ i - y < e'" (is "\<forall>i. ?th i")
  12.435 +  proof
  12.436 +    fix i from Rats_dense_in_real[of "x $$ i - e'" "x $$ i"] e
  12.437 +    show "?th i" by auto
  12.438 +  qed
  12.439 +  from choice[OF this] guess a .. note a = this
  12.440 +  have "\<forall>i. \<exists>y. y \<in> \<rat> \<and> x $$ i < y \<and> y - x $$ i < e'" (is "\<forall>i. ?th i")
  12.441 +  proof
  12.442 +    fix i from Rats_dense_in_real[of "x $$ i" "x $$ i + e'"] e
  12.443 +    show "?th i" by auto
  12.444 +  qed
  12.445 +  from choice[OF this] guess b .. note b = this
  12.446 +  { fix y :: 'a assume *: "Chi a < y" "y < Chi b"
  12.447 +    have "dist x y = sqrt (\<Sum>i<DIM('a). (dist (x $$ i) (y $$ i))\<twosuperior>)"
  12.448 +      unfolding setL2_def[symmetric] by (rule euclidean_dist_l2)
  12.449 +    also have "\<dots> < sqrt (\<Sum>i<DIM('a). e^2 / real (DIM('a)))"
  12.450 +    proof (rule real_sqrt_less_mono, rule setsum_strict_mono)
  12.451 +      fix i assume i: "i \<in> {..<DIM('a)}"
  12.452 +      have "a i < y$$i \<and> y$$i < b i" using * i eucl_less[where 'a='a] by auto
  12.453 +      moreover have "a i < x$$i" "x$$i - a i < e'" using a by auto
  12.454 +      moreover have "x$$i < b i" "b i - x$$i < e'" using b by auto
  12.455 +      ultimately have "\<bar>x$$i - y$$i\<bar> < 2 * e'" by auto
  12.456 +      then have "dist (x $$ i) (y $$ i) < e/sqrt (real (DIM('a)))"
  12.457 +        unfolding e'_def by (auto simp: dist_real_def)
  12.458 +      then have "(dist (x $$ i) (y $$ i))\<twosuperior> < (e/sqrt (real (DIM('a))))\<twosuperior>"
  12.459 +        by (rule power_strict_mono) auto
  12.460 +      then show "(dist (x $$ i) (y $$ i))\<twosuperior> < e\<twosuperior> / real DIM('a)"
  12.461 +        by (simp add: power_divide)
  12.462 +    qed auto
  12.463 +    also have "\<dots> = e" using `0 < e` by (simp add: real_eq_of_nat DIM_positive)
  12.464 +    finally have "dist x y < e" . }
  12.465 +  with a b show ?thesis
  12.466 +    apply (rule_tac exI[of _ "Chi a"])
  12.467 +    apply (rule_tac exI[of _ "Chi b"])
  12.468 +    using eucl_less[where 'a='a] by auto
  12.469 +qed
  12.470 +
  12.471 +lemma ex_rat_list:
  12.472 +  fixes x :: "'a\<Colon>ordered_euclidean_space"
  12.473 +  assumes "\<And> i. x $$ i \<in> \<rat>"
  12.474 +  shows "\<exists> r. length r = DIM('a) \<and> (\<forall> i < DIM('a). of_rat (r ! i) = x $$ i)"
  12.475 +proof -
  12.476 +  have "\<forall>i. \<exists>r. x $$ i = of_rat r" using assms unfolding Rats_def by blast
  12.477 +  from choice[OF this] guess r ..
  12.478 +  then show ?thesis by (auto intro!: exI[of _ "map r [0 ..< DIM('a)]"])
  12.479 +qed
  12.480 +
  12.481 +lemma open_UNION:
  12.482 +  fixes M :: "'a\<Colon>ordered_euclidean_space set"
  12.483 +  assumes "open M"
  12.484 +  shows "M = UNION {(a, b) | a b. {Chi (of_rat \<circ> op ! a) <..< Chi (of_rat \<circ> op ! b)} \<subseteq> M}
  12.485 +                   (\<lambda> (a, b). {Chi (of_rat \<circ> op ! a) <..< Chi (of_rat \<circ> op ! b)})"
  12.486 +    (is "M = UNION ?idx ?box")
  12.487 +proof safe
  12.488 +  fix x assume "x \<in> M"
  12.489 +  obtain e where e: "e > 0" "ball x e \<subseteq> M"
  12.490 +    using openE[OF assms `x \<in> M`] by auto
  12.491 +  then obtain a b where ab: "x \<in> {a <..< b}" "\<And>i. a $$ i \<in> \<rat>" "\<And>i. b $$ i \<in> \<rat>" "{a <..< b} \<subseteq> ball x e"
  12.492 +    using rational_boxes[OF e(1)] by blast
  12.493 +  then obtain p q where pq: "length p = DIM ('a)"
  12.494 +                            "length q = DIM ('a)"
  12.495 +                            "\<forall> i < DIM ('a). of_rat (p ! i) = a $$ i \<and> of_rat (q ! i) = b $$ i"
  12.496 +    using ex_rat_list[OF ab(2)] ex_rat_list[OF ab(3)] by blast
  12.497 +  hence p: "Chi (of_rat \<circ> op ! p) = a"
  12.498 +    using euclidean_eq[of "Chi (of_rat \<circ> op ! p)" a]
  12.499 +    unfolding o_def by auto
  12.500 +  from pq have q: "Chi (of_rat \<circ> op ! q) = b"
  12.501 +    using euclidean_eq[of "Chi (of_rat \<circ> op ! q)" b]
  12.502 +    unfolding o_def by auto
  12.503 +  have "x \<in> ?box (p, q)"
  12.504 +    using p q ab by auto
  12.505 +  thus "x \<in> UNION ?idx ?box" using ab e p q exI[of _ p] exI[of _ q] by auto
  12.506 +qed auto
  12.507 +
  12.508 +lemma halfspace_span_open:
  12.509 +  "sets (sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})))
  12.510 +    \<subseteq> sets borel_space"
  12.511 +  by (auto intro!: borel_space.sigma_sets_subset[simplified] borel_space_open
  12.512 +                   open_halfspace_component_lt simp: sets_sigma)
  12.513 +
  12.514 +lemma halfspace_lt_in_halfspace:
  12.515 +  "{x\<Colon>'a. x $$ i < a} \<in> sets (sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})))"
  12.516 +  unfolding sets_sigma by (rule sigma_sets.Basic) auto
  12.517 +
  12.518 +lemma halfspace_gt_in_halfspace:
  12.519 +  "{x\<Colon>'a. a < x $$ i} \<in> sets (sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})))"
  12.520 +    (is "?set \<in> sets ?SIGMA")
  12.521 +proof -
  12.522 +  interpret sigma_algebra ?SIGMA by (rule sigma_algebra_sigma) simp
  12.523 +  have *: "?set = (\<Union>n. space ?SIGMA - {x\<Colon>'a. x $$ i < a + 1 / real (Suc n)})"
  12.524 +  proof (safe, simp_all add: not_less)
  12.525 +    fix x assume "a < x $$ i"
  12.526 +    with reals_Archimedean[of "x $$ i - a"]
  12.527 +    obtain n where "a + 1 / real (Suc n) < x $$ i"
  12.528 +      by (auto simp: inverse_eq_divide field_simps)
  12.529 +    then show "\<exists>n. a + 1 / real (Suc n) \<le> x $$ i"
  12.530 +      by (blast intro: less_imp_le)
  12.531 +  next
  12.532 +    fix x n
  12.533 +    have "a < a + 1 / real (Suc n)" by auto
  12.534 +    also assume "\<dots> \<le> x"
  12.535 +    finally show "a < x" .
  12.536 +  qed
  12.537 +  show "?set \<in> sets ?SIGMA" unfolding *
  12.538 +    by (safe intro!: countable_UN Diff halfspace_lt_in_halfspace)
  12.539  qed
  12.540  
  12.541 -lemma (in measure_space) borel_measurable_add_borel_measurable:
  12.542 +lemma (in sigma_algebra) sets_sigma_subset:
  12.543 +  assumes "A = space M"
  12.544 +  assumes "B \<subseteq> sets M"
  12.545 +  shows "sets (sigma A B) \<subseteq> sets M"
  12.546 +  by (unfold assms sets_sigma, rule sigma_sets_subset, rule assms)
  12.547 +
  12.548 +lemma open_span_halfspace:
  12.549 +  "sets borel_space \<subseteq> sets (sigma UNIV (range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x $$ i < a})))"
  12.550 +    (is "_ \<subseteq> sets ?SIGMA")
  12.551 +proof (unfold borel_space_def, rule sigma_algebra.sets_sigma_subset, safe)
  12.552 +  show "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) simp
  12.553 +  then interpret sigma_algebra ?SIGMA .
  12.554 +  fix S :: "'a set" assume "S \<in> open" then have "open S" unfolding mem_def .
  12.555 +  from open_UNION[OF this]
  12.556 +  obtain I where *: "S =
  12.557 +    (\<Union>(a, b)\<in>I.
  12.558 +        (\<Inter> i<DIM('a). {x. (Chi (real_of_rat \<circ> op ! a)::'a) $$ i < x $$ i}) \<inter>
  12.559 +        (\<Inter> i<DIM('a). {x. x $$ i < (Chi (real_of_rat \<circ> op ! b)::'a) $$ i}))"
  12.560 +    unfolding greaterThanLessThan_def
  12.561 +    unfolding eucl_greaterThan_eq_halfspaces[where 'a='a]
  12.562 +    unfolding eucl_lessThan_eq_halfspaces[where 'a='a]
  12.563 +    by blast
  12.564 +  show "S \<in> sets ?SIGMA"
  12.565 +    unfolding *
  12.566 +    by (auto intro!: countable_UN Int countable_INT halfspace_lt_in_halfspace halfspace_gt_in_halfspace)
  12.567 +qed auto
  12.568 +
  12.569 +lemma halfspace_span_halfspace_le:
  12.570 +  "sets (sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a}))) \<subseteq>
  12.571 +   sets (sigma UNIV (range (\<lambda> (a, i). {x. x $$ i \<le> a})))"
  12.572 +  (is "_ \<subseteq> sets ?SIGMA")
  12.573 +proof (rule sigma_algebra.sets_sigma_subset, safe)
  12.574 +  show "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
  12.575 +  then interpret sigma_algebra ?SIGMA .
  12.576 +  fix a i
  12.577 +  have *: "{x::'a. x$$i < a} = (\<Union>n. {x. x$$i \<le> a - 1/real (Suc n)})"
  12.578 +  proof (safe, simp_all)
  12.579 +    fix x::'a assume *: "x$$i < a"
  12.580 +    with reals_Archimedean[of "a - x$$i"]
  12.581 +    obtain n where "x $$ i < a - 1 / (real (Suc n))"
  12.582 +      by (auto simp: field_simps inverse_eq_divide)
  12.583 +    then show "\<exists>n. x $$ i \<le> a - 1 / (real (Suc n))"
  12.584 +      by (blast intro: less_imp_le)
  12.585 +  next
  12.586 +    fix x::'a and n
  12.587 +    assume "x$$i \<le> a - 1 / real (Suc n)"
  12.588 +    also have "\<dots> < a" by auto
  12.589 +    finally show "x$$i < a" .
  12.590 +  qed
  12.591 +  show "{x. x$$i < a} \<in> sets ?SIGMA" unfolding *
  12.592 +    by (safe intro!: countable_UN)
  12.593 +       (auto simp: sets_sigma intro!: sigma_sets.Basic)
  12.594 +qed auto
  12.595 +
  12.596 +lemma halfspace_span_halfspace_ge:
  12.597 +  "sets (sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a}))) \<subseteq> 
  12.598 +   sets (sigma UNIV (range (\<lambda> (a, i). {x. a \<le> x $$ i})))"
  12.599 +  (is "_ \<subseteq> sets ?SIGMA")
  12.600 +proof (rule sigma_algebra.sets_sigma_subset, safe)
  12.601 +  show "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
  12.602 +  then interpret sigma_algebra ?SIGMA .
  12.603 +  fix a i have *: "{x::'a. x$$i < a} = space ?SIGMA - {x::'a. a \<le> x$$i}" by auto
  12.604 +  show "{x. x$$i < a} \<in> sets ?SIGMA" unfolding *
  12.605 +    by (safe intro!: Diff)
  12.606 +       (auto simp: sets_sigma intro!: sigma_sets.Basic)
  12.607 +qed auto
  12.608 +
  12.609 +lemma halfspace_le_span_halfspace_gt:
  12.610 +  "sets (sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a}))) \<subseteq> 
  12.611 +   sets (sigma UNIV (range (\<lambda> (a, i). {x. a < x $$ i})))"
  12.612 +  (is "_ \<subseteq> sets ?SIGMA")
  12.613 +proof (rule sigma_algebra.sets_sigma_subset, safe)
  12.614 +  show "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
  12.615 +  then interpret sigma_algebra ?SIGMA .
  12.616 +  fix a i have *: "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
  12.617 +  show "{x. x$$i \<le> a} \<in> sets ?SIGMA" unfolding *
  12.618 +    by (safe intro!: Diff)
  12.619 +       (auto simp: sets_sigma intro!: sigma_sets.Basic)
  12.620 +qed auto
  12.621 +
  12.622 +lemma halfspace_le_span_atMost:
  12.623 +  "sets (sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a}))) \<subseteq>
  12.624 +   sets (sigma UNIV (range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space})))"
  12.625 +  (is "_ \<subseteq> sets ?SIGMA")
  12.626 +proof (rule sigma_algebra.sets_sigma_subset, safe)
  12.627 +  show "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
  12.628 +  then interpret sigma_algebra ?SIGMA .
  12.629 +  fix a i
  12.630 +  show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
  12.631 +  proof cases
  12.632 +    assume "i < DIM('a)"
  12.633 +    then have *: "{x::'a. x$$i \<le> a} = (\<Union>k::nat. {.. (\<chi>\<chi> n. if n = i then a else real k)})"
  12.634 +    proof (safe, simp_all add: eucl_le[where 'a='a] split: split_if_asm)
  12.635 +      fix x
  12.636 +      from real_arch_simple[of "Max ((\<lambda>i. x$$i)`{..<DIM('a)})"] guess k::nat ..
  12.637 +      then have "\<And>i. i < DIM('a) \<Longrightarrow> x$$i \<le> real k"
  12.638 +        by (subst (asm) Max_le_iff) auto
  12.639 +      then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> x $$ ia \<le> real k"
  12.640 +        by (auto intro!: exI[of _ k])
  12.641 +    qed
  12.642 +    show "{x. x$$i \<le> a} \<in> sets ?SIGMA" unfolding *
  12.643 +      by (safe intro!: countable_UN)
  12.644 +         (auto simp: sets_sigma intro!: sigma_sets.Basic)
  12.645 +  next
  12.646 +    assume "\<not> i < DIM('a)"
  12.647 +    then show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
  12.648 +      using top by auto
  12.649 +  qed
  12.650 +qed auto
  12.651 +
  12.652 +lemma halfspace_le_span_greaterThan:
  12.653 +  "sets (sigma UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a}))) \<subseteq>
  12.654 +   sets (sigma UNIV (range (\<lambda>a. {a<..})))"
  12.655 +  (is "_ \<subseteq> sets ?SIGMA")
  12.656 +proof (rule sigma_algebra.sets_sigma_subset, safe)
  12.657 +  show "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
  12.658 +  then interpret sigma_algebra ?SIGMA .
  12.659 +  fix a i
  12.660 +  show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
  12.661 +  proof cases
  12.662 +    assume "i < DIM('a)"
  12.663 +    have "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
  12.664 +    also have *: "{x::'a. a < x$$i} = (\<Union>k::nat. {(\<chi>\<chi> n. if n = i then a else -real k) <..})" using `i <DIM('a)`
  12.665 +    proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
  12.666 +      fix x
  12.667 +      from real_arch_lt[of "Max ((\<lambda>i. -x$$i)`{..<DIM('a)})"]
  12.668 +      guess k::nat .. note k = this
  12.669 +      { fix i assume "i < DIM('a)"
  12.670 +        then have "-x$$i < real k"
  12.671 +          using k by (subst (asm) Max_less_iff) auto
  12.672 +        then have "- real k < x$$i" by simp }
  12.673 +      then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> -real k < x $$ ia"
  12.674 +        by (auto intro!: exI[of _ k])
  12.675 +    qed
  12.676 +    finally show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
  12.677 +      apply (simp only:)
  12.678 +      apply (safe intro!: countable_UN Diff)
  12.679 +      by (auto simp: sets_sigma intro!: sigma_sets.Basic)
  12.680 +  next
  12.681 +    assume "\<not> i < DIM('a)"
  12.682 +    then show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
  12.683 +      using top by auto
  12.684 +  qed
  12.685 +qed auto
  12.686 +
  12.687 +lemma atMost_span_atLeastAtMost:
  12.688 +  "sets (sigma UNIV (range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space}))) \<subseteq>
  12.689 +   sets (sigma UNIV (range (\<lambda>(a,b). {a..b})))"
  12.690 +  (is "_ \<subseteq> sets ?SIGMA")
  12.691 +proof (rule sigma_algebra.sets_sigma_subset, safe)
  12.692 +  show "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
  12.693 +  then interpret sigma_algebra ?SIGMA .
  12.694 +  fix a::'a
  12.695 +  have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
  12.696 +  proof (safe, simp_all add: eucl_le[where 'a='a])
  12.697 +    fix x
  12.698 +    from real_arch_simple[of "Max ((\<lambda>i. - x$$i)`{..<DIM('a)})"]
  12.699 +    guess k::nat .. note k = this
  12.700 +    { fix i assume "i < DIM('a)"
  12.701 +      with k have "- x$$i \<le> real k"
  12.702 +        by (subst (asm) Max_le_iff) (auto simp: field_simps)
  12.703 +      then have "- real k \<le> x$$i" by simp }
  12.704 +    then show "\<exists>n::nat. \<forall>i<DIM('a). - real n \<le> x $$ i"
  12.705 +      by (auto intro!: exI[of _ k])
  12.706 +  qed
  12.707 +  show "{..a} \<in> sets ?SIGMA" unfolding *
  12.708 +    by (safe intro!: countable_UN)
  12.709 +       (auto simp: sets_sigma intro!: sigma_sets.Basic)
  12.710 +qed auto
  12.711 +
  12.712 +lemma borel_space_eq_greaterThanLessThan:
  12.713 +  "sets borel_space = sets (sigma UNIV (range (\<lambda> (a, b). {a <..< (b :: 'a \<Colon> ordered_euclidean_space)})))"
  12.714 +    (is "_ = sets ?SIGMA")
  12.715 +proof (rule antisym)
  12.716 +  show "sets ?SIGMA \<subseteq> sets borel_space"
  12.717 +    by (rule borel_space.sets_sigma_subset) auto
  12.718 +  show "sets borel_space \<subseteq> sets ?SIGMA"
  12.719 +    unfolding borel_space_def
  12.720 +  proof (rule sigma_algebra.sets_sigma_subset, safe)
  12.721 +    show "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
  12.722 +    then interpret sigma_algebra ?SIGMA .
  12.723 +    fix M :: "'a set" assume "M \<in> open"
  12.724 +    then have "open M" by (simp add: mem_def)
  12.725 +    show "M \<in> sets ?SIGMA"
  12.726 +      apply (subst open_UNION[OF `open M`])
  12.727 +      apply (safe intro!: countable_UN)
  12.728 +      by (auto simp add: sigma_def intro!: sigma_sets.Basic)
  12.729 +  qed auto
  12.730 +qed
  12.731 +
  12.732 +lemma borel_space_eq_atMost:
  12.733 +  "sets borel_space = sets (sigma UNIV (range (\<lambda> a. {.. a::'a\<Colon>ordered_euclidean_space})))"
  12.734 +    (is "_ = sets ?SIGMA")
  12.735 +proof (rule antisym)
  12.736 +  show "sets borel_space \<subseteq> sets ?SIGMA"
  12.737 +    using halfspace_le_span_atMost halfspace_span_halfspace_le open_span_halfspace
  12.738 +    by auto
  12.739 +  show "sets ?SIGMA \<subseteq> sets borel_space"
  12.740 +    by (rule borel_space.sets_sigma_subset) auto
  12.741 +qed
  12.742 +
  12.743 +lemma borel_space_eq_atLeastAtMost:
  12.744 +  "sets borel_space = sets (sigma UNIV (range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space, b). {a .. b})))"
  12.745 +   (is "_ = sets ?SIGMA")
  12.746 +proof (rule antisym)
  12.747 +  show "sets borel_space \<subseteq> sets ?SIGMA"
  12.748 +    using atMost_span_atLeastAtMost halfspace_le_span_atMost
  12.749 +      halfspace_span_halfspace_le open_span_halfspace
  12.750 +    by auto
  12.751 +  show "sets ?SIGMA \<subseteq> sets borel_space"
  12.752 +    by (rule borel_space.sets_sigma_subset) auto
  12.753 +qed
  12.754 +
  12.755 +lemma borel_space_eq_greaterThan:
  12.756 +  "sets borel_space = sets (sigma UNIV (range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space). {a <..})))"
  12.757 +   (is "_ = sets ?SIGMA")
  12.758 +proof (rule antisym)
  12.759 +  show "sets borel_space \<subseteq> sets ?SIGMA"
  12.760 +    using halfspace_le_span_greaterThan
  12.761 +      halfspace_span_halfspace_le open_span_halfspace
  12.762 +    by auto
  12.763 +  show "sets ?SIGMA \<subseteq> sets borel_space"
  12.764 +    by (rule borel_space.sets_sigma_subset) auto
  12.765 +qed
  12.766 +
  12.767 +lemma borel_space_eq_halfspace_le:
  12.768 +  "sets borel_space = sets (sigma UNIV (range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x$$i \<le> a})))"
  12.769 +   (is "_ = sets ?SIGMA")
  12.770 +proof (rule antisym)
  12.771 +  show "sets borel_space \<subseteq> sets ?SIGMA"
  12.772 +    using open_span_halfspace halfspace_span_halfspace_le by auto
  12.773 +  show "sets ?SIGMA \<subseteq> sets borel_space"
  12.774 +    by (rule borel_space.sets_sigma_subset) auto
  12.775 +qed
  12.776 +
  12.777 +lemma borel_space_eq_halfspace_less:
  12.778 +  "sets borel_space = sets (sigma UNIV (range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x$$i < a})))"
  12.779 +   (is "_ = sets ?SIGMA")
  12.780 +proof (rule antisym)
  12.781 +  show "sets borel_space \<subseteq> sets ?SIGMA"
  12.782 +    using open_span_halfspace .
  12.783 +  show "sets ?SIGMA \<subseteq> sets borel_space"
  12.784 +    by (rule borel_space.sets_sigma_subset) auto
  12.785 +qed
  12.786 +
  12.787 +lemma borel_space_eq_halfspace_gt:
  12.788 +  "sets borel_space = sets (sigma UNIV (range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. a < x$$i})))"
  12.789 +   (is "_ = sets ?SIGMA")
  12.790 +proof (rule antisym)
  12.791 +  show "sets borel_space \<subseteq> sets ?SIGMA"
  12.792 +    using halfspace_le_span_halfspace_gt open_span_halfspace halfspace_span_halfspace_le by auto
  12.793 +  show "sets ?SIGMA \<subseteq> sets borel_space"
  12.794 +    by (rule borel_space.sets_sigma_subset) auto
  12.795 +qed
  12.796 +
  12.797 +lemma borel_space_eq_halfspace_ge:
  12.798 +  "sets borel_space = sets (sigma UNIV (range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. a \<le> x$$i})))"
  12.799 +   (is "_ = sets ?SIGMA")
  12.800 +proof (rule antisym)
  12.801 +  show "sets borel_space \<subseteq> sets ?SIGMA"
  12.802 +    using halfspace_span_halfspace_ge open_span_halfspace by auto
  12.803 +  show "sets ?SIGMA \<subseteq> sets borel_space"
  12.804 +    by (rule borel_space.sets_sigma_subset) auto
  12.805 +qed
  12.806 +
  12.807 +lemma (in sigma_algebra) borel_measurable_halfspacesI:
  12.808 +  fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
  12.809 +  assumes "sets borel_space = sets (sigma UNIV (range F))"
  12.810 +  and "\<And>a i. S a i = f -` F (a,i) \<inter> space M"
  12.811 +  and "\<And>a i. \<not> i < DIM('c) \<Longrightarrow> S a i \<in> sets M"
  12.812 +  shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a::real. S a i \<in> sets M)"
  12.813 +proof safe
  12.814 +  fix a :: real and i assume i: "i < DIM('c)" and f: "f \<in> borel_measurable M"
  12.815 +  then show "S a i \<in> sets M" unfolding assms
  12.816 +    by (auto intro!: measurable_sets sigma_sets.Basic simp: assms(1) sigma_def)
  12.817 +next
  12.818 +  assume a: "\<forall>i<DIM('c). \<forall>a. S a i \<in> sets M"
  12.819 +  { fix a i have "S a i \<in> sets M"
  12.820 +    proof cases
  12.821 +      assume "i < DIM('c)"
  12.822 +      with a show ?thesis unfolding assms(2) by simp
  12.823 +    next
  12.824 +      assume "\<not> i < DIM('c)"
  12.825 +      from assms(3)[OF this] show ?thesis .
  12.826 +    qed }
  12.827 +  then have "f \<in> measurable M (sigma UNIV (range F))"
  12.828 +    by (auto intro!: measurable_sigma simp: assms(2))
  12.829 +  then show "f \<in> borel_measurable M" unfolding measurable_def
  12.830 +    unfolding assms(1) by simp
  12.831 +qed
  12.832 +
  12.833 +lemma (in sigma_algebra) borel_measurable_iff_halfspace_le:
  12.834 +  fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
  12.835 +  shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i \<le> a} \<in> sets M)"
  12.836 +  by (rule borel_measurable_halfspacesI[OF borel_space_eq_halfspace_le]) auto
  12.837 +
  12.838 +lemma (in sigma_algebra) borel_measurable_iff_halfspace_less:
  12.839 +  fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
  12.840 +  shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i < a} \<in> sets M)"
  12.841 +  by (rule borel_measurable_halfspacesI[OF borel_space_eq_halfspace_less]) auto
  12.842 +
  12.843 +lemma (in sigma_algebra) borel_measurable_iff_halfspace_ge:
  12.844 +  fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
  12.845 +  shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a \<le> f w $$ i} \<in> sets M)"
  12.846 +  by (rule borel_measurable_halfspacesI[OF borel_space_eq_halfspace_ge]) auto
  12.847 +
  12.848 +lemma (in sigma_algebra) borel_measurable_iff_halfspace_greater:
  12.849 +  fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
  12.850 +  shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a < f w $$ i} \<in> sets M)"
  12.851 +  by (rule borel_measurable_halfspacesI[OF borel_space_eq_halfspace_gt]) auto
  12.852 +
  12.853 +lemma (in sigma_algebra) borel_measurable_iff_le:
  12.854 +  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
  12.855 +  using borel_measurable_iff_halfspace_le[where 'c=real] by simp
  12.856 +
  12.857 +lemma (in sigma_algebra) borel_measurable_iff_less:
  12.858 +  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
  12.859 +  using borel_measurable_iff_halfspace_less[where 'c=real] by simp
  12.860 +
  12.861 +lemma (in sigma_algebra) borel_measurable_iff_ge:
  12.862 +  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
  12.863 +  using borel_measurable_iff_halfspace_ge[where 'c=real] by simp
  12.864 +
  12.865 +lemma (in sigma_algebra) borel_measurable_iff_greater:
  12.866 +  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
  12.867 +  using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
  12.868 +
  12.869 +subsection "Borel measurable operators"
  12.870 +
  12.871 +lemma (in sigma_algebra) affine_borel_measurable_vector:
  12.872 +  fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
  12.873 +  assumes "f \<in> borel_measurable M"
  12.874 +  shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
  12.875 +proof (rule borel_measurableI)
  12.876 +  fix S :: "'x set" assume "open S"
  12.877 +  show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
  12.878 +  proof cases
  12.879 +    assume "b \<noteq> 0"
  12.880 +    with `open S` have "((\<lambda>x. (- a + x) /\<^sub>R b) ` S) \<in> open" (is "?S \<in> open")
  12.881 +      by (auto intro!: open_affinity simp: scaleR.add_right mem_def)
  12.882 +    hence "?S \<in> sets borel_space"
  12.883 +      unfolding borel_space_def by (auto simp: sigma_def intro!: sigma_sets.Basic)
  12.884 +    moreover
  12.885 +    from `b \<noteq> 0` have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
  12.886 +      apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
  12.887 +    ultimately show ?thesis using assms unfolding in_borel_measurable_borel_space
  12.888 +      by auto
  12.889 +  qed simp
  12.890 +qed
  12.891 +
  12.892 +lemma (in sigma_algebra) affine_borel_measurable:
  12.893 +  fixes g :: "'a \<Rightarrow> real"
  12.894 +  assumes g: "g \<in> borel_measurable M"
  12.895 +  shows "(\<lambda>x. a + (g x) * b) \<in> borel_measurable M"
  12.896 +  using affine_borel_measurable_vector[OF assms] by (simp add: mult_commute)
  12.897 +
  12.898 +lemma (in sigma_algebra) borel_measurable_add[simp, intro]:
  12.899 +  fixes f :: "'a \<Rightarrow> real"
  12.900    assumes f: "f \<in> borel_measurable M"
  12.901    assumes g: "g \<in> borel_measurable M"
  12.902    shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  12.903  proof -
  12.904 -  have 1:"!!a. {w \<in> space M. a \<le> f w + g w} = {w \<in> space M. a + (g w) * -1 \<le> f w}"
  12.905 +  have 1: "\<And>a. {w\<in>space M. a \<le> f w + g w} = {w \<in> space M. a + g w * -1 \<le> f w}"
  12.906      by auto
  12.907 -  have "!!a. (\<lambda>w. a + (g w) * -1) \<in> borel_measurable M"
  12.908 -    by (rule affine_borel_measurable [OF g]) 
  12.909 -  hence "!!a. {w \<in> space M. (\<lambda>w. a + (g w) * -1)(w) \<le> f w} \<in> sets M" using f
  12.910 -    by (rule borel_measurable_leq_borel_measurable) 
  12.911 -  hence "!!a. {w \<in> space M. a \<le> f w + g w} \<in> sets M"
  12.912 -    by (simp add: 1) 
  12.913 -  thus ?thesis
  12.914 -    by (simp add: borel_measurable_ge_iff) 
  12.915 +  have "\<And>a. (\<lambda>w. a + (g w) * -1) \<in> borel_measurable M"
  12.916 +    by (rule affine_borel_measurable [OF g])
  12.917 +  then have "\<And>a. {w \<in> space M. (\<lambda>w. a + (g w) * -1)(w) \<le> f w} \<in> sets M" using f
  12.918 +    by auto
  12.919 +  then have "\<And>a. {w \<in> space M. a \<le> f w + g w} \<in> sets M"
  12.920 +    by (simp add: 1)
  12.921 +  then show ?thesis
  12.922 +    by (simp add: borel_measurable_iff_ge)
  12.923  qed
  12.924  
  12.925 -
  12.926 -lemma (in measure_space) borel_measurable_square:
  12.927 +lemma (in sigma_algebra) borel_measurable_square:
  12.928 +  fixes f :: "'a \<Rightarrow> real"
  12.929    assumes f: "f \<in> borel_measurable M"
  12.930    shows "(\<lambda>x. (f x)^2) \<in> borel_measurable M"
  12.931  proof -
  12.932 @@ -286,21 +715,21 @@
  12.933      have "{w \<in> space M. (f w)\<twosuperior> \<le> a} \<in> sets M"
  12.934      proof (cases rule: linorder_cases [of a 0])
  12.935        case less
  12.936 -      hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} = {}" 
  12.937 +      hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} = {}"
  12.938          by auto (metis less order_le_less_trans power2_less_0)
  12.939        also have "... \<in> sets M"
  12.940 -        by (rule empty_sets) 
  12.941 +        by (rule empty_sets)
  12.942        finally show ?thesis .
  12.943      next
  12.944        case equal
  12.945 -      hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} = 
  12.946 +      hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} =
  12.947               {w \<in> space M. f w \<le> 0} \<inter> {w \<in> space M. 0 \<le> f w}"
  12.948          by auto
  12.949        also have "... \<in> sets M"
  12.950 -        apply (insert f) 
  12.951 -        apply (rule Int) 
  12.952 -        apply (simp add: borel_measurable_le_iff)
  12.953 -        apply (simp add: borel_measurable_ge_iff)
  12.954 +        apply (insert f)
  12.955 +        apply (rule Int)
  12.956 +        apply (simp add: borel_measurable_iff_le)
  12.957 +        apply (simp add: borel_measurable_iff_ge)
  12.958          done
  12.959        finally show ?thesis .
  12.960      next
  12.961 @@ -309,329 +738,536 @@
  12.962          by (metis abs_le_interval_iff abs_of_pos greater real_sqrt_abs
  12.963                    real_sqrt_le_iff real_sqrt_power)
  12.964        hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} =
  12.965 -             {w \<in> space M. -(sqrt a) \<le> f w} \<inter> {w \<in> space M. f w \<le> sqrt a}" 
  12.966 +             {w \<in> space M. -(sqrt a) \<le> f w} \<inter> {w \<in> space M. f w \<le> sqrt a}"
  12.967          using greater by auto
  12.968        also have "... \<in> sets M"
  12.969 -        apply (insert f) 
  12.970 -        apply (rule Int) 
  12.971 -        apply (simp add: borel_measurable_ge_iff)
  12.972 -        apply (simp add: borel_measurable_le_iff)
  12.973 +        apply (insert f)
  12.974 +        apply (rule Int)
  12.975 +        apply (simp add: borel_measurable_iff_ge)
  12.976 +        apply (simp add: borel_measurable_iff_le)
  12.977          done
  12.978        finally show ?thesis .
  12.979      qed
  12.980    }
  12.981 -  thus ?thesis by (auto simp add: borel_measurable_le_iff) 
  12.982 +  thus ?thesis by (auto simp add: borel_measurable_iff_le)
  12.983  qed
  12.984  
  12.985  lemma times_eq_sum_squares:
  12.986     fixes x::real
  12.987     shows"x*y = ((x+y)^2)/4 - ((x-y)^ 2)/4"
  12.988 -by (simp add: power2_eq_square ring_distribs diff_divide_distrib [symmetric]) 
  12.989 +by (simp add: power2_eq_square ring_distribs diff_divide_distrib [symmetric])
  12.990  
  12.991 -
  12.992 -lemma (in measure_space) borel_measurable_uminus_borel_measurable:
  12.993 +lemma (in sigma_algebra) borel_measurable_uminus[simp, intro]:
  12.994 +  fixes g :: "'a \<Rightarrow> real"
  12.995    assumes g: "g \<in> borel_measurable M"
  12.996    shows "(\<lambda>x. - g x) \<in> borel_measurable M"
  12.997  proof -
  12.998    have "(\<lambda>x. - g x) = (\<lambda>x. 0 + (g x) * -1)"
  12.999      by simp
 12.1000 -  also have "... \<in> borel_measurable M" 
 12.1001 -    by (fast intro: affine_borel_measurable g) 
 12.1002 +  also have "... \<in> borel_measurable M"
 12.1003 +    by (fast intro: affine_borel_measurable g)
 12.1004    finally show ?thesis .
 12.1005  qed
 12.1006  
 12.1007 -lemma (in measure_space) borel_measurable_times_borel_measurable:
 12.1008 +lemma (in sigma_algebra) borel_measurable_times[simp, intro]:
 12.1009 +  fixes f :: "'a \<Rightarrow> real"
 12.1010    assumes f: "f \<in> borel_measurable M"
 12.1011    assumes g: "g \<in> borel_measurable M"
 12.1012    shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
 12.1013  proof -
 12.1014    have 1: "(\<lambda>x. 0 + (f x + g x)\<twosuperior> * inverse 4) \<in> borel_measurable M"
 12.1015 -    by (fast intro: affine_borel_measurable borel_measurable_square 
 12.1016 -                    borel_measurable_add_borel_measurable f g) 
 12.1017 -  have "(\<lambda>x. -((f x + -g x) ^ 2 * inverse 4)) = 
 12.1018 +    using assms by (fast intro: affine_borel_measurable borel_measurable_square)
 12.1019 +  have "(\<lambda>x. -((f x + -g x) ^ 2 * inverse 4)) =
 12.1020          (\<lambda>x. 0 + ((f x + -g x) ^ 2 * inverse -4))"
 12.1021      by (simp add: minus_divide_right)
 12.1022 -  also have "... \<in> borel_measurable M" 
 12.1023 -    by (fast intro: affine_borel_measurable borel_measurable_square 
 12.1024 -                    borel_measurable_add_borel_measurable 
 12.1025 -                    borel_measurable_uminus_borel_measurable f g)
 12.1026 +  also have "... \<in> borel_measurable M"
 12.1027 +    using f g by (fast intro: affine_borel_measurable borel_measurable_square f g)
 12.1028    finally have 2: "(\<lambda>x. -((f x + -g x) ^ 2 * inverse 4)) \<in> borel_measurable M" .
 12.1029    show ?thesis
 12.1030 -    apply (simp add: times_eq_sum_squares diff_minus) 
 12.1031 -    using 1 2 apply (simp add: borel_measurable_add_borel_measurable) 
 12.1032 -    done
 12.1033 +    apply (simp add: times_eq_sum_squares diff_minus)
 12.1034 +    using 1 2 by simp
 12.1035  qed
 12.1036  
 12.1037 -lemma (in measure_space) borel_measurable_diff_borel_measurable:
 12.1038 +lemma (in sigma_algebra) borel_measurable_diff[simp, intro]:
 12.1039 +  fixes f :: "'a \<Rightarrow> real"
 12.1040    assumes f: "f \<in> borel_measurable M"
 12.1041    assumes g: "g \<in> borel_measurable M"
 12.1042    shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
 12.1043 -unfolding diff_minus
 12.1044 -  by (fast intro: borel_measurable_add_borel_measurable 
 12.1045 -                  borel_measurable_uminus_borel_measurable f g)
 12.1046 +  unfolding diff_minus using assms by fast
 12.1047  
 12.1048 -lemma (in measure_space) borel_measurable_setsum_borel_measurable:
 12.1049 -  assumes s: "finite s"
 12.1050 -  shows "(!!i. i \<in> s ==> f i \<in> borel_measurable M) \<Longrightarrow> (\<lambda>x. setsum (\<lambda>i. f i x) s) \<in> borel_measurable M" using s
 12.1051 -proof (induct s)
 12.1052 -  case empty
 12.1053 -  thus ?case
 12.1054 -    by (simp add: borel_measurable_const)
 12.1055 -next
 12.1056 -  case (insert x s)
 12.1057 -  thus ?case
 12.1058 -    by (auto simp add: borel_measurable_add_borel_measurable) 
 12.1059 -qed
 12.1060 +lemma (in sigma_algebra) borel_measurable_setsum[simp, intro]:
 12.1061 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
 12.1062 +  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
 12.1063 +  shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
 12.1064 +proof cases
 12.1065 +  assume "finite S"
 12.1066 +  thus ?thesis using assms by induct auto
 12.1067 +qed simp
 12.1068  
 12.1069 -lemma (in measure_space) borel_measurable_cong:
 12.1070 -  assumes "\<And> w. w \<in> space M \<Longrightarrow> f w = g w"
 12.1071 -  shows "f \<in> borel_measurable M \<longleftrightarrow> g \<in> borel_measurable M"
 12.1072 -using assms unfolding in_borel_measurable by (simp cong: vimage_inter_cong)
 12.1073 -
 12.1074 -lemma (in measure_space) borel_measurable_inverse:
 12.1075 +lemma (in sigma_algebra) borel_measurable_inverse[simp, intro]:
 12.1076 +  fixes f :: "'a \<Rightarrow> real"
 12.1077    assumes "f \<in> borel_measurable M"
 12.1078    shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
 12.1079 -  unfolding borel_measurable_ge_iff
 12.1080 -proof (safe, rule linorder_cases)
 12.1081 -  fix a :: real assume "0 < a"
 12.1082 -  { fix w
 12.1083 -    from `0 < a` have "a \<le> inverse (f w) \<longleftrightarrow> 0 < f w \<and> f w \<le> 1 / a"
 12.1084 -      by (metis inverse_eq_divide inverse_inverse_eq le_imp_inverse_le
 12.1085 -                less_le_trans zero_less_divide_1_iff) }
 12.1086 -  hence "{w \<in> space M. a \<le> inverse (f w)} =
 12.1087 -    {w \<in> space M. 0 < f w} \<inter> {w \<in> space M. f w \<le> 1 / a}" by auto
 12.1088 -  with Int assms[unfolded borel_measurable_gr_iff]
 12.1089 -    assms[unfolded borel_measurable_le_iff]
 12.1090 -  show "{w \<in> space M. a \<le> inverse (f w)} \<in> sets M" by simp
 12.1091 -next
 12.1092 -  fix a :: real assume "0 = a"
 12.1093 -  { fix w have "a \<le> inverse (f w) \<longleftrightarrow> 0 \<le> f w"
 12.1094 -      unfolding `0 = a`[symmetric] by auto }
 12.1095 -  thus "{w \<in> space M. a \<le> inverse (f w)} \<in> sets M"
 12.1096 -    using assms[unfolded borel_measurable_ge_iff] by simp
 12.1097 -next
 12.1098 -  fix a :: real assume "a < 0"
 12.1099 -  { fix w
 12.1100 -    from `a < 0` have "a \<le> inverse (f w) \<longleftrightarrow> f w \<le> 1 / a \<or> 0 \<le> f w"
 12.1101 -      apply (cases "0 \<le> f w")
 12.1102 -      apply (metis inverse_eq_divide linorder_not_le xt1(8) xt1(9)
 12.1103 -                   zero_le_divide_1_iff)
 12.1104 -      apply (metis inverse_eq_divide inverse_inverse_eq inverse_le_imp_le_neg
 12.1105 -                   linorder_not_le order_refl order_trans)
 12.1106 -      done }
 12.1107 -  hence "{w \<in> space M. a \<le> inverse (f w)} =
 12.1108 -    {w \<in> space M. f w \<le> 1 / a} \<union> {w \<in> space M. 0 \<le> f w}" by auto
 12.1109 -  with Un assms[unfolded borel_measurable_ge_iff]
 12.1110 -    assms[unfolded borel_measurable_le_iff]
 12.1111 -  show "{w \<in> space M. a \<le> inverse (f w)} \<in> sets M" by simp
 12.1112 +  unfolding borel_measurable_iff_ge unfolding inverse_eq_divide
 12.1113 +proof safe
 12.1114 +  fix a :: real
 12.1115 +  have *: "{w \<in> space M. a \<le> 1 / f w} =
 12.1116 +      ({w \<in> space M. 0 < f w} \<inter> {w \<in> space M. a * f w \<le> 1}) \<union>
 12.1117 +      ({w \<in> space M. f w < 0} \<inter> {w \<in> space M. 1 \<le> a * f w}) \<union>
 12.1118 +      ({w \<in> space M. f w = 0} \<inter> {w \<in> space M. a \<le> 0})" by (auto simp: le_divide_eq)
 12.1119 +  show "{w \<in> space M. a \<le> 1 / f w} \<in> sets M" using assms unfolding *
 12.1120 +    by (auto intro!: Int Un)
 12.1121  qed
 12.1122  
 12.1123 -lemma (in measure_space) borel_measurable_divide:
 12.1124 +lemma (in sigma_algebra) borel_measurable_divide[simp, intro]:
 12.1125 +  fixes f :: "'a \<Rightarrow> real"
 12.1126    assumes "f \<in> borel_measurable M"
 12.1127    and "g \<in> borel_measurable M"
 12.1128    shows "(\<lambda>x. f x / g x) \<in> borel_measurable M"
 12.1129    unfolding field_divide_inverse
 12.1130 -  by (rule borel_measurable_inverse borel_measurable_times_borel_measurable assms)+
 12.1131 +  by (rule borel_measurable_inverse borel_measurable_times assms)+
 12.1132 +
 12.1133 +lemma (in sigma_algebra) borel_measurable_max[intro, simp]:
 12.1134 +  fixes f g :: "'a \<Rightarrow> real"
 12.1135 +  assumes "f \<in> borel_measurable M"
 12.1136 +  assumes "g \<in> borel_measurable M"
 12.1137 +  shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
 12.1138 +  unfolding borel_measurable_iff_le
 12.1139 +proof safe
 12.1140 +  fix a
 12.1141 +  have "{x \<in> space M. max (g x) (f x) \<le> a} =
 12.1142 +    {x \<in> space M. g x \<le> a} \<inter> {x \<in> space M. f x \<le> a}" by auto
 12.1143 +  thus "{x \<in> space M. max (g x) (f x) \<le> a} \<in> sets M"
 12.1144 +    using assms unfolding borel_measurable_iff_le
 12.1145 +    by (auto intro!: Int)
 12.1146 +qed
 12.1147 +
 12.1148 +lemma (in sigma_algebra) borel_measurable_min[intro, simp]:
 12.1149 +  fixes f g :: "'a \<Rightarrow> real"
 12.1150 +  assumes "f \<in> borel_measurable M"
 12.1151 +  assumes "g \<in> borel_measurable M"
 12.1152 +  shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
 12.1153 +  unfolding borel_measurable_iff_ge
 12.1154 +proof safe
 12.1155 +  fix a
 12.1156 +  have "{x \<in> space M. a \<le> min (g x) (f x)} =
 12.1157 +    {x \<in> space M. a \<le> g x} \<inter> {x \<in> space M. a \<le> f x}" by auto
 12.1158 +  thus "{x \<in> space M. a \<le> min (g x) (f x)} \<in> sets M"
 12.1159 +    using assms unfolding borel_measurable_iff_ge
 12.1160 +    by (auto intro!: Int)
 12.1161 +qed
 12.1162 +
 12.1163 +lemma (in sigma_algebra) borel_measurable_abs[simp, intro]:
 12.1164 +  assumes "f \<in> borel_measurable M"
 12.1165 +  shows "(\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
 12.1166 +proof -
 12.1167 +  have *: "\<And>x. \<bar>f x\<bar> = max 0 (f x) + max 0 (- f x)" by (simp add: max_def)
 12.1168 +  show ?thesis unfolding * using assms by auto
 12.1169 +qed
 12.1170 +
 12.1171 +section "Borel space over the real line with infinity"
 12.1172  
 12.1173 -lemma (in measure_space) borel_measurable_vimage:
 12.1174 -  assumes borel: "f \<in> borel_measurable M"
 12.1175 -  shows "f -` {X} \<inter> space M \<in> sets M"
 12.1176 -proof -
 12.1177 -  have "{w \<in> space M. f w = X} = {w. f w = X} \<inter> space M" by auto
 12.1178 -  with borel_measurable_eq_borel_measurable[OF borel borel_measurable_const, of X]
 12.1179 -  show ?thesis unfolding vimage_def by simp
 12.1180 +lemma borel_space_Real_measurable:
 12.1181 +  "A \<in> sets borel_space \<Longrightarrow> Real -` A \<in> sets borel_space"
 12.1182 +proof (rule borel_measurable_translate)
 12.1183 +  fix B :: "pinfreal set" assume "open B"
 12.1184 +  then obtain T x where T: "open T" "Real ` (T \<inter> {0..}) = B - {\<omega>}" and
 12.1185 +    x: "\<omega> \<in> B \<Longrightarrow> 0 \<le> x" "\<omega> \<in> B \<Longrightarrow> {Real x <..} \<subseteq> B"
 12.1186 +    unfolding open_pinfreal_def by blast
 12.1187 +
 12.1188 +  have "Real -` B = Real -` (B - {\<omega>})" by auto
 12.1189 +  also have "\<dots> = Real -` (Real ` (T \<inter> {0..}))" using T by simp
 12.1190 +  also have "\<dots> = (if 0 \<in> T then T \<union> {.. 0} else T \<inter> {0..})"
 12.1191 +    apply (auto simp add: Real_eq_Real image_iff)
 12.1192 +    apply (rule_tac x="max 0 x" in bexI)
 12.1193 +    by (auto simp: max_def)
 12.1194 +  finally show "Real -` B \<in> sets borel_space"
 12.1195 +    using `open T` by auto
 12.1196 +qed simp
 12.1197 +
 12.1198 +lemma borel_space_real_measurable:
 12.1199 +  "A \<in> sets borel_space \<Longrightarrow> (real -` A :: pinfreal set) \<in> sets borel_space"
 12.1200 +proof (rule borel_measurable_translate)
 12.1201 +  fix B :: "real set" assume "open B"
 12.1202 +  { fix x have "0 < real x \<longleftrightarrow> (\<exists>r>0. x = Real r)" by (cases x) auto }
 12.1203 +  note Ex_less_real = this
 12.1204 +  have *: "real -` B = (if 0 \<in> B then real -` (B \<inter> {0 <..}) \<union> {0, \<omega>} else real -` (B \<inter> {0 <..}))"
 12.1205 +    by (force simp: Ex_less_real)
 12.1206 +
 12.1207 +  have "open (real -` (B \<inter> {0 <..}) :: pinfreal set)"
 12.1208 +    unfolding open_pinfreal_def using `open B`
 12.1209 +    by (auto intro!: open_Int exI[of _ "B \<inter> {0 <..}"] simp: image_iff Ex_less_real)
 12.1210 +  then show "(real -` B :: pinfreal set) \<in> sets borel_space" unfolding * by auto
 12.1211 +qed simp
 12.1212 +
 12.1213 +lemma (in sigma_algebra) borel_measurable_Real[intro, simp]:
 12.1214 +  assumes "f \<in> borel_measurable M"
 12.1215 +  shows "(\<lambda>x. Real (f x)) \<in> borel_measurable M"
 12.1216 +  unfolding in_borel_measurable_borel_space
 12.1217 +proof safe
 12.1218 +  fix S :: "pinfreal set" assume "S \<in> sets borel_space"
 12.1219 +  from borel_space_Real_measurable[OF this]
 12.1220 +  have "(Real \<circ> f) -` S \<inter> space M \<in> sets M"
 12.1221 +    using assms
 12.1222 +    unfolding vimage_compose in_borel_measurable_borel_space
 12.1223 +    by auto
 12.1224 +  thus "(\<lambda>x. Real (f x)) -` S \<inter> space M \<in> sets M" by (simp add: comp_def)
 12.1225  qed
 12.1226  
 12.1227 -section "Monotone convergence"
 12.1228 -
 12.1229 -definition mono_convergent where
 12.1230 -  "mono_convergent u f s \<equiv>
 12.1231 -        (\<forall>x\<in>s. incseq (\<lambda>n. u n x)) \<and>
 12.1232 -        (\<forall>x \<in> s. (\<lambda>i. u i x) ----> f x)"
 12.1233 -
 12.1234 -definition "upclose f g x = max (f x) (g x)"
 12.1235 +lemma (in sigma_algebra) borel_measurable_real[intro, simp]:
 12.1236 +  fixes f :: "'a \<Rightarrow> pinfreal"
 12.1237 +  assumes "f \<in> borel_measurable M"
 12.1238 +  shows "(\<lambda>x. real (f x)) \<in> borel_measurable M"
 12.1239 +  unfolding in_borel_measurable_borel_space
 12.1240 +proof safe
 12.1241 +  fix S :: "real set" assume "S \<in> sets borel_space"
 12.1242 +  from borel_space_real_measurable[OF this]
 12.1243 +  have "(real \<circ> f) -` S \<inter> space M \<in> sets M"
 12.1244 +    using assms
 12.1245 +    unfolding vimage_compose in_borel_measurable_borel_space
 12.1246 +    by auto
 12.1247 +  thus "(\<lambda>x. real (f x)) -` S \<inter> space M \<in> sets M" by (simp add: comp_def)
 12.1248 +qed
 12.1249  
 12.1250 -primrec mon_upclose where
 12.1251 -"mon_upclose 0 u = u 0" |
 12.1252 -"mon_upclose (Suc n) u = upclose (u (Suc n)) (mon_upclose n u)"
 12.1253 -
 12.1254 -lemma mono_convergentD:
 12.1255 -  assumes "mono_convergent u f s" and "x \<in> s"
 12.1256 -  shows "incseq (\<lambda>n. u n x)" and "(\<lambda>i. u i x) ----> f x"
 12.1257 -  using assms unfolding mono_convergent_def by auto
 12.1258 +lemma (in sigma_algebra) borel_measurable_Real_eq:
 12.1259 +  assumes "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
 12.1260 +  shows "(\<lambda>x. Real (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
 12.1261 +proof
 12.1262 +  have [simp]: "(\<lambda>x. Real (f x)) -` {\<omega>} \<inter> space M = {}"
 12.1263 +    by auto
 12.1264 +  assume "(\<lambda>x. Real (f x)) \<in> borel_measurable M"
 12.1265 +  hence "(\<lambda>x. real (Real (f x))) \<in> borel_measurable M"
 12.1266 +    by (rule borel_measurable_real)
 12.1267 +  moreover have "\<And>x. x \<in> space M \<Longrightarrow> real (Real (f x)) = f x"
 12.1268 +    using assms by auto
 12.1269 +  ultimately show "f \<in> borel_measurable M"
 12.1270 +    by (simp cong: measurable_cong)
 12.1271 +qed auto
 12.1272  
 12.1273 -lemma mono_convergentI:
 12.1274 -  assumes "\<And>x. x \<in> s \<Longrightarrow> incseq (\<lambda>n. u n x)"
 12.1275 -  assumes "\<And>x. x \<in> s \<Longrightarrow> (\<lambda>i. u i x) ----> f x"
 12.1276 -  shows "mono_convergent u f s"
 12.1277 -  using assms unfolding mono_convergent_def by auto
 12.1278 +lemma (in sigma_algebra) borel_measurable_pinfreal_eq_real:
 12.1279 +  "f \<in> borel_measurable M \<longleftrightarrow>
 12.1280 +    ((\<lambda>x. real (f x)) \<in> borel_measurable M \<and> f -` {\<omega>} \<inter> space M \<in> sets M)"
 12.1281 +proof safe
 12.1282 +  assume "f \<in> borel_measurable M"
 12.1283 +  then show "(\<lambda>x. real (f x)) \<in> borel_measurable M" "f -` {\<omega>} \<inter> space M \<in> sets M"
 12.1284 +    by (auto intro: borel_measurable_vimage borel_measurable_real)
 12.1285 +next
 12.1286 +  assume *: "(\<lambda>x. real (f x)) \<in> borel_measurable M" "f -` {\<omega>} \<inter> space M \<in> sets M"
 12.1287 +  have "f -` {\<omega>} \<inter> space M = {x\<in>space M. f x = \<omega>}" by auto
 12.1288 +  with * have **: "{x\<in>space M. f x = \<omega>} \<in> sets M" by simp
 12.1289 +  have f: "f = (\<lambda>x. if f x = \<omega> then \<omega> else Real (real (f x)))"
 12.1290 +    by (simp add: expand_fun_eq Real_real)
 12.1291 +  show "f \<in> borel_measurable M"
 12.1292 +    apply (subst f)
 12.1293 +    apply (rule measurable_If)
 12.1294 +    using * ** by auto
 12.1295 +qed
 12.1296 +
 12.1297 +lemma (in sigma_algebra) less_eq_ge_measurable:
 12.1298 +  fixes f :: "'a \<Rightarrow> 'c::linorder"
 12.1299 +  shows "{x\<in>space M. a < f x} \<in> sets M \<longleftrightarrow> {x\<in>space M. f x \<le> a} \<in> sets M"
 12.1300 +proof
 12.1301 +  assume "{x\<in>space M. f x \<le> a} \<in> sets M"
 12.1302 +  moreover have "{x\<in>space M. a < f x} = space M - {x\<in>space M. f x \<le> a}" by auto
 12.1303 +  ultimately show "{x\<in>space M. a < f x} \<in> sets M" by auto
 12.1304 +next
 12.1305 +  assume "{x\<in>space M. a < f x} \<in> sets M"
 12.1306 +  moreover have "{x\<in>space M. f x \<le> a} = space M - {x\<in>space M. a < f x}" by auto
 12.1307 +  ultimately show "{x\<in>space M. f x \<le> a} \<in> sets M" by auto
 12.1308 +qed
 12.1309  
 12.1310 -lemma (in measure_space) mono_convergent_borel_measurable:
 12.1311 -  assumes u: "!!n. u n \<in> borel_measurable M"
 12.1312 -  assumes mc: "mono_convergent u f (space M)"
 12.1313 -  shows "f \<in> borel_measurable M"
 12.1314 -proof -
 12.1315 -  {
 12.1316 -    fix a
 12.1317 -    have 1: "!!w. w \<in> space M & f w <= a \<longleftrightarrow> w \<in> space M & (\<forall>i. u i w <= a)"
 12.1318 +lemma (in sigma_algebra) greater_eq_le_measurable:
 12.1319 +  fixes f :: "'a \<Rightarrow> 'c::linorder"
 12.1320 +  shows "{x\<in>space M. f x < a} \<in> sets M \<longleftrightarrow> {x\<in>space M. a \<le> f x} \<in> sets M"
 12.1321 +proof
 12.1322 +  assume "{x\<in>space M. a \<le> f x} \<in> sets M"
 12.1323 +  moreover have "{x\<in>space M. f x < a} = space M - {x\<in>space M. a \<le> f x}" by auto
 12.1324 +  ultimately show "{x\<in>space M. f x < a} \<in> sets M" by auto
 12.1325 +next
 12.1326 +  assume "{x\<in>space M. f x < a} \<in> sets M"
 12.1327 +  moreover have "{x\<in>space M. a \<le> f x} = space M - {x\<in>space M. f x < a}" by auto
 12.1328 +  ultimately show "{x\<in>space M. a \<le> f x} \<in> sets M" by auto
 12.1329 +qed
 12.1330 +
 12.1331 +lemma (in sigma_algebra) less_eq_le_pinfreal_measurable:
 12.1332 +  fixes f :: "'a \<Rightarrow> pinfreal"
 12.1333 +  shows "(\<forall>a. {x\<in>space M. a < f x} \<in> sets M) \<longleftrightarrow> (\<forall>a. {x\<in>space M. a \<le> f x} \<in> sets M)"
 12.1334 +proof
 12.1335 +  assume a: "\<forall>a. {x\<in>space M. a \<le> f x} \<in> sets M"
 12.1336 +  show "\<forall>a. {x \<in> space M. a < f x} \<in> sets M"
 12.1337 +  proof
 12.1338 +    fix a show "{x \<in> space M. a < f x} \<in> sets M"
 12.1339 +    proof (cases a)
 12.1340 +      case (preal r)
 12.1341 +      have "{x\<in>space M. a < f x} = (\<Union>i. {x\<in>space M. a + inverse (of_nat (Suc i)) \<le> f x})"
 12.1342        proof safe
 12.1343 -        fix w i
 12.1344 -        assume w: "w \<in> space M" and f: "f w \<le> a"
 12.1345 -        hence "u i w \<le> f w"
 12.1346 -          by (auto intro: SEQ.incseq_le
 12.1347 -                   simp add: mc [unfolded mono_convergent_def])
 12.1348 -        thus "u i w \<le> a" using f
 12.1349 +        fix x assume "a < f x" and [simp]: "x \<in> space M"
 12.1350 +        with ex_pinfreal_inverse_of_nat_Suc_less[of "f x - a"]
 12.1351 +        obtain n where "a + inverse (of_nat (Suc n)) < f x"
 12.1352 +          by (cases "f x", auto simp: pinfreal_minus_order)
 12.1353 +        then have "a + inverse (of_nat (Suc n)) \<le> f x" by simp
 12.1354 +        then show "x \<in> (\<Union>i. {x \<in> space M. a + inverse (of_nat (Suc i)) \<le> f x})"
 12.1355            by auto
 12.1356        next
 12.1357 -        fix w 
 12.1358 -        assume w: "w \<in> space M" and u: "\<forall>i. u i w \<le> a"
 12.1359 -        thus "f w \<le> a"
 12.1360 -          by (metis LIMSEQ_le_const2 mc [unfolded mono_convergent_def])
 12.1361 +        fix i x assume [simp]: "x \<in> space M"
 12.1362 +        have "a < a + inverse (of_nat (Suc i))" using preal by auto
 12.1363 +        also assume "a + inverse (of_nat (Suc i)) \<le> f x"
 12.1364 +        finally show "a < f x" .
 12.1365        qed
 12.1366 -    have "{w \<in> space M. f w \<le> a} = {w \<in> space M. (\<forall>i. u i w <= a)}"
 12.1367 -      by (simp add: 1)
 12.1368 -    also have "... = (\<Inter>i. {w \<in> space M. u i w \<le> a})" 
 12.1369 -      by auto
 12.1370 -    also have "...  \<in> sets M" using u
 12.1371 -      by (auto simp add: borel_measurable_le_iff intro: countable_INT) 
 12.1372 -    finally have "{w \<in> space M. f w \<le> a} \<in> sets M" .
 12.1373 -  }
 12.1374 -  thus ?thesis 
 12.1375 -    by (auto simp add: borel_measurable_le_iff) 
 12.1376 -qed
 12.1377 -
 12.1378 -lemma mono_convergent_le:
 12.1379 -  assumes "mono_convergent u f s" and "t \<in> s"
 12.1380 -  shows "u n t \<le> f t"
 12.1381 -using mono_convergentD[OF assms] by (auto intro!: incseq_le)
 12.1382 -
 12.1383 -lemma mon_upclose_ex:
 12.1384 -  fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ('b\<Colon>linorder)"
 12.1385 -  shows "\<exists>n \<le> m. mon_upclose m u x = u n x"
 12.1386 -proof (induct m)
 12.1387 -  case (Suc m)
 12.1388 -  then obtain n where "n \<le> m" and *: "mon_upclose m u x = u n x" by blast
 12.1389 -  thus ?case
 12.1390 -  proof (cases "u n x \<le> u (Suc m) x")
 12.1391 -    case True with min_max.sup_absorb1 show ?thesis
 12.1392 -      by (auto simp: * upclose_def intro!: exI[of _ "Suc m"])
 12.1393 -  next
 12.1394 -    case False
 12.1395 -     with min_max.sup_absorb2 `n \<le> m` show ?thesis
 12.1396 -       by (auto simp: * upclose_def intro!: exI[of _ n] min_max.sup_absorb2)
 12.1397 +      with a show ?thesis by auto
 12.1398 +    qed simp
 12.1399    qed
 12.1400 -qed simp
 12.1401 -
 12.1402 -lemma mon_upclose_all:
 12.1403 -  fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ('b\<Colon>linorder)"
 12.1404 -  assumes "m \<le> n"
 12.1405 -  shows "u m x \<le> mon_upclose n u x"
 12.1406 -using assms proof (induct n)
 12.1407 -  case 0 thus ?case by auto
 12.1408  next
 12.1409 -  case (Suc n)
 12.1410 -  show ?case
 12.1411 -  proof (cases "m = Suc n")
 12.1412 -    case True thus ?thesis by (simp add: upclose_def)
 12.1413 -  next
 12.1414 -    case False
 12.1415 -    hence "m \<le> n" using `m \<le> Suc n` by simp
 12.1416 -    from Suc.hyps[OF this]
 12.1417 -    show ?thesis by (auto simp: upclose_def intro!: min_max.le_supI2)
 12.1418 +  assume a': "\<forall>a. {x \<in> space M. a < f x} \<in> sets M"
 12.1419 +  then have a: "\<forall>a. {x \<in> space M. f x \<le> a} \<in> sets M" unfolding less_eq_ge_measurable .
 12.1420 +  show "\<forall>a. {x \<in> space M. a \<le> f x} \<in> sets M" unfolding greater_eq_le_measurable[symmetric]
 12.1421 +  proof
 12.1422 +    fix a show "{x \<in> space M. f x < a} \<in> sets M"
 12.1423 +    proof (cases a)
 12.1424 +      case (preal r)
 12.1425 +      show ?thesis
 12.1426 +      proof cases
 12.1427 +        assume "a = 0" then show ?thesis by simp
 12.1428 +      next
 12.1429 +        assume "a \<noteq> 0"
 12.1430 +        have "{x\<in>space M. f x < a} = (\<Union>i. {x\<in>space M. f x \<le> a - inverse (of_nat (Suc i))})"
 12.1431 +        proof safe
 12.1432 +          fix x assume "f x < a" and [simp]: "x \<in> space M"
 12.1433 +          with ex_pinfreal_inverse_of_nat_Suc_less[of "a - f x"]
 12.1434 +          obtain n where "inverse (of_nat (Suc n)) < a - f x"
 12.1435 +            using preal by (cases "f x") auto
 12.1436 +          then have "f x \<le> a - inverse (of_nat (Suc n)) "
 12.1437 +            using preal by (cases "f x") (auto split: split_if_asm)
 12.1438 +          then show "x \<in> (\<Union>i. {x \<in> space M. f x \<le> a - inverse (of_nat (Suc i))})"
 12.1439 +            by auto
 12.1440 +        next
 12.1441 +          fix i x assume [simp]: "x \<in> space M"
 12.1442 +          assume "f x \<le> a - inverse (of_nat (Suc i))"
 12.1443 +          also have "\<dots> < a" using `a \<noteq> 0` preal by auto
 12.1444 +          finally show "f x < a" .
 12.1445 +        qed
 12.1446 +        with a show ?thesis by auto
 12.1447 +      qed
 12.1448 +    next
 12.1449 +      case infinite
 12.1450 +      have "f -` {\<omega>} \<inter> space M = (\<Inter>n. {x\<in>space M. of_nat n < f x})"
 12.1451 +      proof (safe, simp_all, safe)
 12.1452 +        fix x assume *: "\<forall>n::nat. Real (real n) < f x"
 12.1453 +        show "f x = \<omega>"    proof (rule ccontr)
 12.1454 +          assume "f x \<noteq> \<omega>"
 12.1455 +          with real_arch_lt[of "real (f x)"] obtain n where "f x < of_nat n"
 12.1456 +            by (auto simp: pinfreal_noteq_omega_Ex)
 12.1457 +          with *[THEN spec, of n] show False by auto
 12.1458 +        qed
 12.1459 +      qed
 12.1460 +      with a' have \<omega>: "f -` {\<omega>} \<inter> space M \<in> sets M" by auto
 12.1461 +      moreover have "{x \<in> space M. f x < a} = space M - f -` {\<omega>} \<inter> space M"
 12.1462 +        using infinite by auto
 12.1463 +      ultimately show ?thesis by auto
 12.1464 +    qed
 12.1465    qed
 12.1466  qed
 12.1467  
 12.1468 -lemma mono_convergent_limit:
 12.1469 -  fixes f :: "'a \<Rightarrow> real"
 12.1470 -  assumes "mono_convergent u f s" and "x \<in> s" and "0 < r"
 12.1471 -  shows "\<exists>N. \<forall>n\<ge>N. f x - u n x < r"
 12.1472 -proof -
 12.1473 -  from LIMSEQ_D[OF mono_convergentD(2)[OF assms(1,2)] `0 < r`]
 12.1474 -  obtain N where "\<And>n. N \<le> n \<Longrightarrow> \<bar> u n x - f x \<bar> < r" by auto
 12.1475 -  with mono_convergent_le[OF assms(1,2)] `0 < r`
 12.1476 -  show ?thesis by (auto intro!: exI[of _ N])
 12.1477 -qed
 12.1478 -
 12.1479 -lemma mon_upclose_le_mono_convergent:
 12.1480 -  assumes mc: "\<And>n. mono_convergent (\<lambda>m. u m n) (f n) s" and "x \<in> s"
 12.1481 -  and "incseq (\<lambda>n. f n x)"
 12.1482 -  shows "mon_upclose n (u n) x <= f n x"
 12.1483 -proof -
 12.1484 -  obtain m where *: "mon_upclose n (u n) x = u n m x" and "m \<le> n"
 12.1485 -    using mon_upclose_ex[of n "u n" x] by auto
 12.1486 -  note this(1)
 12.1487 -  also have "u n m x \<le> f m x" using mono_convergent_le[OF assms(1,2)] .
 12.1488 -  also have "... \<le> f n x" using assms(3) `m \<le> n` unfolding incseq_def by auto
 12.1489 -  finally show ?thesis .
 12.1490 -qed
 12.1491 -
 12.1492 -lemma mon_upclose_mono_convergent:
 12.1493 -  assumes mc_u: "\<And>n. mono_convergent (\<lambda>m. u m n) (f n) s"
 12.1494 -  and mc_f: "mono_convergent f h s"
 12.1495 -  shows "mono_convergent (\<lambda>n. mon_upclose n (u n)) h s"
 12.1496 -proof (rule mono_convergentI)
 12.1497 -  fix x assume "x \<in> s"
 12.1498 -  show "incseq (\<lambda>n. mon_upclose n (u n) x)" unfolding incseq_def
 12.1499 -  proof safe
 12.1500 -    fix m n :: nat assume "m \<le> n"
 12.1501 -    obtain i where mon: "mon_upclose m (u m) x = u m i x" and "i \<le> m"
 12.1502 -      using mon_upclose_ex[of m "u m" x] by auto
 12.1503 -    hence "i \<le> n" using `m \<le> n` by auto
 12.1504 +lemma (in sigma_algebra) borel_measurable_pinfreal_iff_greater:
 12.1505 +  "(f::'a \<Rightarrow> pinfreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. a < f x} \<in> sets M)"
 12.1506 +proof safe
 12.1507 +  fix a assume f: "f \<in> borel_measurable M"
 12.1508 +  have "{x\<in>space M. a < f x} = f -` {a <..} \<inter> space M" by auto
 12.1509 +  with f show "{x\<in>space M. a < f x} \<in> sets M"
 12.1510 +    by (auto intro!: measurable_sets)
 12.1511 +next
 12.1512 +  assume *: "\<forall>a. {x\<in>space M. a < f x} \<in> sets M"
 12.1513 +  hence **: "\<forall>a. {x\<in>space M. f x < a} \<in> sets M"
 12.1514 +    unfolding less_eq_le_pinfreal_measurable
 12.1515 +    unfolding greater_eq_le_measurable .
 12.1516  
 12.1517 -    note mon
 12.1518 -    also have "u m i x \<le> u n i x"
 12.1519 -      using mono_convergentD(1)[OF mc_u `x \<in> s`] `m \<le> n`
 12.1520 -      unfolding incseq_def by auto
 12.1521 -    also have "u n i x \<le> mon_upclose n (u n) x"
 12.1522 -      using mon_upclose_all[OF `i \<le> n`, of "u n" x] .
 12.1523 -    finally show "mon_upclose m (u m) x \<le> mon_upclose n (u n) x" .
 12.1524 -  qed
 12.1525 -
 12.1526 -  show "(\<lambda>i. mon_upclose i (u i) x) ----> h x"
 12.1527 -  proof (rule LIMSEQ_I)
 12.1528 -    fix r :: real assume "0 < r"
 12.1529 -    hence "0 < r / 2" by auto
 12.1530 -    from mono_convergent_limit[OF mc_f `x \<in> s` this]
 12.1531 -    obtain N where f_h: "\<And>n. N \<le> n \<Longrightarrow> h x - f n x < r / 2" by auto
 12.1532 -
 12.1533 -    from mono_convergent_limit[OF mc_u `x \<in> s` `0 < r / 2`]
 12.1534 -    obtain N' where u_f: "\<And>n. N' \<le> n \<Longrightarrow> f N x - u n N x < r / 2" by auto
 12.1535 +  show "f \<in> borel_measurable M" unfolding borel_measurable_pinfreal_eq_real borel_measurable_iff_greater
 12.1536 +  proof safe
 12.1537 +    have "f -` {\<omega>} \<inter> space M = space M - {x\<in>space M. f x < \<omega>}" by auto
 12.1538 +    then show \<omega>: "f -` {\<omega>} \<inter> space M \<in> sets M" using ** by auto
 12.1539  
 12.1540 -    show "\<exists>N. \<forall>n\<ge>N. norm (mon_upclose n (u n) x - h x) < r"
 12.1541 -    proof (rule exI[of _ "max N N'"], safe)
 12.1542 -      fix n assume "max N N' \<le> n"
 12.1543 -      hence "N \<le> n" and "N' \<le> n" by auto
 12.1544 -      hence "u n N x \<le> mon_upclose n (u n) x"
 12.1545 -        using mon_upclose_all[of N n "u n" x] by auto
 12.1546 -      moreover
 12.1547 -      from add_strict_mono[OF u_f[OF `N' \<le> n`] f_h[OF order_refl]]
 12.1548 -      have "h x - u n N x < r" by auto
 12.1549 -      ultimately have "h x - mon_upclose n (u n) x < r" by auto
 12.1550 -      moreover
 12.1551 -      obtain i where "mon_upclose n (u n) x = u n i x"
 12.1552 -        using mon_upclose_ex[of n "u n"] by blast
 12.1553 -      with mono_convergent_le[OF mc_u `x \<in> s`, of n i]
 12.1554 -           mono_convergent_le[OF mc_f `x \<in> s`, of i]
 12.1555 -      have "mon_upclose n (u n) x \<le> h x" by auto
 12.1556 -      ultimately
 12.1557 -      show "norm (mon_upclose n (u n) x - h x) < r" by auto
 12.1558 -     qed
 12.1559 +    fix a
 12.1560 +    have "{w \<in> space M. a < real (f w)} =
 12.1561 +      (if 0 \<le> a then {w\<in>space M. Real a < f w} - (f -` {\<omega>} \<inter> space M) else space M)"
 12.1562 +    proof (split split_if, safe del: notI)
 12.1563 +      fix x assume "0 \<le> a"
 12.1564 +      { assume "a < real (f x)" then show "Real a < f x" "x \<notin> f -` {\<omega>} \<inter> space M"
 12.1565 +          using `0 \<le> a` by (cases "f x", auto) }
 12.1566 +      { assume "Real a < f x" "x \<notin> f -` {\<omega>}" then show "a < real (f x)"
 12.1567 +          using `0 \<le> a` by (cases "f x", auto) }
 12.1568 +    next
 12.1569 +      fix x assume "\<not> 0 \<le> a" then show "a < real (f x)" by (cases "f x") auto
 12.1570 +    qed
 12.1571 +    then show "{w \<in> space M. a < real (f w)} \<in> sets M"
 12.1572 +      using \<omega> * by (auto intro!: Diff)
 12.1573    qed
 12.1574  qed
 12.1575  
 12.1576 -lemma mono_conv_outgrow:
 12.1577 -  assumes "incseq x" "x ----> y" "z < y"
 12.1578 -  shows "\<exists>b. \<forall> a \<ge> b. z < x a"
 12.1579 -using assms
 12.1580 +lemma (in sigma_algebra) borel_measurable_pinfreal_iff_less:
 12.1581 +  "(f::'a \<Rightarrow> pinfreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. f x < a} \<in> sets M)"
 12.1582 +  using borel_measurable_pinfreal_iff_greater unfolding less_eq_le_pinfreal_measurable greater_eq_le_measurable .
 12.1583 +
 12.1584 +lemma (in sigma_algebra) borel_measurable_pinfreal_iff_le:
 12.1585 +  "(f::'a \<Rightarrow> pinfreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. f x \<le> a} \<in> sets M)"
 12.1586 +  using borel_measurable_pinfreal_iff_greater unfolding less_eq_ge_measurable .
 12.1587 +
 12.1588 +lemma (in sigma_algebra) borel_measurable_pinfreal_iff_ge:
 12.1589 +  "(f::'a \<Rightarrow> pinfreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. a \<le> f x} \<in> sets M)"
 12.1590 +  using borel_measurable_pinfreal_iff_greater unfolding less_eq_le_pinfreal_measurable .
 12.1591 +
 12.1592 +lemma (in sigma_algebra) borel_measurable_pinfreal_eq_const:
 12.1593 +  fixes f :: "'a \<Rightarrow> pinfreal" assumes "f \<in> borel_measurable M"
 12.1594 +  shows "{x\<in>space M. f x = c} \<in> sets M"
 12.1595 +proof -
 12.1596 +  have "{x\<in>space M. f x = c} = (f -` {c} \<inter> space M)" by auto
 12.1597 +  then show ?thesis using assms by (auto intro!: measurable_sets)
 12.1598 +qed
 12.1599 +
 12.1600 +lemma (in sigma_algebra) borel_measurable_pinfreal_neq_const:
 12.1601 +  fixes f :: "'a \<Rightarrow> pinfreal"
 12.1602 +  assumes "f \<in> borel_measurable M"
 12.1603 +  shows "{x\<in>space M. f x \<noteq> c} \<in> sets M"
 12.1604 +proof -
 12.1605 +  have "{x\<in>space M. f x \<noteq> c} = space M - (f -` {c} \<inter> space M)" by auto
 12.1606 +  then show ?thesis using assms by (auto intro!: measurable_sets)
 12.1607 +qed
 12.1608 +
 12.1609 +lemma (in sigma_algebra) borel_measurable_pinfreal_less[intro,simp]:
 12.1610 +  fixes f g :: "'a \<Rightarrow> pinfreal"
 12.1611 +  assumes f: "f \<in> borel_measurable M"
 12.1612 +  assumes g: "g \<in> borel_measurable M"
 12.1613 +  shows "{x \<in> space M. f x < g x} \<in> sets M"
 12.1614 +proof -
 12.1615 +  have "(\<lambda>x. real (f x)) \<in> borel_measurable M"
 12.1616 +    "(\<lambda>x. real (g x)) \<in> borel_measurable M"
 12.1617 +    using assms by (auto intro!: borel_measurable_real)
 12.1618 +  from borel_measurable_less[OF this]
 12.1619 +  have "{x \<in> space M. real (f x) < real (g x)} \<in> sets M" .
 12.1620 +  moreover have "{x \<in> space M. f x \<noteq> \<omega>} \<in> sets M" using f by (rule borel_measurable_pinfreal_neq_const)
 12.1621 +  moreover have "{x \<in> space M. g x = \<omega>} \<in> sets M" using g by (rule borel_measurable_pinfreal_eq_const)
 12.1622 +  moreover have "{x \<in> space M. g x \<noteq> \<omega>} \<in> sets M" using g by (rule borel_measurable_pinfreal_neq_const)
 12.1623 +  moreover have "{x \<in> space M. f x < g x} = ({x \<in> space M. g x = \<omega>} \<inter> {x \<in> space M. f x \<noteq> \<omega>}) \<union>
 12.1624 +    ({x \<in> space M. g x \<noteq> \<omega>} \<inter> {x \<in> space M. f x \<noteq> \<omega>} \<inter> {x \<in> space M. real (f x) < real (g x)})"
 12.1625 +    by (auto simp: real_of_pinfreal_strict_mono_iff)
 12.1626 +  ultimately show ?thesis by auto
 12.1627 +qed
 12.1628 +
 12.1629 +lemma (in sigma_algebra) borel_measurable_pinfreal_le[intro,simp]:
 12.1630 +  fixes f :: "'a \<Rightarrow> pinfreal"
 12.1631 +  assumes f: "f \<in> borel_measurable M"
 12.1632 +  assumes g: "g \<in> borel_measurable M"
 12.1633 +  shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
 12.1634 +proof -
 12.1635 +  have "{x \<in> space M. f x \<le> g x} = space M - {x \<in> space M. g x < f x}" by auto
 12.1636 +  then show ?thesis using g f by auto
 12.1637 +qed
 12.1638 +
 12.1639 +lemma (in sigma_algebra) borel_measurable_pinfreal_eq[intro,simp]:
 12.1640 +  fixes f :: "'a \<Rightarrow> pinfreal"
 12.1641 +  assumes f: "f \<in> borel_measurable M"
 12.1642 +  assumes g: "g \<in> borel_measurable M"
 12.1643 +  shows "{w \<in> space M. f w = g w} \<in> sets M"
 12.1644 +proof -
 12.1645 +  have "{x \<in> space M. f x = g x} = {x \<in> space M. g x \<le> f x} \<inter> {x \<in> space M. f x \<le> g x}" by auto
 12.1646 +  then show ?thesis using g f by auto
 12.1647 +qed
 12.1648 +
 12.1649 +lemma (in sigma_algebra) borel_measurable_pinfreal_neq[intro,simp]:
 12.1650 +  fixes f :: "'a \<Rightarrow> pinfreal"
 12.1651 +  assumes f: "f \<in> borel_measurable M"
 12.1652 +  assumes g: "g \<in> borel_measurable M"
 12.1653 +  shows "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
 12.1654  proof -
 12.1655 -  from assms have "y - z > 0" by simp
 12.1656 -  hence A: "\<exists>n. (\<forall> m \<ge> n. \<bar> x m + - y \<bar> < y - z)" using assms
 12.1657 -    unfolding incseq_def LIMSEQ_def dist_real_def diff_minus
 12.1658 -    by simp
 12.1659 -  have "\<forall>m. x m \<le> y" using incseq_le assms by auto
 12.1660 -  hence B: "\<forall>m. \<bar> x m + - y \<bar> = y - x m"
 12.1661 -    by (metis abs_if abs_minus_add_cancel less_iff_diff_less_0 linorder_not_le diff_minus)
 12.1662 -  from A B show ?thesis by auto
 12.1663 +  have "{w \<in> space M. f w \<noteq> g w} = space M - {w \<in> space M. f w = g w}" by auto
 12.1664 +  thus ?thesis using f g by auto
 12.1665 +qed
 12.1666 +
 12.1667 +lemma (in sigma_algebra) borel_measurable_pinfreal_add[intro, simp]:
 12.1668 +  fixes f :: "'a \<Rightarrow> pinfreal"
 12.1669 +  assumes measure: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
 12.1670 +  shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
 12.1671 +proof -
 12.1672 +  have *: "(\<lambda>x. f x + g x) =
 12.1673 +     (\<lambda>x. if f x = \<omega> then \<omega> else if g x = \<omega> then \<omega> else Real (real (f x) + real (g x)))"
 12.1674 +     by (auto simp: expand_fun_eq pinfreal_noteq_omega_Ex)
 12.1675 +  show ?thesis using assms unfolding *
 12.1676 +    by (auto intro!: measurable_If)
 12.1677 +qed
 12.1678 +
 12.1679 +lemma (in sigma_algebra) borel_measurable_pinfreal_times[intro, simp]:
 12.1680 +  fixes f :: "'a \<Rightarrow> pinfreal" assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
 12.1681 +  shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
 12.1682 +proof -
 12.1683 +  have *: "(\<lambda>x. f x * g x) =
 12.1684 +     (\<lambda>x. if f x = 0 then 0 else if g x = 0 then 0 else if f x = \<omega> then \<omega> else if g x = \<omega> then \<omega> else
 12.1685 +      Real (real (f x) * real (g x)))"
 12.1686 +     by (auto simp: expand_fun_eq pinfreal_noteq_omega_Ex)
 12.1687 +  show ?thesis using assms unfolding *
 12.1688 +    by (auto intro!: measurable_If)
 12.1689 +qed
 12.1690 +
 12.1691 +lemma (in sigma_algebra) borel_measurable_pinfreal_setsum[simp, intro]:
 12.1692 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> pinfreal"
 12.1693 +  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
 12.1694 +  shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
 12.1695 +proof cases
 12.1696 +  assume "finite S"
 12.1697 +  thus ?thesis using assms
 12.1698 +    by induct auto
 12.1699 +qed (simp add: borel_measurable_const)
 12.1700 +
 12.1701 +lemma (in sigma_algebra) borel_measurable_pinfreal_min[intro, simp]:
 12.1702 +  fixes f g :: "'a \<Rightarrow> pinfreal"
 12.1703 +  assumes "f \<in> borel_measurable M"
 12.1704 +  assumes "g \<in> borel_measurable M"
 12.1705 +  shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
 12.1706 +  using assms unfolding min_def by (auto intro!: measurable_If)
 12.1707 +
 12.1708 +lemma (in sigma_algebra) borel_measurable_pinfreal_max[intro]:
 12.1709 +  fixes f g :: "'a \<Rightarrow> pinfreal"
 12.1710 +  assumes "f \<in> borel_measurable M"
 12.1711 +  and "g \<in> borel_measurable M"
 12.1712 +  shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
 12.1713 +  using assms unfolding max_def by (auto intro!: measurable_If)
 12.1714 +
 12.1715 +lemma (in sigma_algebra) borel_measurable_SUP[simp, intro]:
 12.1716 +  fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> pinfreal"
 12.1717 +  assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
 12.1718 +  shows "(SUP i : A. f i) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
 12.1719 +  unfolding borel_measurable_pinfreal_iff_greater
 12.1720 +proof safe
 12.1721 +  fix a
 12.1722 +  have "{x\<in>space M. a < ?sup x} = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
 12.1723 +    by (auto simp: less_Sup_iff SUPR_def[where 'a=pinfreal] SUPR_fun_expand[where 'b=pinfreal])
 12.1724 +  then show "{x\<in>space M. a < ?sup x} \<in> sets M"
 12.1725 +    using assms by auto
 12.1726 +qed
 12.1727 +
 12.1728 +lemma (in sigma_algebra) borel_measurable_INF[simp, intro]:
 12.1729 +  fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> pinfreal"
 12.1730 +  assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
 12.1731 +  shows "(INF i : A. f i) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
 12.1732 +  unfolding borel_measurable_pinfreal_iff_less
 12.1733 +proof safe
 12.1734 +  fix a
 12.1735 +  have "{x\<in>space M. ?inf x < a} = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
 12.1736 +    by (auto simp: Inf_less_iff INFI_def[where 'a=pinfreal] INFI_fun_expand)
 12.1737 +  then show "{x\<in>space M. ?inf x < a} \<in> sets M"
 12.1738 +    using assms by auto
 12.1739 +qed
 12.1740 +
 12.1741 +lemma (in sigma_algebra) borel_measurable_pinfreal_diff:
 12.1742 +  fixes f g :: "'a \<Rightarrow> pinfreal"
 12.1743 +  assumes "f \<in> borel_measurable M"
 12.1744 +  assumes "g \<in> borel_measurable M"
 12.1745 +  shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
 12.1746 +  unfolding borel_measurable_pinfreal_iff_greater
 12.1747 +proof safe
 12.1748 +  fix a
 12.1749 +  have "{x \<in> space M. a < f x - g x} = {x \<in> space M. g x + a < f x}"
 12.1750 +    by (simp add: pinfreal_less_minus_iff)
 12.1751 +  then show "{x \<in> space M. a < f x - g x} \<in> sets M"
 12.1752 +    using assms by auto
 12.1753  qed
 12.1754  
 12.1755  end
    13.1 --- a/src/HOL/Probability/Caratheodory.thy	Mon Aug 23 17:46:13 2010 +0200
    13.2 +++ b/src/HOL/Probability/Caratheodory.thy	Mon Aug 23 19:35:57 2010 +0200
    13.3 @@ -1,43 +1,28 @@
    13.4  header {*Caratheodory Extension Theorem*}
    13.5  
    13.6  theory Caratheodory
    13.7 -  imports Sigma_Algebra SeriesPlus
    13.8 +  imports Sigma_Algebra Positive_Infinite_Real
    13.9  begin
   13.10  
   13.11  text{*From the Hurd/Coble measure theory development, translated by Lawrence Paulson.*}
   13.12  
   13.13  subsection {* Measure Spaces *}
   13.14  
   13.15 -text {*A measure assigns a nonnegative real to every measurable set. 
   13.16 -       It is countably additive for disjoint sets.*}
   13.17 -
   13.18 -record 'a measure_space = "'a algebra" +
   13.19 -  measure:: "'a set \<Rightarrow> real"
   13.20 -
   13.21 -definition
   13.22 -  disjoint_family_on  where
   13.23 -  "disjoint_family_on A S \<longleftrightarrow> (\<forall>m\<in>S. \<forall>n\<in>S. m \<noteq> n \<longrightarrow> A m \<inter> A n = {})"
   13.24 -
   13.25 -abbreviation
   13.26 -  "disjoint_family A \<equiv> disjoint_family_on A UNIV"
   13.27 -
   13.28 -definition
   13.29 -  positive  where
   13.30 -  "positive M f \<longleftrightarrow> f {} = (0::real) & (\<forall>x \<in> sets M. 0 \<le> f x)"
   13.31 +definition "positive f \<longleftrightarrow> f {} = (0::pinfreal)" -- "Positive is enforced by the type"
   13.32  
   13.33  definition
   13.34    additive  where
   13.35 -  "additive M f \<longleftrightarrow> 
   13.36 -    (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<inter> y = {} 
   13.37 +  "additive M f \<longleftrightarrow>
   13.38 +    (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<inter> y = {}
   13.39      \<longrightarrow> f (x \<union> y) = f x + f y)"
   13.40  
   13.41  definition
   13.42    countably_additive  where
   13.43 -  "countably_additive M f \<longleftrightarrow> 
   13.44 -    (\<forall>A. range A \<subseteq> sets M \<longrightarrow> 
   13.45 +  "countably_additive M f \<longleftrightarrow>
   13.46 +    (\<forall>A. range A \<subseteq> sets M \<longrightarrow>
   13.47           disjoint_family A \<longrightarrow>
   13.48 -         (\<Union>i. A i) \<in> sets M \<longrightarrow> 
   13.49 -         (\<lambda>n. f (A n))  sums  f (\<Union>i. A i))"
   13.50 +         (\<Union>i. A i) \<in> sets M \<longrightarrow>
   13.51 +         (\<Sum>\<^isub>\<infinity> n. f (A n)) = f (\<Union>i. A i))"
   13.52  
   13.53  definition
   13.54    increasing  where
   13.55 @@ -45,90 +30,58 @@
   13.56  
   13.57  definition
   13.58    subadditive  where
   13.59 -  "subadditive M f \<longleftrightarrow> 
   13.60 -    (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<inter> y = {} 
   13.61 +  "subadditive M f \<longleftrightarrow>
   13.62 +    (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<inter> y = {}
   13.63      \<longrightarrow> f (x \<union> y) \<le> f x + f y)"
   13.64  
   13.65  definition
   13.66    countably_subadditive  where
   13.67 -  "countably_subadditive M f \<longleftrightarrow> 
   13.68 -    (\<forall>A. range A \<subseteq> sets M \<longrightarrow> 
   13.69 +  "countably_subadditive M f \<longleftrightarrow>
   13.70 +    (\<forall>A. range A \<subseteq> sets M \<longrightarrow>
   13.71           disjoint_family A \<longrightarrow>
   13.72 -         (\<Union>i. A i) \<in> sets M \<longrightarrow> 
   13.73 -         summable (f o A) \<longrightarrow>
   13.74 -         f (\<Union>i. A i) \<le> suminf (\<lambda>n. f (A n)))"
   13.75 +         (\<Union>i. A i) \<in> sets M \<longrightarrow>
   13.76 +         f (\<Union>i. A i) \<le> psuminf (\<lambda>n. f (A n)))"
   13.77  
   13.78  definition
   13.79    lambda_system where
   13.80 -  "lambda_system M f = 
   13.81 +  "lambda_system M f =
   13.82      {l. l \<in> sets M & (\<forall>x \<in> sets M. f (l \<inter> x) + f ((space M - l) \<inter> x) = f x)}"
   13.83  
   13.84  definition
   13.85    outer_measure_space where
   13.86 -  "outer_measure_space M f  \<longleftrightarrow> 
   13.87 -     positive M f & increasing M f & countably_subadditive M f"
   13.88 +  "outer_measure_space M f  \<longleftrightarrow>
   13.89 +     positive f \<and> increasing M f \<and> countably_subadditive M f"
   13.90  
   13.91  definition
   13.92    measure_set where
   13.93    "measure_set M f X =
   13.94 -     {r . \<exists>A. range A \<subseteq> sets M & disjoint_family A & X \<subseteq> (\<Union>i. A i) & (f \<circ> A) sums r}"
   13.95 -
   13.96 +     {r . \<exists>A. range A \<subseteq> sets M \<and> disjoint_family A \<and> X \<subseteq> (\<Union>i. A i) \<and> (\<Sum>\<^isub>\<infinity> i. f (A i)) = r}"
   13.97  
   13.98  locale measure_space = sigma_algebra +
   13.99 -  assumes positive: "!!a. a \<in> sets M \<Longrightarrow> 0 \<le> measure M a"
  13.100 -      and empty_measure [simp]: "measure M {} = (0::real)"
  13.101 -      and ca: "countably_additive M (measure M)"
  13.102 -
  13.103 -subsection {* Basic Lemmas *}
  13.104 -
  13.105 -lemma positive_imp_0: "positive M f \<Longrightarrow> f {} = 0"
  13.106 -  by (simp add: positive_def) 
  13.107 -
  13.108 -lemma positive_imp_pos: "positive M f \<Longrightarrow> x \<in> sets M \<Longrightarrow> 0 \<le> f x"
  13.109 -  by (simp add: positive_def) 
  13.110 +  fixes \<mu> :: "'a set \<Rightarrow> pinfreal"
  13.111 +  assumes empty_measure [simp]: "\<mu> {} = 0"
  13.112 +      and ca: "countably_additive M \<mu>"
  13.113  
  13.114  lemma increasingD:
  13.115       "increasing M f \<Longrightarrow> x \<subseteq> y \<Longrightarrow> x\<in>sets M \<Longrightarrow> y\<in>sets M \<Longrightarrow> f x \<le> f y"
  13.116    by (auto simp add: increasing_def)
  13.117  
  13.118  lemma subadditiveD:
  13.119 -     "subadditive M f \<Longrightarrow> x \<inter> y = {} \<Longrightarrow> x\<in>sets M \<Longrightarrow> y\<in>sets M 
  13.120 +     "subadditive M f \<Longrightarrow> x \<inter> y = {} \<Longrightarrow> x\<in>sets M \<Longrightarrow> y\<in>sets M
  13.121        \<Longrightarrow> f (x \<union> y) \<le> f x + f y"
  13.122    by (auto simp add: subadditive_def)
  13.123  
  13.124  lemma additiveD:
  13.125 -     "additive M f \<Longrightarrow> x \<inter> y = {} \<Longrightarrow> x\<in>sets M \<Longrightarrow> y\<in>sets M 
  13.126 +     "additive M f \<Longrightarrow> x \<inter> y = {} \<Longrightarrow> x\<in>sets M \<Longrightarrow> y\<in>sets M
  13.127        \<Longrightarrow> f (x \<union> y) = f x + f y"
  13.128    by (auto simp add: additive_def)
  13.129  
  13.130  lemma countably_additiveD:
  13.131    "countably_additive M f \<Longrightarrow> range A \<subseteq> sets M \<Longrightarrow> disjoint_family A
  13.132 -   \<Longrightarrow> (\<Union>i. A i) \<in> sets M \<Longrightarrow> (\<lambda>n. f (A n))  sums  f (\<Union>i. A i)"
  13.133 +   \<Longrightarrow> (\<Union>i. A i) \<in> sets M \<Longrightarrow> (\<Sum>\<^isub>\<infinity> n. f (A n)) = f (\<Union>i. A i)"
  13.134    by (simp add: countably_additive_def)
  13.135  
  13.136 -lemma Int_Diff_disjoint: "A \<inter> B \<inter> (A - B) = {}"
  13.137 -  by blast
  13.138 -
  13.139 -lemma Int_Diff_Un: "A \<inter> B \<union> (A - B) = A"
  13.140 -  by blast
  13.141 -
  13.142 -lemma disjoint_family_subset:
  13.143 -     "disjoint_family A \<Longrightarrow> (!!x. B x \<subseteq> A x) \<Longrightarrow> disjoint_family B"
  13.144 -  by (force simp add: disjoint_family_on_def)
  13.145 -
  13.146 -subsection {* A Two-Element Series *}
  13.147 -
  13.148 -definition binaryset :: "'a set \<Rightarrow> 'a set \<Rightarrow> nat \<Rightarrow> 'a set "
  13.149 -  where "binaryset A B = (\<lambda>\<^isup>x. {})(0 := A, Suc 0 := B)"
  13.150 -
  13.151 -lemma range_binaryset_eq: "range(binaryset A B) = {A,B,{}}"
  13.152 -  apply (simp add: binaryset_def)
  13.153 -  apply (rule set_ext)
  13.154 -  apply (auto simp add: image_iff)
  13.155 -  done
  13.156 -
  13.157 -lemma UN_binaryset_eq: "(\<Union>i. binaryset A B i) = A \<union> B"
  13.158 -  by (simp add: UNION_eq_Union_image range_binaryset_eq)
  13.159 +section "Extend binary sets"
  13.160  
  13.161  lemma LIMSEQ_binaryset:
  13.162    assumes f: "f {} = 0"
  13.163 @@ -153,17 +106,31 @@
  13.164  lemma binaryset_sums:
  13.165    assumes f: "f {} = 0"
  13.166    shows  "(\<lambda>n. f (binaryset A B n)) sums (f A + f B)"
  13.167 -    by (simp add: sums_def LIMSEQ_binaryset [where f=f, OF f]) 
  13.168 +    by (simp add: sums_def LIMSEQ_binaryset [where f=f, OF f])
  13.169  
  13.170  lemma suminf_binaryset_eq:
  13.171       "f {} = 0 \<Longrightarrow> suminf (\<lambda>n. f (binaryset A B n)) = f A + f B"
  13.172    by (metis binaryset_sums sums_unique)
  13.173  
  13.174 +lemma binaryset_psuminf:
  13.175 +  assumes "f {} = 0"
  13.176 +  shows "(\<Sum>\<^isub>\<infinity> n. f (binaryset A B n)) = f A + f B" (is "?suminf = ?sum")
  13.177 +proof -
  13.178 +  have *: "{..<2} = {0, 1::nat}" by auto
  13.179 +  have "\<forall>n\<ge>2. f (binaryset A B n) = 0"
  13.180 +    unfolding binaryset_def
  13.181 +    using assms by auto
  13.182 +  hence "?suminf = (\<Sum>N<2. f (binaryset A B N))"
  13.183 +    by (rule psuminf_finite)
  13.184 +  also have "... = ?sum" unfolding * binaryset_def
  13.185 +    by simp
  13.186 +  finally show ?thesis .
  13.187 +qed
  13.188  
  13.189  subsection {* Lambda Systems *}
  13.190  
  13.191  lemma (in algebra) lambda_system_eq:
  13.192 -    "lambda_system M f = 
  13.193 +    "lambda_system M f =
  13.194          {l. l \<in> sets M & (\<forall>x \<in> sets M. f (x \<inter> l) + f (x - l) = f x)}"
  13.195  proof -
  13.196    have [simp]: "!!l x. l \<in> sets M \<Longrightarrow> x \<in> sets M \<Longrightarrow> (space M - l) \<inter> x = x - l"
  13.197 @@ -173,28 +140,28 @@
  13.198  qed
  13.199  
  13.200  lemma (in algebra) lambda_system_empty:
  13.201 -    "positive M f \<Longrightarrow> {} \<in> lambda_system M f"
  13.202 -  by (auto simp add: positive_def lambda_system_eq) 
  13.203 +  "positive f \<Longrightarrow> {} \<in> lambda_system M f"
  13.204 +  by (auto simp add: positive_def lambda_system_eq)
  13.205  
  13.206  lemma lambda_system_sets:
  13.207      "x \<in> lambda_system M f \<Longrightarrow> x \<in> sets M"
  13.208    by (simp add:  lambda_system_def)
  13.209  
  13.210  lemma (in algebra) lambda_system_Compl:
  13.211 -  fixes f:: "'a set \<Rightarrow> real"
  13.212 +  fixes f:: "'a set \<Rightarrow> pinfreal"
  13.213    assumes x: "x \<in> lambda_system M f"
  13.214    shows "space M - x \<in> lambda_system M f"
  13.215    proof -
  13.216      have "x \<subseteq> space M"
  13.217        by (metis sets_into_space lambda_system_sets x)
  13.218      hence "space M - (space M - x) = x"
  13.219 -      by (metis double_diff equalityE) 
  13.220 +      by (metis double_diff equalityE)
  13.221      with x show ?thesis
  13.222 -      by (force simp add: lambda_system_def)
  13.223 +      by (force simp add: lambda_system_def ac_simps)
  13.224    qed
  13.225  
  13.226  lemma (in algebra) lambda_system_Int:
  13.227 -  fixes f:: "'a set \<Rightarrow> real"
  13.228 +  fixes f:: "'a set \<Rightarrow> pinfreal"
  13.229    assumes xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
  13.230    shows "x \<inter> y \<in> lambda_system M f"
  13.231    proof -
  13.232 @@ -213,42 +180,42 @@
  13.233          ultimately
  13.234          have ey: "f (u - x \<inter> y) = f (u \<inter> y - x) + f (u - y)" using fy
  13.235            by force
  13.236 -        have "f (u \<inter> (x \<inter> y)) + f (u - x \<inter> y) 
  13.237 +        have "f (u \<inter> (x \<inter> y)) + f (u - x \<inter> y)
  13.238                = (f (u \<inter> (x \<inter> y)) + f (u \<inter> y - x)) + f (u - y)"
  13.239 -          by (simp add: ey) 
  13.240 +          by (simp add: ey ac_simps)
  13.241          also have "... =  (f ((u \<inter> y) \<inter> x) + f (u \<inter> y - x)) + f (u - y)"
  13.242 -          by (simp add: Int_ac) 
  13.243 +          by (simp add: Int_ac)
  13.244          also have "... = f (u \<inter> y) + f (u - y)"
  13.245            using fx [THEN bspec, of "u \<inter> y"] Int y u
  13.246            by force
  13.247          also have "... = f u"
  13.248 -          by (metis fy u) 
  13.249 +          by (metis fy u)
  13.250          finally show "f (u \<inter> (x \<inter> y)) + f (u - x \<inter> y) = f u" .
  13.251        qed
  13.252    qed
  13.253  
  13.254  
  13.255  lemma (in algebra) lambda_system_Un:
  13.256 -  fixes f:: "'a set \<Rightarrow> real"
  13.257 +  fixes f:: "'a set \<Rightarrow> pinfreal"
  13.258    assumes xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
  13.259    shows "x \<union> y \<in> lambda_system M f"
  13.260  proof -
  13.261    have "(space M - x) \<inter> (space M - y) \<in> sets M"
  13.262 -    by (metis Diff_Un Un compl_sets lambda_system_sets xl yl) 
  13.263 +    by (metis Diff_Un Un compl_sets lambda_system_sets xl yl)
  13.264    moreover
  13.265    have "x \<union> y = space M - ((space M - x) \<inter> (space M - y))"
  13.266      by auto  (metis subsetD lambda_system_sets sets_into_space xl yl)+
  13.267    ultimately show ?thesis
  13.268 -    by (metis lambda_system_Compl lambda_system_Int xl yl) 
  13.269 +    by (metis lambda_system_Compl lambda_system_Int xl yl)
  13.270  qed
  13.271  
  13.272  lemma (in algebra) lambda_system_algebra:
  13.273 -    "positive M f \<Longrightarrow> algebra (M (|sets := lambda_system M f|))"
  13.274 -  apply (auto simp add: algebra_def) 
  13.275 +  "positive f \<Longrightarrow> algebra (M (|sets := lambda_system M f|))"
  13.276 +  apply (auto simp add: algebra_def)
  13.277    apply (metis lambda_system_sets set_mp sets_into_space)
  13.278    apply (metis lambda_system_empty)
  13.279    apply (metis lambda_system_Compl)
  13.280 -  apply (metis lambda_system_Un) 
  13.281 +  apply (metis lambda_system_Un)
  13.282    done
  13.283  
  13.284  lemma (in algebra) lambda_system_strong_additive:
  13.285 @@ -259,19 +226,13 @@
  13.286      have "z \<inter> x = (z \<inter> (x \<union> y)) \<inter> x" using disj by blast
  13.287      moreover
  13.288      have "z \<inter> y = (z \<inter> (x \<union> y)) - x" using disj by blast
  13.289 -    moreover 
  13.290 +    moreover
  13.291      have "(z \<inter> (x \<union> y)) \<in> sets M"
  13.292 -      by (metis Int Un lambda_system_sets xl yl z) 
  13.293 +      by (metis Int Un lambda_system_sets xl yl z)
  13.294      ultimately show ?thesis using xl yl
  13.295        by (simp add: lambda_system_eq)
  13.296    qed
  13.297  
  13.298 -lemma (in algebra) Int_space_eq1 [simp]: "x \<in> sets M \<Longrightarrow> space M \<inter> x = x"
  13.299 -  by (metis Int_absorb1 sets_into_space)
  13.300 -
  13.301 -lemma (in algebra) Int_space_eq2 [simp]: "x \<in> sets M \<Longrightarrow> x \<inter> space M = x"
  13.302 -  by (metis Int_absorb2 sets_into_space)
  13.303 -
  13.304  lemma (in algebra) lambda_system_additive:
  13.305       "additive (M (|sets := lambda_system M f|)) f"
  13.306    proof (auto simp add: additive_def)
  13.307 @@ -279,14 +240,14 @@
  13.308      assume disj: "x \<inter> y = {}"
  13.309         and xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
  13.310      hence  "x \<in> sets M" "y \<in> sets M" by (blast intro: lambda_system_sets)+
  13.311 -    thus "f (x \<union> y) = f x + f y" 
  13.312 +    thus "f (x \<union> y) = f x + f y"
  13.313        using lambda_system_strong_additive [OF top disj xl yl]
  13.314        by (simp add: Un)
  13.315    qed
  13.316  
  13.317  
  13.318  lemma (in algebra) countably_subadditive_subadditive:
  13.319 -  assumes f: "positive M f" and cs: "countably_subadditive M f"
  13.320 +  assumes f: "positive f" and cs: "countably_subadditive M f"
  13.321    shows  "subadditive M f"
  13.322  proof (auto simp add: subadditive_def)
  13.323    fix x y
  13.324 @@ -295,159 +256,80 @@
  13.325      by (auto simp add: disjoint_family_on_def binaryset_def)
  13.326    hence "range (binaryset x y) \<subseteq> sets M \<longrightarrow>
  13.327           (\<Union>i. binaryset x y i) \<in> sets M \<longrightarrow>
  13.328 -         summable (f o (binaryset x y)) \<longrightarrow>
  13.329 -         f (\<Union>i. binaryset x y i) \<le> suminf (\<lambda>n. f (binaryset x y n))"
  13.330 +         f (\<Union>i. binaryset x y i) \<le> (\<Sum>\<^isub>\<infinity> n. f (binaryset x y n))"
  13.331      using cs by (simp add: countably_subadditive_def)
  13.332    hence "{x,y,{}} \<subseteq> sets M \<longrightarrow> x \<union> y \<in> sets M \<longrightarrow>
  13.333 -         summable (f o (binaryset x y)) \<longrightarrow>
  13.334 -         f (x \<union> y) \<le> suminf (\<lambda>n. f (binaryset x y n))"
  13.335 +         f (x \<union> y) \<le> (\<Sum>\<^isub>\<infinity> n. f (binaryset x y n))"
  13.336      by (simp add: range_binaryset_eq UN_binaryset_eq)
  13.337 -  thus "f (x \<union> y) \<le>  f x + f y" using f x y binaryset_sums
  13.338 -    by (auto simp add: Un sums_iff positive_def o_def)
  13.339 -qed
  13.340 -
  13.341 -
  13.342 -definition disjointed :: "(nat \<Rightarrow> 'a set) \<Rightarrow> nat \<Rightarrow> 'a set "
  13.343 -  where "disjointed A n = A n - (\<Union>i\<in>{0..<n}. A i)"
  13.344 -
  13.345 -lemma finite_UN_disjointed_eq: "(\<Union>i\<in>{0..<n}. disjointed A i) = (\<Union>i\<in>{0..<n}. A i)"
  13.346 -proof (induct n)
  13.347 -  case 0 show ?case by simp
  13.348 -next
  13.349 -  case (Suc n)
  13.350 -  thus ?case by (simp add: atLeastLessThanSuc disjointed_def)
  13.351 +  thus "f (x \<union> y) \<le>  f x + f y" using f x y
  13.352 +    by (auto simp add: Un o_def binaryset_psuminf positive_def)
  13.353  qed
  13.354  
  13.355 -lemma UN_disjointed_eq: "(\<Union>i. disjointed A i) = (\<Union>i. A i)"
  13.356 -  apply (rule UN_finite2_eq [where k=0])
  13.357 -  apply (simp add: finite_UN_disjointed_eq)
  13.358 -  done
  13.359 -
  13.360 -lemma less_disjoint_disjointed: "m<n \<Longrightarrow> disjointed A m \<inter> disjointed A n = {}"
  13.361 -  by (auto simp add: disjointed_def)
  13.362 -
  13.363 -lemma disjoint_family_disjointed: "disjoint_family (disjointed A)"
  13.364 -  by (simp add: disjoint_family_on_def)
  13.365 -     (metis neq_iff Int_commute less_disjoint_disjointed)
  13.366 -
  13.367 -lemma disjointed_subset: "disjointed A n \<subseteq> A n"
  13.368 -  by (auto simp add: disjointed_def)
  13.369 -
  13.370 -
  13.371 -lemma (in algebra) UNION_in_sets:
  13.372 -  fixes A:: "nat \<Rightarrow> 'a set"
  13.373 -  assumes A: "range A \<subseteq> sets M "
  13.374 -  shows  "(\<Union>i\<in>{0..<n}. A i) \<in> sets M"
  13.375 -proof (induct n)
  13.376 -  case 0 show ?case by simp
  13.377 -next
  13.378 -  case (Suc n) 
  13.379 -  thus ?case
  13.380 -    by (simp add: atLeastLessThanSuc) (metis A Un UNIV_I image_subset_iff)
  13.381 -qed
  13.382 -
  13.383 -lemma (in algebra) range_disjointed_sets:
  13.384 -  assumes A: "range A \<subseteq> sets M "
  13.385 -  shows  "range (disjointed A) \<subseteq> sets M"
  13.386 -proof (auto simp add: disjointed_def) 
  13.387 -  fix n
  13.388 -  show "A n - (\<Union>i\<in>{0..<n}. A i) \<in> sets M" using UNION_in_sets
  13.389 -    by (metis A Diff UNIV_I image_subset_iff)
  13.390 -qed
  13.391 -
  13.392 -lemma sigma_algebra_disjoint_iff: 
  13.393 -     "sigma_algebra M \<longleftrightarrow> 
  13.394 -      algebra M &
  13.395 -      (\<forall>A. range A \<subseteq> sets M \<longrightarrow> disjoint_family A \<longrightarrow> 
  13.396 -           (\<Union>i::nat. A i) \<in> sets M)"
  13.397 -proof (auto simp add: sigma_algebra_iff)
  13.398 -  fix A :: "nat \<Rightarrow> 'a set"
  13.399 -  assume M: "algebra M"
  13.400 -     and A: "range A \<subseteq> sets M"
  13.401 -     and UnA: "\<forall>A. range A \<subseteq> sets M \<longrightarrow>
  13.402 -               disjoint_family A \<longrightarrow> (\<Union>i::nat. A i) \<in> sets M"
  13.403 -  hence "range (disjointed A) \<subseteq> sets M \<longrightarrow>
  13.404 -         disjoint_family (disjointed A) \<longrightarrow>
  13.405 -         (\<Union>i. disjointed A i) \<in> sets M" by blast
  13.406 -  hence "(\<Union>i. disjointed A i) \<in> sets M"
  13.407 -    by (simp add: algebra.range_disjointed_sets M A disjoint_family_disjointed) 
  13.408 -  thus "(\<Union>i::nat. A i) \<in> sets M" by (simp add: UN_disjointed_eq)
  13.409 -qed
  13.410 -
  13.411 -
  13.412  lemma (in algebra) additive_sum:
  13.413    fixes A:: "nat \<Rightarrow> 'a set"
  13.414 -  assumes f: "positive M f" and ad: "additive M f"
  13.415 +  assumes f: "positive f" and ad: "additive M f"
  13.416        and A: "range A \<subseteq> sets M"
  13.417        and disj: "disjoint_family A"
  13.418 -  shows  "setsum (f o A) {0..<n} = f (\<Union>i\<in>{0..<n}. A i)"
  13.419 +  shows  "setsum (f \<circ> A) {0..<n} = f (\<Union>i\<in>{0..<n}. A i)"
  13.420  proof (induct n)
  13.421 -  case 0 show ?case using f by (simp add: positive_def) 
  13.422 +  case 0 show ?case using f by (simp add: positive_def)
  13.423  next
  13.424 -  case (Suc n) 
  13.425 -  have "A n \<inter> (\<Union>i\<in>{0..<n}. A i) = {}" using disj 
  13.426 +  case (Suc n)
  13.427 +  have "A n \<inter> (\<Union>i\<in>{0..<n}. A i) = {}" using disj
  13.428      by (auto simp add: disjoint_family_on_def neq_iff) blast
  13.429 -  moreover 
  13.430 -  have "A n \<in> sets M" using A by blast 
  13.431 +  moreover
  13.432 +  have "A n \<in> sets M" using A by blast
  13.433    moreover have "(\<Union>i\<in>{0..<n}. A i) \<in> sets M"
  13.434      by (metis A UNION_in_sets atLeast0LessThan)
  13.435 -  moreover 
  13.436 +  moreover
  13.437    ultimately have "f (A n \<union> (\<Union>i\<in>{0..<n}. A i)) = f (A n) + f(\<Union>i\<in>{0..<n}. A i)"
  13.438 -    using ad UNION_in_sets A by (auto simp add: additive_def) 
  13.439 +    using ad UNION_in_sets A by (auto simp add: additive_def)
  13.440    with Suc.hyps show ?case using ad
  13.441 -    by (auto simp add: atLeastLessThanSuc additive_def) 
  13.442 +    by (auto simp add: atLeastLessThanSuc additive_def)
  13.443  qed
  13.444  
  13.445  
  13.446  lemma countably_subadditiveD:
  13.447    "countably_subadditive M f \<Longrightarrow> range A \<subseteq> sets M \<Longrightarrow> disjoint_family A \<Longrightarrow>
  13.448 -   (\<Union>i. A i) \<in> sets M \<Longrightarrow> summable (f o A) \<Longrightarrow> f (\<Union>i. A i) \<le> suminf (f o A)" 
  13.449 +   (\<Union>i. A i) \<in> sets M \<Longrightarrow> f (\<Union>i. A i) \<le> psuminf (f o A)"
  13.450    by (auto simp add: countably_subadditive_def o_def)
  13.451  
  13.452 -lemma (in algebra) increasing_additive_summable:
  13.453 -  fixes A:: "nat \<Rightarrow> 'a set"
  13.454 -  assumes f: "positive M f" and ad: "additive M f"
  13.455 +lemma (in algebra) increasing_additive_bound:
  13.456 +  fixes A:: "nat \<Rightarrow> 'a set" and  f :: "'a set \<Rightarrow> pinfreal"
  13.457 +  assumes f: "positive f" and ad: "additive M f"
  13.458        and inc: "increasing M f"
  13.459        and A: "range A \<subseteq> sets M"
  13.460        and disj: "disjoint_family A"
  13.461 -  shows  "summable (f o A)"
  13.462 -proof (rule pos_summable) 
  13.463 -  fix n
  13.464 -  show "0 \<le> (f \<circ> A) n" using f A
  13.465 -    by (force simp add: positive_def)
  13.466 -  next
  13.467 -  fix n
  13.468 -  have "setsum (f \<circ> A) {0..<n} = f (\<Union>i\<in>{0..<n}. A i)"
  13.469 -    by (rule additive_sum [OF f ad A disj]) 
  13.470 +  shows  "psuminf (f \<circ> A) \<le> f (space M)"
  13.471 +proof (safe intro!: psuminf_bound)
  13.472 +  fix N
  13.473 +  have "setsum (f \<circ> A) {0..<N} = f (\<Union>i\<in>{0..<N}. A i)"
  13.474 +    by (rule additive_sum [OF f ad A disj])
  13.475    also have "... \<le> f (space M)" using space_closed A
  13.476 -    by (blast intro: increasingD [OF inc] UNION_in_sets top) 
  13.477 -  finally show "setsum (f \<circ> A) {0..<n} \<le> f (space M)" .
  13.478 +    by (blast intro: increasingD [OF inc] UNION_in_sets top)
  13.479 +  finally show "setsum (f \<circ> A) {..<N} \<le> f (space M)" by (simp add: atLeast0LessThan)
  13.480  qed
  13.481  
  13.482 -lemma lambda_system_positive:
  13.483 -     "positive M f \<Longrightarrow> positive (M (|sets := lambda_system M f|)) f"
  13.484 -  by (simp add: positive_def lambda_system_def) 
  13.485 -
  13.486  lemma lambda_system_increasing:
  13.487     "increasing M f \<Longrightarrow> increasing (M (|sets := lambda_system M f|)) f"
  13.488 -  by (simp add: increasing_def lambda_system_def) 
  13.489 +  by (simp add: increasing_def lambda_system_def)
  13.490  
  13.491  lemma (in algebra) lambda_system_strong_sum:
  13.492 -  fixes A:: "nat \<Rightarrow> 'a set"
  13.493 -  assumes f: "positive M f" and a: "a \<in> sets M"
  13.494 +  fixes A:: "nat \<Rightarrow> 'a set" and f :: "'a set \<Rightarrow> pinfreal"
  13.495 +  assumes f: "positive f" and a: "a \<in> sets M"
  13.496        and A: "range A \<subseteq> lambda_system M f"
  13.497        and disj: "disjoint_family A"
  13.498    shows  "(\<Sum>i = 0..<n. f (a \<inter>A i)) = f (a \<inter> (\<Union>i\<in>{0..<n}. A i))"
  13.499  proof (induct n)
  13.500 -  case 0 show ?case using f by (simp add: positive_def) 
  13.501 +  case 0 show ?case using f by (simp add: positive_def)
  13.502  next
  13.503 -  case (Suc n) 
  13.504 +  case (Suc n)
  13.505    have 2: "A n \<inter> UNION {0..<n} A = {}" using disj
  13.506 -    by (force simp add: disjoint_family_on_def neq_iff) 
  13.507 +    by (force simp add: disjoint_family_on_def neq_iff)
  13.508    have 3: "A n \<in> lambda_system M f" using A
  13.509      by blast
  13.510    have 4: "UNION {0..<n} A \<in> lambda_system M f"
  13.511 -    using A algebra.UNION_in_sets [OF local.lambda_system_algebra [OF f]] 
  13.512 +    using A algebra.UNION_in_sets [OF local.lambda_system_algebra, of f, OF f]
  13.513      by simp
  13.514    from Suc.hyps show ?case
  13.515      by (simp add: atLeastLessThanSuc lambda_system_strong_additive [OF a 2 3 4])
  13.516 @@ -458,89 +340,77 @@
  13.517    assumes oms: "outer_measure_space M f"
  13.518        and A: "range A \<subseteq> lambda_system M f"
  13.519        and disj: "disjoint_family A"
  13.520 -  shows  "(\<Union>i. A i) \<in> lambda_system M f & (f \<circ> A)  sums  f (\<Union>i. A i)"
  13.521 +  shows  "(\<Union>i. A i) \<in> lambda_system M f \<and> psuminf (f \<circ> A) = f (\<Union>i. A i)"
  13.522  proof -
  13.523 -  have pos: "positive M f" and inc: "increasing M f" 
  13.524 -   and csa: "countably_subadditive M f" 
  13.525 +  have pos: "positive f" and inc: "increasing M f"
  13.526 +   and csa: "countably_subadditive M f"
  13.527      by (metis oms outer_measure_space_def)+
  13.528    have sa: "subadditive M f"
  13.529 -    by (metis countably_subadditive_subadditive csa pos) 
  13.530 -  have A': "range A \<subseteq> sets (M(|sets := lambda_system M f|))" using A 
  13.531 +    by (metis countably_subadditive_subadditive csa pos)
  13.532 +  have A': "range A \<subseteq> sets (M(|sets := lambda_system M f|))" using A
  13.533      by simp
  13.534    have alg_ls: "algebra (M(|sets := lambda_system M f|))"
  13.535 -    by (rule lambda_system_algebra [OF pos]) 
  13.536 +    by (rule lambda_system_algebra) (rule pos)
  13.537    have A'': "range A \<subseteq> sets M"
  13.538       by (metis A image_subset_iff lambda_system_sets)
  13.539 -  have sumfA: "summable (f \<circ> A)" 
  13.540 -    by (metis algebra.increasing_additive_summable [OF alg_ls]
  13.541 -          lambda_system_positive lambda_system_additive lambda_system_increasing
  13.542 -          A' oms outer_measure_space_def disj)
  13.543 +
  13.544    have U_in: "(\<Union>i. A i) \<in> sets M"
  13.545      by (metis A'' countable_UN)
  13.546 -  have U_eq: "f (\<Union>i. A i) = suminf (f o A)" 
  13.547 +  have U_eq: "f (\<Union>i. A i) = psuminf (f o A)"
  13.548      proof (rule antisym)
  13.549 -      show "f (\<Union>i. A i) \<le> suminf (f \<circ> A)"
  13.550 -        by (rule countably_subadditiveD [OF csa A'' disj U_in sumfA]) 
  13.551 -      show "suminf (f \<circ> A) \<le> f (\<Union>i. A i)"
  13.552 -        by (rule suminf_le [OF sumfA]) 
  13.553 +      show "f (\<Union>i. A i) \<le> psuminf (f \<circ> A)"
  13.554 +        by (rule countably_subadditiveD [OF csa A'' disj U_in])
  13.555 +      show "psuminf (f \<circ> A) \<le> f (\<Union>i. A i)"
  13.556 +        by (rule psuminf_bound, unfold atLeast0LessThan[symmetric])
  13.557             (metis algebra.additive_sum [OF alg_ls] pos disj UN_Un Un_UNIV_right
  13.558 -                  lambda_system_positive lambda_system_additive 
  13.559 -                  subset_Un_eq increasingD [OF inc] A' A'' UNION_in_sets U_in) 
  13.560 +                  lambda_system_additive subset_Un_eq increasingD [OF inc]
  13.561 +                  A' A'' UNION_in_sets U_in)
  13.562      qed
  13.563    {
  13.564 -    fix a 
  13.565 -    assume a [iff]: "a \<in> sets M" 
  13.566 +    fix a
  13.567 +    assume a [iff]: "a \<in> sets M"
  13.568      have "f (a \<inter> (\<Union>i. A i)) + f (a - (\<Union>i. A i)) = f a"
  13.569      proof -
  13.570 -      have summ: "summable (f \<circ> (\<lambda>i. a \<inter> i) \<circ> A)" using pos A'' 
  13.571 -        apply -
  13.572 -        apply (rule summable_comparison_test [OF _ sumfA]) 
  13.573 -        apply (rule_tac x="0" in exI) 
  13.574 -        apply (simp add: positive_def) 
  13.575 -        apply (auto simp add: )
  13.576 -        apply (subst abs_of_nonneg)
  13.577 -        apply (metis A'' Int UNIV_I a image_subset_iff)
  13.578 -        apply (blast intro:  increasingD [OF inc])
  13.579 -        done
  13.580        show ?thesis
  13.581        proof (rule antisym)
  13.582          have "range (\<lambda>i. a \<inter> A i) \<subseteq> sets M" using A''
  13.583            by blast
  13.584 -        moreover 
  13.585 +        moreover
  13.586          have "disjoint_family (\<lambda>i. a \<inter> A i)" using disj
  13.587 -          by (auto simp add: disjoint_family_on_def) 
  13.588 -        moreover 
  13.589 +          by (auto simp add: disjoint_family_on_def)
  13.590 +        moreover
  13.591          have "a \<inter> (\<Union>i. A i) \<in> sets M"
  13.592            by (metis Int U_in a)
  13.593 -        ultimately 
  13.594 -        have "f (a \<inter> (\<Union>i. A i)) \<le> suminf (f \<circ> (\<lambda>i. a \<inter> i) \<circ> A)"
  13.595 -          using countably_subadditiveD [OF csa, of "(\<lambda>i. a \<inter> A i)"] summ
  13.596 -          by (simp add: o_def) 
  13.597 -        moreover 
  13.598 -        have "suminf (f \<circ> (\<lambda>i. a \<inter> i) \<circ> A)  \<le> f a - f (a - (\<Union>i. A i))"
  13.599 -          proof (rule suminf_le [OF summ])
  13.600 +        ultimately
  13.601 +        have "f (a \<inter> (\<Union>i. A i)) \<le> psuminf (f \<circ> (\<lambda>i. a \<inter> i) \<circ> A)"
  13.602 +          using countably_subadditiveD [OF csa, of "(\<lambda>i. a \<inter> A i)"]
  13.603 +          by (simp add: o_def)
  13.604 +        hence "f (a \<inter> (\<Union>i. A i)) + f (a - (\<Union>i. A i)) \<le>
  13.605 +            psuminf (f \<circ> (\<lambda>i. a \<inter> i) \<circ> A) + f (a - (\<Union>i. A i))"
  13.606 +          by (rule add_right_mono)
  13.607 +        moreover
  13.608 +        have "psuminf (f \<circ> (\<lambda>i. a \<inter> i) \<circ> A) + f (a - (\<Union>i. A i)) \<le> f a"
  13.609 +          proof (safe intro!: psuminf_bound_add)
  13.610              fix n
  13.611              have UNION_in: "(\<Union>i\<in>{0..<n}. A i) \<in> sets M"
  13.612 -              by (metis A'' UNION_in_sets) 
  13.613 +              by (metis A'' UNION_in_sets)
  13.614              have le_fa: "f (UNION {0..<n} A \<inter> a) \<le> f a" using A''
  13.615                by (blast intro: increasingD [OF inc] A'' UNION_in_sets)
  13.616              have ls: "(\<Union>i\<in>{0..<n}. A i) \<in> lambda_system M f"
  13.617 -              using algebra.UNION_in_sets [OF lambda_system_algebra [OF pos]]
  13.618 -              by (simp add: A) 
  13.619 -            hence eq_fa: "f (a \<inter> (\<Union>i\<in>{0..<n}. A i)) + f (a - (\<Union>i\<in>{0..<n}. A i)) = f a"
  13.620 +              using algebra.UNION_in_sets [OF lambda_system_algebra [of f, OF pos]]
  13.621 +              by (simp add: A)
  13.622 +            hence eq_fa: "f a = f (a \<inter> (\<Union>i\<in>{0..<n}. A i)) + f (a - (\<Union>i\<in>{0..<n}. A i))"
  13.623                by (simp add: lambda_system_eq UNION_in)
  13.624              have "f (a - (\<Union>i. A i)) \<le> f (a - (\<Union>i\<in>{0..<n}. A i))"
  13.625 -              by (blast intro: increasingD [OF inc] UNION_eq_Union_image 
  13.626 +              by (blast intro: increasingD [OF inc] UNION_eq_Union_image
  13.627                                 UNION_in U_in)
  13.628 -            thus "setsum (f \<circ> (\<lambda>i. a \<inter> i) \<circ> A) {0..<n} \<le> f a - f (a - (\<Union>i. A i))"
  13.629 -              using eq_fa
  13.630 -              by (simp add: suminf_le [OF summ] lambda_system_strong_sum pos 
  13.631 -                            a A disj)
  13.632 +            thus "setsum (f \<circ> (\<lambda>i. a \<inter> i) \<circ> A) {..<n} + f (a - (\<Union>i. A i)) \<le> f a"
  13.633 +              by (simp add: lambda_system_strong_sum pos A disj eq_fa add_left_mono atLeast0LessThan[symmetric])
  13.634            qed
  13.635 -        ultimately show "f (a \<inter> (\<Union>i. A i)) + f (a - (\<Union>i. A i)) \<le> f a" 
  13.636 -          by arith
  13.637 +        ultimately show "f (a \<inter> (\<Union>i. A i)) + f (a - (\<Union>i. A i)) \<le> f a"
  13.638 +          by (rule order_trans)
  13.639        next
  13.640 -        have "f a \<le> f (a \<inter> (\<Union>i. A i) \<union> (a - (\<Union>i. A i)))" 
  13.641 +        have "f a \<le> f (a \<inter> (\<Union>i. A i) \<union> (a - (\<Union>i. A i)))"
  13.642            by (blast intro:  increasingD [OF inc] U_in)
  13.643          also have "... \<le>  f (a \<inter> (\<Union>i. A i)) + f (a - (\<Union>i. A i))"
  13.644            by (blast intro: subadditiveD [OF sa] U_in)
  13.645 @@ -549,68 +419,54 @@
  13.646       qed
  13.647    }
  13.648    thus  ?thesis
  13.649 -    by (simp add: lambda_system_eq sums_iff U_eq U_in sumfA)
  13.650 +    by (simp add: lambda_system_eq sums_iff U_eq U_in)
  13.651  qed
  13.652  
  13.653  lemma (in sigma_algebra) caratheodory_lemma:
  13.654    assumes oms: "outer_measure_space M f"
  13.655 -  shows "measure_space (|space = space M, sets = lambda_system M f, measure = f|)"
  13.656 +  shows "measure_space (|space = space M, sets = lambda_system M f|) f"
  13.657  proof -
  13.658 -  have pos: "positive M f" 
  13.659 +  have pos: "positive f"
  13.660      by (metis oms outer_measure_space_def)
  13.661 -  have alg: "algebra (|space = space M, sets = lambda_system M f, measure = f|)"
  13.662 -    using lambda_system_algebra [OF pos]
  13.663 -    by (simp add: algebra_def) 
  13.664 -  then moreover 
  13.665 -  have "sigma_algebra (|space = space M, sets = lambda_system M f, measure = f|)"
  13.666 +  have alg: "algebra (|space = space M, sets = lambda_system M f|)"
  13.667 +    using lambda_system_algebra [of f, OF pos]
  13.668 +    by (simp add: algebra_def)
  13.669 +  then moreover
  13.670 +  have "sigma_algebra (|space = space M, sets = lambda_system M f|)"
  13.671      using lambda_system_caratheodory [OF oms]
  13.672 -    by (simp add: sigma_algebra_disjoint_iff) 
  13.673 -  moreover 
  13.674 -  have "measure_space_axioms (|space = space M, sets = lambda_system M f, measure = f|)" 
  13.675 +    by (simp add: sigma_algebra_disjoint_iff)
  13.676 +  moreover
  13.677 +  have "measure_space_axioms (|space = space M, sets = lambda_system M f|) f"
  13.678      using pos lambda_system_caratheodory [OF oms]
  13.679 -    by (simp add: measure_space_axioms_def positive_def lambda_system_sets 
  13.680 -                  countably_additive_def o_def) 
  13.681 -  ultimately 
  13.682 +    by (simp add: measure_space_axioms_def positive_def lambda_system_sets
  13.683 +                  countably_additive_def o_def)
  13.684 +  ultimately
  13.685    show ?thesis
  13.686 -    by intro_locales (auto simp add: sigma_algebra_def) 
  13.687 +    by intro_locales (auto simp add: sigma_algebra_def)
  13.688  qed
  13.689  
  13.690  
  13.691  lemma (in algebra) inf_measure_nonempty:
  13.692 -  assumes f: "positive M f" and b: "b \<in> sets M" and a: "a \<subseteq> b"
  13.693 +  assumes f: "positive f" and b: "b \<in> sets M" and a: "a \<subseteq> b"
  13.694    shows "f b \<in> measure_set M f a"
  13.695  proof -
  13.696 -  have "(f \<circ> (\<lambda>i. {})(0 := b)) sums setsum (f \<circ> (\<lambda>i. {})(0 := b)) {0..<1::nat}"
  13.697 -    by (rule series_zero)  (simp add: positive_imp_0 [OF f]) 
  13.698 -  also have "... = f b" 
  13.699 +  have "psuminf (f \<circ> (\<lambda>i. {})(0 := b)) = setsum (f \<circ> (\<lambda>i. {})(0 := b)) {..<1::nat}"
  13.700 +    by (rule psuminf_finite) (simp add: f[unfolded positive_def])
  13.701 +  also have "... = f b"
  13.702      by simp
  13.703 -  finally have "(f \<circ> (\<lambda>i. {})(0 := b)) sums f b" .
  13.704 -  thus ?thesis using a
  13.705 -    by (auto intro!: exI [of _ "(\<lambda>i. {})(0 := b)"] 
  13.706 -             simp add: measure_set_def disjoint_family_on_def b split_if_mem2) 
  13.707 -qed  
  13.708 -
  13.709 -lemma (in algebra) inf_measure_pos0:
  13.710 -     "positive M f \<Longrightarrow> x \<in> measure_set M f a \<Longrightarrow> 0 \<le> x"
  13.711 -apply (auto simp add: positive_def measure_set_def sums_iff intro!: suminf_ge_zero)
  13.712 -apply blast
  13.713 -done
  13.714 -
  13.715 -lemma (in algebra) inf_measure_pos:
  13.716 -  shows "positive M f \<Longrightarrow> x \<subseteq> space M \<Longrightarrow> 0 \<le> Inf (measure_set M f x)"
  13.717 -apply (rule Inf_greatest)
  13.718 -apply (metis emptyE inf_measure_nonempty top)
  13.719 -apply (metis inf_measure_pos0) 
  13.720 -done
  13.721 +  finally have "psuminf (f \<circ> (\<lambda>i. {})(0 := b)) = f b" .
  13.722 +  thus ?thesis using a b
  13.723 +    by (auto intro!: exI [of _ "(\<lambda>i. {})(0 := b)"]
  13.724 +             simp: measure_set_def disjoint_family_on_def split_if_mem2 comp_def)
  13.725 +qed
  13.726  
  13.727  lemma (in algebra) additive_increasing:
  13.728 -  assumes posf: "positive M f" and addf: "additive M f" 
  13.729 +  assumes posf: "positive f" and addf: "additive M f"
  13.730    shows "increasing M f"
  13.731 -proof (auto simp add: increasing_def) 
  13.732 +proof (auto simp add: increasing_def)
  13.733    fix x y
  13.734    assume xy: "x \<in> sets M" "y \<in> sets M" "x \<subseteq> y"
  13.735 -  have "f x \<le> f x + f (y-x)" using posf
  13.736 -    by (simp add: positive_def) (metis Diff xy(1,2))
  13.737 +  have "f x \<le> f x + f (y-x)" ..
  13.738    also have "... = f (x \<union> (y-x))" using addf
  13.739      by (auto simp add: additive_def) (metis Diff_disjoint Un_Diff_cancel Diff xy(1,2))
  13.740    also have "... = f y"
  13.741 @@ -619,42 +475,42 @@
  13.742  qed
  13.743  
  13.744  lemma (in algebra) countably_additive_additive:
  13.745 -  assumes posf: "positive M f" and ca: "countably_additive M f" 
  13.746 +  assumes posf: "positive f" and ca: "countably_additive M f"
  13.747    shows "additive M f"
  13.748 -proof (auto simp add: additive_def) 
  13.749 +proof (auto simp add: additive_def)
  13.750    fix x y
  13.751    assume x: "x \<in> sets M" and y: "y \<in> sets M" and "x \<inter> y = {}"
  13.752    hence "disjoint_family (binaryset x y)"
  13.753 -    by (auto simp add: disjoint_family_on_def binaryset_def) 
  13.754 -  hence "range (binaryset x y) \<subseteq> sets M \<longrightarrow> 
  13.755 -         (\<Union>i. binaryset x y i) \<in> sets M \<longrightarrow> 
  13.756 -         f (\<Union>i. binaryset x y i) = suminf (\<lambda>n. f (binaryset x y n))"
  13.757 +    by (auto simp add: disjoint_family_on_def binaryset_def)
  13.758 +  hence "range (binaryset x y) \<subseteq> sets M \<longrightarrow>
  13.759 +         (\<Union>i. binaryset x y i) \<in> sets M \<longrightarrow>
  13.760 +         f (\<Union>i. binaryset x y i) = (\<Sum>\<^isub>\<infinity> n. f (binaryset x y n))"
  13.761      using ca
  13.762 -    by (simp add: countably_additive_def) (metis UN_binaryset_eq sums_unique) 
  13.763 -  hence "{x,y,{}} \<subseteq> sets M \<longrightarrow> x \<union> y \<in> sets M \<longrightarrow> 
  13.764 -         f (x \<union> y) = suminf (\<lambda>n. f (binaryset x y n))"
  13.765 +    by (simp add: countably_additive_def)
  13.766 +  hence "{x,y,{}} \<subseteq> sets M \<longrightarrow> x \<union> y \<in> sets M \<longrightarrow>
  13.767 +         f (x \<union> y) = (\<Sum>\<^isub>\<infinity> n. f (binaryset x y n))"
  13.768      by (simp add: range_binaryset_eq UN_binaryset_eq)
  13.769    thus "f (x \<union> y) = f x + f y" using posf x y
  13.770 -    by (simp add: Un suminf_binaryset_eq positive_def)
  13.771 -qed 
  13.772 - 
  13.773 +    by (auto simp add: Un binaryset_psuminf positive_def)
  13.774 +qed
  13.775 +
  13.776  lemma (in algebra) inf_measure_agrees:
  13.777 -  assumes posf: "positive M f" and ca: "countably_additive M f" 
  13.778 -      and s: "s \<in> sets M"  
  13.779 +  assumes posf: "positive f" and ca: "countably_additive M f"
  13.780 +      and s: "s \<in> sets M"
  13.781    shows "Inf (measure_set M f s) = f s"
  13.782 -proof (rule Inf_eq) 
  13.783 +  unfolding Inf_pinfreal_def
  13.784 +proof (safe intro!: Greatest_equality)
  13.785    fix z
  13.786    assume z: "z \<in> measure_set M f s"
  13.787 -  from this obtain A where 
  13.788 +  from this obtain A where
  13.789      A: "range A \<subseteq> sets M" and disj: "disjoint_family A"
  13.790 -    and "s \<subseteq> (\<Union>x. A x)" and sm: "summable (f \<circ> A)"
  13.791 -    and si: "suminf (f \<circ> A) = z"
  13.792 -    by (auto simp add: measure_set_def sums_iff) 
  13.793 +    and "s \<subseteq> (\<Union>x. A x)" and si: "psuminf (f \<circ> A) = z"
  13.794 +    by (auto simp add: measure_set_def comp_def)
  13.795    hence seq: "s = (\<Union>i. A i \<inter> s)" by blast
  13.796    have inc: "increasing M f"
  13.797      by (metis additive_increasing ca countably_additive_additive posf)
  13.798 -  have sums: "(\<lambda>i. f (A i \<inter> s)) sums f (\<Union>i. A i \<inter> s)"
  13.799 -    proof (rule countably_additiveD [OF ca]) 
  13.800 +  have sums: "psuminf (\<lambda>i. f (A i \<inter> s)) = f (\<Union>i. A i \<inter> s)"
  13.801 +    proof (rule countably_additiveD [OF ca])
  13.802        show "range (\<lambda>n. A n \<inter> s) \<subseteq> sets M" using A s
  13.803          by blast
  13.804        show "disjoint_family (\<lambda>n. A n \<inter> s)" using disj
  13.805 @@ -662,228 +518,184 @@
  13.806        show "(\<Union>i. A i \<inter> s) \<in> sets M" using A s
  13.807          by (metis UN_extend_simps(4) s seq)
  13.808      qed
  13.809 -  hence "f s = suminf (\<lambda>i. f (A i \<inter> s))"
  13.810 +  hence "f s = psuminf (\<lambda>i. f (A i \<inter> s))"
  13.811      using seq [symmetric] by (simp add: sums_iff)
  13.812 -  also have "... \<le> suminf (f \<circ> A)" 
  13.813 -    proof (rule summable_le [OF _ _ sm]) 
  13.814 -      show "\<forall>n. f (A n \<inter> s) \<le> (f \<circ> A) n" using A s
  13.815 -        by (force intro: increasingD [OF inc]) 
  13.816 -      show "summable (\<lambda>i. f (A i \<inter> s))" using sums
  13.817 -        by (simp add: sums_iff) 
  13.818 +  also have "... \<le> psuminf (f \<circ> A)"
  13.819 +    proof (rule psuminf_le)
  13.820 +      fix n show "f (A n \<inter> s) \<le> (f \<circ> A) n" using A s
  13.821 +        by (force intro: increasingD [OF inc])
  13.822      qed
  13.823 -  also have "... = z" by (rule si) 
  13.824 +  also have "... = z" by (rule si)
  13.825    finally show "f s \<le> z" .
  13.826  next
  13.827    fix y
  13.828 -  assume y: "!!u. u \<in> measure_set M f s \<Longrightarrow> y \<le> u"
  13.829 +  assume y: "\<forall>u \<in> measure_set M f s. y \<le> u"
  13.830    thus "y \<le> f s"
  13.831 -    by (blast intro: inf_measure_nonempty [OF posf s subset_refl])
  13.832 +    by (blast intro: inf_measure_nonempty [of f, OF posf s subset_refl])
  13.833  qed
  13.834  
  13.835  lemma (in algebra) inf_measure_empty:
  13.836 -  assumes posf: "positive M f"
  13.837 +  assumes posf: "positive f"
  13.838    shows "Inf (measure_set M f {}) = 0"
  13.839  proof (rule antisym)
  13.840 -  show "0 \<le> Inf (measure_set M f {})"
  13.841 -    by (metis empty_subsetI inf_measure_pos posf) 
  13.842    show "Inf (measure_set M f {}) \<le> 0"
  13.843 -    by (metis Inf_lower empty_sets inf_measure_pos0 inf_measure_nonempty posf
  13.844 -              positive_imp_0 subset_refl) 
  13.845 -qed
  13.846 +    by (metis complete_lattice_class.Inf_lower empty_sets inf_measure_nonempty[OF posf] subset_refl posf[unfolded positive_def])
  13.847 +qed simp
  13.848  
  13.849  lemma (in algebra) inf_measure_positive:
  13.850 -  "positive M f \<Longrightarrow> 
  13.851 -   positive (| space = space M, sets = Pow (space M) |)
  13.852 -                  (\<lambda>x. Inf (measure_set M f x))"
  13.853 -  by (simp add: positive_def inf_measure_empty inf_measure_pos) 
  13.854 +  "positive f \<Longrightarrow>
  13.855 +   positive (\<lambda>x. Inf (measure_set M f x))"
  13.856 +  by (simp add: positive_def inf_measure_empty) 
  13.857  
  13.858  lemma (in algebra) inf_measure_increasing:
  13.859 -  assumes posf: "positive M f"
  13.860 +  assumes posf: "positive f"
  13.861    shows "increasing (| space = space M, sets = Pow (space M) |)
  13.862                      (\<lambda>x. Inf (measure_set M f x))"
  13.863 -apply (auto simp add: increasing_def) 
  13.864 -apply (rule Inf_greatest, metis emptyE inf_measure_nonempty top posf)
  13.865 -apply (rule Inf_lower) 
  13.866 +apply (auto simp add: increasing_def)
  13.867 +apply (rule complete_lattice_class.Inf_greatest)
  13.868 +apply (rule complete_lattice_class.Inf_lower)
  13.869  apply (clarsimp simp add: measure_set_def, rule_tac x=A in exI, blast)
  13.870 -apply (blast intro: inf_measure_pos0 posf)
  13.871  done
  13.872  
  13.873  
  13.874  lemma (in algebra) inf_measure_le:
  13.875 -  assumes posf: "positive M f" and inc: "increasing M f" 
  13.876 -      and x: "x \<in> {r . \<exists>A. range A \<subseteq> sets M & s \<subseteq> (\<Union>i. A i) & (f \<circ> A) sums r}"
  13.877 +  assumes posf: "positive f" and inc: "increasing M f"
  13.878 +      and x: "x \<in> {r . \<exists>A. range A \<subseteq> sets M \<and> s \<subseteq> (\<Union>i. A i) \<and> psuminf (f \<circ> A) = r}"
  13.879    shows "Inf (measure_set M f s) \<le> x"
  13.880  proof -
  13.881    from x
  13.882 -  obtain A where A: "range A \<subseteq> sets M" and ss: "s \<subseteq> (\<Union>i. A i)" 
  13.883 -             and sm: "summable (f \<circ> A)" and xeq: "suminf (f \<circ> A) = x"
  13.884 -    by (auto simp add: sums_iff)
  13.885 +  obtain A where A: "range A \<subseteq> sets M" and ss: "s \<subseteq> (\<Union>i. A i)"
  13.886 +             and xeq: "psuminf (f \<circ> A) = x"
  13.887 +    by auto
  13.888    have dA: "range (disjointed A) \<subseteq> sets M"
  13.889      by (metis A range_disjointed_sets)
  13.890 -  have "\<forall>n. \<bar>(f o disjointed A) n\<bar> \<le> (f \<circ> A) n"
  13.891 -    proof (auto)
  13.892 -      fix n
  13.893 -      have "\<bar>f (disjointed A n)\<bar> = f (disjointed A n)" using posf dA
  13.894 -        by (auto simp add: positive_def image_subset_iff)
  13.895 -      also have "... \<le> f (A n)" 
  13.896 -        by (metis increasingD [OF inc] UNIV_I dA image_subset_iff disjointed_subset A)
  13.897 -      finally show "\<bar>f (disjointed A n)\<bar> \<le> f (A n)" .
  13.898 -    qed
  13.899 -  from Series.summable_le2 [OF this sm]
  13.900 -  have sda:  "summable (f o disjointed A)"  
  13.901 -             "suminf (f o disjointed A) \<le> suminf (f \<circ> A)"
  13.902 -    by blast+
  13.903 -  hence ley: "suminf (f o disjointed A) \<le> x"
  13.904 -    by (metis xeq) 
  13.905 -  from sda have "(f \<circ> disjointed A) sums suminf (f \<circ> disjointed A)"
  13.906 -    by (simp add: sums_iff) 
  13.907 -  hence y: "suminf (f o disjointed A) \<in> measure_set M f s"
  13.908 +  have "\<forall>n.(f o disjointed A) n \<le> (f \<circ> A) n" unfolding comp_def
  13.909 +    by (metis increasingD [OF inc] UNIV_I dA image_subset_iff disjointed_subset A comp_def)
  13.910 +  hence sda: "psuminf (f o disjointed A) \<le> psuminf (f \<circ> A)"
  13.911 +    by (blast intro: psuminf_le)
  13.912 +  hence ley: "psuminf (f o disjointed A) \<le> x"
  13.913 +    by (metis xeq)
  13.914 +  hence y: "psuminf (f o disjointed A) \<in> measure_set M f s"
  13.915      apply (auto simp add: measure_set_def)
  13.916 -    apply (rule_tac x="disjointed A" in exI) 
  13.917 -    apply (simp add: disjoint_family_disjointed UN_disjointed_eq ss dA)
  13.918 +    apply (rule_tac x="disjointed A" in exI)
  13.919 +    apply (simp add: disjoint_family_disjointed UN_disjointed_eq ss dA comp_def)
  13.920      done
  13.921    show ?thesis
  13.922 -    by (blast intro: y order_trans [OF _ ley] inf_measure_pos0 posf)
  13.923 +    by (blast intro: y order_trans [OF _ ley] posf complete_lattice_class.Inf_lower)
  13.924  qed
  13.925  
  13.926  lemma (in algebra) inf_measure_close:
  13.927 -  assumes posf: "positive M f" and e: "0 < e" and ss: "s \<subseteq> (space M)"
  13.928 -  shows "\<exists>A l. range A \<subseteq> sets M & disjoint_family A & s \<subseteq> (\<Union>i. A i) & 
  13.929 -               (f \<circ> A) sums l & l \<le> Inf (measure_set M f s) + e"
  13.930 -proof -
  13.931 -  have " measure_set M f s \<noteq> {}" 
  13.932 -    by (metis emptyE ss inf_measure_nonempty [OF posf top])
  13.933 -  hence "\<exists>l \<in> measure_set M f s. l < Inf (measure_set M f s) + e" 
  13.934 -    by (rule Inf_close [OF _ e])
  13.935 -  thus ?thesis 
  13.936 -    by (auto simp add: measure_set_def, rule_tac x=" A" in exI, auto)
  13.937 +  assumes posf: "positive f" and e: "0 < e" and ss: "s \<subseteq> (space M)"
  13.938 +  shows "\<exists>A. range A \<subseteq> sets M \<and> disjoint_family A \<and> s \<subseteq> (\<Union>i. A i) \<and>
  13.939 +               psuminf (f \<circ> A) \<le> Inf (measure_set M f s) + e"
  13.940 +proof (cases "Inf (measure_set M f s) = \<omega>")
  13.941 +  case False
  13.942 +  obtain l where "l \<in> measure_set M f s" "l \<le> Inf (measure_set M f s) + e"
  13.943 +    using Inf_close[OF False e] by auto
  13.944 +  thus ?thesis
  13.945 +    by (auto intro!: exI[of _ l] simp: measure_set_def comp_def)
  13.946 +next
  13.947 +  case True
  13.948 +  have "measure_set M f s \<noteq> {}"
  13.949 +    by (metis emptyE ss inf_measure_nonempty [of f, OF posf top])
  13.950 +  then obtain l where "l \<in> measure_set M f s" by auto
  13.951 +  moreover from True have "l \<le> Inf (measure_set M f s) + e" by simp
  13.952 +  ultimately show ?thesis
  13.953 +    by (auto intro!: exI[of _ l] simp: measure_set_def comp_def)
  13.954  qed
  13.955  
  13.956  lemma (in algebra) inf_measure_countably_subadditive:
  13.957 -  assumes posf: "positive M f" and inc: "increasing M f" 
  13.958 +  assumes posf: "positive f" and inc: "increasing M f"
  13.959    shows "countably_subadditive (| space = space M, sets = Pow (space M) |)
  13.960                    (\<lambda>x. Inf (measure_set M f x))"
  13.961 -proof (auto simp add: countably_subadditive_def o_def, rule field_le_epsilon)
  13.962 -  fix A :: "nat \<Rightarrow> 'a set" and e :: real
  13.963 -    assume A: "range A \<subseteq> Pow (space M)"
  13.964 -       and disj: "disjoint_family A"
  13.965 -       and sb: "(\<Union>i. A i) \<subseteq> space M"
  13.966 -       and sum1: "summable (\<lambda>n. Inf (measure_set M f (A n)))"
  13.967 -       and e: "0 < e"
  13.968 -    have "!!n. \<exists>B l. range B \<subseteq> sets M \<and> disjoint_family B \<and> A n \<subseteq> (\<Union>i. B i) \<and>
  13.969 -                    (f o B) sums l \<and>
  13.970 -                    l \<le> Inf (measure_set M f (A n)) + e * (1/2)^(Suc n)"
  13.971 -      apply (rule inf_measure_close [OF posf])
  13.972 -      apply (metis e half mult_pos_pos zero_less_power) 
  13.973 -      apply (metis UNIV_I UN_subset_iff sb)
  13.974 -      done
  13.975 -    hence "\<exists>BB ll. \<forall>n. range (BB n) \<subseteq> sets M \<and> disjoint_family (BB n) \<and>
  13.976 -                       A n \<subseteq> (\<Union>i. BB n i) \<and> (f o BB n) sums ll n \<and>
  13.977 -                       ll n \<le> Inf (measure_set M f (A n)) + e * (1/2)^(Suc n)"
  13.978 -      by (rule choice2)
  13.979 -    then obtain BB ll
  13.980 -      where BB: "!!n. (range (BB n) \<subseteq> sets M)"
  13.981 -        and disjBB: "!!n. disjoint_family (BB n)" 
  13.982 -        and sbBB: "!!n. A n \<subseteq> (\<Union>i. BB n i)"
  13.983 -        and BBsums: "!!n. (f o BB n) sums ll n"
  13.984 -        and ll: "!!n. ll n \<le> Inf (measure_set M f (A n)) + e * (1/2)^(Suc n)"
  13.985 -      by auto blast
  13.986 -    have llpos: "!!n. 0 \<le> ll n"
  13.987 -        by (metis BBsums sums_iff o_apply posf positive_imp_pos suminf_ge_zero 
  13.988 -              range_subsetD BB) 
  13.989 -    have sll: "summable ll &
  13.990 -               suminf ll \<le> suminf (\<lambda>n. Inf (measure_set M f (A n))) + e"
  13.991 -      proof -
  13.992 -        have "(\<lambda>n. e * (1/2)^(Suc n)) sums (e*1)"
  13.993 -          by (rule sums_mult [OF power_half_series]) 
  13.994 -        hence sum0: "summable (\<lambda>n. e * (1 / 2) ^ Suc n)"
  13.995 -          and eqe:  "(\<Sum>n. e * (1 / 2) ^ n / 2) = e"
  13.996 -          by (auto simp add: sums_iff) 
  13.997 -        have 0: "suminf (\<lambda>n. Inf (measure_set M f (A n))) +
  13.998 -                 suminf (\<lambda>n. e * (1/2)^(Suc n)) =
  13.999 -                 suminf (\<lambda>n. Inf (measure_set M f (A n)) + e * (1/2)^(Suc n))"
 13.1000 -          by (rule suminf_add [OF sum1 sum0]) 
 13.1001 -        have 1: "\<forall>n. \<bar>ll n\<bar> \<le> Inf (measure_set M f (A n)) + e * (1/2) ^ Suc n"
 13.1002 -          by (metis ll llpos abs_of_nonneg)
 13.1003 -        have 2: "summable (\<lambda>n. Inf (measure_set M f (A n)) + e*(1/2)^(Suc n))"
 13.1004 -          by (rule summable_add [OF sum1 sum0]) 
 13.1005 -        have "suminf ll \<le> (\<Sum>n. Inf (measure_set M f (A n)) + e*(1/2) ^ Suc n)"
 13.1006 -          using Series.summable_le2 [OF 1 2] by auto
 13.1007 -        also have "... = (\<Sum>n. Inf (measure_set M f (A n))) + 
 13.1008 -                         (\<Sum>n. e * (1 / 2) ^ Suc n)"
 13.1009 -          by (metis 0) 
 13.1010 -        also have "... = (\<Sum>n. Inf (measure_set M f (A n))) + e"
 13.1011 -          by (simp add: eqe) 
 13.1012 -        finally show ?thesis using  Series.summable_le2 [OF 1 2] by auto
 13.1013 -      qed
 13.1014 -    def C \<equiv> "(split BB) o prod_decode"
 13.1015 -    have C: "!!n. C n \<in> sets M"
 13.1016 -      apply (rule_tac p="prod_decode n" in PairE)
 13.1017 -      apply (simp add: C_def)
 13.1018 -      apply (metis BB subsetD rangeI)  
 13.1019 -      done
 13.1020 -    have sbC: "(\<Union>i. A i) \<subseteq> (\<Union>i. C i)"
 13.1021 -      proof (auto simp add: C_def)
 13.1022 -        fix x i
 13.1023 -        assume x: "x \<in> A i"
 13.1024 -        with sbBB [of i] obtain j where "x \<in> BB i j"
 13.1025 -          by blast        
 13.1026 -        thus "\<exists>i. x \<in> split BB (prod_decode i)"
 13.1027 -          by (metis prod_encode_inverse prod.cases)
 13.1028 -      qed 
 13.1029 -    have "(f \<circ> C) = (f \<circ> (\<lambda>(x, y). BB x y)) \<circ> prod_decode"
 13.1030 -      by (rule ext)  (auto simp add: C_def) 
 13.1031 -    also have "... sums suminf ll" 
 13.1032 -      proof (rule suminf_2dimen)
 13.1033 -        show "\<And>m n. 0 \<le> (f \<circ> (\<lambda>(x, y). BB x y)) (m, n)" using posf BB 
 13.1034 -          by (force simp add: positive_def)
 13.1035 -        show "\<And>m. (\<lambda>n. (f \<circ> (\<lambda>(x, y). BB x y)) (m, n)) sums ll m"using BBsums BB
 13.1036 -          by (force simp add: o_def)
 13.1037 -        show "summable ll" using sll
 13.1038 -          by auto
 13.1039 -      qed
 13.1040 -    finally have Csums: "(f \<circ> C) sums suminf ll" .
 13.1041 -    have "Inf (measure_set M f (\<Union>i. A i)) \<le> suminf ll"
 13.1042 -      apply (rule inf_measure_le [OF posf inc], auto)
 13.1043 -      apply (rule_tac x="C" in exI)
 13.1044 -      apply (auto simp add: C sbC Csums) 
 13.1045 -      done
 13.1046 -    also have "... \<le> (\<Sum>n. Inf (measure_set M f (A n))) + e" using sll
 13.1047 -      by blast
 13.1048 -    finally show "Inf (measure_set M f (\<Union>i. A i)) \<le> 
 13.1049 -          (\<Sum>n. Inf (measure_set M f (A n))) + e" .
 13.1050 +  unfolding countably_subadditive_def o_def
 13.1051 +proof (safe, simp, rule pinfreal_le_epsilon)
 13.1052 +  fix A :: "nat \<Rightarrow> 'a set" and e :: pinfreal
 13.1053 +
 13.1054 +  let "?outer n" = "Inf (measure_set M f (A n))"
 13.1055 +  assume A: "range A \<subseteq> Pow (space M)"
 13.1056 +     and disj: "disjoint_family A"
 13.1057 +     and sb: "(\<Union>i. A i) \<subseteq> space M"
 13.1058 +     and e: "0 < e"
 13.1059 +  hence "\<exists>BB. \<forall>n. range (BB n) \<subseteq> sets M \<and> disjoint_family (BB n) \<and>
 13.1060 +                   A n \<subseteq> (\<Union>i. BB n i) \<and>
 13.1061 +                   psuminf (f o BB n) \<le> ?outer n + e * (1/2)^(Suc n)"
 13.1062 +    apply (safe intro!: choice inf_measure_close [of f, OF posf _])
 13.1063 +    using e sb by (cases e, auto simp add: not_le mult_pos_pos)
 13.1064 +  then obtain BB
 13.1065 +    where BB: "\<And>n. (range (BB n) \<subseteq> sets M)"
 13.1066 +      and disjBB: "\<And>n. disjoint_family (BB n)"
 13.1067 +      and sbBB: "\<And>n. A n \<subseteq> (\<Union>i. BB n i)"
 13.1068 +      and BBle: "\<And>n. psuminf (f o BB n) \<le> ?outer n + e * (1/2)^(Suc n)"
 13.1069 +    by auto blast
 13.1070 +  have sll: "(\<Sum>\<^isub>\<infinity> n. psuminf (f o BB n)) \<le> psuminf ?outer + e"
 13.1071 +    proof -
 13.1072 +      have "(\<Sum>\<^isub>\<infinity> n. psuminf (f o BB n)) \<le> (\<Sum>\<^isub>\<infinity> n. ?outer n + e*(1/2) ^ Suc n)"
 13.1073 +        by (rule psuminf_le[OF BBle])
 13.1074 +      also have "... = psuminf ?outer + e"
 13.1075 +        using psuminf_half_series by simp
 13.1076 +      finally show ?thesis .
 13.1077 +    qed
 13.1078 +  def C \<equiv> "(split BB) o prod_decode"
 13.1079 +  have C: "!!n. C n \<in> sets M"
 13.1080 +    apply (rule_tac p="prod_decode n" in PairE)
 13.1081 +    apply (simp add: C_def)
 13.1082 +    apply (metis BB subsetD rangeI)
 13.1083 +    done
 13.1084 +  have sbC: "(\<Union>i. A i) \<subseteq> (\<Union>i. C i)"
 13.1085 +    proof (auto simp add: C_def)
 13.1086 +      fix x i
 13.1087 +      assume x: "x \<in> A i"
 13.1088 +      with sbBB [of i] obtain j where "x \<in> BB i j"
 13.1089 +        by blast
 13.1090 +      thus "\<exists>i. x \<in> split BB (prod_decode i)"
 13.1091 +        by (metis prod_encode_inverse prod.cases)
 13.1092 +    qed
 13.1093 +  have "(f \<circ> C) = (f \<circ> (\<lambda>(x, y). BB x y)) \<circ> prod_decode"
 13.1094 +    by (rule ext)  (auto simp add: C_def)
 13.1095 +  moreover have "psuminf ... = (\<Sum>\<^isub>\<infinity> n. psuminf (f o BB n))" using BBle
 13.1096 +    by (force intro!: psuminf_2dimen simp: o_def)
 13.1097 +  ultimately have Csums: "psuminf (f \<circ> C) = (\<Sum>\<^isub>\<infinity> n. psuminf (f o BB n))" by simp
 13.1098 +  have "Inf (measure_set M f (\<Union>i. A i)) \<le> (\<Sum>\<^isub>\<infinity> n. psuminf (f o BB n))"
 13.1099 +    apply (rule inf_measure_le [OF posf(1) inc], auto)
 13.1100 +    apply (rule_tac x="C" in exI)
 13.1101 +    apply (auto simp add: C sbC Csums)
 13.1102 +    done
 13.1103 +  also have "... \<le> (\<Sum>\<^isub>\<infinity>n. Inf (measure_set M f (A n))) + e" using sll
 13.1104 +    by blast
 13.1105 +  finally show "Inf (measure_set M f (\<Union>i. A i)) \<le> psuminf ?outer + e" .
 13.1106  qed
 13.1107  
 13.1108  lemma (in algebra) inf_measure_outer:
 13.1109 -  "positive M f \<Longrightarrow> increasing M f 
 13.1110 +  "\<lbrakk> positive f ; increasing M f \<rbrakk>
 13.1111     \<Longrightarrow> outer_measure_space (| space = space M, sets = Pow (space M) |)
 13.1112                            (\<lambda>x. Inf (measure_set M f x))"
 13.1113 -  by (simp add: outer_measure_space_def inf_measure_positive
 13.1114 -                inf_measure_increasing inf_measure_countably_subadditive) 
 13.1115 +  by (simp add: outer_measure_space_def inf_measure_empty
 13.1116 +                inf_measure_increasing inf_measure_countably_subadditive positive_def)
 13.1117  
 13.1118  (*MOVE UP*)
 13.1119  
 13.1120  lemma (in algebra) algebra_subset_lambda_system:
 13.1121 -  assumes posf: "positive M f" and inc: "increasing M f" 
 13.1122 +  assumes posf: "positive f" and inc: "increasing M f"
 13.1123        and add: "additive M f"
 13.1124    shows "sets M \<subseteq> lambda_system (| space = space M, sets = Pow (space M) |)
 13.1125                                  (\<lambda>x. Inf (measure_set M f x))"
 13.1126 -proof (auto dest: sets_into_space 
 13.1127 -            simp add: algebra.lambda_system_eq [OF algebra_Pow]) 
 13.1128 +proof (auto dest: sets_into_space
 13.1129 +            simp add: algebra.lambda_system_eq [OF algebra_Pow])
 13.1130    fix x s
 13.1131    assume x: "x \<in> sets M"
 13.1132       and s: "s \<subseteq> space M"
 13.1133 -  have [simp]: "!!x. x \<in> sets M \<Longrightarrow> s \<inter> (space M - x) = s-x" using s 
 13.1134 +  have [simp]: "!!x. x \<in> sets M \<Longrightarrow> s \<inter> (space M - x) = s-x" using s
 13.1135      by blast
 13.1136    have "Inf (measure_set M f (s\<inter>x)) + Inf (measure_set M f (s-x))
 13.1137          \<le> Inf (measure_set M f s)"
 13.1138 -    proof (rule field_le_epsilon) 
 13.1139 -      fix e :: real
 13.1140 +    proof (rule pinfreal_le_epsilon)
 13.1141 +      fix e :: pinfreal
 13.1142        assume e: "0 < e"
 13.1143 -      from inf_measure_close [OF posf e s]
 13.1144 -      obtain A l where A: "range A \<subseteq> sets M" and disj: "disjoint_family A"
 13.1145 -                   and sUN: "s \<subseteq> (\<Union>i. A i)" and fsums: "(f \<circ> A) sums l"
 13.1146 -                   and l: "l \<le> Inf (measure_set M f s) + e"
 13.1147 +      from inf_measure_close [of f, OF posf e s]
 13.1148 +      obtain A where A: "range A \<subseteq> sets M" and disj: "disjoint_family A"
 13.1149 +                 and sUN: "s \<subseteq> (\<Union>i. A i)"
 13.1150 +                 and l: "psuminf (f \<circ> A) \<le> Inf (measure_set M f s) + e"
 13.1151          by auto
 13.1152        have [simp]: "!!x. x \<in> sets M \<Longrightarrow>
 13.1153                        (f o (\<lambda>z. z \<inter> (space M - x)) o A) = (f o (\<lambda>z. z - x) o A)"
 13.1154 @@ -891,104 +703,87 @@
 13.1155        have  [simp]: "!!n. f (A n \<inter> x) + f (A n - x) = f (A n)"
 13.1156          by (subst additiveD [OF add, symmetric])
 13.1157             (auto simp add: x range_subsetD [OF A] Int_Diff_Un Int_Diff_disjoint)
 13.1158 -      have fsumb: "summable (f \<circ> A)"
 13.1159 -        by (metis fsums sums_iff) 
 13.1160        { fix u
 13.1161          assume u: "u \<in> sets M"
 13.1162 -        have [simp]: "\<And>n. \<bar>f (A n \<inter> u)\<bar> \<le> f (A n)"
 13.1163 -          by (simp add: positive_imp_pos [OF posf]  increasingD [OF inc] 
 13.1164 -                        u Int  range_subsetD [OF A]) 
 13.1165 -        have 1: "summable (f o (\<lambda>z. z\<inter>u) o A)" 
 13.1166 -          by (rule summable_comparison_test [OF _ fsumb]) simp
 13.1167 -        have 2: "Inf (measure_set M f (s\<inter>u)) \<le> suminf (f o (\<lambda>z. z\<inter>u) o A)"
 13.1168 -          proof (rule Inf_lower) 
 13.1169 -            show "suminf (f \<circ> (\<lambda>z. z \<inter> u) \<circ> A) \<in> measure_set M f (s \<inter> u)"
 13.1170 -              apply (simp add: measu