Rename extreal => ereal
authorhoelzl
Tue Jul 19 14:36:12 2011 +0200 (2011-07-19)
changeset 43920cedb5cb948fd
parent 43919 a7e4fb1a0502
child 43921 e8511be08ddd
Rename extreal => ereal
src/HOL/IsaMakefile
src/HOL/Library/Extended_Real.thy
src/HOL/Library/Extended_Reals.thy
src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy
src/HOL/Probability/Binary_Product_Measure.thy
src/HOL/Probability/Borel_Space.thy
src/HOL/Probability/Caratheodory.thy
src/HOL/Probability/Complete_Measure.thy
src/HOL/Probability/Conditional_Probability.thy
src/HOL/Probability/Finite_Product_Measure.thy
src/HOL/Probability/Independent_Family.thy
src/HOL/Probability/Infinite_Product_Measure.thy
src/HOL/Probability/Information.thy
src/HOL/Probability/Lebesgue_Integration.thy
src/HOL/Probability/Lebesgue_Measure.thy
src/HOL/Probability/Measure.thy
src/HOL/Probability/Probability_Measure.thy
src/HOL/Probability/Radon_Nikodym.thy
src/HOL/Probability/ex/Dining_Cryptographers.thy
src/HOL/Probability/ex/Koepf_Duermuth_Countermeasure.thy
     1.1 --- a/src/HOL/IsaMakefile	Tue Jul 19 14:35:44 2011 +0200
     1.2 +++ b/src/HOL/IsaMakefile	Tue Jul 19 14:36:12 2011 +0200
     1.3 @@ -450,7 +450,7 @@
     1.4    Library/Continuity.thy Library/Convex.thy Library/Countable.thy	\
     1.5    Library/Diagonalize.thy Library/Dlist.thy Library/Dlist_Cset.thy 	\
     1.6    Library/Efficient_Nat.thy Library/Eval_Witness.thy 			\
     1.7 -  Library/Executable_Set.thy Library/Extended_Reals.thy			\
     1.8 +  Library/Executable_Set.thy Library/Extended_Real.thy			\
     1.9    Library/Extended_Nat.thy Library/Float.thy				\
    1.10    Library/Formal_Power_Series.thy Library/Fraction_Field.thy		\
    1.11    Library/FrechetDeriv.thy Library/Cset.thy Library/FuncSet.thy		\
    1.12 @@ -1203,7 +1203,7 @@
    1.13    Multivariate_Analysis/Topology_Euclidean_Space.thy			\
    1.14    Multivariate_Analysis/document/root.tex				\
    1.15    Multivariate_Analysis/normarith.ML Library/Glbs.thy			\
    1.16 -  Library/Extended_Reals.thy Library/Indicator_Function.thy		\
    1.17 +  Library/Extended_Real.thy Library/Indicator_Function.thy		\
    1.18    Library/Inner_Product.thy Library/Numeral_Type.thy Library/Convex.thy	\
    1.19    Library/FrechetDeriv.thy Library/Product_Vector.thy			\
    1.20    Library/Product_plus.thy
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/HOL/Library/Extended_Real.thy	Tue Jul 19 14:36:12 2011 +0200
     2.3 @@ -0,0 +1,2535 @@
     2.4 +(*  Title:      HOL/Library/Extended_Real.thy
     2.5 +    Author:     Johannes Hölzl, TU München
     2.6 +    Author:     Robert Himmelmann, TU München
     2.7 +    Author:     Armin Heller, TU München
     2.8 +    Author:     Bogdan Grechuk, University of Edinburgh
     2.9 +*)
    2.10 +
    2.11 +header {* Extended real number line *}
    2.12 +
    2.13 +theory Extended_Real
    2.14 +  imports Complex_Main
    2.15 +begin
    2.16 +
    2.17 +text {*
    2.18 +
    2.19 +For more lemmas about the extended real numbers go to
    2.20 +  @{text "src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy"}
    2.21 +
    2.22 +*}
    2.23 +
    2.24 +lemma (in complete_lattice) atLeast_eq_UNIV_iff: "{x..} = UNIV \<longleftrightarrow> x = bot"
    2.25 +proof
    2.26 +  assume "{x..} = UNIV"
    2.27 +  show "x = bot"
    2.28 +  proof (rule ccontr)
    2.29 +    assume "x \<noteq> bot" then have "bot \<notin> {x..}" by (simp add: le_less)
    2.30 +    then show False using `{x..} = UNIV` by simp
    2.31 +  qed
    2.32 +qed auto
    2.33 +
    2.34 +lemma SUPR_pair:
    2.35 +  "(SUP i : A. SUP j : B. f i j) = (SUP p : A \<times> B. f (fst p) (snd p))"
    2.36 +  by (rule antisym) (auto intro!: SUP_leI le_SUPI2)
    2.37 +
    2.38 +lemma INFI_pair:
    2.39 +  "(INF i : A. INF j : B. f i j) = (INF p : A \<times> B. f (fst p) (snd p))"
    2.40 +  by (rule antisym) (auto intro!: le_INFI INF_leI2)
    2.41 +
    2.42 +subsection {* Definition and basic properties *}
    2.43 +
    2.44 +datatype ereal = ereal real | PInfty | MInfty
    2.45 +
    2.46 +notation (xsymbols)
    2.47 +  PInfty  ("\<infinity>")
    2.48 +
    2.49 +notation (HTML output)
    2.50 +  PInfty  ("\<infinity>")
    2.51 +
    2.52 +declare [[coercion "ereal :: real \<Rightarrow> ereal"]]
    2.53 +
    2.54 +instantiation ereal :: uminus
    2.55 +begin
    2.56 +  fun uminus_ereal where
    2.57 +    "- (ereal r) = ereal (- r)"
    2.58 +  | "- \<infinity> = MInfty"
    2.59 +  | "- MInfty = \<infinity>"
    2.60 +  instance ..
    2.61 +end
    2.62 +
    2.63 +lemma inj_ereal[simp]: "inj_on ereal A"
    2.64 +  unfolding inj_on_def by auto
    2.65 +
    2.66 +lemma MInfty_neq_PInfty[simp]:
    2.67 +  "\<infinity> \<noteq> - \<infinity>" "- \<infinity> \<noteq> \<infinity>" by simp_all
    2.68 +
    2.69 +lemma MInfty_neq_ereal[simp]:
    2.70 +  "ereal r \<noteq> - \<infinity>" "- \<infinity> \<noteq> ereal r" by simp_all
    2.71 +
    2.72 +lemma MInfinity_cases[simp]:
    2.73 +  "(case - \<infinity> of ereal r \<Rightarrow> f r | \<infinity> \<Rightarrow> y | MInfinity \<Rightarrow> z) = z"
    2.74 +  by simp
    2.75 +
    2.76 +lemma ereal_uminus_uminus[simp]:
    2.77 +  fixes a :: ereal shows "- (- a) = a"
    2.78 +  by (cases a) simp_all
    2.79 +
    2.80 +lemma MInfty_eq[simp, code_post]:
    2.81 +  "MInfty = - \<infinity>" by simp
    2.82 +
    2.83 +declare uminus_ereal.simps(2)[code_inline, simp del]
    2.84 +
    2.85 +lemma ereal_cases[case_names real PInf MInf, cases type: ereal]:
    2.86 +  assumes "\<And>r. x = ereal r \<Longrightarrow> P"
    2.87 +  assumes "x = \<infinity> \<Longrightarrow> P"
    2.88 +  assumes "x = -\<infinity> \<Longrightarrow> P"
    2.89 +  shows P
    2.90 +  using assms by (cases x) auto
    2.91 +
    2.92 +lemmas ereal2_cases = ereal_cases[case_product ereal_cases]
    2.93 +lemmas ereal3_cases = ereal2_cases[case_product ereal_cases]
    2.94 +
    2.95 +lemma ereal_uminus_eq_iff[simp]:
    2.96 +  fixes a b :: ereal shows "-a = -b \<longleftrightarrow> a = b"
    2.97 +  by (cases rule: ereal2_cases[of a b]) simp_all
    2.98 +
    2.99 +function of_ereal :: "ereal \<Rightarrow> real" where
   2.100 +"of_ereal (ereal r) = r" |
   2.101 +"of_ereal \<infinity> = 0" |
   2.102 +"of_ereal (-\<infinity>) = 0"
   2.103 +  by (auto intro: ereal_cases)
   2.104 +termination proof qed (rule wf_empty)
   2.105 +
   2.106 +defs (overloaded)
   2.107 +  real_of_ereal_def [code_unfold]: "real \<equiv> of_ereal"
   2.108 +
   2.109 +lemma real_of_ereal[simp]:
   2.110 +    "real (- x :: ereal) = - (real x)"
   2.111 +    "real (ereal r) = r"
   2.112 +    "real \<infinity> = 0"
   2.113 +  by (cases x) (simp_all add: real_of_ereal_def)
   2.114 +
   2.115 +lemma range_ereal[simp]: "range ereal = UNIV - {\<infinity>, -\<infinity>}"
   2.116 +proof safe
   2.117 +  fix x assume "x \<notin> range ereal" "x \<noteq> \<infinity>"
   2.118 +  then show "x = -\<infinity>" by (cases x) auto
   2.119 +qed auto
   2.120 +
   2.121 +lemma ereal_range_uminus[simp]: "range uminus = (UNIV::ereal set)"
   2.122 +proof safe
   2.123 +  fix x :: ereal show "x \<in> range uminus" by (intro image_eqI[of _ _ "-x"]) auto
   2.124 +qed auto
   2.125 +
   2.126 +instantiation ereal :: number
   2.127 +begin
   2.128 +definition [simp]: "number_of x = ereal (number_of x)"
   2.129 +instance proof qed
   2.130 +end
   2.131 +
   2.132 +instantiation ereal :: abs
   2.133 +begin
   2.134 +  function abs_ereal where
   2.135 +    "\<bar>ereal r\<bar> = ereal \<bar>r\<bar>"
   2.136 +  | "\<bar>-\<infinity>\<bar> = \<infinity>"
   2.137 +  | "\<bar>\<infinity>\<bar> = \<infinity>"
   2.138 +  by (auto intro: ereal_cases)
   2.139 +  termination proof qed (rule wf_empty)
   2.140 +  instance ..
   2.141 +end
   2.142 +
   2.143 +lemma abs_eq_infinity_cases[elim!]: "\<lbrakk> \<bar>x\<bar> = \<infinity> ; x = \<infinity> \<Longrightarrow> P ; x = -\<infinity> \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
   2.144 +  by (cases x) auto
   2.145 +
   2.146 +lemma abs_neq_infinity_cases[elim!]: "\<lbrakk> \<bar>x\<bar> \<noteq> \<infinity> ; \<And>r. x = ereal r \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
   2.147 +  by (cases x) auto
   2.148 +
   2.149 +lemma abs_ereal_uminus[simp]: "\<bar>- x\<bar> = \<bar>x::ereal\<bar>"
   2.150 +  by (cases x) auto
   2.151 +
   2.152 +subsubsection "Addition"
   2.153 +
   2.154 +instantiation ereal :: comm_monoid_add
   2.155 +begin
   2.156 +
   2.157 +definition "0 = ereal 0"
   2.158 +
   2.159 +function plus_ereal where
   2.160 +"ereal r + ereal p = ereal (r + p)" |
   2.161 +"\<infinity> + a = \<infinity>" |
   2.162 +"a + \<infinity> = \<infinity>" |
   2.163 +"ereal r + -\<infinity> = - \<infinity>" |
   2.164 +"-\<infinity> + ereal p = -\<infinity>" |
   2.165 +"-\<infinity> + -\<infinity> = -\<infinity>"
   2.166 +proof -
   2.167 +  case (goal1 P x)
   2.168 +  moreover then obtain a b where "x = (a, b)" by (cases x) auto
   2.169 +  ultimately show P
   2.170 +   by (cases rule: ereal2_cases[of a b]) auto
   2.171 +qed auto
   2.172 +termination proof qed (rule wf_empty)
   2.173 +
   2.174 +lemma Infty_neq_0[simp]:
   2.175 +  "\<infinity> \<noteq> 0" "0 \<noteq> \<infinity>"
   2.176 +  "-\<infinity> \<noteq> 0" "0 \<noteq> -\<infinity>"
   2.177 +  by (simp_all add: zero_ereal_def)
   2.178 +
   2.179 +lemma ereal_eq_0[simp]:
   2.180 +  "ereal r = 0 \<longleftrightarrow> r = 0"
   2.181 +  "0 = ereal r \<longleftrightarrow> r = 0"
   2.182 +  unfolding zero_ereal_def by simp_all
   2.183 +
   2.184 +instance
   2.185 +proof
   2.186 +  fix a :: ereal show "0 + a = a"
   2.187 +    by (cases a) (simp_all add: zero_ereal_def)
   2.188 +  fix b :: ereal show "a + b = b + a"
   2.189 +    by (cases rule: ereal2_cases[of a b]) simp_all
   2.190 +  fix c :: ereal show "a + b + c = a + (b + c)"
   2.191 +    by (cases rule: ereal3_cases[of a b c]) simp_all
   2.192 +qed
   2.193 +end
   2.194 +
   2.195 +lemma real_of_ereal_0[simp]: "real (0::ereal) = 0"
   2.196 +  unfolding real_of_ereal_def zero_ereal_def by simp
   2.197 +
   2.198 +lemma abs_ereal_zero[simp]: "\<bar>0\<bar> = (0::ereal)"
   2.199 +  unfolding zero_ereal_def abs_ereal.simps by simp
   2.200 +
   2.201 +lemma ereal_uminus_zero[simp]:
   2.202 +  "- 0 = (0::ereal)"
   2.203 +  by (simp add: zero_ereal_def)
   2.204 +
   2.205 +lemma ereal_uminus_zero_iff[simp]:
   2.206 +  fixes a :: ereal shows "-a = 0 \<longleftrightarrow> a = 0"
   2.207 +  by (cases a) simp_all
   2.208 +
   2.209 +lemma ereal_plus_eq_PInfty[simp]:
   2.210 +  shows "a + b = \<infinity> \<longleftrightarrow> a = \<infinity> \<or> b = \<infinity>"
   2.211 +  by (cases rule: ereal2_cases[of a b]) auto
   2.212 +
   2.213 +lemma ereal_plus_eq_MInfty[simp]:
   2.214 +  shows "a + b = -\<infinity> \<longleftrightarrow>
   2.215 +    (a = -\<infinity> \<or> b = -\<infinity>) \<and> a \<noteq> \<infinity> \<and> b \<noteq> \<infinity>"
   2.216 +  by (cases rule: ereal2_cases[of a b]) auto
   2.217 +
   2.218 +lemma ereal_add_cancel_left:
   2.219 +  assumes "a \<noteq> -\<infinity>"
   2.220 +  shows "a + b = a + c \<longleftrightarrow> (a = \<infinity> \<or> b = c)"
   2.221 +  using assms by (cases rule: ereal3_cases[of a b c]) auto
   2.222 +
   2.223 +lemma ereal_add_cancel_right:
   2.224 +  assumes "a \<noteq> -\<infinity>"
   2.225 +  shows "b + a = c + a \<longleftrightarrow> (a = \<infinity> \<or> b = c)"
   2.226 +  using assms by (cases rule: ereal3_cases[of a b c]) auto
   2.227 +
   2.228 +lemma ereal_real:
   2.229 +  "ereal (real x) = (if \<bar>x\<bar> = \<infinity> then 0 else x)"
   2.230 +  by (cases x) simp_all
   2.231 +
   2.232 +lemma real_of_ereal_add:
   2.233 +  fixes a b :: ereal
   2.234 +  shows "real (a + b) = (if (\<bar>a\<bar> = \<infinity>) \<and> (\<bar>b\<bar> = \<infinity>) \<or> (\<bar>a\<bar> \<noteq> \<infinity>) \<and> (\<bar>b\<bar> \<noteq> \<infinity>) then real a + real b else 0)"
   2.235 +  by (cases rule: ereal2_cases[of a b]) auto
   2.236 +
   2.237 +subsubsection "Linear order on @{typ ereal}"
   2.238 +
   2.239 +instantiation ereal :: linorder
   2.240 +begin
   2.241 +
   2.242 +function less_ereal where
   2.243 +"ereal x < ereal y \<longleftrightarrow> x < y" |
   2.244 +"        \<infinity> < a         \<longleftrightarrow> False" |
   2.245 +"        a < -\<infinity>        \<longleftrightarrow> False" |
   2.246 +"ereal x < \<infinity>         \<longleftrightarrow> True" |
   2.247 +"       -\<infinity> < ereal r \<longleftrightarrow> True" |
   2.248 +"       -\<infinity> < \<infinity>         \<longleftrightarrow> True"
   2.249 +proof -
   2.250 +  case (goal1 P x)
   2.251 +  moreover then obtain a b where "x = (a,b)" by (cases x) auto
   2.252 +  ultimately show P by (cases rule: ereal2_cases[of a b]) auto
   2.253 +qed simp_all
   2.254 +termination by (relation "{}") simp
   2.255 +
   2.256 +definition "x \<le> (y::ereal) \<longleftrightarrow> x < y \<or> x = y"
   2.257 +
   2.258 +lemma ereal_infty_less[simp]:
   2.259 +  "x < \<infinity> \<longleftrightarrow> (x \<noteq> \<infinity>)"
   2.260 +  "-\<infinity> < x \<longleftrightarrow> (x \<noteq> -\<infinity>)"
   2.261 +  by (cases x, simp_all) (cases x, simp_all)
   2.262 +
   2.263 +lemma ereal_infty_less_eq[simp]:
   2.264 +  "\<infinity> \<le> x \<longleftrightarrow> x = \<infinity>"
   2.265 +  "x \<le> -\<infinity> \<longleftrightarrow> x = -\<infinity>"
   2.266 +  by (auto simp add: less_eq_ereal_def)
   2.267 +
   2.268 +lemma ereal_less[simp]:
   2.269 +  "ereal r < 0 \<longleftrightarrow> (r < 0)"
   2.270 +  "0 < ereal r \<longleftrightarrow> (0 < r)"
   2.271 +  "0 < \<infinity>"
   2.272 +  "-\<infinity> < 0"
   2.273 +  by (simp_all add: zero_ereal_def)
   2.274 +
   2.275 +lemma ereal_less_eq[simp]:
   2.276 +  "x \<le> \<infinity>"
   2.277 +  "-\<infinity> \<le> x"
   2.278 +  "ereal r \<le> ereal p \<longleftrightarrow> r \<le> p"
   2.279 +  "ereal r \<le> 0 \<longleftrightarrow> r \<le> 0"
   2.280 +  "0 \<le> ereal r \<longleftrightarrow> 0 \<le> r"
   2.281 +  by (auto simp add: less_eq_ereal_def zero_ereal_def)
   2.282 +
   2.283 +lemma ereal_infty_less_eq2:
   2.284 +  "a \<le> b \<Longrightarrow> a = \<infinity> \<Longrightarrow> b = \<infinity>"
   2.285 +  "a \<le> b \<Longrightarrow> b = -\<infinity> \<Longrightarrow> a = -\<infinity>"
   2.286 +  by simp_all
   2.287 +
   2.288 +instance
   2.289 +proof
   2.290 +  fix x :: ereal show "x \<le> x"
   2.291 +    by (cases x) simp_all
   2.292 +  fix y :: ereal show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
   2.293 +    by (cases rule: ereal2_cases[of x y]) auto
   2.294 +  show "x \<le> y \<or> y \<le> x "
   2.295 +    by (cases rule: ereal2_cases[of x y]) auto
   2.296 +  { assume "x \<le> y" "y \<le> x" then show "x = y"
   2.297 +    by (cases rule: ereal2_cases[of x y]) auto }
   2.298 +  { fix z assume "x \<le> y" "y \<le> z" then show "x \<le> z"
   2.299 +    by (cases rule: ereal3_cases[of x y z]) auto }
   2.300 +qed
   2.301 +end
   2.302 +
   2.303 +instance ereal :: ordered_ab_semigroup_add
   2.304 +proof
   2.305 +  fix a b c :: ereal assume "a \<le> b" then show "c + a \<le> c + b"
   2.306 +    by (cases rule: ereal3_cases[of a b c]) auto
   2.307 +qed
   2.308 +
   2.309 +lemma real_of_ereal_positive_mono:
   2.310 +  "\<lbrakk>0 \<le> x; x \<le> y; y \<noteq> \<infinity>\<rbrakk> \<Longrightarrow> real x \<le> real y"
   2.311 +  by (cases rule: ereal2_cases[of x y]) auto
   2.312 +
   2.313 +lemma ereal_MInfty_lessI[intro, simp]:
   2.314 +  "a \<noteq> -\<infinity> \<Longrightarrow> -\<infinity> < a"
   2.315 +  by (cases a) auto
   2.316 +
   2.317 +lemma ereal_less_PInfty[intro, simp]:
   2.318 +  "a \<noteq> \<infinity> \<Longrightarrow> a < \<infinity>"
   2.319 +  by (cases a) auto
   2.320 +
   2.321 +lemma ereal_less_ereal_Ex:
   2.322 +  fixes a b :: ereal
   2.323 +  shows "x < ereal r \<longleftrightarrow> x = -\<infinity> \<or> (\<exists>p. p < r \<and> x = ereal p)"
   2.324 +  by (cases x) auto
   2.325 +
   2.326 +lemma less_PInf_Ex_of_nat: "x \<noteq> \<infinity> \<longleftrightarrow> (\<exists>n::nat. x < ereal (real n))"
   2.327 +proof (cases x)
   2.328 +  case (real r) then show ?thesis
   2.329 +    using reals_Archimedean2[of r] by simp
   2.330 +qed simp_all
   2.331 +
   2.332 +lemma ereal_add_mono:
   2.333 +  fixes a b c d :: ereal assumes "a \<le> b" "c \<le> d" shows "a + c \<le> b + d"
   2.334 +  using assms
   2.335 +  apply (cases a)
   2.336 +  apply (cases rule: ereal3_cases[of b c d], auto)
   2.337 +  apply (cases rule: ereal3_cases[of b c d], auto)
   2.338 +  done
   2.339 +
   2.340 +lemma ereal_minus_le_minus[simp]:
   2.341 +  fixes a b :: ereal shows "- a \<le> - b \<longleftrightarrow> b \<le> a"
   2.342 +  by (cases rule: ereal2_cases[of a b]) auto
   2.343 +
   2.344 +lemma ereal_minus_less_minus[simp]:
   2.345 +  fixes a b :: ereal shows "- a < - b \<longleftrightarrow> b < a"
   2.346 +  by (cases rule: ereal2_cases[of a b]) auto
   2.347 +
   2.348 +lemma ereal_le_real_iff:
   2.349 +  "x \<le> real y \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> ereal x \<le> y) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> x \<le> 0))"
   2.350 +  by (cases y) auto
   2.351 +
   2.352 +lemma real_le_ereal_iff:
   2.353 +  "real y \<le> x \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> y \<le> ereal x) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> 0 \<le> x))"
   2.354 +  by (cases y) auto
   2.355 +
   2.356 +lemma ereal_less_real_iff:
   2.357 +  "x < real y \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> ereal x < y) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> x < 0))"
   2.358 +  by (cases y) auto
   2.359 +
   2.360 +lemma real_less_ereal_iff:
   2.361 +  "real y < x \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> y < ereal x) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> 0 < x))"
   2.362 +  by (cases y) auto
   2.363 +
   2.364 +lemma real_of_ereal_pos:
   2.365 +  fixes x :: ereal shows "0 \<le> x \<Longrightarrow> 0 \<le> real x" by (cases x) auto
   2.366 +
   2.367 +lemmas real_of_ereal_ord_simps =
   2.368 +  ereal_le_real_iff real_le_ereal_iff ereal_less_real_iff real_less_ereal_iff
   2.369 +
   2.370 +lemma abs_ereal_ge0[simp]: "0 \<le> x \<Longrightarrow> \<bar>x :: ereal\<bar> = x"
   2.371 +  by (cases x) auto
   2.372 +
   2.373 +lemma abs_ereal_less0[simp]: "x < 0 \<Longrightarrow> \<bar>x :: ereal\<bar> = -x"
   2.374 +  by (cases x) auto
   2.375 +
   2.376 +lemma abs_ereal_pos[simp]: "0 \<le> \<bar>x :: ereal\<bar>"
   2.377 +  by (cases x) auto
   2.378 +
   2.379 +lemma real_of_ereal_le_0[simp]: "real (X :: ereal) \<le> 0 \<longleftrightarrow> (X \<le> 0 \<or> X = \<infinity>)"
   2.380 +  by (cases X) auto
   2.381 +
   2.382 +lemma abs_real_of_ereal[simp]: "\<bar>real (X :: ereal)\<bar> = real \<bar>X\<bar>"
   2.383 +  by (cases X) auto
   2.384 +
   2.385 +lemma zero_less_real_of_ereal: "0 < real X \<longleftrightarrow> (0 < X \<and> X \<noteq> \<infinity>)"
   2.386 +  by (cases X) auto
   2.387 +
   2.388 +lemma ereal_0_le_uminus_iff[simp]:
   2.389 +  fixes a :: ereal shows "0 \<le> -a \<longleftrightarrow> a \<le> 0"
   2.390 +  by (cases rule: ereal2_cases[of a]) auto
   2.391 +
   2.392 +lemma ereal_uminus_le_0_iff[simp]:
   2.393 +  fixes a :: ereal shows "-a \<le> 0 \<longleftrightarrow> 0 \<le> a"
   2.394 +  by (cases rule: ereal2_cases[of a]) auto
   2.395 +
   2.396 +lemma ereal_dense:
   2.397 +  fixes x y :: ereal assumes "x < y"
   2.398 +  shows "EX z. x < z & z < y"
   2.399 +proof -
   2.400 +{ assume a: "x = (-\<infinity>)"
   2.401 +  { assume "y = \<infinity>" hence ?thesis using a by (auto intro!: exI[of _ "0"]) }
   2.402 +  moreover
   2.403 +  { assume "y ~= \<infinity>"
   2.404 +    with `x < y` obtain r where r: "y = ereal r" by (cases y) auto
   2.405 +    hence ?thesis using `x < y` a by (auto intro!: exI[of _ "ereal (r - 1)"])
   2.406 +  } ultimately have ?thesis by auto
   2.407 +}
   2.408 +moreover
   2.409 +{ assume "x ~= (-\<infinity>)"
   2.410 +  with `x < y` obtain p where p: "x = ereal p" by (cases x) auto
   2.411 +  { assume "y = \<infinity>" hence ?thesis using `x < y` p
   2.412 +       by (auto intro!: exI[of _ "ereal (p + 1)"]) }
   2.413 +  moreover
   2.414 +  { assume "y ~= \<infinity>"
   2.415 +    with `x < y` obtain r where r: "y = ereal r" by (cases y) auto
   2.416 +    with p `x < y` have "p < r" by auto
   2.417 +    with dense obtain z where "p < z" "z < r" by auto
   2.418 +    hence ?thesis using r p by (auto intro!: exI[of _ "ereal z"])
   2.419 +  } ultimately have ?thesis by auto
   2.420 +} ultimately show ?thesis by auto
   2.421 +qed
   2.422 +
   2.423 +lemma ereal_dense2:
   2.424 +  fixes x y :: ereal assumes "x < y"
   2.425 +  shows "EX z. x < ereal z & ereal z < y"
   2.426 +  by (metis ereal_dense[OF `x < y`] ereal_cases less_ereal.simps(2,3))
   2.427 +
   2.428 +lemma ereal_add_strict_mono:
   2.429 +  fixes a b c d :: ereal
   2.430 +  assumes "a = b" "0 \<le> a" "a \<noteq> \<infinity>" "c < d"
   2.431 +  shows "a + c < b + d"
   2.432 +  using assms by (cases rule: ereal3_cases[case_product ereal_cases, of a b c d]) auto
   2.433 +
   2.434 +lemma ereal_less_add: "\<bar>a\<bar> \<noteq> \<infinity> \<Longrightarrow> c < b \<Longrightarrow> a + c < a + b"
   2.435 +  by (cases rule: ereal2_cases[of b c]) auto
   2.436 +
   2.437 +lemma ereal_uminus_eq_reorder: "- a = b \<longleftrightarrow> a = (-b::ereal)" by auto
   2.438 +
   2.439 +lemma ereal_uminus_less_reorder: "- a < b \<longleftrightarrow> -b < (a::ereal)"
   2.440 +  by (subst (3) ereal_uminus_uminus[symmetric]) (simp only: ereal_minus_less_minus)
   2.441 +
   2.442 +lemma ereal_uminus_le_reorder: "- a \<le> b \<longleftrightarrow> -b \<le> (a::ereal)"
   2.443 +  by (subst (3) ereal_uminus_uminus[symmetric]) (simp only: ereal_minus_le_minus)
   2.444 +
   2.445 +lemmas ereal_uminus_reorder =
   2.446 +  ereal_uminus_eq_reorder ereal_uminus_less_reorder ereal_uminus_le_reorder
   2.447 +
   2.448 +lemma ereal_bot:
   2.449 +  fixes x :: ereal assumes "\<And>B. x \<le> ereal B" shows "x = - \<infinity>"
   2.450 +proof (cases x)
   2.451 +  case (real r) with assms[of "r - 1"] show ?thesis by auto
   2.452 +next case PInf with assms[of 0] show ?thesis by auto
   2.453 +next case MInf then show ?thesis by simp
   2.454 +qed
   2.455 +
   2.456 +lemma ereal_top:
   2.457 +  fixes x :: ereal assumes "\<And>B. x \<ge> ereal B" shows "x = \<infinity>"
   2.458 +proof (cases x)
   2.459 +  case (real r) with assms[of "r + 1"] show ?thesis by auto
   2.460 +next case MInf with assms[of 0] show ?thesis by auto
   2.461 +next case PInf then show ?thesis by simp
   2.462 +qed
   2.463 +
   2.464 +lemma
   2.465 +  shows ereal_max[simp]: "ereal (max x y) = max (ereal x) (ereal y)"
   2.466 +    and ereal_min[simp]: "ereal (min x y) = min (ereal x) (ereal y)"
   2.467 +  by (simp_all add: min_def max_def)
   2.468 +
   2.469 +lemma ereal_max_0: "max 0 (ereal r) = ereal (max 0 r)"
   2.470 +  by (auto simp: zero_ereal_def)
   2.471 +
   2.472 +lemma
   2.473 +  fixes f :: "nat \<Rightarrow> ereal"
   2.474 +  shows incseq_uminus[simp]: "incseq (\<lambda>x. - f x) \<longleftrightarrow> decseq f"
   2.475 +  and decseq_uminus[simp]: "decseq (\<lambda>x. - f x) \<longleftrightarrow> incseq f"
   2.476 +  unfolding decseq_def incseq_def by auto
   2.477 +
   2.478 +lemma incseq_ereal: "incseq f \<Longrightarrow> incseq (\<lambda>x. ereal (f x))"
   2.479 +  unfolding incseq_def by auto
   2.480 +
   2.481 +lemma ereal_add_nonneg_nonneg:
   2.482 +  fixes a b :: ereal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a + b"
   2.483 +  using add_mono[of 0 a 0 b] by simp
   2.484 +
   2.485 +lemma image_eqD: "f ` A = B \<Longrightarrow> (\<forall>x\<in>A. f x \<in> B)"
   2.486 +  by auto
   2.487 +
   2.488 +lemma incseq_setsumI:
   2.489 +  fixes f :: "nat \<Rightarrow> 'a::{comm_monoid_add, ordered_ab_semigroup_add}"
   2.490 +  assumes "\<And>i. 0 \<le> f i"
   2.491 +  shows "incseq (\<lambda>i. setsum f {..< i})"
   2.492 +proof (intro incseq_SucI)
   2.493 +  fix n have "setsum f {..< n} + 0 \<le> setsum f {..<n} + f n"
   2.494 +    using assms by (rule add_left_mono)
   2.495 +  then show "setsum f {..< n} \<le> setsum f {..< Suc n}"
   2.496 +    by auto
   2.497 +qed
   2.498 +
   2.499 +lemma incseq_setsumI2:
   2.500 +  fixes f :: "'i \<Rightarrow> nat \<Rightarrow> 'a::{comm_monoid_add, ordered_ab_semigroup_add}"
   2.501 +  assumes "\<And>n. n \<in> A \<Longrightarrow> incseq (f n)"
   2.502 +  shows "incseq (\<lambda>i. \<Sum>n\<in>A. f n i)"
   2.503 +  using assms unfolding incseq_def by (auto intro: setsum_mono)
   2.504 +
   2.505 +subsubsection "Multiplication"
   2.506 +
   2.507 +instantiation ereal :: "{comm_monoid_mult, sgn}"
   2.508 +begin
   2.509 +
   2.510 +definition "1 = ereal 1"
   2.511 +
   2.512 +function sgn_ereal where
   2.513 +  "sgn (ereal r) = ereal (sgn r)"
   2.514 +| "sgn \<infinity> = 1"
   2.515 +| "sgn (-\<infinity>) = -1"
   2.516 +by (auto intro: ereal_cases)
   2.517 +termination proof qed (rule wf_empty)
   2.518 +
   2.519 +function times_ereal where
   2.520 +"ereal r * ereal p = ereal (r * p)" |
   2.521 +"ereal r * \<infinity> = (if r = 0 then 0 else if r > 0 then \<infinity> else -\<infinity>)" |
   2.522 +"\<infinity> * ereal r = (if r = 0 then 0 else if r > 0 then \<infinity> else -\<infinity>)" |
   2.523 +"ereal r * -\<infinity> = (if r = 0 then 0 else if r > 0 then -\<infinity> else \<infinity>)" |
   2.524 +"-\<infinity> * ereal r = (if r = 0 then 0 else if r > 0 then -\<infinity> else \<infinity>)" |
   2.525 +"\<infinity> * \<infinity> = \<infinity>" |
   2.526 +"-\<infinity> * \<infinity> = -\<infinity>" |
   2.527 +"\<infinity> * -\<infinity> = -\<infinity>" |
   2.528 +"-\<infinity> * -\<infinity> = \<infinity>"
   2.529 +proof -
   2.530 +  case (goal1 P x)
   2.531 +  moreover then obtain a b where "x = (a, b)" by (cases x) auto
   2.532 +  ultimately show P by (cases rule: ereal2_cases[of a b]) auto
   2.533 +qed simp_all
   2.534 +termination by (relation "{}") simp
   2.535 +
   2.536 +instance
   2.537 +proof
   2.538 +  fix a :: ereal show "1 * a = a"
   2.539 +    by (cases a) (simp_all add: one_ereal_def)
   2.540 +  fix b :: ereal show "a * b = b * a"
   2.541 +    by (cases rule: ereal2_cases[of a b]) simp_all
   2.542 +  fix c :: ereal show "a * b * c = a * (b * c)"
   2.543 +    by (cases rule: ereal3_cases[of a b c])
   2.544 +       (simp_all add: zero_ereal_def zero_less_mult_iff)
   2.545 +qed
   2.546 +end
   2.547 +
   2.548 +lemma real_of_ereal_le_1:
   2.549 +  fixes a :: ereal shows "a \<le> 1 \<Longrightarrow> real a \<le> 1"
   2.550 +  by (cases a) (auto simp: one_ereal_def)
   2.551 +
   2.552 +lemma abs_ereal_one[simp]: "\<bar>1\<bar> = (1::ereal)"
   2.553 +  unfolding one_ereal_def by simp
   2.554 +
   2.555 +lemma ereal_mult_zero[simp]:
   2.556 +  fixes a :: ereal shows "a * 0 = 0"
   2.557 +  by (cases a) (simp_all add: zero_ereal_def)
   2.558 +
   2.559 +lemma ereal_zero_mult[simp]:
   2.560 +  fixes a :: ereal shows "0 * a = 0"
   2.561 +  by (cases a) (simp_all add: zero_ereal_def)
   2.562 +
   2.563 +lemma ereal_m1_less_0[simp]:
   2.564 +  "-(1::ereal) < 0"
   2.565 +  by (simp add: zero_ereal_def one_ereal_def)
   2.566 +
   2.567 +lemma ereal_zero_m1[simp]:
   2.568 +  "1 \<noteq> (0::ereal)"
   2.569 +  by (simp add: zero_ereal_def one_ereal_def)
   2.570 +
   2.571 +lemma ereal_times_0[simp]:
   2.572 +  fixes x :: ereal shows "0 * x = 0"
   2.573 +  by (cases x) (auto simp: zero_ereal_def)
   2.574 +
   2.575 +lemma ereal_times[simp]:
   2.576 +  "1 \<noteq> \<infinity>" "\<infinity> \<noteq> 1"
   2.577 +  "1 \<noteq> -\<infinity>" "-\<infinity> \<noteq> 1"
   2.578 +  by (auto simp add: times_ereal_def one_ereal_def)
   2.579 +
   2.580 +lemma ereal_plus_1[simp]:
   2.581 +  "1 + ereal r = ereal (r + 1)" "ereal r + 1 = ereal (r + 1)"
   2.582 +  "1 + -\<infinity> = -\<infinity>" "-\<infinity> + 1 = -\<infinity>"
   2.583 +  unfolding one_ereal_def by auto
   2.584 +
   2.585 +lemma ereal_zero_times[simp]:
   2.586 +  fixes a b :: ereal shows "a * b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
   2.587 +  by (cases rule: ereal2_cases[of a b]) auto
   2.588 +
   2.589 +lemma ereal_mult_eq_PInfty[simp]:
   2.590 +  shows "a * b = \<infinity> \<longleftrightarrow>
   2.591 +    (a = \<infinity> \<and> b > 0) \<or> (a > 0 \<and> b = \<infinity>) \<or> (a = -\<infinity> \<and> b < 0) \<or> (a < 0 \<and> b = -\<infinity>)"
   2.592 +  by (cases rule: ereal2_cases[of a b]) auto
   2.593 +
   2.594 +lemma ereal_mult_eq_MInfty[simp]:
   2.595 +  shows "a * b = -\<infinity> \<longleftrightarrow>
   2.596 +    (a = \<infinity> \<and> b < 0) \<or> (a < 0 \<and> b = \<infinity>) \<or> (a = -\<infinity> \<and> b > 0) \<or> (a > 0 \<and> b = -\<infinity>)"
   2.597 +  by (cases rule: ereal2_cases[of a b]) auto
   2.598 +
   2.599 +lemma ereal_0_less_1[simp]: "0 < (1::ereal)"
   2.600 +  by (simp_all add: zero_ereal_def one_ereal_def)
   2.601 +
   2.602 +lemma ereal_zero_one[simp]: "0 \<noteq> (1::ereal)"
   2.603 +  by (simp_all add: zero_ereal_def one_ereal_def)
   2.604 +
   2.605 +lemma ereal_mult_minus_left[simp]:
   2.606 +  fixes a b :: ereal shows "-a * b = - (a * b)"
   2.607 +  by (cases rule: ereal2_cases[of a b]) auto
   2.608 +
   2.609 +lemma ereal_mult_minus_right[simp]:
   2.610 +  fixes a b :: ereal shows "a * -b = - (a * b)"
   2.611 +  by (cases rule: ereal2_cases[of a b]) auto
   2.612 +
   2.613 +lemma ereal_mult_infty[simp]:
   2.614 +  "a * \<infinity> = (if a = 0 then 0 else if 0 < a then \<infinity> else - \<infinity>)"
   2.615 +  by (cases a) auto
   2.616 +
   2.617 +lemma ereal_infty_mult[simp]:
   2.618 +  "\<infinity> * a = (if a = 0 then 0 else if 0 < a then \<infinity> else - \<infinity>)"
   2.619 +  by (cases a) auto
   2.620 +
   2.621 +lemma ereal_mult_strict_right_mono:
   2.622 +  assumes "a < b" and "0 < c" "c < \<infinity>"
   2.623 +  shows "a * c < b * c"
   2.624 +  using assms
   2.625 +  by (cases rule: ereal3_cases[of a b c])
   2.626 +     (auto simp: zero_le_mult_iff ereal_less_PInfty)
   2.627 +
   2.628 +lemma ereal_mult_strict_left_mono:
   2.629 +  "\<lbrakk> a < b ; 0 < c ; c < \<infinity>\<rbrakk> \<Longrightarrow> c * a < c * b"
   2.630 +  using ereal_mult_strict_right_mono by (simp add: mult_commute[of c])
   2.631 +
   2.632 +lemma ereal_mult_right_mono:
   2.633 +  fixes a b c :: ereal shows "\<lbrakk>a \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> a*c \<le> b*c"
   2.634 +  using assms
   2.635 +  apply (cases "c = 0") apply simp
   2.636 +  by (cases rule: ereal3_cases[of a b c])
   2.637 +     (auto simp: zero_le_mult_iff ereal_less_PInfty)
   2.638 +
   2.639 +lemma ereal_mult_left_mono:
   2.640 +  fixes a b c :: ereal shows "\<lbrakk>a \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> c * a \<le> c * b"
   2.641 +  using ereal_mult_right_mono by (simp add: mult_commute[of c])
   2.642 +
   2.643 +lemma zero_less_one_ereal[simp]: "0 \<le> (1::ereal)"
   2.644 +  by (simp add: one_ereal_def zero_ereal_def)
   2.645 +
   2.646 +lemma ereal_0_le_mult[simp]: "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a * (b :: ereal)"
   2.647 +  by (cases rule: ereal2_cases[of a b]) (auto simp: mult_nonneg_nonneg)
   2.648 +
   2.649 +lemma ereal_right_distrib:
   2.650 +  fixes r a b :: ereal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> r * (a + b) = r * a + r * b"
   2.651 +  by (cases rule: ereal3_cases[of r a b]) (simp_all add: field_simps)
   2.652 +
   2.653 +lemma ereal_left_distrib:
   2.654 +  fixes r a b :: ereal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> (a + b) * r = a * r + b * r"
   2.655 +  by (cases rule: ereal3_cases[of r a b]) (simp_all add: field_simps)
   2.656 +
   2.657 +lemma ereal_mult_le_0_iff:
   2.658 +  fixes a b :: ereal
   2.659 +  shows "a * b \<le> 0 \<longleftrightarrow> (0 \<le> a \<and> b \<le> 0) \<or> (a \<le> 0 \<and> 0 \<le> b)"
   2.660 +  by (cases rule: ereal2_cases[of a b]) (simp_all add: mult_le_0_iff)
   2.661 +
   2.662 +lemma ereal_zero_le_0_iff:
   2.663 +  fixes a b :: ereal
   2.664 +  shows "0 \<le> a * b \<longleftrightarrow> (0 \<le> a \<and> 0 \<le> b) \<or> (a \<le> 0 \<and> b \<le> 0)"
   2.665 +  by (cases rule: ereal2_cases[of a b]) (simp_all add: zero_le_mult_iff)
   2.666 +
   2.667 +lemma ereal_mult_less_0_iff:
   2.668 +  fixes a b :: ereal
   2.669 +  shows "a * b < 0 \<longleftrightarrow> (0 < a \<and> b < 0) \<or> (a < 0 \<and> 0 < b)"
   2.670 +  by (cases rule: ereal2_cases[of a b]) (simp_all add: mult_less_0_iff)
   2.671 +
   2.672 +lemma ereal_zero_less_0_iff:
   2.673 +  fixes a b :: ereal
   2.674 +  shows "0 < a * b \<longleftrightarrow> (0 < a \<and> 0 < b) \<or> (a < 0 \<and> b < 0)"
   2.675 +  by (cases rule: ereal2_cases[of a b]) (simp_all add: zero_less_mult_iff)
   2.676 +
   2.677 +lemma ereal_distrib:
   2.678 +  fixes a b c :: ereal
   2.679 +  assumes "a \<noteq> \<infinity> \<or> b \<noteq> -\<infinity>" "a \<noteq> -\<infinity> \<or> b \<noteq> \<infinity>" "\<bar>c\<bar> \<noteq> \<infinity>"
   2.680 +  shows "(a + b) * c = a * c + b * c"
   2.681 +  using assms
   2.682 +  by (cases rule: ereal3_cases[of a b c]) (simp_all add: field_simps)
   2.683 +
   2.684 +lemma ereal_le_epsilon:
   2.685 +  fixes x y :: ereal
   2.686 +  assumes "ALL e. 0 < e --> x <= y + e"
   2.687 +  shows "x <= y"
   2.688 +proof-
   2.689 +{ assume a: "EX r. y = ereal r"
   2.690 +  from this obtain r where r_def: "y = ereal r" by auto
   2.691 +  { assume "x=(-\<infinity>)" hence ?thesis by auto }
   2.692 +  moreover
   2.693 +  { assume "~(x=(-\<infinity>))"
   2.694 +    from this obtain p where p_def: "x = ereal p"
   2.695 +    using a assms[rule_format, of 1] by (cases x) auto
   2.696 +    { fix e have "0 < e --> p <= r + e"
   2.697 +      using assms[rule_format, of "ereal e"] p_def r_def by auto }
   2.698 +    hence "p <= r" apply (subst field_le_epsilon) by auto
   2.699 +    hence ?thesis using r_def p_def by auto
   2.700 +  } ultimately have ?thesis by blast
   2.701 +}
   2.702 +moreover
   2.703 +{ assume "y=(-\<infinity>) | y=\<infinity>" hence ?thesis
   2.704 +    using assms[rule_format, of 1] by (cases x) auto
   2.705 +} ultimately show ?thesis by (cases y) auto
   2.706 +qed
   2.707 +
   2.708 +
   2.709 +lemma ereal_le_epsilon2:
   2.710 +  fixes x y :: ereal
   2.711 +  assumes "ALL e. 0 < e --> x <= y + ereal e"
   2.712 +  shows "x <= y"
   2.713 +proof-
   2.714 +{ fix e :: ereal assume "e>0"
   2.715 +  { assume "e=\<infinity>" hence "x<=y+e" by auto }
   2.716 +  moreover
   2.717 +  { assume "e~=\<infinity>"
   2.718 +    from this obtain r where "e = ereal r" using `e>0` apply (cases e) by auto
   2.719 +    hence "x<=y+e" using assms[rule_format, of r] `e>0` by auto
   2.720 +  } ultimately have "x<=y+e" by blast
   2.721 +} from this show ?thesis using ereal_le_epsilon by auto
   2.722 +qed
   2.723 +
   2.724 +lemma ereal_le_real:
   2.725 +  fixes x y :: ereal
   2.726 +  assumes "ALL z. x <= ereal z --> y <= ereal z"
   2.727 +  shows "y <= x"
   2.728 +by (metis assms ereal.exhaust ereal_bot ereal_less_eq(1)
   2.729 +          ereal_less_eq(2) order_refl uminus_ereal.simps(2))
   2.730 +
   2.731 +lemma ereal_le_ereal:
   2.732 +  fixes x y :: ereal
   2.733 +  assumes "\<And>B. B < x \<Longrightarrow> B <= y"
   2.734 +  shows "x <= y"
   2.735 +by (metis assms ereal_dense leD linorder_le_less_linear)
   2.736 +
   2.737 +lemma ereal_ge_ereal:
   2.738 +  fixes x y :: ereal
   2.739 +  assumes "ALL B. B>x --> B >= y"
   2.740 +  shows "x >= y"
   2.741 +by (metis assms ereal_dense leD linorder_le_less_linear)
   2.742 +
   2.743 +lemma setprod_ereal_0:
   2.744 +  fixes f :: "'a \<Rightarrow> ereal"
   2.745 +  shows "(\<Prod>i\<in>A. f i) = 0 \<longleftrightarrow> (finite A \<and> (\<exists>i\<in>A. f i = 0))"
   2.746 +proof cases
   2.747 +  assume "finite A"
   2.748 +  then show ?thesis by (induct A) auto
   2.749 +qed auto
   2.750 +
   2.751 +lemma setprod_ereal_pos:
   2.752 +  fixes f :: "'a \<Rightarrow> ereal" assumes pos: "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i" shows "0 \<le> (\<Prod>i\<in>I. f i)"
   2.753 +proof cases
   2.754 +  assume "finite I" from this pos show ?thesis by induct auto
   2.755 +qed simp
   2.756 +
   2.757 +lemma setprod_PInf:
   2.758 +  assumes "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i"
   2.759 +  shows "(\<Prod>i\<in>I. f i) = \<infinity> \<longleftrightarrow> finite I \<and> (\<exists>i\<in>I. f i = \<infinity>) \<and> (\<forall>i\<in>I. f i \<noteq> 0)"
   2.760 +proof cases
   2.761 +  assume "finite I" from this assms show ?thesis
   2.762 +  proof (induct I)
   2.763 +    case (insert i I)
   2.764 +    then have pos: "0 \<le> f i" "0 \<le> setprod f I" by (auto intro!: setprod_ereal_pos)
   2.765 +    from insert have "(\<Prod>j\<in>insert i I. f j) = \<infinity> \<longleftrightarrow> setprod f I * f i = \<infinity>" by auto
   2.766 +    also have "\<dots> \<longleftrightarrow> (setprod f I = \<infinity> \<or> f i = \<infinity>) \<and> f i \<noteq> 0 \<and> setprod f I \<noteq> 0"
   2.767 +      using setprod_ereal_pos[of I f] pos
   2.768 +      by (cases rule: ereal2_cases[of "f i" "setprod f I"]) auto
   2.769 +    also have "\<dots> \<longleftrightarrow> finite (insert i I) \<and> (\<exists>j\<in>insert i I. f j = \<infinity>) \<and> (\<forall>j\<in>insert i I. f j \<noteq> 0)"
   2.770 +      using insert by (auto simp: setprod_ereal_0)
   2.771 +    finally show ?case .
   2.772 +  qed simp
   2.773 +qed simp
   2.774 +
   2.775 +lemma setprod_ereal: "(\<Prod>i\<in>A. ereal (f i)) = ereal (setprod f A)"
   2.776 +proof cases
   2.777 +  assume "finite A" then show ?thesis
   2.778 +    by induct (auto simp: one_ereal_def)
   2.779 +qed (simp add: one_ereal_def)
   2.780 +
   2.781 +subsubsection {* Power *}
   2.782 +
   2.783 +instantiation ereal :: power
   2.784 +begin
   2.785 +primrec power_ereal where
   2.786 +  "power_ereal x 0 = 1" |
   2.787 +  "power_ereal x (Suc n) = x * x ^ n"
   2.788 +instance ..
   2.789 +end
   2.790 +
   2.791 +lemma ereal_power[simp]: "(ereal x) ^ n = ereal (x^n)"
   2.792 +  by (induct n) (auto simp: one_ereal_def)
   2.793 +
   2.794 +lemma ereal_power_PInf[simp]: "\<infinity> ^ n = (if n = 0 then 1 else \<infinity>)"
   2.795 +  by (induct n) (auto simp: one_ereal_def)
   2.796 +
   2.797 +lemma ereal_power_uminus[simp]:
   2.798 +  fixes x :: ereal
   2.799 +  shows "(- x) ^ n = (if even n then x ^ n else - (x^n))"
   2.800 +  by (induct n) (auto simp: one_ereal_def)
   2.801 +
   2.802 +lemma ereal_power_number_of[simp]:
   2.803 +  "(number_of num :: ereal) ^ n = ereal (number_of num ^ n)"
   2.804 +  by (induct n) (auto simp: one_ereal_def)
   2.805 +
   2.806 +lemma zero_le_power_ereal[simp]:
   2.807 +  fixes a :: ereal assumes "0 \<le> a"
   2.808 +  shows "0 \<le> a ^ n"
   2.809 +  using assms by (induct n) (auto simp: ereal_zero_le_0_iff)
   2.810 +
   2.811 +subsubsection {* Subtraction *}
   2.812 +
   2.813 +lemma ereal_minus_minus_image[simp]:
   2.814 +  fixes S :: "ereal set"
   2.815 +  shows "uminus ` uminus ` S = S"
   2.816 +  by (auto simp: image_iff)
   2.817 +
   2.818 +lemma ereal_uminus_lessThan[simp]:
   2.819 +  fixes a :: ereal shows "uminus ` {..<a} = {-a<..}"
   2.820 +proof (safe intro!: image_eqI)
   2.821 +  fix x assume "-a < x"
   2.822 +  then have "- x < - (- a)" by (simp del: ereal_uminus_uminus)
   2.823 +  then show "- x < a" by simp
   2.824 +qed auto
   2.825 +
   2.826 +lemma ereal_uminus_greaterThan[simp]:
   2.827 +  "uminus ` {(a::ereal)<..} = {..<-a}"
   2.828 +  by (metis ereal_uminus_lessThan ereal_uminus_uminus
   2.829 +            ereal_minus_minus_image)
   2.830 +
   2.831 +instantiation ereal :: minus
   2.832 +begin
   2.833 +definition "x - y = x + -(y::ereal)"
   2.834 +instance ..
   2.835 +end
   2.836 +
   2.837 +lemma ereal_minus[simp]:
   2.838 +  "ereal r - ereal p = ereal (r - p)"
   2.839 +  "-\<infinity> - ereal r = -\<infinity>"
   2.840 +  "ereal r - \<infinity> = -\<infinity>"
   2.841 +  "\<infinity> - x = \<infinity>"
   2.842 +  "-\<infinity> - \<infinity> = -\<infinity>"
   2.843 +  "x - -y = x + y"
   2.844 +  "x - 0 = x"
   2.845 +  "0 - x = -x"
   2.846 +  by (simp_all add: minus_ereal_def)
   2.847 +
   2.848 +lemma ereal_x_minus_x[simp]:
   2.849 +  "x - x = (if \<bar>x\<bar> = \<infinity> then \<infinity> else 0)"
   2.850 +  by (cases x) simp_all
   2.851 +
   2.852 +lemma ereal_eq_minus_iff:
   2.853 +  fixes x y z :: ereal
   2.854 +  shows "x = z - y \<longleftrightarrow>
   2.855 +    (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x + y = z) \<and>
   2.856 +    (y = -\<infinity> \<longrightarrow> x = \<infinity>) \<and>
   2.857 +    (y = \<infinity> \<longrightarrow> z = \<infinity> \<longrightarrow> x = \<infinity>) \<and>
   2.858 +    (y = \<infinity> \<longrightarrow> z \<noteq> \<infinity> \<longrightarrow> x = -\<infinity>)"
   2.859 +  by (cases rule: ereal3_cases[of x y z]) auto
   2.860 +
   2.861 +lemma ereal_eq_minus:
   2.862 +  fixes x y z :: ereal
   2.863 +  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x = z - y \<longleftrightarrow> x + y = z"
   2.864 +  by (auto simp: ereal_eq_minus_iff)
   2.865 +
   2.866 +lemma ereal_less_minus_iff:
   2.867 +  fixes x y z :: ereal
   2.868 +  shows "x < z - y \<longleftrightarrow>
   2.869 +    (y = \<infinity> \<longrightarrow> z = \<infinity> \<and> x \<noteq> \<infinity>) \<and>
   2.870 +    (y = -\<infinity> \<longrightarrow> x \<noteq> \<infinity>) \<and>
   2.871 +    (\<bar>y\<bar> \<noteq> \<infinity>\<longrightarrow> x + y < z)"
   2.872 +  by (cases rule: ereal3_cases[of x y z]) auto
   2.873 +
   2.874 +lemma ereal_less_minus:
   2.875 +  fixes x y z :: ereal
   2.876 +  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x < z - y \<longleftrightarrow> x + y < z"
   2.877 +  by (auto simp: ereal_less_minus_iff)
   2.878 +
   2.879 +lemma ereal_le_minus_iff:
   2.880 +  fixes x y z :: ereal
   2.881 +  shows "x \<le> z - y \<longleftrightarrow>
   2.882 +    (y = \<infinity> \<longrightarrow> z \<noteq> \<infinity> \<longrightarrow> x = -\<infinity>) \<and>
   2.883 +    (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x + y \<le> z)"
   2.884 +  by (cases rule: ereal3_cases[of x y z]) auto
   2.885 +
   2.886 +lemma ereal_le_minus:
   2.887 +  fixes x y z :: ereal
   2.888 +  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x \<le> z - y \<longleftrightarrow> x + y \<le> z"
   2.889 +  by (auto simp: ereal_le_minus_iff)
   2.890 +
   2.891 +lemma ereal_minus_less_iff:
   2.892 +  fixes x y z :: ereal
   2.893 +  shows "x - y < z \<longleftrightarrow>
   2.894 +    y \<noteq> -\<infinity> \<and> (y = \<infinity> \<longrightarrow> x \<noteq> \<infinity> \<and> z \<noteq> -\<infinity>) \<and>
   2.895 +    (y \<noteq> \<infinity> \<longrightarrow> x < z + y)"
   2.896 +  by (cases rule: ereal3_cases[of x y z]) auto
   2.897 +
   2.898 +lemma ereal_minus_less:
   2.899 +  fixes x y z :: ereal
   2.900 +  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x - y < z \<longleftrightarrow> x < z + y"
   2.901 +  by (auto simp: ereal_minus_less_iff)
   2.902 +
   2.903 +lemma ereal_minus_le_iff:
   2.904 +  fixes x y z :: ereal
   2.905 +  shows "x - y \<le> z \<longleftrightarrow>
   2.906 +    (y = -\<infinity> \<longrightarrow> z = \<infinity>) \<and>
   2.907 +    (y = \<infinity> \<longrightarrow> x = \<infinity> \<longrightarrow> z = \<infinity>) \<and>
   2.908 +    (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x \<le> z + y)"
   2.909 +  by (cases rule: ereal3_cases[of x y z]) auto
   2.910 +
   2.911 +lemma ereal_minus_le:
   2.912 +  fixes x y z :: ereal
   2.913 +  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x - y \<le> z \<longleftrightarrow> x \<le> z + y"
   2.914 +  by (auto simp: ereal_minus_le_iff)
   2.915 +
   2.916 +lemma ereal_minus_eq_minus_iff:
   2.917 +  fixes a b c :: ereal
   2.918 +  shows "a - b = a - c \<longleftrightarrow>
   2.919 +    b = c \<or> a = \<infinity> \<or> (a = -\<infinity> \<and> b \<noteq> -\<infinity> \<and> c \<noteq> -\<infinity>)"
   2.920 +  by (cases rule: ereal3_cases[of a b c]) auto
   2.921 +
   2.922 +lemma ereal_add_le_add_iff:
   2.923 +  "c + a \<le> c + b \<longleftrightarrow>
   2.924 +    a \<le> b \<or> c = \<infinity> \<or> (c = -\<infinity> \<and> a \<noteq> \<infinity> \<and> b \<noteq> \<infinity>)"
   2.925 +  by (cases rule: ereal3_cases[of a b c]) (simp_all add: field_simps)
   2.926 +
   2.927 +lemma ereal_mult_le_mult_iff:
   2.928 +  "\<bar>c\<bar> \<noteq> \<infinity> \<Longrightarrow> c * a \<le> c * b \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
   2.929 +  by (cases rule: ereal3_cases[of a b c]) (simp_all add: mult_le_cancel_left)
   2.930 +
   2.931 +lemma ereal_minus_mono:
   2.932 +  fixes A B C D :: ereal assumes "A \<le> B" "D \<le> C"
   2.933 +  shows "A - C \<le> B - D"
   2.934 +  using assms
   2.935 +  by (cases rule: ereal3_cases[case_product ereal_cases, of A B C D]) simp_all
   2.936 +
   2.937 +lemma real_of_ereal_minus:
   2.938 +  "real (a - b) = (if \<bar>a\<bar> = \<infinity> \<or> \<bar>b\<bar> = \<infinity> then 0 else real a - real b)"
   2.939 +  by (cases rule: ereal2_cases[of a b]) auto
   2.940 +
   2.941 +lemma ereal_diff_positive:
   2.942 +  fixes a b :: ereal shows "a \<le> b \<Longrightarrow> 0 \<le> b - a"
   2.943 +  by (cases rule: ereal2_cases[of a b]) auto
   2.944 +
   2.945 +lemma ereal_between:
   2.946 +  fixes x e :: ereal
   2.947 +  assumes "\<bar>x\<bar> \<noteq> \<infinity>" "0 < e"
   2.948 +  shows "x - e < x" "x < x + e"
   2.949 +using assms apply (cases x, cases e) apply auto
   2.950 +using assms by (cases x, cases e) auto
   2.951 +
   2.952 +subsubsection {* Division *}
   2.953 +
   2.954 +instantiation ereal :: inverse
   2.955 +begin
   2.956 +
   2.957 +function inverse_ereal where
   2.958 +"inverse (ereal r) = (if r = 0 then \<infinity> else ereal (inverse r))" |
   2.959 +"inverse \<infinity> = 0" |
   2.960 +"inverse (-\<infinity>) = 0"
   2.961 +  by (auto intro: ereal_cases)
   2.962 +termination by (relation "{}") simp
   2.963 +
   2.964 +definition "x / y = x * inverse (y :: ereal)"
   2.965 +
   2.966 +instance proof qed
   2.967 +end
   2.968 +
   2.969 +lemma real_of_ereal_inverse[simp]:
   2.970 +  fixes a :: ereal
   2.971 +  shows "real (inverse a) = 1 / real a"
   2.972 +  by (cases a) (auto simp: inverse_eq_divide)
   2.973 +
   2.974 +lemma ereal_inverse[simp]:
   2.975 +  "inverse 0 = \<infinity>"
   2.976 +  "inverse (1::ereal) = 1"
   2.977 +  by (simp_all add: one_ereal_def zero_ereal_def)
   2.978 +
   2.979 +lemma ereal_divide[simp]:
   2.980 +  "ereal r / ereal p = (if p = 0 then ereal r * \<infinity> else ereal (r / p))"
   2.981 +  unfolding divide_ereal_def by (auto simp: divide_real_def)
   2.982 +
   2.983 +lemma ereal_divide_same[simp]:
   2.984 +  "x / x = (if \<bar>x\<bar> = \<infinity> \<or> x = 0 then 0 else 1)"
   2.985 +  by (cases x)
   2.986 +     (simp_all add: divide_real_def divide_ereal_def one_ereal_def)
   2.987 +
   2.988 +lemma ereal_inv_inv[simp]:
   2.989 +  "inverse (inverse x) = (if x \<noteq> -\<infinity> then x else \<infinity>)"
   2.990 +  by (cases x) auto
   2.991 +
   2.992 +lemma ereal_inverse_minus[simp]:
   2.993 +  "inverse (- x) = (if x = 0 then \<infinity> else -inverse x)"
   2.994 +  by (cases x) simp_all
   2.995 +
   2.996 +lemma ereal_uminus_divide[simp]:
   2.997 +  fixes x y :: ereal shows "- x / y = - (x / y)"
   2.998 +  unfolding divide_ereal_def by simp
   2.999 +
  2.1000 +lemma ereal_divide_Infty[simp]:
  2.1001 +  "x / \<infinity> = 0" "x / -\<infinity> = 0"
  2.1002 +  unfolding divide_ereal_def by simp_all
  2.1003 +
  2.1004 +lemma ereal_divide_one[simp]:
  2.1005 +  "x / 1 = (x::ereal)"
  2.1006 +  unfolding divide_ereal_def by simp
  2.1007 +
  2.1008 +lemma ereal_divide_ereal[simp]:
  2.1009 +  "\<infinity> / ereal r = (if 0 \<le> r then \<infinity> else -\<infinity>)"
  2.1010 +  unfolding divide_ereal_def by simp
  2.1011 +
  2.1012 +lemma zero_le_divide_ereal[simp]:
  2.1013 +  fixes a :: ereal assumes "0 \<le> a" "0 \<le> b"
  2.1014 +  shows "0 \<le> a / b"
  2.1015 +  using assms by (cases rule: ereal2_cases[of a b]) (auto simp: zero_le_divide_iff)
  2.1016 +
  2.1017 +lemma ereal_le_divide_pos:
  2.1018 +  "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y \<le> z / x \<longleftrightarrow> x * y \<le> z"
  2.1019 +  by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
  2.1020 +
  2.1021 +lemma ereal_divide_le_pos:
  2.1022 +  "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> z / x \<le> y \<longleftrightarrow> z \<le> x * y"
  2.1023 +  by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
  2.1024 +
  2.1025 +lemma ereal_le_divide_neg:
  2.1026 +  "x < 0 \<Longrightarrow> x \<noteq> -\<infinity> \<Longrightarrow> y \<le> z / x \<longleftrightarrow> z \<le> x * y"
  2.1027 +  by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
  2.1028 +
  2.1029 +lemma ereal_divide_le_neg:
  2.1030 +  "x < 0 \<Longrightarrow> x \<noteq> -\<infinity> \<Longrightarrow> z / x \<le> y \<longleftrightarrow> x * y \<le> z"
  2.1031 +  by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
  2.1032 +
  2.1033 +lemma ereal_inverse_antimono_strict:
  2.1034 +  fixes x y :: ereal
  2.1035 +  shows "0 \<le> x \<Longrightarrow> x < y \<Longrightarrow> inverse y < inverse x"
  2.1036 +  by (cases rule: ereal2_cases[of x y]) auto
  2.1037 +
  2.1038 +lemma ereal_inverse_antimono:
  2.1039 +  fixes x y :: ereal
  2.1040 +  shows "0 \<le> x \<Longrightarrow> x <= y \<Longrightarrow> inverse y <= inverse x"
  2.1041 +  by (cases rule: ereal2_cases[of x y]) auto
  2.1042 +
  2.1043 +lemma inverse_inverse_Pinfty_iff[simp]:
  2.1044 +  "inverse x = \<infinity> \<longleftrightarrow> x = 0"
  2.1045 +  by (cases x) auto
  2.1046 +
  2.1047 +lemma ereal_inverse_eq_0:
  2.1048 +  "inverse x = 0 \<longleftrightarrow> x = \<infinity> \<or> x = -\<infinity>"
  2.1049 +  by (cases x) auto
  2.1050 +
  2.1051 +lemma ereal_0_gt_inverse:
  2.1052 +  fixes x :: ereal shows "0 < inverse x \<longleftrightarrow> x \<noteq> \<infinity> \<and> 0 \<le> x"
  2.1053 +  by (cases x) auto
  2.1054 +
  2.1055 +lemma ereal_mult_less_right:
  2.1056 +  assumes "b * a < c * a" "0 < a" "a < \<infinity>"
  2.1057 +  shows "b < c"
  2.1058 +  using assms
  2.1059 +  by (cases rule: ereal3_cases[of a b c])
  2.1060 +     (auto split: split_if_asm simp: zero_less_mult_iff zero_le_mult_iff)
  2.1061 +
  2.1062 +lemma ereal_power_divide:
  2.1063 +  "y \<noteq> 0 \<Longrightarrow> (x / y :: ereal) ^ n = x^n / y^n"
  2.1064 +  by (cases rule: ereal2_cases[of x y])
  2.1065 +     (auto simp: one_ereal_def zero_ereal_def power_divide not_le
  2.1066 +                 power_less_zero_eq zero_le_power_iff)
  2.1067 +
  2.1068 +lemma ereal_le_mult_one_interval:
  2.1069 +  fixes x y :: ereal
  2.1070 +  assumes y: "y \<noteq> -\<infinity>"
  2.1071 +  assumes z: "\<And>z. \<lbrakk> 0 < z ; z < 1 \<rbrakk> \<Longrightarrow> z * x \<le> y"
  2.1072 +  shows "x \<le> y"
  2.1073 +proof (cases x)
  2.1074 +  case PInf with z[of "1 / 2"] show "x \<le> y" by (simp add: one_ereal_def)
  2.1075 +next
  2.1076 +  case (real r) note r = this
  2.1077 +  show "x \<le> y"
  2.1078 +  proof (cases y)
  2.1079 +    case (real p) note p = this
  2.1080 +    have "r \<le> p"
  2.1081 +    proof (rule field_le_mult_one_interval)
  2.1082 +      fix z :: real assume "0 < z" and "z < 1"
  2.1083 +      with z[of "ereal z"]
  2.1084 +      show "z * r \<le> p" using p r by (auto simp: zero_le_mult_iff one_ereal_def)
  2.1085 +    qed
  2.1086 +    then show "x \<le> y" using p r by simp
  2.1087 +  qed (insert y, simp_all)
  2.1088 +qed simp
  2.1089 +
  2.1090 +subsection "Complete lattice"
  2.1091 +
  2.1092 +instantiation ereal :: lattice
  2.1093 +begin
  2.1094 +definition [simp]: "sup x y = (max x y :: ereal)"
  2.1095 +definition [simp]: "inf x y = (min x y :: ereal)"
  2.1096 +instance proof qed simp_all
  2.1097 +end
  2.1098 +
  2.1099 +instantiation ereal :: complete_lattice
  2.1100 +begin
  2.1101 +
  2.1102 +definition "bot = -\<infinity>"
  2.1103 +definition "top = \<infinity>"
  2.1104 +
  2.1105 +definition "Sup S = (LEAST z. ALL x:S. x <= z :: ereal)"
  2.1106 +definition "Inf S = (GREATEST z. ALL x:S. z <= x :: ereal)"
  2.1107 +
  2.1108 +lemma ereal_complete_Sup:
  2.1109 +  fixes S :: "ereal set" assumes "S \<noteq> {}"
  2.1110 +  shows "\<exists>x. (\<forall>y\<in>S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>S. y \<le> z) \<longrightarrow> x \<le> z)"
  2.1111 +proof cases
  2.1112 +  assume "\<exists>x. \<forall>a\<in>S. a \<le> ereal x"
  2.1113 +  then obtain y where y: "\<And>a. a\<in>S \<Longrightarrow> a \<le> ereal y" by auto
  2.1114 +  then have "\<infinity> \<notin> S" by force
  2.1115 +  show ?thesis
  2.1116 +  proof cases
  2.1117 +    assume "S = {-\<infinity>}"
  2.1118 +    then show ?thesis by (auto intro!: exI[of _ "-\<infinity>"])
  2.1119 +  next
  2.1120 +    assume "S \<noteq> {-\<infinity>}"
  2.1121 +    with `S \<noteq> {}` `\<infinity> \<notin> S` obtain x where "x \<in> S - {-\<infinity>}" "x \<noteq> \<infinity>" by auto
  2.1122 +    with y `\<infinity> \<notin> S` have "\<forall>z\<in>real ` (S - {-\<infinity>}). z \<le> y"
  2.1123 +      by (auto simp: real_of_ereal_ord_simps)
  2.1124 +    with reals_complete2[of "real ` (S - {-\<infinity>})"] `x \<in> S - {-\<infinity>}`
  2.1125 +    obtain s where s:
  2.1126 +       "\<forall>y\<in>S - {-\<infinity>}. real y \<le> s" "\<And>z. (\<forall>y\<in>S - {-\<infinity>}. real y \<le> z) \<Longrightarrow> s \<le> z"
  2.1127 +       by auto
  2.1128 +    show ?thesis
  2.1129 +    proof (safe intro!: exI[of _ "ereal s"])
  2.1130 +      fix z assume "z \<in> S" with `\<infinity> \<notin> S` show "z \<le> ereal s"
  2.1131 +      proof (cases z)
  2.1132 +        case (real r)
  2.1133 +        then show ?thesis
  2.1134 +          using s(1)[rule_format, of z] `z \<in> S` `z = ereal r` by auto
  2.1135 +      qed auto
  2.1136 +    next
  2.1137 +      fix z assume *: "\<forall>y\<in>S. y \<le> z"
  2.1138 +      with `S \<noteq> {-\<infinity>}` `S \<noteq> {}` show "ereal s \<le> z"
  2.1139 +      proof (cases z)
  2.1140 +        case (real u)
  2.1141 +        with * have "s \<le> u"
  2.1142 +          by (intro s(2)[of u]) (auto simp: real_of_ereal_ord_simps)
  2.1143 +        then show ?thesis using real by simp
  2.1144 +      qed auto
  2.1145 +    qed
  2.1146 +  qed
  2.1147 +next
  2.1148 +  assume *: "\<not> (\<exists>x. \<forall>a\<in>S. a \<le> ereal x)"
  2.1149 +  show ?thesis
  2.1150 +  proof (safe intro!: exI[of _ \<infinity>])
  2.1151 +    fix y assume **: "\<forall>z\<in>S. z \<le> y"
  2.1152 +    with * show "\<infinity> \<le> y"
  2.1153 +    proof (cases y)
  2.1154 +      case MInf with * ** show ?thesis by (force simp: not_le)
  2.1155 +    qed auto
  2.1156 +  qed simp
  2.1157 +qed
  2.1158 +
  2.1159 +lemma ereal_complete_Inf:
  2.1160 +  fixes S :: "ereal set" assumes "S ~= {}"
  2.1161 +  shows "EX x. (ALL y:S. x <= y) & (ALL z. (ALL y:S. z <= y) --> z <= x)"
  2.1162 +proof-
  2.1163 +def S1 == "uminus ` S"
  2.1164 +hence "S1 ~= {}" using assms by auto
  2.1165 +from this obtain x where x_def: "(ALL y:S1. y <= x) & (ALL z. (ALL y:S1. y <= z) --> x <= z)"
  2.1166 +   using ereal_complete_Sup[of S1] by auto
  2.1167 +{ fix z assume "ALL y:S. z <= y"
  2.1168 +  hence "ALL y:S1. y <= -z" unfolding S1_def by auto
  2.1169 +  hence "x <= -z" using x_def by auto
  2.1170 +  hence "z <= -x"
  2.1171 +    apply (subst ereal_uminus_uminus[symmetric])
  2.1172 +    unfolding ereal_minus_le_minus . }
  2.1173 +moreover have "(ALL y:S. -x <= y)"
  2.1174 +   using x_def unfolding S1_def
  2.1175 +   apply simp
  2.1176 +   apply (subst (3) ereal_uminus_uminus[symmetric])
  2.1177 +   unfolding ereal_minus_le_minus by simp
  2.1178 +ultimately show ?thesis by auto
  2.1179 +qed
  2.1180 +
  2.1181 +lemma ereal_complete_uminus_eq:
  2.1182 +  fixes S :: "ereal set"
  2.1183 +  shows "(\<forall>y\<in>uminus`S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>uminus`S. y \<le> z) \<longrightarrow> x \<le> z)
  2.1184 +     \<longleftrightarrow> (\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> -x)"
  2.1185 +  by simp (metis ereal_minus_le_minus ereal_uminus_uminus)
  2.1186 +
  2.1187 +lemma ereal_Sup_uminus_image_eq:
  2.1188 +  fixes S :: "ereal set"
  2.1189 +  shows "Sup (uminus ` S) = - Inf S"
  2.1190 +proof cases
  2.1191 +  assume "S = {}"
  2.1192 +  moreover have "(THE x. All (op \<le> x)) = (-\<infinity>::ereal)"
  2.1193 +    by (rule the_equality) (auto intro!: ereal_bot)
  2.1194 +  moreover have "(SOME x. \<forall>y. y \<le> x) = (\<infinity>::ereal)"
  2.1195 +    by (rule some_equality) (auto intro!: ereal_top)
  2.1196 +  ultimately show ?thesis unfolding Inf_ereal_def Sup_ereal_def
  2.1197 +    Least_def Greatest_def GreatestM_def by simp
  2.1198 +next
  2.1199 +  assume "S \<noteq> {}"
  2.1200 +  with ereal_complete_Sup[of "uminus`S"]
  2.1201 +  obtain x where x: "(\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> -x)"
  2.1202 +    unfolding ereal_complete_uminus_eq by auto
  2.1203 +  show "Sup (uminus ` S) = - Inf S"
  2.1204 +    unfolding Inf_ereal_def Greatest_def GreatestM_def
  2.1205 +  proof (intro someI2[of _ _ "\<lambda>x. Sup (uminus`S) = - x"])
  2.1206 +    show "(\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>y. (\<forall>z\<in>S. y \<le> z) \<longrightarrow> y \<le> -x)"
  2.1207 +      using x .
  2.1208 +    fix x' assume "(\<forall>y\<in>S. x' \<le> y) \<and> (\<forall>y. (\<forall>z\<in>S. y \<le> z) \<longrightarrow> y \<le> x')"
  2.1209 +    then have "(\<forall>y\<in>uminus`S. y \<le> - x') \<and> (\<forall>y. (\<forall>z\<in>uminus`S. z \<le> y) \<longrightarrow> - x' \<le> y)"
  2.1210 +      unfolding ereal_complete_uminus_eq by simp
  2.1211 +    then show "Sup (uminus ` S) = -x'"
  2.1212 +      unfolding Sup_ereal_def ereal_uminus_eq_iff
  2.1213 +      by (intro Least_equality) auto
  2.1214 +  qed
  2.1215 +qed
  2.1216 +
  2.1217 +instance
  2.1218 +proof
  2.1219 +  { fix x :: ereal and A
  2.1220 +    show "bot <= x" by (cases x) (simp_all add: bot_ereal_def)
  2.1221 +    show "x <= top" by (simp add: top_ereal_def) }
  2.1222 +
  2.1223 +  { fix x :: ereal and A assume "x : A"
  2.1224 +    with ereal_complete_Sup[of A]
  2.1225 +    obtain s where s: "\<forall>y\<in>A. y <= s" "\<forall>z. (\<forall>y\<in>A. y <= z) \<longrightarrow> s <= z" by auto
  2.1226 +    hence "x <= s" using `x : A` by auto
  2.1227 +    also have "... = Sup A" using s unfolding Sup_ereal_def
  2.1228 +      by (auto intro!: Least_equality[symmetric])
  2.1229 +    finally show "x <= Sup A" . }
  2.1230 +  note le_Sup = this
  2.1231 +
  2.1232 +  { fix x :: ereal and A assume *: "!!z. (z : A ==> z <= x)"
  2.1233 +    show "Sup A <= x"
  2.1234 +    proof (cases "A = {}")
  2.1235 +      case True
  2.1236 +      hence "Sup A = -\<infinity>" unfolding Sup_ereal_def
  2.1237 +        by (auto intro!: Least_equality)
  2.1238 +      thus "Sup A <= x" by simp
  2.1239 +    next
  2.1240 +      case False
  2.1241 +      with ereal_complete_Sup[of A]
  2.1242 +      obtain s where s: "\<forall>y\<in>A. y <= s" "\<forall>z. (\<forall>y\<in>A. y <= z) \<longrightarrow> s <= z" by auto
  2.1243 +      hence "Sup A = s"
  2.1244 +        unfolding Sup_ereal_def by (auto intro!: Least_equality)
  2.1245 +      also have "s <= x" using * s by auto
  2.1246 +      finally show "Sup A <= x" .
  2.1247 +    qed }
  2.1248 +  note Sup_le = this
  2.1249 +
  2.1250 +  { fix x :: ereal and A assume "x \<in> A"
  2.1251 +    with le_Sup[of "-x" "uminus`A"] show "Inf A \<le> x"
  2.1252 +      unfolding ereal_Sup_uminus_image_eq by simp }
  2.1253 +
  2.1254 +  { fix x :: ereal and A assume *: "!!z. (z : A ==> x <= z)"
  2.1255 +    with Sup_le[of "uminus`A" "-x"] show "x \<le> Inf A"
  2.1256 +      unfolding ereal_Sup_uminus_image_eq by force }
  2.1257 +qed
  2.1258 +end
  2.1259 +
  2.1260 +lemma ereal_SUPR_uminus:
  2.1261 +  fixes f :: "'a => ereal"
  2.1262 +  shows "(SUP i : R. -(f i)) = -(INF i : R. f i)"
  2.1263 +  unfolding SUPR_def INFI_def
  2.1264 +  using ereal_Sup_uminus_image_eq[of "f`R"]
  2.1265 +  by (simp add: image_image)
  2.1266 +
  2.1267 +lemma ereal_INFI_uminus:
  2.1268 +  fixes f :: "'a => ereal"
  2.1269 +  shows "(INF i : R. -(f i)) = -(SUP i : R. f i)"
  2.1270 +  using ereal_SUPR_uminus[of _ "\<lambda>x. - f x"] by simp
  2.1271 +
  2.1272 +lemma ereal_Inf_uminus_image_eq: "Inf (uminus ` S) = - Sup (S::ereal set)"
  2.1273 +  using ereal_Sup_uminus_image_eq[of "uminus ` S"] by (simp add: image_image)
  2.1274 +
  2.1275 +lemma ereal_inj_on_uminus[intro, simp]: "inj_on uminus (A :: ereal set)"
  2.1276 +  by (auto intro!: inj_onI)
  2.1277 +
  2.1278 +lemma ereal_image_uminus_shift:
  2.1279 +  fixes X Y :: "ereal set" shows "uminus ` X = Y \<longleftrightarrow> X = uminus ` Y"
  2.1280 +proof
  2.1281 +  assume "uminus ` X = Y"
  2.1282 +  then have "uminus ` uminus ` X = uminus ` Y"
  2.1283 +    by (simp add: inj_image_eq_iff)
  2.1284 +  then show "X = uminus ` Y" by (simp add: image_image)
  2.1285 +qed (simp add: image_image)
  2.1286 +
  2.1287 +lemma Inf_ereal_iff:
  2.1288 +  fixes z :: ereal
  2.1289 +  shows "(!!x. x:X ==> z <= x) ==> (EX x:X. x<y) <-> Inf X < y"
  2.1290 +  by (metis complete_lattice_class.Inf_greatest complete_lattice_class.Inf_lower less_le_not_le linear
  2.1291 +            order_less_le_trans)
  2.1292 +
  2.1293 +lemma Sup_eq_MInfty:
  2.1294 +  fixes S :: "ereal set" shows "Sup S = -\<infinity> \<longleftrightarrow> S = {} \<or> S = {-\<infinity>}"
  2.1295 +proof
  2.1296 +  assume a: "Sup S = -\<infinity>"
  2.1297 +  with complete_lattice_class.Sup_upper[of _ S]
  2.1298 +  show "S={} \<or> S={-\<infinity>}" by auto
  2.1299 +next
  2.1300 +  assume "S={} \<or> S={-\<infinity>}" then show "Sup S = -\<infinity>"
  2.1301 +    unfolding Sup_ereal_def by (auto intro!: Least_equality)
  2.1302 +qed
  2.1303 +
  2.1304 +lemma Inf_eq_PInfty:
  2.1305 +  fixes S :: "ereal set" shows "Inf S = \<infinity> \<longleftrightarrow> S = {} \<or> S = {\<infinity>}"
  2.1306 +  using Sup_eq_MInfty[of "uminus`S"]
  2.1307 +  unfolding ereal_Sup_uminus_image_eq ereal_image_uminus_shift by simp
  2.1308 +
  2.1309 +lemma Inf_eq_MInfty: "-\<infinity> : S ==> Inf S = -\<infinity>"
  2.1310 +  unfolding Inf_ereal_def
  2.1311 +  by (auto intro!: Greatest_equality)
  2.1312 +
  2.1313 +lemma Sup_eq_PInfty: "\<infinity> : S ==> Sup S = \<infinity>"
  2.1314 +  unfolding Sup_ereal_def
  2.1315 +  by (auto intro!: Least_equality)
  2.1316 +
  2.1317 +lemma ereal_SUPI:
  2.1318 +  fixes x :: ereal
  2.1319 +  assumes "!!i. i : A ==> f i <= x"
  2.1320 +  assumes "!!y. (!!i. i : A ==> f i <= y) ==> x <= y"
  2.1321 +  shows "(SUP i:A. f i) = x"
  2.1322 +  unfolding SUPR_def Sup_ereal_def
  2.1323 +  using assms by (auto intro!: Least_equality)
  2.1324 +
  2.1325 +lemma ereal_INFI:
  2.1326 +  fixes x :: ereal
  2.1327 +  assumes "!!i. i : A ==> f i >= x"
  2.1328 +  assumes "!!y. (!!i. i : A ==> f i >= y) ==> x >= y"
  2.1329 +  shows "(INF i:A. f i) = x"
  2.1330 +  unfolding INFI_def Inf_ereal_def
  2.1331 +  using assms by (auto intro!: Greatest_equality)
  2.1332 +
  2.1333 +lemma Sup_ereal_close:
  2.1334 +  fixes e :: ereal
  2.1335 +  assumes "0 < e" and S: "\<bar>Sup S\<bar> \<noteq> \<infinity>" "S \<noteq> {}"
  2.1336 +  shows "\<exists>x\<in>S. Sup S - e < x"
  2.1337 +  using assms by (cases e) (auto intro!: less_Sup_iff[THEN iffD1])
  2.1338 +
  2.1339 +lemma Inf_ereal_close:
  2.1340 +  fixes e :: ereal assumes "\<bar>Inf X\<bar> \<noteq> \<infinity>" "0 < e"
  2.1341 +  shows "\<exists>x\<in>X. x < Inf X + e"
  2.1342 +proof (rule Inf_less_iff[THEN iffD1])
  2.1343 +  show "Inf X < Inf X + e" using assms
  2.1344 +    by (cases e) auto
  2.1345 +qed
  2.1346 +
  2.1347 +lemma Sup_eq_top_iff:
  2.1348 +  fixes A :: "'a::{complete_lattice, linorder} set"
  2.1349 +  shows "Sup A = top \<longleftrightarrow> (\<forall>x<top. \<exists>i\<in>A. x < i)"
  2.1350 +proof
  2.1351 +  assume *: "Sup A = top"
  2.1352 +  show "(\<forall>x<top. \<exists>i\<in>A. x < i)" unfolding *[symmetric]
  2.1353 +  proof (intro allI impI)
  2.1354 +    fix x assume "x < Sup A" then show "\<exists>i\<in>A. x < i"
  2.1355 +      unfolding less_Sup_iff by auto
  2.1356 +  qed
  2.1357 +next
  2.1358 +  assume *: "\<forall>x<top. \<exists>i\<in>A. x < i"
  2.1359 +  show "Sup A = top"
  2.1360 +  proof (rule ccontr)
  2.1361 +    assume "Sup A \<noteq> top"
  2.1362 +    with top_greatest[of "Sup A"]
  2.1363 +    have "Sup A < top" unfolding le_less by auto
  2.1364 +    then have "Sup A < Sup A"
  2.1365 +      using * unfolding less_Sup_iff by auto
  2.1366 +    then show False by auto
  2.1367 +  qed
  2.1368 +qed
  2.1369 +
  2.1370 +lemma SUP_eq_top_iff:
  2.1371 +  fixes f :: "'a \<Rightarrow> 'b::{complete_lattice, linorder}"
  2.1372 +  shows "(SUP i:A. f i) = top \<longleftrightarrow> (\<forall>x<top. \<exists>i\<in>A. x < f i)"
  2.1373 +  unfolding SUPR_def Sup_eq_top_iff by auto
  2.1374 +
  2.1375 +lemma SUP_nat_Infty: "(SUP i::nat. ereal (real i)) = \<infinity>"
  2.1376 +proof -
  2.1377 +  { fix x assume "x \<noteq> \<infinity>"
  2.1378 +    then have "\<exists>k::nat. x < ereal (real k)"
  2.1379 +    proof (cases x)
  2.1380 +      case MInf then show ?thesis by (intro exI[of _ 0]) auto
  2.1381 +    next
  2.1382 +      case (real r)
  2.1383 +      moreover obtain k :: nat where "r < real k"
  2.1384 +        using ex_less_of_nat by (auto simp: real_eq_of_nat)
  2.1385 +      ultimately show ?thesis by auto
  2.1386 +    qed simp }
  2.1387 +  then show ?thesis
  2.1388 +    using SUP_eq_top_iff[of UNIV "\<lambda>n::nat. ereal (real n)"]
  2.1389 +    by (auto simp: top_ereal_def)
  2.1390 +qed
  2.1391 +
  2.1392 +lemma ereal_le_Sup:
  2.1393 +  fixes x :: ereal
  2.1394 +  shows "(x <= (SUP i:A. f i)) <-> (ALL y. y < x --> (EX i. i : A & y <= f i))"
  2.1395 +(is "?lhs <-> ?rhs")
  2.1396 +proof-
  2.1397 +{ assume "?rhs"
  2.1398 +  { assume "~(x <= (SUP i:A. f i))" hence "(SUP i:A. f i)<x" by (simp add: not_le)
  2.1399 +    from this obtain y where y_def: "(SUP i:A. f i)<y & y<x" using ereal_dense by auto
  2.1400 +    from this obtain i where "i : A & y <= f i" using `?rhs` by auto
  2.1401 +    hence "y <= (SUP i:A. f i)" using le_SUPI[of i A f] by auto
  2.1402 +    hence False using y_def by auto
  2.1403 +  } hence "?lhs" by auto
  2.1404 +}
  2.1405 +moreover
  2.1406 +{ assume "?lhs" hence "?rhs"
  2.1407 +  by (metis Collect_def Collect_mem_eq SUP_leI assms atLeastatMost_empty atLeastatMost_empty_iff
  2.1408 +      inf_sup_ord(4) linorder_le_cases sup_absorb1 xt1(8))
  2.1409 +} ultimately show ?thesis by auto
  2.1410 +qed
  2.1411 +
  2.1412 +lemma ereal_Inf_le:
  2.1413 +  fixes x :: ereal
  2.1414 +  shows "((INF i:A. f i) <= x) <-> (ALL y. x < y --> (EX i. i : A & f i <= y))"
  2.1415 +(is "?lhs <-> ?rhs")
  2.1416 +proof-
  2.1417 +{ assume "?rhs"
  2.1418 +  { assume "~((INF i:A. f i) <= x)" hence "x < (INF i:A. f i)" by (simp add: not_le)
  2.1419 +    from this obtain y where y_def: "x<y & y<(INF i:A. f i)" using ereal_dense by auto
  2.1420 +    from this obtain i where "i : A & f i <= y" using `?rhs` by auto
  2.1421 +    hence "(INF i:A. f i) <= y" using INF_leI[of i A f] by auto
  2.1422 +    hence False using y_def by auto
  2.1423 +  } hence "?lhs" by auto
  2.1424 +}
  2.1425 +moreover
  2.1426 +{ assume "?lhs" hence "?rhs"
  2.1427 +  by (metis Collect_def Collect_mem_eq le_INFI assms atLeastatMost_empty atLeastatMost_empty_iff
  2.1428 +      inf_sup_ord(4) linorder_le_cases sup_absorb1 xt1(8))
  2.1429 +} ultimately show ?thesis by auto
  2.1430 +qed
  2.1431 +
  2.1432 +lemma Inf_less:
  2.1433 +  fixes x :: ereal
  2.1434 +  assumes "(INF i:A. f i) < x"
  2.1435 +  shows "EX i. i : A & f i <= x"
  2.1436 +proof(rule ccontr)
  2.1437 +  assume "~ (EX i. i : A & f i <= x)"
  2.1438 +  hence "ALL i:A. f i > x" by auto
  2.1439 +  hence "(INF i:A. f i) >= x" apply (subst le_INFI) by auto
  2.1440 +  thus False using assms by auto
  2.1441 +qed
  2.1442 +
  2.1443 +lemma same_INF:
  2.1444 +  assumes "ALL e:A. f e = g e"
  2.1445 +  shows "(INF e:A. f e) = (INF e:A. g e)"
  2.1446 +proof-
  2.1447 +have "f ` A = g ` A" unfolding image_def using assms by auto
  2.1448 +thus ?thesis unfolding INFI_def by auto
  2.1449 +qed
  2.1450 +
  2.1451 +lemma same_SUP:
  2.1452 +  assumes "ALL e:A. f e = g e"
  2.1453 +  shows "(SUP e:A. f e) = (SUP e:A. g e)"
  2.1454 +proof-
  2.1455 +have "f ` A = g ` A" unfolding image_def using assms by auto
  2.1456 +thus ?thesis unfolding SUPR_def by auto
  2.1457 +qed
  2.1458 +
  2.1459 +lemma SUPR_eq:
  2.1460 +  assumes "\<forall>i\<in>A. \<exists>j\<in>B. f i \<le> g j"
  2.1461 +  assumes "\<forall>j\<in>B. \<exists>i\<in>A. g j \<le> f i"
  2.1462 +  shows "(SUP i:A. f i) = (SUP j:B. g j)"
  2.1463 +proof (intro antisym)
  2.1464 +  show "(SUP i:A. f i) \<le> (SUP j:B. g j)"
  2.1465 +    using assms by (metis SUP_leI le_SUPI2)
  2.1466 +  show "(SUP i:B. g i) \<le> (SUP j:A. f j)"
  2.1467 +    using assms by (metis SUP_leI le_SUPI2)
  2.1468 +qed
  2.1469 +
  2.1470 +lemma SUP_ereal_le_addI:
  2.1471 +  assumes "\<And>i. f i + y \<le> z" and "y \<noteq> -\<infinity>"
  2.1472 +  shows "SUPR UNIV f + y \<le> z"
  2.1473 +proof (cases y)
  2.1474 +  case (real r)
  2.1475 +  then have "\<And>i. f i \<le> z - y" using assms by (simp add: ereal_le_minus_iff)
  2.1476 +  then have "SUPR UNIV f \<le> z - y" by (rule SUP_leI)
  2.1477 +  then show ?thesis using real by (simp add: ereal_le_minus_iff)
  2.1478 +qed (insert assms, auto)
  2.1479 +
  2.1480 +lemma SUPR_ereal_add:
  2.1481 +  fixes f g :: "nat \<Rightarrow> ereal"
  2.1482 +  assumes "incseq f" "incseq g" and pos: "\<And>i. f i \<noteq> -\<infinity>" "\<And>i. g i \<noteq> -\<infinity>"
  2.1483 +  shows "(SUP i. f i + g i) = SUPR UNIV f + SUPR UNIV g"
  2.1484 +proof (rule ereal_SUPI)
  2.1485 +  fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> f i + g i \<le> y"
  2.1486 +  have f: "SUPR UNIV f \<noteq> -\<infinity>" using pos
  2.1487 +    unfolding SUPR_def Sup_eq_MInfty by (auto dest: image_eqD)
  2.1488 +  { fix j
  2.1489 +    { fix i
  2.1490 +      have "f i + g j \<le> f i + g (max i j)"
  2.1491 +        using `incseq g`[THEN incseqD] by (rule add_left_mono) auto
  2.1492 +      also have "\<dots> \<le> f (max i j) + g (max i j)"
  2.1493 +        using `incseq f`[THEN incseqD] by (rule add_right_mono) auto
  2.1494 +      also have "\<dots> \<le> y" using * by auto
  2.1495 +      finally have "f i + g j \<le> y" . }
  2.1496 +    then have "SUPR UNIV f + g j \<le> y"
  2.1497 +      using assms(4)[of j] by (intro SUP_ereal_le_addI) auto
  2.1498 +    then have "g j + SUPR UNIV f \<le> y" by (simp add: ac_simps) }
  2.1499 +  then have "SUPR UNIV g + SUPR UNIV f \<le> y"
  2.1500 +    using f by (rule SUP_ereal_le_addI)
  2.1501 +  then show "SUPR UNIV f + SUPR UNIV g \<le> y" by (simp add: ac_simps)
  2.1502 +qed (auto intro!: add_mono le_SUPI)
  2.1503 +
  2.1504 +lemma SUPR_ereal_add_pos:
  2.1505 +  fixes f g :: "nat \<Rightarrow> ereal"
  2.1506 +  assumes inc: "incseq f" "incseq g" and pos: "\<And>i. 0 \<le> f i" "\<And>i. 0 \<le> g i"
  2.1507 +  shows "(SUP i. f i + g i) = SUPR UNIV f + SUPR UNIV g"
  2.1508 +proof (intro SUPR_ereal_add inc)
  2.1509 +  fix i show "f i \<noteq> -\<infinity>" "g i \<noteq> -\<infinity>" using pos[of i] by auto
  2.1510 +qed
  2.1511 +
  2.1512 +lemma SUPR_ereal_setsum:
  2.1513 +  fixes f g :: "'a \<Rightarrow> nat \<Rightarrow> ereal"
  2.1514 +  assumes "\<And>n. n \<in> A \<Longrightarrow> incseq (f n)" and pos: "\<And>n i. n \<in> A \<Longrightarrow> 0 \<le> f n i"
  2.1515 +  shows "(SUP i. \<Sum>n\<in>A. f n i) = (\<Sum>n\<in>A. SUPR UNIV (f n))"
  2.1516 +proof cases
  2.1517 +  assume "finite A" then show ?thesis using assms
  2.1518 +    by induct (auto simp: incseq_setsumI2 setsum_nonneg SUPR_ereal_add_pos)
  2.1519 +qed simp
  2.1520 +
  2.1521 +lemma SUPR_ereal_cmult:
  2.1522 +  fixes f :: "nat \<Rightarrow> ereal" assumes "\<And>i. 0 \<le> f i" "0 \<le> c"
  2.1523 +  shows "(SUP i. c * f i) = c * SUPR UNIV f"
  2.1524 +proof (rule ereal_SUPI)
  2.1525 +  fix i have "f i \<le> SUPR UNIV f" by (rule le_SUPI) auto
  2.1526 +  then show "c * f i \<le> c * SUPR UNIV f"
  2.1527 +    using `0 \<le> c` by (rule ereal_mult_left_mono)
  2.1528 +next
  2.1529 +  fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> c * f i \<le> y"
  2.1530 +  show "c * SUPR UNIV f \<le> y"
  2.1531 +  proof cases
  2.1532 +    assume c: "0 < c \<and> c \<noteq> \<infinity>"
  2.1533 +    with * have "SUPR UNIV f \<le> y / c"
  2.1534 +      by (intro SUP_leI) (auto simp: ereal_le_divide_pos)
  2.1535 +    with c show ?thesis
  2.1536 +      by (auto simp: ereal_le_divide_pos)
  2.1537 +  next
  2.1538 +    { assume "c = \<infinity>" have ?thesis
  2.1539 +      proof cases
  2.1540 +        assume "\<forall>i. f i = 0"
  2.1541 +        moreover then have "range f = {0}" by auto
  2.1542 +        ultimately show "c * SUPR UNIV f \<le> y" using * by (auto simp: SUPR_def)
  2.1543 +      next
  2.1544 +        assume "\<not> (\<forall>i. f i = 0)"
  2.1545 +        then obtain i where "f i \<noteq> 0" by auto
  2.1546 +        with *[of i] `c = \<infinity>` `0 \<le> f i` show ?thesis by (auto split: split_if_asm)
  2.1547 +      qed }
  2.1548 +    moreover assume "\<not> (0 < c \<and> c \<noteq> \<infinity>)"
  2.1549 +    ultimately show ?thesis using * `0 \<le> c` by auto
  2.1550 +  qed
  2.1551 +qed
  2.1552 +
  2.1553 +lemma SUP_PInfty:
  2.1554 +  fixes f :: "'a \<Rightarrow> ereal"
  2.1555 +  assumes "\<And>n::nat. \<exists>i\<in>A. ereal (real n) \<le> f i"
  2.1556 +  shows "(SUP i:A. f i) = \<infinity>"
  2.1557 +  unfolding SUPR_def Sup_eq_top_iff[where 'a=ereal, unfolded top_ereal_def]
  2.1558 +  apply simp
  2.1559 +proof safe
  2.1560 +  fix x assume "x \<noteq> \<infinity>"
  2.1561 +  show "\<exists>i\<in>A. x < f i"
  2.1562 +  proof (cases x)
  2.1563 +    case PInf with `x \<noteq> \<infinity>` show ?thesis by simp
  2.1564 +  next
  2.1565 +    case MInf with assms[of "0"] show ?thesis by force
  2.1566 +  next
  2.1567 +    case (real r)
  2.1568 +    with less_PInf_Ex_of_nat[of x] obtain n :: nat where "x < ereal (real n)" by auto
  2.1569 +    moreover from assms[of n] guess i ..
  2.1570 +    ultimately show ?thesis
  2.1571 +      by (auto intro!: bexI[of _ i])
  2.1572 +  qed
  2.1573 +qed
  2.1574 +
  2.1575 +lemma Sup_countable_SUPR:
  2.1576 +  assumes "A \<noteq> {}"
  2.1577 +  shows "\<exists>f::nat \<Rightarrow> ereal. range f \<subseteq> A \<and> Sup A = SUPR UNIV f"
  2.1578 +proof (cases "Sup A")
  2.1579 +  case (real r)
  2.1580 +  have "\<forall>n::nat. \<exists>x. x \<in> A \<and> Sup A < x + 1 / ereal (real n)"
  2.1581 +  proof
  2.1582 +    fix n ::nat have "\<exists>x\<in>A. Sup A - 1 / ereal (real n) < x"
  2.1583 +      using assms real by (intro Sup_ereal_close) (auto simp: one_ereal_def)
  2.1584 +    then guess x ..
  2.1585 +    then show "\<exists>x. x \<in> A \<and> Sup A < x + 1 / ereal (real n)"
  2.1586 +      by (auto intro!: exI[of _ x] simp: ereal_minus_less_iff)
  2.1587 +  qed
  2.1588 +  from choice[OF this] guess f .. note f = this
  2.1589 +  have "SUPR UNIV f = Sup A"
  2.1590 +  proof (rule ereal_SUPI)
  2.1591 +    fix i show "f i \<le> Sup A" using f
  2.1592 +      by (auto intro!: complete_lattice_class.Sup_upper)
  2.1593 +  next
  2.1594 +    fix y assume bound: "\<And>i. i \<in> UNIV \<Longrightarrow> f i \<le> y"
  2.1595 +    show "Sup A \<le> y"
  2.1596 +    proof (rule ereal_le_epsilon, intro allI impI)
  2.1597 +      fix e :: ereal assume "0 < e"
  2.1598 +      show "Sup A \<le> y + e"
  2.1599 +      proof (cases e)
  2.1600 +        case (real r)
  2.1601 +        hence "0 < r" using `0 < e` by auto
  2.1602 +        then obtain n ::nat where *: "1 / real n < r" "0 < n"
  2.1603 +          using ex_inverse_of_nat_less by (auto simp: real_eq_of_nat inverse_eq_divide)
  2.1604 +        have "Sup A \<le> f n + 1 / ereal (real n)" using f[THEN spec, of n] by auto
  2.1605 +        also have "1 / ereal (real n) \<le> e" using real * by (auto simp: one_ereal_def )
  2.1606 +        with bound have "f n + 1 / ereal (real n) \<le> y + e" by (rule add_mono) simp
  2.1607 +        finally show "Sup A \<le> y + e" .
  2.1608 +      qed (insert `0 < e`, auto)
  2.1609 +    qed
  2.1610 +  qed
  2.1611 +  with f show ?thesis by (auto intro!: exI[of _ f])
  2.1612 +next
  2.1613 +  case PInf
  2.1614 +  from `A \<noteq> {}` obtain x where "x \<in> A" by auto
  2.1615 +  show ?thesis
  2.1616 +  proof cases
  2.1617 +    assume "\<infinity> \<in> A"
  2.1618 +    moreover then have "\<infinity> \<le> Sup A" by (intro complete_lattice_class.Sup_upper)
  2.1619 +    ultimately show ?thesis by (auto intro!: exI[of _ "\<lambda>x. \<infinity>"])
  2.1620 +  next
  2.1621 +    assume "\<infinity> \<notin> A"
  2.1622 +    have "\<exists>x\<in>A. 0 \<le> x"
  2.1623 +      by (metis Infty_neq_0 PInf complete_lattice_class.Sup_least ereal_infty_less_eq2 linorder_linear)
  2.1624 +    then obtain x where "x \<in> A" "0 \<le> x" by auto
  2.1625 +    have "\<forall>n::nat. \<exists>f. f \<in> A \<and> x + ereal (real n) \<le> f"
  2.1626 +    proof (rule ccontr)
  2.1627 +      assume "\<not> ?thesis"
  2.1628 +      then have "\<exists>n::nat. Sup A \<le> x + ereal (real n)"
  2.1629 +        by (simp add: Sup_le_iff not_le less_imp_le Ball_def) (metis less_imp_le)
  2.1630 +      then show False using `x \<in> A` `\<infinity> \<notin> A` PInf
  2.1631 +        by(cases x) auto
  2.1632 +    qed
  2.1633 +    from choice[OF this] guess f .. note f = this
  2.1634 +    have "SUPR UNIV f = \<infinity>"
  2.1635 +    proof (rule SUP_PInfty)
  2.1636 +      fix n :: nat show "\<exists>i\<in>UNIV. ereal (real n) \<le> f i"
  2.1637 +        using f[THEN spec, of n] `0 \<le> x`
  2.1638 +        by (cases rule: ereal2_cases[of "f n" x]) (auto intro!: exI[of _ n])
  2.1639 +    qed
  2.1640 +    then show ?thesis using f PInf by (auto intro!: exI[of _ f])
  2.1641 +  qed
  2.1642 +next
  2.1643 +  case MInf
  2.1644 +  with `A \<noteq> {}` have "A = {-\<infinity>}" by (auto simp: Sup_eq_MInfty)
  2.1645 +  then show ?thesis using MInf by (auto intro!: exI[of _ "\<lambda>x. -\<infinity>"])
  2.1646 +qed
  2.1647 +
  2.1648 +lemma SUPR_countable_SUPR:
  2.1649 +  "A \<noteq> {} \<Longrightarrow> \<exists>f::nat \<Rightarrow> ereal. range f \<subseteq> g`A \<and> SUPR A g = SUPR UNIV f"
  2.1650 +  using Sup_countable_SUPR[of "g`A"] by (auto simp: SUPR_def)
  2.1651 +
  2.1652 +
  2.1653 +lemma Sup_ereal_cadd:
  2.1654 +  fixes A :: "ereal set" assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
  2.1655 +  shows "Sup ((\<lambda>x. a + x) ` A) = a + Sup A"
  2.1656 +proof (rule antisym)
  2.1657 +  have *: "\<And>a::ereal. \<And>A. Sup ((\<lambda>x. a + x) ` A) \<le> a + Sup A"
  2.1658 +    by (auto intro!: add_mono complete_lattice_class.Sup_least complete_lattice_class.Sup_upper)
  2.1659 +  then show "Sup ((\<lambda>x. a + x) ` A) \<le> a + Sup A" .
  2.1660 +  show "a + Sup A \<le> Sup ((\<lambda>x. a + x) ` A)"
  2.1661 +  proof (cases a)
  2.1662 +    case PInf with `A \<noteq> {}` show ?thesis by (auto simp: image_constant)
  2.1663 +  next
  2.1664 +    case (real r)
  2.1665 +    then have **: "op + (- a) ` op + a ` A = A"
  2.1666 +      by (auto simp: image_iff ac_simps zero_ereal_def[symmetric])
  2.1667 +    from *[of "-a" "(\<lambda>x. a + x) ` A"] real show ?thesis unfolding **
  2.1668 +      by (cases rule: ereal2_cases[of "Sup A" "Sup (op + a ` A)"]) auto
  2.1669 +  qed (insert `a \<noteq> -\<infinity>`, auto)
  2.1670 +qed
  2.1671 +
  2.1672 +lemma Sup_ereal_cminus:
  2.1673 +  fixes A :: "ereal set" assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
  2.1674 +  shows "Sup ((\<lambda>x. a - x) ` A) = a - Inf A"
  2.1675 +  using Sup_ereal_cadd[of "uminus ` A" a] assms
  2.1676 +  by (simp add: comp_def image_image minus_ereal_def
  2.1677 +                 ereal_Sup_uminus_image_eq)
  2.1678 +
  2.1679 +lemma SUPR_ereal_cminus:
  2.1680 +  fixes A assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
  2.1681 +  shows "(SUP x:A. a - f x) = a - (INF x:A. f x)"
  2.1682 +  using Sup_ereal_cminus[of "f`A" a] assms
  2.1683 +  unfolding SUPR_def INFI_def image_image by auto
  2.1684 +
  2.1685 +lemma Inf_ereal_cminus:
  2.1686 +  fixes A :: "ereal set" assumes "A \<noteq> {}" and "\<bar>a\<bar> \<noteq> \<infinity>"
  2.1687 +  shows "Inf ((\<lambda>x. a - x) ` A) = a - Sup A"
  2.1688 +proof -
  2.1689 +  { fix x have "-a - -x = -(a - x)" using assms by (cases x) auto }
  2.1690 +  moreover then have "(\<lambda>x. -a - x)`uminus`A = uminus ` (\<lambda>x. a - x) ` A"
  2.1691 +    by (auto simp: image_image)
  2.1692 +  ultimately show ?thesis
  2.1693 +    using Sup_ereal_cminus[of "uminus ` A" "-a"] assms
  2.1694 +    by (auto simp add: ereal_Sup_uminus_image_eq ereal_Inf_uminus_image_eq)
  2.1695 +qed
  2.1696 +
  2.1697 +lemma INFI_ereal_cminus:
  2.1698 +  fixes A assumes "A \<noteq> {}" and "\<bar>a\<bar> \<noteq> \<infinity>"
  2.1699 +  shows "(INF x:A. a - f x) = a - (SUP x:A. f x)"
  2.1700 +  using Inf_ereal_cminus[of "f`A" a] assms
  2.1701 +  unfolding SUPR_def INFI_def image_image
  2.1702 +  by auto
  2.1703 +
  2.1704 +lemma uminus_ereal_add_uminus_uminus:
  2.1705 +  fixes a b :: ereal shows "a \<noteq> \<infinity> \<Longrightarrow> b \<noteq> \<infinity> \<Longrightarrow> - (- a + - b) = a + b"
  2.1706 +  by (cases rule: ereal2_cases[of a b]) auto
  2.1707 +
  2.1708 +lemma INFI_ereal_add:
  2.1709 +  assumes "decseq f" "decseq g" and fin: "\<And>i. f i \<noteq> \<infinity>" "\<And>i. g i \<noteq> \<infinity>"
  2.1710 +  shows "(INF i. f i + g i) = INFI UNIV f + INFI UNIV g"
  2.1711 +proof -
  2.1712 +  have INF_less: "(INF i. f i) < \<infinity>" "(INF i. g i) < \<infinity>"
  2.1713 +    using assms unfolding INF_less_iff by auto
  2.1714 +  { fix i from fin[of i] have "- ((- f i) + (- g i)) = f i + g i"
  2.1715 +      by (rule uminus_ereal_add_uminus_uminus) }
  2.1716 +  then have "(INF i. f i + g i) = (INF i. - ((- f i) + (- g i)))"
  2.1717 +    by simp
  2.1718 +  also have "\<dots> = INFI UNIV f + INFI UNIV g"
  2.1719 +    unfolding ereal_INFI_uminus
  2.1720 +    using assms INF_less
  2.1721 +    by (subst SUPR_ereal_add)
  2.1722 +       (auto simp: ereal_SUPR_uminus intro!: uminus_ereal_add_uminus_uminus)
  2.1723 +  finally show ?thesis .
  2.1724 +qed
  2.1725 +
  2.1726 +subsection "Limits on @{typ ereal}"
  2.1727 +
  2.1728 +subsubsection "Topological space"
  2.1729 +
  2.1730 +instantiation ereal :: topological_space
  2.1731 +begin
  2.1732 +
  2.1733 +definition "open A \<longleftrightarrow> open (ereal -` A)
  2.1734 +       \<and> (\<infinity> \<in> A \<longrightarrow> (\<exists>x. {ereal x <..} \<subseteq> A))
  2.1735 +       \<and> (-\<infinity> \<in> A \<longrightarrow> (\<exists>x. {..<ereal x} \<subseteq> A))"
  2.1736 +
  2.1737 +lemma open_PInfty: "open A \<Longrightarrow> \<infinity> \<in> A \<Longrightarrow> (\<exists>x. {ereal x<..} \<subseteq> A)"
  2.1738 +  unfolding open_ereal_def by auto
  2.1739 +
  2.1740 +lemma open_MInfty: "open A \<Longrightarrow> -\<infinity> \<in> A \<Longrightarrow> (\<exists>x. {..<ereal x} \<subseteq> A)"
  2.1741 +  unfolding open_ereal_def by auto
  2.1742 +
  2.1743 +lemma open_PInfty2: assumes "open A" "\<infinity> \<in> A" obtains x where "{ereal x<..} \<subseteq> A"
  2.1744 +  using open_PInfty[OF assms] by auto
  2.1745 +
  2.1746 +lemma open_MInfty2: assumes "open A" "-\<infinity> \<in> A" obtains x where "{..<ereal x} \<subseteq> A"
  2.1747 +  using open_MInfty[OF assms] by auto
  2.1748 +
  2.1749 +lemma ereal_openE: assumes "open A" obtains x y where
  2.1750 +  "open (ereal -` A)"
  2.1751 +  "\<infinity> \<in> A \<Longrightarrow> {ereal x<..} \<subseteq> A"
  2.1752 +  "-\<infinity> \<in> A \<Longrightarrow> {..<ereal y} \<subseteq> A"
  2.1753 +  using assms open_ereal_def by auto
  2.1754 +
  2.1755 +instance
  2.1756 +proof
  2.1757 +  let ?U = "UNIV::ereal set"
  2.1758 +  show "open ?U" unfolding open_ereal_def
  2.1759 +    by (auto intro!: exI[of _ 0])
  2.1760 +next
  2.1761 +  fix S T::"ereal set" assume "open S" and "open T"
  2.1762 +  from `open S`[THEN ereal_openE] guess xS yS .
  2.1763 +  moreover from `open T`[THEN ereal_openE] guess xT yT .
  2.1764 +  ultimately have
  2.1765 +    "open (ereal -` (S \<inter> T))"
  2.1766 +    "\<infinity> \<in> S \<inter> T \<Longrightarrow> {ereal (max xS xT) <..} \<subseteq> S \<inter> T"
  2.1767 +    "-\<infinity> \<in> S \<inter> T \<Longrightarrow> {..< ereal (min yS yT)} \<subseteq> S \<inter> T"
  2.1768 +    by auto
  2.1769 +  then show "open (S Int T)" unfolding open_ereal_def by blast
  2.1770 +next
  2.1771 +  fix K :: "ereal set set" assume "\<forall>S\<in>K. open S"
  2.1772 +  then have *: "\<forall>S. \<exists>x y. S \<in> K \<longrightarrow> open (ereal -` S) \<and>
  2.1773 +    (\<infinity> \<in> S \<longrightarrow> {ereal x <..} \<subseteq> S) \<and> (-\<infinity> \<in> S \<longrightarrow> {..< ereal y} \<subseteq> S)"
  2.1774 +    by (auto simp: open_ereal_def)
  2.1775 +  then show "open (Union K)" unfolding open_ereal_def
  2.1776 +  proof (intro conjI impI)
  2.1777 +    show "open (ereal -` \<Union>K)"
  2.1778 +      using *[THEN choice] by (auto simp: vimage_Union)
  2.1779 +  qed ((metis UnionE Union_upper subset_trans *)+)
  2.1780 +qed
  2.1781 +end
  2.1782 +
  2.1783 +lemma open_ereal: "open S \<Longrightarrow> open (ereal ` S)"
  2.1784 +  by (auto simp: inj_vimage_image_eq open_ereal_def)
  2.1785 +
  2.1786 +lemma open_ereal_vimage: "open S \<Longrightarrow> open (ereal -` S)"
  2.1787 +  unfolding open_ereal_def by auto
  2.1788 +
  2.1789 +lemma open_ereal_lessThan[intro, simp]: "open {..< a :: ereal}"
  2.1790 +proof -
  2.1791 +  have "\<And>x. ereal -` {..<ereal x} = {..< x}"
  2.1792 +    "ereal -` {..< \<infinity>} = UNIV" "ereal -` {..< -\<infinity>} = {}" by auto
  2.1793 +  then show ?thesis by (cases a) (auto simp: open_ereal_def)
  2.1794 +qed
  2.1795 +
  2.1796 +lemma open_ereal_greaterThan[intro, simp]:
  2.1797 +  "open {a :: ereal <..}"
  2.1798 +proof -
  2.1799 +  have "\<And>x. ereal -` {ereal x<..} = {x<..}"
  2.1800 +    "ereal -` {\<infinity><..} = {}" "ereal -` {-\<infinity><..} = UNIV" by auto
  2.1801 +  then show ?thesis by (cases a) (auto simp: open_ereal_def)
  2.1802 +qed
  2.1803 +
  2.1804 +lemma ereal_open_greaterThanLessThan[intro, simp]: "open {a::ereal <..< b}"
  2.1805 +  unfolding greaterThanLessThan_def by auto
  2.1806 +
  2.1807 +lemma closed_ereal_atLeast[simp, intro]: "closed {a :: ereal ..}"
  2.1808 +proof -
  2.1809 +  have "- {a ..} = {..< a}" by auto
  2.1810 +  then show "closed {a ..}"
  2.1811 +    unfolding closed_def using open_ereal_lessThan by auto
  2.1812 +qed
  2.1813 +
  2.1814 +lemma closed_ereal_atMost[simp, intro]: "closed {.. b :: ereal}"
  2.1815 +proof -
  2.1816 +  have "- {.. b} = {b <..}" by auto
  2.1817 +  then show "closed {.. b}"
  2.1818 +    unfolding closed_def using open_ereal_greaterThan by auto
  2.1819 +qed
  2.1820 +
  2.1821 +lemma closed_ereal_atLeastAtMost[simp, intro]:
  2.1822 +  shows "closed {a :: ereal .. b}"
  2.1823 +  unfolding atLeastAtMost_def by auto
  2.1824 +
  2.1825 +lemma closed_ereal_singleton:
  2.1826 +  "closed {a :: ereal}"
  2.1827 +by (metis atLeastAtMost_singleton closed_ereal_atLeastAtMost)
  2.1828 +
  2.1829 +lemma ereal_open_cont_interval:
  2.1830 +  assumes "open S" "x \<in> S" "\<bar>x\<bar> \<noteq> \<infinity>"
  2.1831 +  obtains e where "e>0" "{x-e <..< x+e} \<subseteq> S"
  2.1832 +proof-
  2.1833 +  from `open S` have "open (ereal -` S)" by (rule ereal_openE)
  2.1834 +  then obtain e where "0 < e" and e: "\<And>y. dist y (real x) < e \<Longrightarrow> ereal y \<in> S"
  2.1835 +    using assms unfolding open_dist by force
  2.1836 +  show thesis
  2.1837 +  proof (intro that subsetI)
  2.1838 +    show "0 < ereal e" using `0 < e` by auto
  2.1839 +    fix y assume "y \<in> {x - ereal e<..<x + ereal e}"
  2.1840 +    with assms obtain t where "y = ereal t" "dist t (real x) < e"
  2.1841 +      apply (cases y) by (auto simp: dist_real_def)
  2.1842 +    then show "y \<in> S" using e[of t] by auto
  2.1843 +  qed
  2.1844 +qed
  2.1845 +
  2.1846 +lemma ereal_open_cont_interval2:
  2.1847 +  assumes "open S" "x \<in> S" and x: "\<bar>x\<bar> \<noteq> \<infinity>"
  2.1848 +  obtains a b where "a < x" "x < b" "{a <..< b} \<subseteq> S"
  2.1849 +proof-
  2.1850 +  guess e using ereal_open_cont_interval[OF assms] .
  2.1851 +  with that[of "x-e" "x+e"] ereal_between[OF x, of e]
  2.1852 +  show thesis by auto
  2.1853 +qed
  2.1854 +
  2.1855 +instance ereal :: t2_space
  2.1856 +proof
  2.1857 +  fix x y :: ereal assume "x ~= y"
  2.1858 +  let "?P x (y::ereal)" = "EX U V. open U & open V & x : U & y : V & U Int V = {}"
  2.1859 +
  2.1860 +  { fix x y :: ereal assume "x < y"
  2.1861 +    from ereal_dense[OF this] obtain z where z: "x < z" "z < y" by auto
  2.1862 +    have "?P x y"
  2.1863 +      apply (rule exI[of _ "{..<z}"])
  2.1864 +      apply (rule exI[of _ "{z<..}"])
  2.1865 +      using z by auto }
  2.1866 +  note * = this
  2.1867 +
  2.1868 +  from `x ~= y`
  2.1869 +  show "EX U V. open U & open V & x : U & y : V & U Int V = {}"
  2.1870 +  proof (cases rule: linorder_cases)
  2.1871 +    assume "x = y" with `x ~= y` show ?thesis by simp
  2.1872 +  next assume "x < y" from *[OF this] show ?thesis by auto
  2.1873 +  next assume "y < x" from *[OF this] show ?thesis by auto
  2.1874 +  qed
  2.1875 +qed
  2.1876 +
  2.1877 +subsubsection {* Convergent sequences *}
  2.1878 +
  2.1879 +lemma lim_ereal[simp]:
  2.1880 +  "((\<lambda>n. ereal (f n)) ---> ereal x) net \<longleftrightarrow> (f ---> x) net" (is "?l = ?r")
  2.1881 +proof (intro iffI topological_tendstoI)
  2.1882 +  fix S assume "?l" "open S" "x \<in> S"
  2.1883 +  then show "eventually (\<lambda>x. f x \<in> S) net"
  2.1884 +    using `?l`[THEN topological_tendstoD, OF open_ereal, OF `open S`]
  2.1885 +    by (simp add: inj_image_mem_iff)
  2.1886 +next
  2.1887 +  fix S assume "?r" "open S" "ereal x \<in> S"
  2.1888 +  show "eventually (\<lambda>x. ereal (f x) \<in> S) net"
  2.1889 +    using `?r`[THEN topological_tendstoD, OF open_ereal_vimage, OF `open S`]
  2.1890 +    using `ereal x \<in> S` by auto
  2.1891 +qed
  2.1892 +
  2.1893 +lemma lim_real_of_ereal[simp]:
  2.1894 +  assumes lim: "(f ---> ereal x) net"
  2.1895 +  shows "((\<lambda>x. real (f x)) ---> x) net"
  2.1896 +proof (intro topological_tendstoI)
  2.1897 +  fix S assume "open S" "x \<in> S"
  2.1898 +  then have S: "open S" "ereal x \<in> ereal ` S"
  2.1899 +    by (simp_all add: inj_image_mem_iff)
  2.1900 +  have "\<forall>x. f x \<in> ereal ` S \<longrightarrow> real (f x) \<in> S" by auto
  2.1901 +  from this lim[THEN topological_tendstoD, OF open_ereal, OF S]
  2.1902 +  show "eventually (\<lambda>x. real (f x) \<in> S) net"
  2.1903 +    by (rule eventually_mono)
  2.1904 +qed
  2.1905 +
  2.1906 +lemma Lim_PInfty: "f ----> \<infinity> <-> (ALL B. EX N. ALL n>=N. f n >= ereal B)" (is "?l = ?r")
  2.1907 +proof assume ?r show ?l apply(rule topological_tendstoI)
  2.1908 +    unfolding eventually_sequentially
  2.1909 +  proof- fix S assume "open S" "\<infinity> : S"
  2.1910 +    from open_PInfty[OF this] guess B .. note B=this
  2.1911 +    from `?r`[rule_format,of "B+1"] guess N .. note N=this
  2.1912 +    show "EX N. ALL n>=N. f n : S" apply(rule_tac x=N in exI)
  2.1913 +    proof safe case goal1
  2.1914 +      have "ereal B < ereal (B + 1)" by auto
  2.1915 +      also have "... <= f n" using goal1 N by auto
  2.1916 +      finally show ?case using B by fastsimp
  2.1917 +    qed
  2.1918 +  qed
  2.1919 +next assume ?l show ?r
  2.1920 +  proof fix B::real have "open {ereal B<..}" "\<infinity> : {ereal B<..}" by auto
  2.1921 +    from topological_tendstoD[OF `?l` this,unfolded eventually_sequentially]
  2.1922 +    guess N .. note N=this
  2.1923 +    show "EX N. ALL n>=N. ereal B <= f n" apply(rule_tac x=N in exI) using N by auto
  2.1924 +  qed
  2.1925 +qed
  2.1926 +
  2.1927 +
  2.1928 +lemma Lim_MInfty: "f ----> (-\<infinity>) <-> (ALL B. EX N. ALL n>=N. f n <= ereal B)" (is "?l = ?r")
  2.1929 +proof assume ?r show ?l apply(rule topological_tendstoI)
  2.1930 +    unfolding eventually_sequentially
  2.1931 +  proof- fix S assume "open S" "(-\<infinity>) : S"
  2.1932 +    from open_MInfty[OF this] guess B .. note B=this
  2.1933 +    from `?r`[rule_format,of "B-(1::real)"] guess N .. note N=this
  2.1934 +    show "EX N. ALL n>=N. f n : S" apply(rule_tac x=N in exI)
  2.1935 +    proof safe case goal1
  2.1936 +      have "ereal (B - 1) >= f n" using goal1 N by auto
  2.1937 +      also have "... < ereal B" by auto
  2.1938 +      finally show ?case using B by fastsimp
  2.1939 +    qed
  2.1940 +  qed
  2.1941 +next assume ?l show ?r
  2.1942 +  proof fix B::real have "open {..<ereal B}" "(-\<infinity>) : {..<ereal B}" by auto
  2.1943 +    from topological_tendstoD[OF `?l` this,unfolded eventually_sequentially]
  2.1944 +    guess N .. note N=this
  2.1945 +    show "EX N. ALL n>=N. ereal B >= f n" apply(rule_tac x=N in exI) using N by auto
  2.1946 +  qed
  2.1947 +qed
  2.1948 +
  2.1949 +
  2.1950 +lemma Lim_bounded_PInfty: assumes lim:"f ----> l" and "!!n. f n <= ereal B" shows "l ~= \<infinity>"
  2.1951 +proof(rule ccontr,unfold not_not) let ?B = "B + 1" assume as:"l=\<infinity>"
  2.1952 +  from lim[unfolded this Lim_PInfty,rule_format,of "?B"]
  2.1953 +  guess N .. note N=this[rule_format,OF le_refl]
  2.1954 +  hence "ereal ?B <= ereal B" using assms(2)[of N] by(rule order_trans)
  2.1955 +  hence "ereal ?B < ereal ?B" apply (rule le_less_trans) by auto
  2.1956 +  thus False by auto
  2.1957 +qed
  2.1958 +
  2.1959 +
  2.1960 +lemma Lim_bounded_MInfty: assumes lim:"f ----> l" and "!!n. f n >= ereal B" shows "l ~= (-\<infinity>)"
  2.1961 +proof(rule ccontr,unfold not_not) let ?B = "B - 1" assume as:"l=(-\<infinity>)"
  2.1962 +  from lim[unfolded this Lim_MInfty,rule_format,of "?B"]
  2.1963 +  guess N .. note N=this[rule_format,OF le_refl]
  2.1964 +  hence "ereal B <= ereal ?B" using assms(2)[of N] order_trans[of "ereal B" "f N" "ereal(B - 1)"] by blast
  2.1965 +  thus False by auto
  2.1966 +qed
  2.1967 +
  2.1968 +
  2.1969 +lemma tendsto_explicit:
  2.1970 +  "f ----> f0 <-> (ALL S. open S --> f0 : S --> (EX N. ALL n>=N. f n : S))"
  2.1971 +  unfolding tendsto_def eventually_sequentially by auto
  2.1972 +
  2.1973 +
  2.1974 +lemma tendsto_obtains_N:
  2.1975 +  assumes "f ----> f0"
  2.1976 +  assumes "open S" "f0 : S"
  2.1977 +  obtains N where "ALL n>=N. f n : S"
  2.1978 +  using tendsto_explicit[of f f0] assms by auto
  2.1979 +
  2.1980 +
  2.1981 +lemma tail_same_limit:
  2.1982 +  fixes X Y N
  2.1983 +  assumes "X ----> L" "ALL n>=N. X n = Y n"
  2.1984 +  shows "Y ----> L"
  2.1985 +proof-
  2.1986 +{ fix S assume "open S" and "L:S"
  2.1987 +  from this obtain N1 where "ALL n>=N1. X n : S"
  2.1988 +     using assms unfolding tendsto_def eventually_sequentially by auto
  2.1989 +  hence "ALL n>=max N N1. Y n : S" using assms by auto
  2.1990 +  hence "EX N. ALL n>=N. Y n : S" apply(rule_tac x="max N N1" in exI) by auto
  2.1991 +}
  2.1992 +thus ?thesis using tendsto_explicit by auto
  2.1993 +qed
  2.1994 +
  2.1995 +
  2.1996 +lemma Lim_bounded_PInfty2:
  2.1997 +assumes lim:"f ----> l" and "ALL n>=N. f n <= ereal B"
  2.1998 +shows "l ~= \<infinity>"
  2.1999 +proof-
  2.2000 +  def g == "(%n. if n>=N then f n else ereal B)"
  2.2001 +  hence "g ----> l" using tail_same_limit[of f l N g] lim by auto
  2.2002 +  moreover have "!!n. g n <= ereal B" using g_def assms by auto
  2.2003 +  ultimately show ?thesis using  Lim_bounded_PInfty by auto
  2.2004 +qed
  2.2005 +
  2.2006 +lemma Lim_bounded_ereal:
  2.2007 +  assumes lim:"f ----> (l :: ereal)"
  2.2008 +  and "ALL n>=M. f n <= C"
  2.2009 +  shows "l<=C"
  2.2010 +proof-
  2.2011 +{ assume "l=(-\<infinity>)" hence ?thesis by auto }
  2.2012 +moreover
  2.2013 +{ assume "~(l=(-\<infinity>))"
  2.2014 +  { assume "C=\<infinity>" hence ?thesis by auto }
  2.2015 +  moreover
  2.2016 +  { assume "C=(-\<infinity>)" hence "ALL n>=M. f n = (-\<infinity>)" using assms by auto
  2.2017 +    hence "l=(-\<infinity>)" using assms
  2.2018 +       tendsto_unique[OF trivial_limit_sequentially] tail_same_limit[of "\<lambda>n. -\<infinity>" "-\<infinity>" M f, OF tendsto_const] by auto
  2.2019 +    hence ?thesis by auto }
  2.2020 +  moreover
  2.2021 +  { assume "EX B. C = ereal B"
  2.2022 +    from this obtain B where B_def: "C=ereal B" by auto
  2.2023 +    hence "~(l=\<infinity>)" using Lim_bounded_PInfty2 assms by auto
  2.2024 +    from this obtain m where m_def: "ereal m=l" using `~(l=(-\<infinity>))` by (cases l) auto
  2.2025 +    from this obtain N where N_def: "ALL n>=N. f n : {ereal(m - 1) <..< ereal(m+1)}"
  2.2026 +       apply (subst tendsto_obtains_N[of f l "{ereal(m - 1) <..< ereal(m+1)}"]) using assms by auto
  2.2027 +    { fix n assume "n>=N"
  2.2028 +      hence "EX r. ereal r = f n" using N_def by (cases "f n") auto
  2.2029 +    } from this obtain g where g_def: "ALL n>=N. ereal (g n) = f n" by metis
  2.2030 +    hence "(%n. ereal (g n)) ----> l" using tail_same_limit[of f l N] assms by auto
  2.2031 +    hence *: "(%n. g n) ----> m" using m_def by auto
  2.2032 +    { fix n assume "n>=max N M"
  2.2033 +      hence "ereal (g n) <= ereal B" using assms g_def B_def by auto
  2.2034 +      hence "g n <= B" by auto
  2.2035 +    } hence "EX N. ALL n>=N. g n <= B" by blast
  2.2036 +    hence "m<=B" using * LIMSEQ_le_const2[of g m B] by auto
  2.2037 +    hence ?thesis using m_def B_def by auto
  2.2038 +  } ultimately have ?thesis by (cases C) auto
  2.2039 +} ultimately show ?thesis by blast
  2.2040 +qed
  2.2041 +
  2.2042 +lemma real_of_ereal_mult[simp]:
  2.2043 +  fixes a b :: ereal shows "real (a * b) = real a * real b"
  2.2044 +  by (cases rule: ereal2_cases[of a b]) auto
  2.2045 +
  2.2046 +lemma real_of_ereal_eq_0:
  2.2047 +  "real x = 0 \<longleftrightarrow> x = \<infinity> \<or> x = -\<infinity> \<or> x = 0"
  2.2048 +  by (cases x) auto
  2.2049 +
  2.2050 +lemma tendsto_ereal_realD:
  2.2051 +  fixes f :: "'a \<Rightarrow> ereal"
  2.2052 +  assumes "x \<noteq> 0" and tendsto: "((\<lambda>x. ereal (real (f x))) ---> x) net"
  2.2053 +  shows "(f ---> x) net"
  2.2054 +proof (intro topological_tendstoI)
  2.2055 +  fix S assume S: "open S" "x \<in> S"
  2.2056 +  with `x \<noteq> 0` have "open (S - {0})" "x \<in> S - {0}" by auto
  2.2057 +  from tendsto[THEN topological_tendstoD, OF this]
  2.2058 +  show "eventually (\<lambda>x. f x \<in> S) net"
  2.2059 +    by (rule eventually_rev_mp) (auto simp: ereal_real real_of_ereal_0)
  2.2060 +qed
  2.2061 +
  2.2062 +lemma tendsto_ereal_realI:
  2.2063 +  fixes f :: "'a \<Rightarrow> ereal"
  2.2064 +  assumes x: "\<bar>x\<bar> \<noteq> \<infinity>" and tendsto: "(f ---> x) net"
  2.2065 +  shows "((\<lambda>x. ereal (real (f x))) ---> x) net"
  2.2066 +proof (intro topological_tendstoI)
  2.2067 +  fix S assume "open S" "x \<in> S"
  2.2068 +  with x have "open (S - {\<infinity>, -\<infinity>})" "x \<in> S - {\<infinity>, -\<infinity>}" by auto
  2.2069 +  from tendsto[THEN topological_tendstoD, OF this]
  2.2070 +  show "eventually (\<lambda>x. ereal (real (f x)) \<in> S) net"
  2.2071 +    by (elim eventually_elim1) (auto simp: ereal_real)
  2.2072 +qed
  2.2073 +
  2.2074 +lemma ereal_mult_cancel_left:
  2.2075 +  fixes a b c :: ereal shows "a * b = a * c \<longleftrightarrow>
  2.2076 +    ((\<bar>a\<bar> = \<infinity> \<and> 0 < b * c) \<or> a = 0 \<or> b = c)"
  2.2077 +  by (cases rule: ereal3_cases[of a b c])
  2.2078 +     (simp_all add: zero_less_mult_iff)
  2.2079 +
  2.2080 +lemma ereal_inj_affinity:
  2.2081 +  assumes "\<bar>m\<bar> \<noteq> \<infinity>" "m \<noteq> 0" "\<bar>t\<bar> \<noteq> \<infinity>"
  2.2082 +  shows "inj_on (\<lambda>x. m * x + t) A"
  2.2083 +  using assms
  2.2084 +  by (cases rule: ereal2_cases[of m t])
  2.2085 +     (auto intro!: inj_onI simp: ereal_add_cancel_right ereal_mult_cancel_left)
  2.2086 +
  2.2087 +lemma ereal_PInfty_eq_plus[simp]:
  2.2088 +  shows "\<infinity> = a + b \<longleftrightarrow> a = \<infinity> \<or> b = \<infinity>"
  2.2089 +  by (cases rule: ereal2_cases[of a b]) auto
  2.2090 +
  2.2091 +lemma ereal_MInfty_eq_plus[simp]:
  2.2092 +  shows "-\<infinity> = a + b \<longleftrightarrow> (a = -\<infinity> \<and> b \<noteq> \<infinity>) \<or> (b = -\<infinity> \<and> a \<noteq> \<infinity>)"
  2.2093 +  by (cases rule: ereal2_cases[of a b]) auto
  2.2094 +
  2.2095 +lemma ereal_less_divide_pos:
  2.2096 +  "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y < z / x \<longleftrightarrow> x * y < z"
  2.2097 +  by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
  2.2098 +
  2.2099 +lemma ereal_divide_less_pos:
  2.2100 +  "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y / x < z \<longleftrightarrow> y < x * z"
  2.2101 +  by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
  2.2102 +
  2.2103 +lemma ereal_divide_eq:
  2.2104 +  "b \<noteq> 0 \<Longrightarrow> \<bar>b\<bar> \<noteq> \<infinity> \<Longrightarrow> a / b = c \<longleftrightarrow> a = b * c"
  2.2105 +  by (cases rule: ereal3_cases[of a b c])
  2.2106 +     (simp_all add: field_simps)
  2.2107 +
  2.2108 +lemma ereal_inverse_not_MInfty[simp]: "inverse a \<noteq> -\<infinity>"
  2.2109 +  by (cases a) auto
  2.2110 +
  2.2111 +lemma ereal_mult_m1[simp]: "x * ereal (-1) = -x"
  2.2112 +  by (cases x) auto
  2.2113 +
  2.2114 +lemma ereal_LimI_finite:
  2.2115 +  assumes "\<bar>x\<bar> \<noteq> \<infinity>"
  2.2116 +  assumes "!!r. 0 < r ==> EX N. ALL n>=N. u n < x + r & x < u n + r"
  2.2117 +  shows "u ----> x"
  2.2118 +proof (rule topological_tendstoI, unfold eventually_sequentially)
  2.2119 +  obtain rx where rx_def: "x=ereal rx" using assms by (cases x) auto
  2.2120 +  fix S assume "open S" "x : S"
  2.2121 +  then have "open (ereal -` S)" unfolding open_ereal_def by auto
  2.2122 +  with `x \<in> S` obtain r where "0 < r" and dist: "!!y. dist y rx < r ==> ereal y \<in> S"
  2.2123 +    unfolding open_real_def rx_def by auto
  2.2124 +  then obtain n where
  2.2125 +    upper: "!!N. n <= N ==> u N < x + ereal r" and
  2.2126 +    lower: "!!N. n <= N ==> x < u N + ereal r" using assms(2)[of "ereal r"] by auto
  2.2127 +  show "EX N. ALL n>=N. u n : S"
  2.2128 +  proof (safe intro!: exI[of _ n])
  2.2129 +    fix N assume "n <= N"
  2.2130 +    from upper[OF this] lower[OF this] assms `0 < r`
  2.2131 +    have "u N ~: {\<infinity>,(-\<infinity>)}" by auto
  2.2132 +    from this obtain ra where ra_def: "(u N) = ereal ra" by (cases "u N") auto
  2.2133 +    hence "rx < ra + r" and "ra < rx + r"
  2.2134 +       using rx_def assms `0 < r` lower[OF `n <= N`] upper[OF `n <= N`] by auto
  2.2135 +    hence "dist (real (u N)) rx < r"
  2.2136 +      using rx_def ra_def
  2.2137 +      by (auto simp: dist_real_def abs_diff_less_iff field_simps)
  2.2138 +    from dist[OF this] show "u N : S" using `u N  ~: {\<infinity>, -\<infinity>}`
  2.2139 +      by (auto simp: ereal_real split: split_if_asm)
  2.2140 +  qed
  2.2141 +qed
  2.2142 +
  2.2143 +lemma ereal_LimI_finite_iff:
  2.2144 +  assumes "\<bar>x\<bar> \<noteq> \<infinity>"
  2.2145 +  shows "u ----> x <-> (ALL r. 0 < r --> (EX N. ALL n>=N. u n < x + r & x < u n + r))"
  2.2146 +  (is "?lhs <-> ?rhs")
  2.2147 +proof
  2.2148 +  assume lim: "u ----> x"
  2.2149 +  { fix r assume "(r::ereal)>0"
  2.2150 +    from this obtain N where N_def: "ALL n>=N. u n : {x - r <..< x + r}"
  2.2151 +       apply (subst tendsto_obtains_N[of u x "{x - r <..< x + r}"])
  2.2152 +       using lim ereal_between[of x r] assms `r>0` by auto
  2.2153 +    hence "EX N. ALL n>=N. u n < x + r & x < u n + r"
  2.2154 +      using ereal_minus_less[of r x] by (cases r) auto
  2.2155 +  } then show "?rhs" by auto
  2.2156 +next
  2.2157 +  assume ?rhs then show "u ----> x"
  2.2158 +    using ereal_LimI_finite[of x] assms by auto
  2.2159 +qed
  2.2160 +
  2.2161 +
  2.2162 +subsubsection {* @{text Liminf} and @{text Limsup} *}
  2.2163 +
  2.2164 +definition
  2.2165 +  "Liminf net f = (GREATEST l. \<forall>y<l. eventually (\<lambda>x. y < f x) net)"
  2.2166 +
  2.2167 +definition
  2.2168 +  "Limsup net f = (LEAST l. \<forall>y>l. eventually (\<lambda>x. f x < y) net)"
  2.2169 +
  2.2170 +lemma Liminf_Sup:
  2.2171 +  fixes f :: "'a => 'b::{complete_lattice, linorder}"
  2.2172 +  shows "Liminf net f = Sup {l. \<forall>y<l. eventually (\<lambda>x. y < f x) net}"
  2.2173 +  by (auto intro!: Greatest_equality complete_lattice_class.Sup_upper simp: less_Sup_iff Liminf_def)
  2.2174 +
  2.2175 +lemma Limsup_Inf:
  2.2176 +  fixes f :: "'a => 'b::{complete_lattice, linorder}"
  2.2177 +  shows "Limsup net f = Inf {l. \<forall>y>l. eventually (\<lambda>x. f x < y) net}"
  2.2178 +  by (auto intro!: Least_equality complete_lattice_class.Inf_lower simp: Inf_less_iff Limsup_def)
  2.2179 +
  2.2180 +lemma ereal_SupI:
  2.2181 +  fixes x :: ereal
  2.2182 +  assumes "\<And>y. y \<in> A \<Longrightarrow> y \<le> x"
  2.2183 +  assumes "\<And>y. (\<And>z. z \<in> A \<Longrightarrow> z \<le> y) \<Longrightarrow> x \<le> y"
  2.2184 +  shows "Sup A = x"
  2.2185 +  unfolding Sup_ereal_def
  2.2186 +  using assms by (auto intro!: Least_equality)
  2.2187 +
  2.2188 +lemma ereal_InfI:
  2.2189 +  fixes x :: ereal
  2.2190 +  assumes "\<And>i. i \<in> A \<Longrightarrow> x \<le> i"
  2.2191 +  assumes "\<And>y. (\<And>i. i \<in> A \<Longrightarrow> y \<le> i) \<Longrightarrow> y \<le> x"
  2.2192 +  shows "Inf A = x"
  2.2193 +  unfolding Inf_ereal_def
  2.2194 +  using assms by (auto intro!: Greatest_equality)
  2.2195 +
  2.2196 +lemma Limsup_const:
  2.2197 +  fixes c :: "'a::{complete_lattice, linorder}"
  2.2198 +  assumes ntriv: "\<not> trivial_limit net"
  2.2199 +  shows "Limsup net (\<lambda>x. c) = c"
  2.2200 +  unfolding Limsup_Inf
  2.2201 +proof (safe intro!: antisym complete_lattice_class.Inf_greatest complete_lattice_class.Inf_lower)
  2.2202 +  fix x assume *: "\<forall>y>x. eventually (\<lambda>_. c < y) net"
  2.2203 +  show "c \<le> x"
  2.2204 +  proof (rule ccontr)
  2.2205 +    assume "\<not> c \<le> x" then have "x < c" by auto
  2.2206 +    then show False using ntriv * by (auto simp: trivial_limit_def)
  2.2207 +  qed
  2.2208 +qed auto
  2.2209 +
  2.2210 +lemma Liminf_const:
  2.2211 +  fixes c :: "'a::{complete_lattice, linorder}"
  2.2212 +  assumes ntriv: "\<not> trivial_limit net"
  2.2213 +  shows "Liminf net (\<lambda>x. c) = c"
  2.2214 +  unfolding Liminf_Sup
  2.2215 +proof (safe intro!: antisym complete_lattice_class.Sup_least complete_lattice_class.Sup_upper)
  2.2216 +  fix x assume *: "\<forall>y<x. eventually (\<lambda>_. y < c) net"
  2.2217 +  show "x \<le> c"
  2.2218 +  proof (rule ccontr)
  2.2219 +    assume "\<not> x \<le> c" then have "c < x" by auto
  2.2220 +    then show False using ntriv * by (auto simp: trivial_limit_def)
  2.2221 +  qed
  2.2222 +qed auto
  2.2223 +
  2.2224 +lemma mono_set:
  2.2225 +  fixes S :: "('a::order) set"
  2.2226 +  shows "mono S \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> x \<in> S \<longrightarrow> y \<in> S)"
  2.2227 +  by (auto simp: mono_def mem_def)
  2.2228 +
  2.2229 +lemma mono_greaterThan[intro, simp]: "mono {B<..}" unfolding mono_set by auto
  2.2230 +lemma mono_atLeast[intro, simp]: "mono {B..}" unfolding mono_set by auto
  2.2231 +lemma mono_UNIV[intro, simp]: "mono UNIV" unfolding mono_set by auto
  2.2232 +lemma mono_empty[intro, simp]: "mono {}" unfolding mono_set by auto
  2.2233 +
  2.2234 +lemma mono_set_iff:
  2.2235 +  fixes S :: "'a::{linorder,complete_lattice} set"
  2.2236 +  defines "a \<equiv> Inf S"
  2.2237 +  shows "mono S \<longleftrightarrow> (S = {a <..} \<or> S = {a..})" (is "_ = ?c")
  2.2238 +proof
  2.2239 +  assume "mono S"
  2.2240 +  then have mono: "\<And>x y. x \<le> y \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S" by (auto simp: mono_set)
  2.2241 +  show ?c
  2.2242 +  proof cases
  2.2243 +    assume "a \<in> S"
  2.2244 +    show ?c
  2.2245 +      using mono[OF _ `a \<in> S`]
  2.2246 +      by (auto intro: complete_lattice_class.Inf_lower simp: a_def)
  2.2247 +  next
  2.2248 +    assume "a \<notin> S"
  2.2249 +    have "S = {a <..}"
  2.2250 +    proof safe
  2.2251 +      fix x assume "x \<in> S"
  2.2252 +      then have "a \<le> x" unfolding a_def by (rule complete_lattice_class.Inf_lower)
  2.2253 +      then show "a < x" using `x \<in> S` `a \<notin> S` by (cases "a = x") auto
  2.2254 +    next
  2.2255 +      fix x assume "a < x"
  2.2256 +      then obtain y where "y < x" "y \<in> S" unfolding a_def Inf_less_iff ..
  2.2257 +      with mono[of y x] show "x \<in> S" by auto
  2.2258 +    qed
  2.2259 +    then show ?c ..
  2.2260 +  qed
  2.2261 +qed auto
  2.2262 +
  2.2263 +lemma lim_imp_Liminf:
  2.2264 +  fixes f :: "'a \<Rightarrow> ereal"
  2.2265 +  assumes ntriv: "\<not> trivial_limit net"
  2.2266 +  assumes lim: "(f ---> f0) net"
  2.2267 +  shows "Liminf net f = f0"
  2.2268 +  unfolding Liminf_Sup
  2.2269 +proof (safe intro!: ereal_SupI)
  2.2270 +  fix y assume *: "\<forall>y'<y. eventually (\<lambda>x. y' < f x) net"
  2.2271 +  show "y \<le> f0"
  2.2272 +  proof (rule ereal_le_ereal)
  2.2273 +    fix B assume "B < y"
  2.2274 +    { assume "f0 < B"
  2.2275 +      then have "eventually (\<lambda>x. f x < B \<and> B < f x) net"
  2.2276 +         using topological_tendstoD[OF lim, of "{..<B}"] *[rule_format, OF `B < y`]
  2.2277 +         by (auto intro: eventually_conj)
  2.2278 +      also have "(\<lambda>x. f x < B \<and> B < f x) = (\<lambda>x. False)" by (auto simp: fun_eq_iff)
  2.2279 +      finally have False using ntriv[unfolded trivial_limit_def] by auto
  2.2280 +    } then show "B \<le> f0" by (metis linorder_le_less_linear)
  2.2281 +  qed
  2.2282 +next
  2.2283 +  fix y assume *: "\<forall>z. z \<in> {l. \<forall>y<l. eventually (\<lambda>x. y < f x) net} \<longrightarrow> z \<le> y"
  2.2284 +  show "f0 \<le> y"
  2.2285 +  proof (safe intro!: *[rule_format])
  2.2286 +    fix y assume "y < f0" then show "eventually (\<lambda>x. y < f x) net"
  2.2287 +      using lim[THEN topological_tendstoD, of "{y <..}"] by auto
  2.2288 +  qed
  2.2289 +qed
  2.2290 +
  2.2291 +lemma ereal_Liminf_le_Limsup:
  2.2292 +  fixes f :: "'a \<Rightarrow> ereal"
  2.2293 +  assumes ntriv: "\<not> trivial_limit net"
  2.2294 +  shows "Liminf net f \<le> Limsup net f"
  2.2295 +  unfolding Limsup_Inf Liminf_Sup
  2.2296 +proof (safe intro!: complete_lattice_class.Inf_greatest  complete_lattice_class.Sup_least)
  2.2297 +  fix u v assume *: "\<forall>y<u. eventually (\<lambda>x. y < f x) net" "\<forall>y>v. eventually (\<lambda>x. f x < y) net"
  2.2298 +  show "u \<le> v"
  2.2299 +  proof (rule ccontr)
  2.2300 +    assume "\<not> u \<le> v"
  2.2301 +    then obtain t where "t < u" "v < t"
  2.2302 +      using ereal_dense[of v u] by (auto simp: not_le)
  2.2303 +    then have "eventually (\<lambda>x. t < f x \<and> f x < t) net"
  2.2304 +      using * by (auto intro: eventually_conj)
  2.2305 +    also have "(\<lambda>x. t < f x \<and> f x < t) = (\<lambda>x. False)" by (auto simp: fun_eq_iff)
  2.2306 +    finally show False using ntriv by (auto simp: trivial_limit_def)
  2.2307 +  qed
  2.2308 +qed
  2.2309 +
  2.2310 +lemma Liminf_mono:
  2.2311 +  fixes f g :: "'a => ereal"
  2.2312 +  assumes ev: "eventually (\<lambda>x. f x \<le> g x) net"
  2.2313 +  shows "Liminf net f \<le> Liminf net g"
  2.2314 +  unfolding Liminf_Sup
  2.2315 +proof (safe intro!: Sup_mono bexI)
  2.2316 +  fix a y assume "\<forall>y<a. eventually (\<lambda>x. y < f x) net" and "y < a"
  2.2317 +  then have "eventually (\<lambda>x. y < f x) net" by auto
  2.2318 +  then show "eventually (\<lambda>x. y < g x) net"
  2.2319 +    by (rule eventually_rev_mp) (rule eventually_mono[OF _ ev], auto)
  2.2320 +qed simp
  2.2321 +
  2.2322 +lemma Liminf_eq:
  2.2323 +  fixes f g :: "'a \<Rightarrow> ereal"
  2.2324 +  assumes "eventually (\<lambda>x. f x = g x) net"
  2.2325 +  shows "Liminf net f = Liminf net g"
  2.2326 +  by (intro antisym Liminf_mono eventually_mono[OF _ assms]) auto
  2.2327 +
  2.2328 +lemma Liminf_mono_all:
  2.2329 +  fixes f g :: "'a \<Rightarrow> ereal"
  2.2330 +  assumes "\<And>x. f x \<le> g x"
  2.2331 +  shows "Liminf net f \<le> Liminf net g"
  2.2332 +  using assms by (intro Liminf_mono always_eventually) auto
  2.2333 +
  2.2334 +lemma Limsup_mono:
  2.2335 +  fixes f g :: "'a \<Rightarrow> ereal"
  2.2336 +  assumes ev: "eventually (\<lambda>x. f x \<le> g x) net"
  2.2337 +  shows "Limsup net f \<le> Limsup net g"
  2.2338 +  unfolding Limsup_Inf
  2.2339 +proof (safe intro!: Inf_mono bexI)
  2.2340 +  fix a y assume "\<forall>y>a. eventually (\<lambda>x. g x < y) net" and "a < y"
  2.2341 +  then have "eventually (\<lambda>x. g x < y) net" by auto
  2.2342 +  then show "eventually (\<lambda>x. f x < y) net"
  2.2343 +    by (rule eventually_rev_mp) (rule eventually_mono[OF _ ev], auto)
  2.2344 +qed simp
  2.2345 +
  2.2346 +lemma Limsup_mono_all:
  2.2347 +  fixes f g :: "'a \<Rightarrow> ereal"
  2.2348 +  assumes "\<And>x. f x \<le> g x"
  2.2349 +  shows "Limsup net f \<le> Limsup net g"
  2.2350 +  using assms by (intro Limsup_mono always_eventually) auto
  2.2351 +
  2.2352 +lemma Limsup_eq:
  2.2353 +  fixes f g :: "'a \<Rightarrow> ereal"
  2.2354 +  assumes "eventually (\<lambda>x. f x = g x) net"
  2.2355 +  shows "Limsup net f = Limsup net g"
  2.2356 +  by (intro antisym Limsup_mono eventually_mono[OF _ assms]) auto
  2.2357 +
  2.2358 +abbreviation "liminf \<equiv> Liminf sequentially"
  2.2359 +
  2.2360 +abbreviation "limsup \<equiv> Limsup sequentially"
  2.2361 +
  2.2362 +lemma (in complete_lattice) less_INFD:
  2.2363 +  assumes "y < INFI A f"" i \<in> A" shows "y < f i"
  2.2364 +proof -
  2.2365 +  note `y < INFI A f`
  2.2366 +  also have "INFI A f \<le> f i" using `i \<in> A` by (rule INF_leI)
  2.2367 +  finally show "y < f i" .
  2.2368 +qed
  2.2369 +
  2.2370 +lemma liminf_SUPR_INFI:
  2.2371 +  fixes f :: "nat \<Rightarrow> ereal"
  2.2372 +  shows "liminf f = (SUP n. INF m:{n..}. f m)"
  2.2373 +  unfolding Liminf_Sup eventually_sequentially
  2.2374 +proof (safe intro!: antisym complete_lattice_class.Sup_least)
  2.2375 +  fix x assume *: "\<forall>y<x. \<exists>N. \<forall>n\<ge>N. y < f n" show "x \<le> (SUP n. INF m:{n..}. f m)"
  2.2376 +  proof (rule ereal_le_ereal)
  2.2377 +    fix y assume "y < x"
  2.2378 +    with * obtain N where "\<And>n. N \<le> n \<Longrightarrow> y < f n" by auto
  2.2379 +    then have "y \<le> (INF m:{N..}. f m)" by (force simp: le_INF_iff)
  2.2380 +    also have "\<dots> \<le> (SUP n. INF m:{n..}. f m)" by (intro le_SUPI) auto
  2.2381 +    finally show "y \<le> (SUP n. INF m:{n..}. f m)" .
  2.2382 +  qed
  2.2383 +next
  2.2384 +  show "(SUP n. INF m:{n..}. f m) \<le> Sup {l. \<forall>y<l. \<exists>N. \<forall>n\<ge>N. y < f n}"
  2.2385 +  proof (unfold SUPR_def, safe intro!: Sup_mono bexI)
  2.2386 +    fix y n assume "y < INFI {n..} f"
  2.2387 +    from less_INFD[OF this] show "\<exists>N. \<forall>n\<ge>N. y < f n" by (intro exI[of _ n]) auto
  2.2388 +  qed (rule order_refl)
  2.2389 +qed
  2.2390 +
  2.2391 +lemma tail_same_limsup:
  2.2392 +  fixes X Y :: "nat => ereal"
  2.2393 +  assumes "\<And>n. N \<le> n \<Longrightarrow> X n = Y n"
  2.2394 +  shows "limsup X = limsup Y"
  2.2395 +  using Limsup_eq[of X Y sequentially] eventually_sequentially assms by auto
  2.2396 +
  2.2397 +lemma tail_same_liminf:
  2.2398 +  fixes X Y :: "nat => ereal"
  2.2399 +  assumes "\<And>n. N \<le> n \<Longrightarrow> X n = Y n"
  2.2400 +  shows "liminf X = liminf Y"
  2.2401 +  using Liminf_eq[of X Y sequentially] eventually_sequentially assms by auto
  2.2402 +
  2.2403 +lemma liminf_mono:
  2.2404 +  fixes X Y :: "nat \<Rightarrow> ereal"
  2.2405 +  assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= Y n"
  2.2406 +  shows "liminf X \<le> liminf Y"
  2.2407 +  using Liminf_mono[of X Y sequentially] eventually_sequentially assms by auto
  2.2408 +
  2.2409 +lemma limsup_mono:
  2.2410 +  fixes X Y :: "nat => ereal"
  2.2411 +  assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= Y n"
  2.2412 +  shows "limsup X \<le> limsup Y"
  2.2413 +  using Limsup_mono[of X Y sequentially] eventually_sequentially assms by auto
  2.2414 +
  2.2415 +declare trivial_limit_sequentially[simp]
  2.2416 +
  2.2417 +lemma
  2.2418 +  fixes X :: "nat \<Rightarrow> ereal"
  2.2419 +  shows ereal_incseq_uminus[simp]: "incseq (\<lambda>i. - X i) = decseq X"
  2.2420 +    and ereal_decseq_uminus[simp]: "decseq (\<lambda>i. - X i) = incseq X"
  2.2421 +  unfolding incseq_def decseq_def by auto
  2.2422 +
  2.2423 +lemma liminf_bounded:
  2.2424 +  fixes X Y :: "nat \<Rightarrow> ereal"
  2.2425 +  assumes "\<And>n. N \<le> n \<Longrightarrow> C \<le> X n"
  2.2426 +  shows "C \<le> liminf X"
  2.2427 +  using liminf_mono[of N "\<lambda>n. C" X] assms Liminf_const[of sequentially C] by simp
  2.2428 +
  2.2429 +lemma limsup_bounded:
  2.2430 +  fixes X Y :: "nat => ereal"
  2.2431 +  assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= C"
  2.2432 +  shows "limsup X \<le> C"
  2.2433 +  using limsup_mono[of N X "\<lambda>n. C"] assms Limsup_const[of sequentially C] by simp
  2.2434 +
  2.2435 +lemma liminf_bounded_iff:
  2.2436 +  fixes x :: "nat \<Rightarrow> ereal"
  2.2437 +  shows "C \<le> liminf x \<longleftrightarrow> (\<forall>B<C. \<exists>N. \<forall>n\<ge>N. B < x n)" (is "?lhs <-> ?rhs")
  2.2438 +proof safe
  2.2439 +  fix B assume "B < C" "C \<le> liminf x"
  2.2440 +  then have "B < liminf x" by auto
  2.2441 +  then obtain N where "B < (INF m:{N..}. x m)"
  2.2442 +    unfolding liminf_SUPR_INFI SUPR_def less_Sup_iff by auto
  2.2443 +  from less_INFD[OF this] show "\<exists>N. \<forall>n\<ge>N. B < x n" by auto
  2.2444 +next
  2.2445 +  assume *: "\<forall>B<C. \<exists>N. \<forall>n\<ge>N. B < x n"
  2.2446 +  { fix B assume "B<C"
  2.2447 +    then obtain N where "\<forall>n\<ge>N. B < x n" using `?rhs` by auto
  2.2448 +    hence "B \<le> (INF m:{N..}. x m)" by (intro le_INFI) auto
  2.2449 +    also have "... \<le> liminf x" unfolding liminf_SUPR_INFI by (intro le_SUPI) simp
  2.2450 +    finally have "B \<le> liminf x" .
  2.2451 +  } then show "?lhs" by (metis * leD liminf_bounded linorder_le_less_linear)
  2.2452 +qed
  2.2453 +
  2.2454 +lemma liminf_subseq_mono:
  2.2455 +  fixes X :: "nat \<Rightarrow> ereal"
  2.2456 +  assumes "subseq r"
  2.2457 +  shows "liminf X \<le> liminf (X \<circ> r) "
  2.2458 +proof-
  2.2459 +  have "\<And>n. (INF m:{n..}. X m) \<le> (INF m:{n..}. (X \<circ> r) m)"
  2.2460 +  proof (safe intro!: INF_mono)
  2.2461 +    fix n m :: nat assume "n \<le> m" then show "\<exists>ma\<in>{n..}. X ma \<le> (X \<circ> r) m"
  2.2462 +      using seq_suble[OF `subseq r`, of m] by (intro bexI[of _ "r m"]) auto
  2.2463 +  qed
  2.2464 +  then show ?thesis by (auto intro!: SUP_mono simp: liminf_SUPR_INFI comp_def)
  2.2465 +qed
  2.2466 +
  2.2467 +lemma ereal_real': assumes "\<bar>x\<bar> \<noteq> \<infinity>" shows "ereal (real x) = x"
  2.2468 +  using assms by auto
  2.2469 +
  2.2470 +lemma ereal_le_ereal_bounded:
  2.2471 +  fixes x y z :: ereal
  2.2472 +  assumes "z \<le> y"
  2.2473 +  assumes *: "\<And>B. z < B \<Longrightarrow> B < x \<Longrightarrow> B \<le> y"
  2.2474 +  shows "x \<le> y"
  2.2475 +proof (rule ereal_le_ereal)
  2.2476 +  fix B assume "B < x"
  2.2477 +  show "B \<le> y"
  2.2478 +  proof cases
  2.2479 +    assume "z < B" from *[OF this `B < x`] show "B \<le> y" .
  2.2480 +  next
  2.2481 +    assume "\<not> z < B" with `z \<le> y` show "B \<le> y" by auto
  2.2482 +  qed
  2.2483 +qed
  2.2484 +
  2.2485 +lemma fixes x y :: ereal
  2.2486 +  shows Sup_atMost[simp]: "Sup {.. y} = y"
  2.2487 +    and Sup_lessThan[simp]: "Sup {..< y} = y"
  2.2488 +    and Sup_atLeastAtMost[simp]: "x \<le> y \<Longrightarrow> Sup { x .. y} = y"
  2.2489 +    and Sup_greaterThanAtMost[simp]: "x < y \<Longrightarrow> Sup { x <.. y} = y"
  2.2490 +    and Sup_atLeastLessThan[simp]: "x < y \<Longrightarrow> Sup { x ..< y} = y"
  2.2491 +  by (auto simp: Sup_ereal_def intro!: Least_equality
  2.2492 +           intro: ereal_le_ereal ereal_le_ereal_bounded[of x])
  2.2493 +
  2.2494 +lemma Sup_greaterThanlessThan[simp]:
  2.2495 +  fixes x y :: ereal assumes "x < y" shows "Sup { x <..< y} = y"
  2.2496 +  unfolding Sup_ereal_def
  2.2497 +proof (intro Least_equality ereal_le_ereal_bounded[of _ _ y])
  2.2498 +  fix z assume z: "\<forall>u\<in>{x<..<y}. u \<le> z"
  2.2499 +  from ereal_dense[OF `x < y`] guess w .. note w = this
  2.2500 +  with z[THEN bspec, of w] show "x \<le> z" by auto
  2.2501 +qed auto
  2.2502 +
  2.2503 +lemma real_ereal_id: "real o ereal = id"
  2.2504 +proof-
  2.2505 +{ fix x have "(real o ereal) x = id x" by auto }
  2.2506 +from this show ?thesis using ext by blast
  2.2507 +qed
  2.2508 +
  2.2509 +lemma open_image_ereal: "open(UNIV-{\<infinity>,(-\<infinity>)})"
  2.2510 +by (metis range_ereal open_ereal open_UNIV)
  2.2511 +
  2.2512 +lemma ereal_le_distrib:
  2.2513 +  fixes a b c :: ereal shows "c * (a + b) \<le> c * a + c * b"
  2.2514 +  by (cases rule: ereal3_cases[of a b c])
  2.2515 +     (auto simp add: field_simps not_le mult_le_0_iff mult_less_0_iff)
  2.2516 +
  2.2517 +lemma ereal_pos_distrib:
  2.2518 +  fixes a b c :: ereal assumes "0 \<le> c" "c \<noteq> \<infinity>" shows "c * (a + b) = c * a + c * b"
  2.2519 +  using assms by (cases rule: ereal3_cases[of a b c])
  2.2520 +                 (auto simp add: field_simps not_le mult_le_0_iff mult_less_0_iff)
  2.2521 +
  2.2522 +lemma ereal_pos_le_distrib:
  2.2523 +fixes a b c :: ereal
  2.2524 +assumes "c>=0"
  2.2525 +shows "c * (a + b) <= c * a + c * b"
  2.2526 +  using assms by (cases rule: ereal3_cases[of a b c])
  2.2527 +                 (auto simp add: field_simps)
  2.2528 +
  2.2529 +lemma ereal_max_mono:
  2.2530 +  "[| (a::ereal) <= b; c <= d |] ==> max a c <= max b d"
  2.2531 +  by (metis sup_ereal_def sup_mono)
  2.2532 +
  2.2533 +
  2.2534 +lemma ereal_max_least:
  2.2535 +  "[| (a::ereal) <= x; c <= x |] ==> max a c <= x"
  2.2536 +  by (metis sup_ereal_def sup_least)
  2.2537 +
  2.2538 +end
     3.1 --- a/src/HOL/Library/Extended_Reals.thy	Tue Jul 19 14:35:44 2011 +0200
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,2535 +0,0 @@
     3.4 -(*  Title:      HOL/Library/Extended_Reals.thy
     3.5 -    Author:     Johannes Hölzl, TU München
     3.6 -    Author:     Robert Himmelmann, TU München
     3.7 -    Author:     Armin Heller, TU München
     3.8 -    Author:     Bogdan Grechuk, University of Edinburgh
     3.9 -*)
    3.10 -
    3.11 -header {* Extended real number line *}
    3.12 -
    3.13 -theory Extended_Reals
    3.14 -  imports Complex_Main
    3.15 -begin
    3.16 -
    3.17 -text {*
    3.18 -
    3.19 -For more lemmas about the extended real numbers go to
    3.20 -  @{text "src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy"}
    3.21 -
    3.22 -*}
    3.23 -
    3.24 -lemma (in complete_lattice) atLeast_eq_UNIV_iff: "{x..} = UNIV \<longleftrightarrow> x = bot"
    3.25 -proof
    3.26 -  assume "{x..} = UNIV"
    3.27 -  show "x = bot"
    3.28 -  proof (rule ccontr)
    3.29 -    assume "x \<noteq> bot" then have "bot \<notin> {x..}" by (simp add: le_less)
    3.30 -    then show False using `{x..} = UNIV` by simp
    3.31 -  qed
    3.32 -qed auto
    3.33 -
    3.34 -lemma SUPR_pair:
    3.35 -  "(SUP i : A. SUP j : B. f i j) = (SUP p : A \<times> B. f (fst p) (snd p))"
    3.36 -  by (rule antisym) (auto intro!: SUP_leI le_SUPI2)
    3.37 -
    3.38 -lemma INFI_pair:
    3.39 -  "(INF i : A. INF j : B. f i j) = (INF p : A \<times> B. f (fst p) (snd p))"
    3.40 -  by (rule antisym) (auto intro!: le_INFI INF_leI2)
    3.41 -
    3.42 -subsection {* Definition and basic properties *}
    3.43 -
    3.44 -datatype extreal = extreal real | PInfty | MInfty
    3.45 -
    3.46 -notation (xsymbols)
    3.47 -  PInfty  ("\<infinity>")
    3.48 -
    3.49 -notation (HTML output)
    3.50 -  PInfty  ("\<infinity>")
    3.51 -
    3.52 -declare [[coercion "extreal :: real \<Rightarrow> extreal"]]
    3.53 -
    3.54 -instantiation extreal :: uminus
    3.55 -begin
    3.56 -  fun uminus_extreal where
    3.57 -    "- (extreal r) = extreal (- r)"
    3.58 -  | "- \<infinity> = MInfty"
    3.59 -  | "- MInfty = \<infinity>"
    3.60 -  instance ..
    3.61 -end
    3.62 -
    3.63 -lemma inj_extreal[simp]: "inj_on extreal A"
    3.64 -  unfolding inj_on_def by auto
    3.65 -
    3.66 -lemma MInfty_neq_PInfty[simp]:
    3.67 -  "\<infinity> \<noteq> - \<infinity>" "- \<infinity> \<noteq> \<infinity>" by simp_all
    3.68 -
    3.69 -lemma MInfty_neq_extreal[simp]:
    3.70 -  "extreal r \<noteq> - \<infinity>" "- \<infinity> \<noteq> extreal r" by simp_all
    3.71 -
    3.72 -lemma MInfinity_cases[simp]:
    3.73 -  "(case - \<infinity> of extreal r \<Rightarrow> f r | \<infinity> \<Rightarrow> y | MInfinity \<Rightarrow> z) = z"
    3.74 -  by simp
    3.75 -
    3.76 -lemma extreal_uminus_uminus[simp]:
    3.77 -  fixes a :: extreal shows "- (- a) = a"
    3.78 -  by (cases a) simp_all
    3.79 -
    3.80 -lemma MInfty_eq[simp, code_post]:
    3.81 -  "MInfty = - \<infinity>" by simp
    3.82 -
    3.83 -declare uminus_extreal.simps(2)[code_inline, simp del]
    3.84 -
    3.85 -lemma extreal_cases[case_names real PInf MInf, cases type: extreal]:
    3.86 -  assumes "\<And>r. x = extreal r \<Longrightarrow> P"
    3.87 -  assumes "x = \<infinity> \<Longrightarrow> P"
    3.88 -  assumes "x = -\<infinity> \<Longrightarrow> P"
    3.89 -  shows P
    3.90 -  using assms by (cases x) auto
    3.91 -
    3.92 -lemmas extreal2_cases = extreal_cases[case_product extreal_cases]
    3.93 -lemmas extreal3_cases = extreal2_cases[case_product extreal_cases]
    3.94 -
    3.95 -lemma extreal_uminus_eq_iff[simp]:
    3.96 -  fixes a b :: extreal shows "-a = -b \<longleftrightarrow> a = b"
    3.97 -  by (cases rule: extreal2_cases[of a b]) simp_all
    3.98 -
    3.99 -function of_extreal :: "extreal \<Rightarrow> real" where
   3.100 -"of_extreal (extreal r) = r" |
   3.101 -"of_extreal \<infinity> = 0" |
   3.102 -"of_extreal (-\<infinity>) = 0"
   3.103 -  by (auto intro: extreal_cases)
   3.104 -termination proof qed (rule wf_empty)
   3.105 -
   3.106 -defs (overloaded)
   3.107 -  real_of_extreal_def [code_unfold]: "real \<equiv> of_extreal"
   3.108 -
   3.109 -lemma real_of_extreal[simp]:
   3.110 -    "real (- x :: extreal) = - (real x)"
   3.111 -    "real (extreal r) = r"
   3.112 -    "real \<infinity> = 0"
   3.113 -  by (cases x) (simp_all add: real_of_extreal_def)
   3.114 -
   3.115 -lemma range_extreal[simp]: "range extreal = UNIV - {\<infinity>, -\<infinity>}"
   3.116 -proof safe
   3.117 -  fix x assume "x \<notin> range extreal" "x \<noteq> \<infinity>"
   3.118 -  then show "x = -\<infinity>" by (cases x) auto
   3.119 -qed auto
   3.120 -
   3.121 -lemma extreal_range_uminus[simp]: "range uminus = (UNIV::extreal set)"
   3.122 -proof safe
   3.123 -  fix x :: extreal show "x \<in> range uminus" by (intro image_eqI[of _ _ "-x"]) auto
   3.124 -qed auto
   3.125 -
   3.126 -instantiation extreal :: number
   3.127 -begin
   3.128 -definition [simp]: "number_of x = extreal (number_of x)"
   3.129 -instance proof qed
   3.130 -end
   3.131 -
   3.132 -instantiation extreal :: abs
   3.133 -begin
   3.134 -  function abs_extreal where
   3.135 -    "\<bar>extreal r\<bar> = extreal \<bar>r\<bar>"
   3.136 -  | "\<bar>-\<infinity>\<bar> = \<infinity>"
   3.137 -  | "\<bar>\<infinity>\<bar> = \<infinity>"
   3.138 -  by (auto intro: extreal_cases)
   3.139 -  termination proof qed (rule wf_empty)
   3.140 -  instance ..
   3.141 -end
   3.142 -
   3.143 -lemma abs_eq_infinity_cases[elim!]: "\<lbrakk> \<bar>x\<bar> = \<infinity> ; x = \<infinity> \<Longrightarrow> P ; x = -\<infinity> \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
   3.144 -  by (cases x) auto
   3.145 -
   3.146 -lemma abs_neq_infinity_cases[elim!]: "\<lbrakk> \<bar>x\<bar> \<noteq> \<infinity> ; \<And>r. x = extreal r \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
   3.147 -  by (cases x) auto
   3.148 -
   3.149 -lemma abs_extreal_uminus[simp]: "\<bar>- x\<bar> = \<bar>x::extreal\<bar>"
   3.150 -  by (cases x) auto
   3.151 -
   3.152 -subsubsection "Addition"
   3.153 -
   3.154 -instantiation extreal :: comm_monoid_add
   3.155 -begin
   3.156 -
   3.157 -definition "0 = extreal 0"
   3.158 -
   3.159 -function plus_extreal where
   3.160 -"extreal r + extreal p = extreal (r + p)" |
   3.161 -"\<infinity> + a = \<infinity>" |
   3.162 -"a + \<infinity> = \<infinity>" |
   3.163 -"extreal r + -\<infinity> = - \<infinity>" |
   3.164 -"-\<infinity> + extreal p = -\<infinity>" |
   3.165 -"-\<infinity> + -\<infinity> = -\<infinity>"
   3.166 -proof -
   3.167 -  case (goal1 P x)
   3.168 -  moreover then obtain a b where "x = (a, b)" by (cases x) auto
   3.169 -  ultimately show P
   3.170 -   by (cases rule: extreal2_cases[of a b]) auto
   3.171 -qed auto
   3.172 -termination proof qed (rule wf_empty)
   3.173 -
   3.174 -lemma Infty_neq_0[simp]:
   3.175 -  "\<infinity> \<noteq> 0" "0 \<noteq> \<infinity>"
   3.176 -  "-\<infinity> \<noteq> 0" "0 \<noteq> -\<infinity>"
   3.177 -  by (simp_all add: zero_extreal_def)
   3.178 -
   3.179 -lemma extreal_eq_0[simp]:
   3.180 -  "extreal r = 0 \<longleftrightarrow> r = 0"
   3.181 -  "0 = extreal r \<longleftrightarrow> r = 0"
   3.182 -  unfolding zero_extreal_def by simp_all
   3.183 -
   3.184 -instance
   3.185 -proof
   3.186 -  fix a :: extreal show "0 + a = a"
   3.187 -    by (cases a) (simp_all add: zero_extreal_def)
   3.188 -  fix b :: extreal show "a + b = b + a"
   3.189 -    by (cases rule: extreal2_cases[of a b]) simp_all
   3.190 -  fix c :: extreal show "a + b + c = a + (b + c)"
   3.191 -    by (cases rule: extreal3_cases[of a b c]) simp_all
   3.192 -qed
   3.193 -end
   3.194 -
   3.195 -lemma real_of_extreal_0[simp]: "real (0::extreal) = 0"
   3.196 -  unfolding real_of_extreal_def zero_extreal_def by simp
   3.197 -
   3.198 -lemma abs_extreal_zero[simp]: "\<bar>0\<bar> = (0::extreal)"
   3.199 -  unfolding zero_extreal_def abs_extreal.simps by simp
   3.200 -
   3.201 -lemma extreal_uminus_zero[simp]:
   3.202 -  "- 0 = (0::extreal)"
   3.203 -  by (simp add: zero_extreal_def)
   3.204 -
   3.205 -lemma extreal_uminus_zero_iff[simp]:
   3.206 -  fixes a :: extreal shows "-a = 0 \<longleftrightarrow> a = 0"
   3.207 -  by (cases a) simp_all
   3.208 -
   3.209 -lemma extreal_plus_eq_PInfty[simp]:
   3.210 -  shows "a + b = \<infinity> \<longleftrightarrow> a = \<infinity> \<or> b = \<infinity>"
   3.211 -  by (cases rule: extreal2_cases[of a b]) auto
   3.212 -
   3.213 -lemma extreal_plus_eq_MInfty[simp]:
   3.214 -  shows "a + b = -\<infinity> \<longleftrightarrow>
   3.215 -    (a = -\<infinity> \<or> b = -\<infinity>) \<and> a \<noteq> \<infinity> \<and> b \<noteq> \<infinity>"
   3.216 -  by (cases rule: extreal2_cases[of a b]) auto
   3.217 -
   3.218 -lemma extreal_add_cancel_left:
   3.219 -  assumes "a \<noteq> -\<infinity>"
   3.220 -  shows "a + b = a + c \<longleftrightarrow> (a = \<infinity> \<or> b = c)"
   3.221 -  using assms by (cases rule: extreal3_cases[of a b c]) auto
   3.222 -
   3.223 -lemma extreal_add_cancel_right:
   3.224 -  assumes "a \<noteq> -\<infinity>"
   3.225 -  shows "b + a = c + a \<longleftrightarrow> (a = \<infinity> \<or> b = c)"
   3.226 -  using assms by (cases rule: extreal3_cases[of a b c]) auto
   3.227 -
   3.228 -lemma extreal_real:
   3.229 -  "extreal (real x) = (if \<bar>x\<bar> = \<infinity> then 0 else x)"
   3.230 -  by (cases x) simp_all
   3.231 -
   3.232 -lemma real_of_extreal_add:
   3.233 -  fixes a b :: extreal
   3.234 -  shows "real (a + b) = (if (\<bar>a\<bar> = \<infinity>) \<and> (\<bar>b\<bar> = \<infinity>) \<or> (\<bar>a\<bar> \<noteq> \<infinity>) \<and> (\<bar>b\<bar> \<noteq> \<infinity>) then real a + real b else 0)"
   3.235 -  by (cases rule: extreal2_cases[of a b]) auto
   3.236 -
   3.237 -subsubsection "Linear order on @{typ extreal}"
   3.238 -
   3.239 -instantiation extreal :: linorder
   3.240 -begin
   3.241 -
   3.242 -function less_extreal where
   3.243 -"extreal x < extreal y \<longleftrightarrow> x < y" |
   3.244 -"        \<infinity> < a         \<longleftrightarrow> False" |
   3.245 -"        a < -\<infinity>        \<longleftrightarrow> False" |
   3.246 -"extreal x < \<infinity>         \<longleftrightarrow> True" |
   3.247 -"       -\<infinity> < extreal r \<longleftrightarrow> True" |
   3.248 -"       -\<infinity> < \<infinity>         \<longleftrightarrow> True"
   3.249 -proof -
   3.250 -  case (goal1 P x)
   3.251 -  moreover then obtain a b where "x = (a,b)" by (cases x) auto
   3.252 -  ultimately show P by (cases rule: extreal2_cases[of a b]) auto
   3.253 -qed simp_all
   3.254 -termination by (relation "{}") simp
   3.255 -
   3.256 -definition "x \<le> (y::extreal) \<longleftrightarrow> x < y \<or> x = y"
   3.257 -
   3.258 -lemma extreal_infty_less[simp]:
   3.259 -  "x < \<infinity> \<longleftrightarrow> (x \<noteq> \<infinity>)"
   3.260 -  "-\<infinity> < x \<longleftrightarrow> (x \<noteq> -\<infinity>)"
   3.261 -  by (cases x, simp_all) (cases x, simp_all)
   3.262 -
   3.263 -lemma extreal_infty_less_eq[simp]:
   3.264 -  "\<infinity> \<le> x \<longleftrightarrow> x = \<infinity>"
   3.265 -  "x \<le> -\<infinity> \<longleftrightarrow> x = -\<infinity>"
   3.266 -  by (auto simp add: less_eq_extreal_def)
   3.267 -
   3.268 -lemma extreal_less[simp]:
   3.269 -  "extreal r < 0 \<longleftrightarrow> (r < 0)"
   3.270 -  "0 < extreal r \<longleftrightarrow> (0 < r)"
   3.271 -  "0 < \<infinity>"
   3.272 -  "-\<infinity> < 0"
   3.273 -  by (simp_all add: zero_extreal_def)
   3.274 -
   3.275 -lemma extreal_less_eq[simp]:
   3.276 -  "x \<le> \<infinity>"
   3.277 -  "-\<infinity> \<le> x"
   3.278 -  "extreal r \<le> extreal p \<longleftrightarrow> r \<le> p"
   3.279 -  "extreal r \<le> 0 \<longleftrightarrow> r \<le> 0"
   3.280 -  "0 \<le> extreal r \<longleftrightarrow> 0 \<le> r"
   3.281 -  by (auto simp add: less_eq_extreal_def zero_extreal_def)
   3.282 -
   3.283 -lemma extreal_infty_less_eq2:
   3.284 -  "a \<le> b \<Longrightarrow> a = \<infinity> \<Longrightarrow> b = \<infinity>"
   3.285 -  "a \<le> b \<Longrightarrow> b = -\<infinity> \<Longrightarrow> a = -\<infinity>"
   3.286 -  by simp_all
   3.287 -
   3.288 -instance
   3.289 -proof
   3.290 -  fix x :: extreal show "x \<le> x"
   3.291 -    by (cases x) simp_all
   3.292 -  fix y :: extreal show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
   3.293 -    by (cases rule: extreal2_cases[of x y]) auto
   3.294 -  show "x \<le> y \<or> y \<le> x "
   3.295 -    by (cases rule: extreal2_cases[of x y]) auto
   3.296 -  { assume "x \<le> y" "y \<le> x" then show "x = y"
   3.297 -    by (cases rule: extreal2_cases[of x y]) auto }
   3.298 -  { fix z assume "x \<le> y" "y \<le> z" then show "x \<le> z"
   3.299 -    by (cases rule: extreal3_cases[of x y z]) auto }
   3.300 -qed
   3.301 -end
   3.302 -
   3.303 -instance extreal :: ordered_ab_semigroup_add
   3.304 -proof
   3.305 -  fix a b c :: extreal assume "a \<le> b" then show "c + a \<le> c + b"
   3.306 -    by (cases rule: extreal3_cases[of a b c]) auto
   3.307 -qed
   3.308 -
   3.309 -lemma real_of_extreal_positive_mono:
   3.310 -  "\<lbrakk>0 \<le> x; x \<le> y; y \<noteq> \<infinity>\<rbrakk> \<Longrightarrow> real x \<le> real y"
   3.311 -  by (cases rule: extreal2_cases[of x y]) auto
   3.312 -
   3.313 -lemma extreal_MInfty_lessI[intro, simp]:
   3.314 -  "a \<noteq> -\<infinity> \<Longrightarrow> -\<infinity> < a"
   3.315 -  by (cases a) auto
   3.316 -
   3.317 -lemma extreal_less_PInfty[intro, simp]:
   3.318 -  "a \<noteq> \<infinity> \<Longrightarrow> a < \<infinity>"
   3.319 -  by (cases a) auto
   3.320 -
   3.321 -lemma extreal_less_extreal_Ex:
   3.322 -  fixes a b :: extreal
   3.323 -  shows "x < extreal r \<longleftrightarrow> x = -\<infinity> \<or> (\<exists>p. p < r \<and> x = extreal p)"
   3.324 -  by (cases x) auto
   3.325 -
   3.326 -lemma less_PInf_Ex_of_nat: "x \<noteq> \<infinity> \<longleftrightarrow> (\<exists>n::nat. x < extreal (real n))"
   3.327 -proof (cases x)
   3.328 -  case (real r) then show ?thesis
   3.329 -    using reals_Archimedean2[of r] by simp
   3.330 -qed simp_all
   3.331 -
   3.332 -lemma extreal_add_mono:
   3.333 -  fixes a b c d :: extreal assumes "a \<le> b" "c \<le> d" shows "a + c \<le> b + d"
   3.334 -  using assms
   3.335 -  apply (cases a)
   3.336 -  apply (cases rule: extreal3_cases[of b c d], auto)
   3.337 -  apply (cases rule: extreal3_cases[of b c d], auto)
   3.338 -  done
   3.339 -
   3.340 -lemma extreal_minus_le_minus[simp]:
   3.341 -  fixes a b :: extreal shows "- a \<le> - b \<longleftrightarrow> b \<le> a"
   3.342 -  by (cases rule: extreal2_cases[of a b]) auto
   3.343 -
   3.344 -lemma extreal_minus_less_minus[simp]:
   3.345 -  fixes a b :: extreal shows "- a < - b \<longleftrightarrow> b < a"
   3.346 -  by (cases rule: extreal2_cases[of a b]) auto
   3.347 -
   3.348 -lemma extreal_le_real_iff:
   3.349 -  "x \<le> real y \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> extreal x \<le> y) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> x \<le> 0))"
   3.350 -  by (cases y) auto
   3.351 -
   3.352 -lemma real_le_extreal_iff:
   3.353 -  "real y \<le> x \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> y \<le> extreal x) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> 0 \<le> x))"
   3.354 -  by (cases y) auto
   3.355 -
   3.356 -lemma extreal_less_real_iff:
   3.357 -  "x < real y \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> extreal x < y) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> x < 0))"
   3.358 -  by (cases y) auto
   3.359 -
   3.360 -lemma real_less_extreal_iff:
   3.361 -  "real y < x \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> y < extreal x) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> 0 < x))"
   3.362 -  by (cases y) auto
   3.363 -
   3.364 -lemma real_of_extreal_pos:
   3.365 -  fixes x :: extreal shows "0 \<le> x \<Longrightarrow> 0 \<le> real x" by (cases x) auto
   3.366 -
   3.367 -lemmas real_of_extreal_ord_simps =
   3.368 -  extreal_le_real_iff real_le_extreal_iff extreal_less_real_iff real_less_extreal_iff
   3.369 -
   3.370 -lemma abs_extreal_ge0[simp]: "0 \<le> x \<Longrightarrow> \<bar>x :: extreal\<bar> = x"
   3.371 -  by (cases x) auto
   3.372 -
   3.373 -lemma abs_extreal_less0[simp]: "x < 0 \<Longrightarrow> \<bar>x :: extreal\<bar> = -x"
   3.374 -  by (cases x) auto
   3.375 -
   3.376 -lemma abs_extreal_pos[simp]: "0 \<le> \<bar>x :: extreal\<bar>"
   3.377 -  by (cases x) auto
   3.378 -
   3.379 -lemma real_of_extreal_le_0[simp]: "real (X :: extreal) \<le> 0 \<longleftrightarrow> (X \<le> 0 \<or> X = \<infinity>)"
   3.380 -  by (cases X) auto
   3.381 -
   3.382 -lemma abs_real_of_extreal[simp]: "\<bar>real (X :: extreal)\<bar> = real \<bar>X\<bar>"
   3.383 -  by (cases X) auto
   3.384 -
   3.385 -lemma zero_less_real_of_extreal: "0 < real X \<longleftrightarrow> (0 < X \<and> X \<noteq> \<infinity>)"
   3.386 -  by (cases X) auto
   3.387 -
   3.388 -lemma extreal_0_le_uminus_iff[simp]:
   3.389 -  fixes a :: extreal shows "0 \<le> -a \<longleftrightarrow> a \<le> 0"
   3.390 -  by (cases rule: extreal2_cases[of a]) auto
   3.391 -
   3.392 -lemma extreal_uminus_le_0_iff[simp]:
   3.393 -  fixes a :: extreal shows "-a \<le> 0 \<longleftrightarrow> 0 \<le> a"
   3.394 -  by (cases rule: extreal2_cases[of a]) auto
   3.395 -
   3.396 -lemma extreal_dense:
   3.397 -  fixes x y :: extreal assumes "x < y"
   3.398 -  shows "EX z. x < z & z < y"
   3.399 -proof -
   3.400 -{ assume a: "x = (-\<infinity>)"
   3.401 -  { assume "y = \<infinity>" hence ?thesis using a by (auto intro!: exI[of _ "0"]) }
   3.402 -  moreover
   3.403 -  { assume "y ~= \<infinity>"
   3.404 -    with `x < y` obtain r where r: "y = extreal r" by (cases y) auto
   3.405 -    hence ?thesis using `x < y` a by (auto intro!: exI[of _ "extreal (r - 1)"])
   3.406 -  } ultimately have ?thesis by auto
   3.407 -}
   3.408 -moreover
   3.409 -{ assume "x ~= (-\<infinity>)"
   3.410 -  with `x < y` obtain p where p: "x = extreal p" by (cases x) auto
   3.411 -  { assume "y = \<infinity>" hence ?thesis using `x < y` p
   3.412 -       by (auto intro!: exI[of _ "extreal (p + 1)"]) }
   3.413 -  moreover
   3.414 -  { assume "y ~= \<infinity>"
   3.415 -    with `x < y` obtain r where r: "y = extreal r" by (cases y) auto
   3.416 -    with p `x < y` have "p < r" by auto
   3.417 -    with dense obtain z where "p < z" "z < r" by auto
   3.418 -    hence ?thesis using r p by (auto intro!: exI[of _ "extreal z"])
   3.419 -  } ultimately have ?thesis by auto
   3.420 -} ultimately show ?thesis by auto
   3.421 -qed
   3.422 -
   3.423 -lemma extreal_dense2:
   3.424 -  fixes x y :: extreal assumes "x < y"
   3.425 -  shows "EX z. x < extreal z & extreal z < y"
   3.426 -  by (metis extreal_dense[OF `x < y`] extreal_cases less_extreal.simps(2,3))
   3.427 -
   3.428 -lemma extreal_add_strict_mono:
   3.429 -  fixes a b c d :: extreal
   3.430 -  assumes "a = b" "0 \<le> a" "a \<noteq> \<infinity>" "c < d"
   3.431 -  shows "a + c < b + d"
   3.432 -  using assms by (cases rule: extreal3_cases[case_product extreal_cases, of a b c d]) auto
   3.433 -
   3.434 -lemma extreal_less_add: "\<bar>a\<bar> \<noteq> \<infinity> \<Longrightarrow> c < b \<Longrightarrow> a + c < a + b"
   3.435 -  by (cases rule: extreal2_cases[of b c]) auto
   3.436 -
   3.437 -lemma extreal_uminus_eq_reorder: "- a = b \<longleftrightarrow> a = (-b::extreal)" by auto
   3.438 -
   3.439 -lemma extreal_uminus_less_reorder: "- a < b \<longleftrightarrow> -b < (a::extreal)"
   3.440 -  by (subst (3) extreal_uminus_uminus[symmetric]) (simp only: extreal_minus_less_minus)
   3.441 -
   3.442 -lemma extreal_uminus_le_reorder: "- a \<le> b \<longleftrightarrow> -b \<le> (a::extreal)"
   3.443 -  by (subst (3) extreal_uminus_uminus[symmetric]) (simp only: extreal_minus_le_minus)
   3.444 -
   3.445 -lemmas extreal_uminus_reorder =
   3.446 -  extreal_uminus_eq_reorder extreal_uminus_less_reorder extreal_uminus_le_reorder
   3.447 -
   3.448 -lemma extreal_bot:
   3.449 -  fixes x :: extreal assumes "\<And>B. x \<le> extreal B" shows "x = - \<infinity>"
   3.450 -proof (cases x)
   3.451 -  case (real r) with assms[of "r - 1"] show ?thesis by auto
   3.452 -next case PInf with assms[of 0] show ?thesis by auto
   3.453 -next case MInf then show ?thesis by simp
   3.454 -qed
   3.455 -
   3.456 -lemma extreal_top:
   3.457 -  fixes x :: extreal assumes "\<And>B. x \<ge> extreal B" shows "x = \<infinity>"
   3.458 -proof (cases x)
   3.459 -  case (real r) with assms[of "r + 1"] show ?thesis by auto
   3.460 -next case MInf with assms[of 0] show ?thesis by auto
   3.461 -next case PInf then show ?thesis by simp
   3.462 -qed
   3.463 -
   3.464 -lemma
   3.465 -  shows extreal_max[simp]: "extreal (max x y) = max (extreal x) (extreal y)"
   3.466 -    and extreal_min[simp]: "extreal (min x y) = min (extreal x) (extreal y)"
   3.467 -  by (simp_all add: min_def max_def)
   3.468 -
   3.469 -lemma extreal_max_0: "max 0 (extreal r) = extreal (max 0 r)"
   3.470 -  by (auto simp: zero_extreal_def)
   3.471 -
   3.472 -lemma
   3.473 -  fixes f :: "nat \<Rightarrow> extreal"
   3.474 -  shows incseq_uminus[simp]: "incseq (\<lambda>x. - f x) \<longleftrightarrow> decseq f"
   3.475 -  and decseq_uminus[simp]: "decseq (\<lambda>x. - f x) \<longleftrightarrow> incseq f"
   3.476 -  unfolding decseq_def incseq_def by auto
   3.477 -
   3.478 -lemma incseq_extreal: "incseq f \<Longrightarrow> incseq (\<lambda>x. extreal (f x))"
   3.479 -  unfolding incseq_def by auto
   3.480 -
   3.481 -lemma extreal_add_nonneg_nonneg:
   3.482 -  fixes a b :: extreal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a + b"
   3.483 -  using add_mono[of 0 a 0 b] by simp
   3.484 -
   3.485 -lemma image_eqD: "f ` A = B \<Longrightarrow> (\<forall>x\<in>A. f x \<in> B)"
   3.486 -  by auto
   3.487 -
   3.488 -lemma incseq_setsumI:
   3.489 -  fixes f :: "nat \<Rightarrow> 'a::{comm_monoid_add, ordered_ab_semigroup_add}"
   3.490 -  assumes "\<And>i. 0 \<le> f i"
   3.491 -  shows "incseq (\<lambda>i. setsum f {..< i})"
   3.492 -proof (intro incseq_SucI)
   3.493 -  fix n have "setsum f {..< n} + 0 \<le> setsum f {..<n} + f n"
   3.494 -    using assms by (rule add_left_mono)
   3.495 -  then show "setsum f {..< n} \<le> setsum f {..< Suc n}"
   3.496 -    by auto
   3.497 -qed
   3.498 -
   3.499 -lemma incseq_setsumI2:
   3.500 -  fixes f :: "'i \<Rightarrow> nat \<Rightarrow> 'a::{comm_monoid_add, ordered_ab_semigroup_add}"
   3.501 -  assumes "\<And>n. n \<in> A \<Longrightarrow> incseq (f n)"
   3.502 -  shows "incseq (\<lambda>i. \<Sum>n\<in>A. f n i)"
   3.503 -  using assms unfolding incseq_def by (auto intro: setsum_mono)
   3.504 -
   3.505 -subsubsection "Multiplication"
   3.506 -
   3.507 -instantiation extreal :: "{comm_monoid_mult, sgn}"
   3.508 -begin
   3.509 -
   3.510 -definition "1 = extreal 1"
   3.511 -
   3.512 -function sgn_extreal where
   3.513 -  "sgn (extreal r) = extreal (sgn r)"
   3.514 -| "sgn \<infinity> = 1"
   3.515 -| "sgn (-\<infinity>) = -1"
   3.516 -by (auto intro: extreal_cases)
   3.517 -termination proof qed (rule wf_empty)
   3.518 -
   3.519 -function times_extreal where
   3.520 -"extreal r * extreal p = extreal (r * p)" |
   3.521 -"extreal r * \<infinity> = (if r = 0 then 0 else if r > 0 then \<infinity> else -\<infinity>)" |
   3.522 -"\<infinity> * extreal r = (if r = 0 then 0 else if r > 0 then \<infinity> else -\<infinity>)" |
   3.523 -"extreal r * -\<infinity> = (if r = 0 then 0 else if r > 0 then -\<infinity> else \<infinity>)" |
   3.524 -"-\<infinity> * extreal r = (if r = 0 then 0 else if r > 0 then -\<infinity> else \<infinity>)" |
   3.525 -"\<infinity> * \<infinity> = \<infinity>" |
   3.526 -"-\<infinity> * \<infinity> = -\<infinity>" |
   3.527 -"\<infinity> * -\<infinity> = -\<infinity>" |
   3.528 -"-\<infinity> * -\<infinity> = \<infinity>"
   3.529 -proof -
   3.530 -  case (goal1 P x)
   3.531 -  moreover then obtain a b where "x = (a, b)" by (cases x) auto
   3.532 -  ultimately show P by (cases rule: extreal2_cases[of a b]) auto
   3.533 -qed simp_all
   3.534 -termination by (relation "{}") simp
   3.535 -
   3.536 -instance
   3.537 -proof
   3.538 -  fix a :: extreal show "1 * a = a"
   3.539 -    by (cases a) (simp_all add: one_extreal_def)
   3.540 -  fix b :: extreal show "a * b = b * a"
   3.541 -    by (cases rule: extreal2_cases[of a b]) simp_all
   3.542 -  fix c :: extreal show "a * b * c = a * (b * c)"
   3.543 -    by (cases rule: extreal3_cases[of a b c])
   3.544 -       (simp_all add: zero_extreal_def zero_less_mult_iff)
   3.545 -qed
   3.546 -end
   3.547 -
   3.548 -lemma real_of_extreal_le_1:
   3.549 -  fixes a :: extreal shows "a \<le> 1 \<Longrightarrow> real a \<le> 1"
   3.550 -  by (cases a) (auto simp: one_extreal_def)
   3.551 -
   3.552 -lemma abs_extreal_one[simp]: "\<bar>1\<bar> = (1::extreal)"
   3.553 -  unfolding one_extreal_def by simp
   3.554 -
   3.555 -lemma extreal_mult_zero[simp]:
   3.556 -  fixes a :: extreal shows "a * 0 = 0"
   3.557 -  by (cases a) (simp_all add: zero_extreal_def)
   3.558 -
   3.559 -lemma extreal_zero_mult[simp]:
   3.560 -  fixes a :: extreal shows "0 * a = 0"
   3.561 -  by (cases a) (simp_all add: zero_extreal_def)
   3.562 -
   3.563 -lemma extreal_m1_less_0[simp]:
   3.564 -  "-(1::extreal) < 0"
   3.565 -  by (simp add: zero_extreal_def one_extreal_def)
   3.566 -
   3.567 -lemma extreal_zero_m1[simp]:
   3.568 -  "1 \<noteq> (0::extreal)"
   3.569 -  by (simp add: zero_extreal_def one_extreal_def)
   3.570 -
   3.571 -lemma extreal_times_0[simp]:
   3.572 -  fixes x :: extreal shows "0 * x = 0"
   3.573 -  by (cases x) (auto simp: zero_extreal_def)
   3.574 -
   3.575 -lemma extreal_times[simp]:
   3.576 -  "1 \<noteq> \<infinity>" "\<infinity> \<noteq> 1"
   3.577 -  "1 \<noteq> -\<infinity>" "-\<infinity> \<noteq> 1"
   3.578 -  by (auto simp add: times_extreal_def one_extreal_def)
   3.579 -
   3.580 -lemma extreal_plus_1[simp]:
   3.581 -  "1 + extreal r = extreal (r + 1)" "extreal r + 1 = extreal (r + 1)"
   3.582 -  "1 + -\<infinity> = -\<infinity>" "-\<infinity> + 1 = -\<infinity>"
   3.583 -  unfolding one_extreal_def by auto
   3.584 -
   3.585 -lemma extreal_zero_times[simp]:
   3.586 -  fixes a b :: extreal shows "a * b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
   3.587 -  by (cases rule: extreal2_cases[of a b]) auto
   3.588 -
   3.589 -lemma extreal_mult_eq_PInfty[simp]:
   3.590 -  shows "a * b = \<infinity> \<longleftrightarrow>
   3.591 -    (a = \<infinity> \<and> b > 0) \<or> (a > 0 \<and> b = \<infinity>) \<or> (a = -\<infinity> \<and> b < 0) \<or> (a < 0 \<and> b = -\<infinity>)"
   3.592 -  by (cases rule: extreal2_cases[of a b]) auto
   3.593 -
   3.594 -lemma extreal_mult_eq_MInfty[simp]:
   3.595 -  shows "a * b = -\<infinity> \<longleftrightarrow>
   3.596 -    (a = \<infinity> \<and> b < 0) \<or> (a < 0 \<and> b = \<infinity>) \<or> (a = -\<infinity> \<and> b > 0) \<or> (a > 0 \<and> b = -\<infinity>)"
   3.597 -  by (cases rule: extreal2_cases[of a b]) auto
   3.598 -
   3.599 -lemma extreal_0_less_1[simp]: "0 < (1::extreal)"
   3.600 -  by (simp_all add: zero_extreal_def one_extreal_def)
   3.601 -
   3.602 -lemma extreal_zero_one[simp]: "0 \<noteq> (1::extreal)"
   3.603 -  by (simp_all add: zero_extreal_def one_extreal_def)
   3.604 -
   3.605 -lemma extreal_mult_minus_left[simp]:
   3.606 -  fixes a b :: extreal shows "-a * b = - (a * b)"
   3.607 -  by (cases rule: extreal2_cases[of a b]) auto
   3.608 -
   3.609 -lemma extreal_mult_minus_right[simp]:
   3.610 -  fixes a b :: extreal shows "a * -b = - (a * b)"
   3.611 -  by (cases rule: extreal2_cases[of a b]) auto
   3.612 -
   3.613 -lemma extreal_mult_infty[simp]:
   3.614 -  "a * \<infinity> = (if a = 0 then 0 else if 0 < a then \<infinity> else - \<infinity>)"
   3.615 -  by (cases a) auto
   3.616 -
   3.617 -lemma extreal_infty_mult[simp]:
   3.618 -  "\<infinity> * a = (if a = 0 then 0 else if 0 < a then \<infinity> else - \<infinity>)"
   3.619 -  by (cases a) auto
   3.620 -
   3.621 -lemma extreal_mult_strict_right_mono:
   3.622 -  assumes "a < b" and "0 < c" "c < \<infinity>"
   3.623 -  shows "a * c < b * c"
   3.624 -  using assms
   3.625 -  by (cases rule: extreal3_cases[of a b c])
   3.626 -     (auto simp: zero_le_mult_iff extreal_less_PInfty)
   3.627 -
   3.628 -lemma extreal_mult_strict_left_mono:
   3.629 -  "\<lbrakk> a < b ; 0 < c ; c < \<infinity>\<rbrakk> \<Longrightarrow> c * a < c * b"
   3.630 -  using extreal_mult_strict_right_mono by (simp add: mult_commute[of c])
   3.631 -
   3.632 -lemma extreal_mult_right_mono:
   3.633 -  fixes a b c :: extreal shows "\<lbrakk>a \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> a*c \<le> b*c"
   3.634 -  using assms
   3.635 -  apply (cases "c = 0") apply simp
   3.636 -  by (cases rule: extreal3_cases[of a b c])
   3.637 -     (auto simp: zero_le_mult_iff extreal_less_PInfty)
   3.638 -
   3.639 -lemma extreal_mult_left_mono:
   3.640 -  fixes a b c :: extreal shows "\<lbrakk>a \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> c * a \<le> c * b"
   3.641 -  using extreal_mult_right_mono by (simp add: mult_commute[of c])
   3.642 -
   3.643 -lemma zero_less_one_extreal[simp]: "0 \<le> (1::extreal)"
   3.644 -  by (simp add: one_extreal_def zero_extreal_def)
   3.645 -
   3.646 -lemma extreal_0_le_mult[simp]: "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a * (b :: extreal)"
   3.647 -  by (cases rule: extreal2_cases[of a b]) (auto simp: mult_nonneg_nonneg)
   3.648 -
   3.649 -lemma extreal_right_distrib:
   3.650 -  fixes r a b :: extreal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> r * (a + b) = r * a + r * b"
   3.651 -  by (cases rule: extreal3_cases[of r a b]) (simp_all add: field_simps)
   3.652 -
   3.653 -lemma extreal_left_distrib:
   3.654 -  fixes r a b :: extreal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> (a + b) * r = a * r + b * r"
   3.655 -  by (cases rule: extreal3_cases[of r a b]) (simp_all add: field_simps)
   3.656 -
   3.657 -lemma extreal_mult_le_0_iff:
   3.658 -  fixes a b :: extreal
   3.659 -  shows "a * b \<le> 0 \<longleftrightarrow> (0 \<le> a \<and> b \<le> 0) \<or> (a \<le> 0 \<and> 0 \<le> b)"
   3.660 -  by (cases rule: extreal2_cases[of a b]) (simp_all add: mult_le_0_iff)
   3.661 -
   3.662 -lemma extreal_zero_le_0_iff:
   3.663 -  fixes a b :: extreal
   3.664 -  shows "0 \<le> a * b \<longleftrightarrow> (0 \<le> a \<and> 0 \<le> b) \<or> (a \<le> 0 \<and> b \<le> 0)"
   3.665 -  by (cases rule: extreal2_cases[of a b]) (simp_all add: zero_le_mult_iff)
   3.666 -
   3.667 -lemma extreal_mult_less_0_iff:
   3.668 -  fixes a b :: extreal
   3.669 -  shows "a * b < 0 \<longleftrightarrow> (0 < a \<and> b < 0) \<or> (a < 0 \<and> 0 < b)"
   3.670 -  by (cases rule: extreal2_cases[of a b]) (simp_all add: mult_less_0_iff)
   3.671 -
   3.672 -lemma extreal_zero_less_0_iff:
   3.673 -  fixes a b :: extreal
   3.674 -  shows "0 < a * b \<longleftrightarrow> (0 < a \<and> 0 < b) \<or> (a < 0 \<and> b < 0)"
   3.675 -  by (cases rule: extreal2_cases[of a b]) (simp_all add: zero_less_mult_iff)
   3.676 -
   3.677 -lemma extreal_distrib:
   3.678 -  fixes a b c :: extreal
   3.679 -  assumes "a \<noteq> \<infinity> \<or> b \<noteq> -\<infinity>" "a \<noteq> -\<infinity> \<or> b \<noteq> \<infinity>" "\<bar>c\<bar> \<noteq> \<infinity>"
   3.680 -  shows "(a + b) * c = a * c + b * c"
   3.681 -  using assms
   3.682 -  by (cases rule: extreal3_cases[of a b c]) (simp_all add: field_simps)
   3.683 -
   3.684 -lemma extreal_le_epsilon:
   3.685 -  fixes x y :: extreal
   3.686 -  assumes "ALL e. 0 < e --> x <= y + e"
   3.687 -  shows "x <= y"
   3.688 -proof-
   3.689 -{ assume a: "EX r. y = extreal r"
   3.690 -  from this obtain r where r_def: "y = extreal r" by auto
   3.691 -  { assume "x=(-\<infinity>)" hence ?thesis by auto }
   3.692 -  moreover
   3.693 -  { assume "~(x=(-\<infinity>))"
   3.694 -    from this obtain p where p_def: "x = extreal p"
   3.695 -    using a assms[rule_format, of 1] by (cases x) auto
   3.696 -    { fix e have "0 < e --> p <= r + e"
   3.697 -      using assms[rule_format, of "extreal e"] p_def r_def by auto }
   3.698 -    hence "p <= r" apply (subst field_le_epsilon) by auto
   3.699 -    hence ?thesis using r_def p_def by auto
   3.700 -  } ultimately have ?thesis by blast
   3.701 -}
   3.702 -moreover
   3.703 -{ assume "y=(-\<infinity>) | y=\<infinity>" hence ?thesis
   3.704 -    using assms[rule_format, of 1] by (cases x) auto
   3.705 -} ultimately show ?thesis by (cases y) auto
   3.706 -qed
   3.707 -
   3.708 -
   3.709 -lemma extreal_le_epsilon2:
   3.710 -  fixes x y :: extreal
   3.711 -  assumes "ALL e. 0 < e --> x <= y + extreal e"
   3.712 -  shows "x <= y"
   3.713 -proof-
   3.714 -{ fix e :: extreal assume "e>0"
   3.715 -  { assume "e=\<infinity>" hence "x<=y+e" by auto }
   3.716 -  moreover
   3.717 -  { assume "e~=\<infinity>"
   3.718 -    from this obtain r where "e = extreal r" using `e>0` apply (cases e) by auto
   3.719 -    hence "x<=y+e" using assms[rule_format, of r] `e>0` by auto
   3.720 -  } ultimately have "x<=y+e" by blast
   3.721 -} from this show ?thesis using extreal_le_epsilon by auto
   3.722 -qed
   3.723 -
   3.724 -lemma extreal_le_real:
   3.725 -  fixes x y :: extreal
   3.726 -  assumes "ALL z. x <= extreal z --> y <= extreal z"
   3.727 -  shows "y <= x"
   3.728 -by (metis assms extreal.exhaust extreal_bot extreal_less_eq(1)
   3.729 -          extreal_less_eq(2) order_refl uminus_extreal.simps(2))
   3.730 -
   3.731 -lemma extreal_le_extreal:
   3.732 -  fixes x y :: extreal
   3.733 -  assumes "\<And>B. B < x \<Longrightarrow> B <= y"
   3.734 -  shows "x <= y"
   3.735 -by (metis assms extreal_dense leD linorder_le_less_linear)
   3.736 -
   3.737 -lemma extreal_ge_extreal:
   3.738 -  fixes x y :: extreal
   3.739 -  assumes "ALL B. B>x --> B >= y"
   3.740 -  shows "x >= y"
   3.741 -by (metis assms extreal_dense leD linorder_le_less_linear)
   3.742 -
   3.743 -lemma setprod_extreal_0:
   3.744 -  fixes f :: "'a \<Rightarrow> extreal"
   3.745 -  shows "(\<Prod>i\<in>A. f i) = 0 \<longleftrightarrow> (finite A \<and> (\<exists>i\<in>A. f i = 0))"
   3.746 -proof cases
   3.747 -  assume "finite A"
   3.748 -  then show ?thesis by (induct A) auto
   3.749 -qed auto
   3.750 -
   3.751 -lemma setprod_extreal_pos:
   3.752 -  fixes f :: "'a \<Rightarrow> extreal" assumes pos: "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i" shows "0 \<le> (\<Prod>i\<in>I. f i)"
   3.753 -proof cases
   3.754 -  assume "finite I" from this pos show ?thesis by induct auto
   3.755 -qed simp
   3.756 -
   3.757 -lemma setprod_PInf:
   3.758 -  assumes "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i"
   3.759 -  shows "(\<Prod>i\<in>I. f i) = \<infinity> \<longleftrightarrow> finite I \<and> (\<exists>i\<in>I. f i = \<infinity>) \<and> (\<forall>i\<in>I. f i \<noteq> 0)"
   3.760 -proof cases
   3.761 -  assume "finite I" from this assms show ?thesis
   3.762 -  proof (induct I)
   3.763 -    case (insert i I)
   3.764 -    then have pos: "0 \<le> f i" "0 \<le> setprod f I" by (auto intro!: setprod_extreal_pos)
   3.765 -    from insert have "(\<Prod>j\<in>insert i I. f j) = \<infinity> \<longleftrightarrow> setprod f I * f i = \<infinity>" by auto
   3.766 -    also have "\<dots> \<longleftrightarrow> (setprod f I = \<infinity> \<or> f i = \<infinity>) \<and> f i \<noteq> 0 \<and> setprod f I \<noteq> 0"
   3.767 -      using setprod_extreal_pos[of I f] pos
   3.768 -      by (cases rule: extreal2_cases[of "f i" "setprod f I"]) auto
   3.769 -    also have "\<dots> \<longleftrightarrow> finite (insert i I) \<and> (\<exists>j\<in>insert i I. f j = \<infinity>) \<and> (\<forall>j\<in>insert i I. f j \<noteq> 0)"
   3.770 -      using insert by (auto simp: setprod_extreal_0)
   3.771 -    finally show ?case .
   3.772 -  qed simp
   3.773 -qed simp
   3.774 -
   3.775 -lemma setprod_extreal: "(\<Prod>i\<in>A. extreal (f i)) = extreal (setprod f A)"
   3.776 -proof cases
   3.777 -  assume "finite A" then show ?thesis
   3.778 -    by induct (auto simp: one_extreal_def)
   3.779 -qed (simp add: one_extreal_def)
   3.780 -
   3.781 -subsubsection {* Power *}
   3.782 -
   3.783 -instantiation extreal :: power
   3.784 -begin
   3.785 -primrec power_extreal where
   3.786 -  "power_extreal x 0 = 1" |
   3.787 -  "power_extreal x (Suc n) = x * x ^ n"
   3.788 -instance ..
   3.789 -end
   3.790 -
   3.791 -lemma extreal_power[simp]: "(extreal x) ^ n = extreal (x^n)"
   3.792 -  by (induct n) (auto simp: one_extreal_def)
   3.793 -
   3.794 -lemma extreal_power_PInf[simp]: "\<infinity> ^ n = (if n = 0 then 1 else \<infinity>)"
   3.795 -  by (induct n) (auto simp: one_extreal_def)
   3.796 -
   3.797 -lemma extreal_power_uminus[simp]:
   3.798 -  fixes x :: extreal
   3.799 -  shows "(- x) ^ n = (if even n then x ^ n else - (x^n))"
   3.800 -  by (induct n) (auto simp: one_extreal_def)
   3.801 -
   3.802 -lemma extreal_power_number_of[simp]:
   3.803 -  "(number_of num :: extreal) ^ n = extreal (number_of num ^ n)"
   3.804 -  by (induct n) (auto simp: one_extreal_def)
   3.805 -
   3.806 -lemma zero_le_power_extreal[simp]:
   3.807 -  fixes a :: extreal assumes "0 \<le> a"
   3.808 -  shows "0 \<le> a ^ n"
   3.809 -  using assms by (induct n) (auto simp: extreal_zero_le_0_iff)
   3.810 -
   3.811 -subsubsection {* Subtraction *}
   3.812 -
   3.813 -lemma extreal_minus_minus_image[simp]:
   3.814 -  fixes S :: "extreal set"
   3.815 -  shows "uminus ` uminus ` S = S"
   3.816 -  by (auto simp: image_iff)
   3.817 -
   3.818 -lemma extreal_uminus_lessThan[simp]:
   3.819 -  fixes a :: extreal shows "uminus ` {..<a} = {-a<..}"
   3.820 -proof (safe intro!: image_eqI)
   3.821 -  fix x assume "-a < x"
   3.822 -  then have "- x < - (- a)" by (simp del: extreal_uminus_uminus)
   3.823 -  then show "- x < a" by simp
   3.824 -qed auto
   3.825 -
   3.826 -lemma extreal_uminus_greaterThan[simp]:
   3.827 -  "uminus ` {(a::extreal)<..} = {..<-a}"
   3.828 -  by (metis extreal_uminus_lessThan extreal_uminus_uminus
   3.829 -            extreal_minus_minus_image)
   3.830 -
   3.831 -instantiation extreal :: minus
   3.832 -begin
   3.833 -definition "x - y = x + -(y::extreal)"
   3.834 -instance ..
   3.835 -end
   3.836 -
   3.837 -lemma extreal_minus[simp]:
   3.838 -  "extreal r - extreal p = extreal (r - p)"
   3.839 -  "-\<infinity> - extreal r = -\<infinity>"
   3.840 -  "extreal r - \<infinity> = -\<infinity>"
   3.841 -  "\<infinity> - x = \<infinity>"
   3.842 -  "-\<infinity> - \<infinity> = -\<infinity>"
   3.843 -  "x - -y = x + y"
   3.844 -  "x - 0 = x"
   3.845 -  "0 - x = -x"
   3.846 -  by (simp_all add: minus_extreal_def)
   3.847 -
   3.848 -lemma extreal_x_minus_x[simp]:
   3.849 -  "x - x = (if \<bar>x\<bar> = \<infinity> then \<infinity> else 0)"
   3.850 -  by (cases x) simp_all
   3.851 -
   3.852 -lemma extreal_eq_minus_iff:
   3.853 -  fixes x y z :: extreal
   3.854 -  shows "x = z - y \<longleftrightarrow>
   3.855 -    (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x + y = z) \<and>
   3.856 -    (y = -\<infinity> \<longrightarrow> x = \<infinity>) \<and>
   3.857 -    (y = \<infinity> \<longrightarrow> z = \<infinity> \<longrightarrow> x = \<infinity>) \<and>
   3.858 -    (y = \<infinity> \<longrightarrow> z \<noteq> \<infinity> \<longrightarrow> x = -\<infinity>)"
   3.859 -  by (cases rule: extreal3_cases[of x y z]) auto
   3.860 -
   3.861 -lemma extreal_eq_minus:
   3.862 -  fixes x y z :: extreal
   3.863 -  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x = z - y \<longleftrightarrow> x + y = z"
   3.864 -  by (auto simp: extreal_eq_minus_iff)
   3.865 -
   3.866 -lemma extreal_less_minus_iff:
   3.867 -  fixes x y z :: extreal
   3.868 -  shows "x < z - y \<longleftrightarrow>
   3.869 -    (y = \<infinity> \<longrightarrow> z = \<infinity> \<and> x \<noteq> \<infinity>) \<and>
   3.870 -    (y = -\<infinity> \<longrightarrow> x \<noteq> \<infinity>) \<and>
   3.871 -    (\<bar>y\<bar> \<noteq> \<infinity>\<longrightarrow> x + y < z)"
   3.872 -  by (cases rule: extreal3_cases[of x y z]) auto
   3.873 -
   3.874 -lemma extreal_less_minus:
   3.875 -  fixes x y z :: extreal
   3.876 -  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x < z - y \<longleftrightarrow> x + y < z"
   3.877 -  by (auto simp: extreal_less_minus_iff)
   3.878 -
   3.879 -lemma extreal_le_minus_iff:
   3.880 -  fixes x y z :: extreal
   3.881 -  shows "x \<le> z - y \<longleftrightarrow>
   3.882 -    (y = \<infinity> \<longrightarrow> z \<noteq> \<infinity> \<longrightarrow> x = -\<infinity>) \<and>
   3.883 -    (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x + y \<le> z)"
   3.884 -  by (cases rule: extreal3_cases[of x y z]) auto
   3.885 -
   3.886 -lemma extreal_le_minus:
   3.887 -  fixes x y z :: extreal
   3.888 -  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x \<le> z - y \<longleftrightarrow> x + y \<le> z"
   3.889 -  by (auto simp: extreal_le_minus_iff)
   3.890 -
   3.891 -lemma extreal_minus_less_iff:
   3.892 -  fixes x y z :: extreal
   3.893 -  shows "x - y < z \<longleftrightarrow>
   3.894 -    y \<noteq> -\<infinity> \<and> (y = \<infinity> \<longrightarrow> x \<noteq> \<infinity> \<and> z \<noteq> -\<infinity>) \<and>
   3.895 -    (y \<noteq> \<infinity> \<longrightarrow> x < z + y)"
   3.896 -  by (cases rule: extreal3_cases[of x y z]) auto
   3.897 -
   3.898 -lemma extreal_minus_less:
   3.899 -  fixes x y z :: extreal
   3.900 -  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x - y < z \<longleftrightarrow> x < z + y"
   3.901 -  by (auto simp: extreal_minus_less_iff)
   3.902 -
   3.903 -lemma extreal_minus_le_iff:
   3.904 -  fixes x y z :: extreal
   3.905 -  shows "x - y \<le> z \<longleftrightarrow>
   3.906 -    (y = -\<infinity> \<longrightarrow> z = \<infinity>) \<and>
   3.907 -    (y = \<infinity> \<longrightarrow> x = \<infinity> \<longrightarrow> z = \<infinity>) \<and>
   3.908 -    (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x \<le> z + y)"
   3.909 -  by (cases rule: extreal3_cases[of x y z]) auto
   3.910 -
   3.911 -lemma extreal_minus_le:
   3.912 -  fixes x y z :: extreal
   3.913 -  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x - y \<le> z \<longleftrightarrow> x \<le> z + y"
   3.914 -  by (auto simp: extreal_minus_le_iff)
   3.915 -
   3.916 -lemma extreal_minus_eq_minus_iff:
   3.917 -  fixes a b c :: extreal
   3.918 -  shows "a - b = a - c \<longleftrightarrow>
   3.919 -    b = c \<or> a = \<infinity> \<or> (a = -\<infinity> \<and> b \<noteq> -\<infinity> \<and> c \<noteq> -\<infinity>)"
   3.920 -  by (cases rule: extreal3_cases[of a b c]) auto
   3.921 -
   3.922 -lemma extreal_add_le_add_iff:
   3.923 -  "c + a \<le> c + b \<longleftrightarrow>
   3.924 -    a \<le> b \<or> c = \<infinity> \<or> (c = -\<infinity> \<and> a \<noteq> \<infinity> \<and> b \<noteq> \<infinity>)"
   3.925 -  by (cases rule: extreal3_cases[of a b c]) (simp_all add: field_simps)
   3.926 -
   3.927 -lemma extreal_mult_le_mult_iff:
   3.928 -  "\<bar>c\<bar> \<noteq> \<infinity> \<Longrightarrow> c * a \<le> c * b \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
   3.929 -  by (cases rule: extreal3_cases[of a b c]) (simp_all add: mult_le_cancel_left)
   3.930 -
   3.931 -lemma extreal_minus_mono:
   3.932 -  fixes A B C D :: extreal assumes "A \<le> B" "D \<le> C"
   3.933 -  shows "A - C \<le> B - D"
   3.934 -  using assms
   3.935 -  by (cases rule: extreal3_cases[case_product extreal_cases, of A B C D]) simp_all
   3.936 -
   3.937 -lemma real_of_extreal_minus:
   3.938 -  "real (a - b) = (if \<bar>a\<bar> = \<infinity> \<or> \<bar>b\<bar> = \<infinity> then 0 else real a - real b)"
   3.939 -  by (cases rule: extreal2_cases[of a b]) auto
   3.940 -
   3.941 -lemma extreal_diff_positive:
   3.942 -  fixes a b :: extreal shows "a \<le> b \<Longrightarrow> 0 \<le> b - a"
   3.943 -  by (cases rule: extreal2_cases[of a b]) auto
   3.944 -
   3.945 -lemma extreal_between:
   3.946 -  fixes x e :: extreal
   3.947 -  assumes "\<bar>x\<bar> \<noteq> \<infinity>" "0 < e"
   3.948 -  shows "x - e < x" "x < x + e"
   3.949 -using assms apply (cases x, cases e) apply auto
   3.950 -using assms by (cases x, cases e) auto
   3.951 -
   3.952 -subsubsection {* Division *}
   3.953 -
   3.954 -instantiation extreal :: inverse
   3.955 -begin
   3.956 -
   3.957 -function inverse_extreal where
   3.958 -"inverse (extreal r) = (if r = 0 then \<infinity> else extreal (inverse r))" |
   3.959 -"inverse \<infinity> = 0" |
   3.960 -"inverse (-\<infinity>) = 0"
   3.961 -  by (auto intro: extreal_cases)
   3.962 -termination by (relation "{}") simp
   3.963 -
   3.964 -definition "x / y = x * inverse (y :: extreal)"
   3.965 -
   3.966 -instance proof qed
   3.967 -end
   3.968 -
   3.969 -lemma real_of_extreal_inverse[simp]:
   3.970 -  fixes a :: extreal
   3.971 -  shows "real (inverse a) = 1 / real a"
   3.972 -  by (cases a) (auto simp: inverse_eq_divide)
   3.973 -
   3.974 -lemma extreal_inverse[simp]:
   3.975 -  "inverse 0 = \<infinity>"
   3.976 -  "inverse (1::extreal) = 1"
   3.977 -  by (simp_all add: one_extreal_def zero_extreal_def)
   3.978 -
   3.979 -lemma extreal_divide[simp]:
   3.980 -  "extreal r / extreal p = (if p = 0 then extreal r * \<infinity> else extreal (r / p))"
   3.981 -  unfolding divide_extreal_def by (auto simp: divide_real_def)
   3.982 -
   3.983 -lemma extreal_divide_same[simp]:
   3.984 -  "x / x = (if \<bar>x\<bar> = \<infinity> \<or> x = 0 then 0 else 1)"
   3.985 -  by (cases x)
   3.986 -     (simp_all add: divide_real_def divide_extreal_def one_extreal_def)
   3.987 -
   3.988 -lemma extreal_inv_inv[simp]:
   3.989 -  "inverse (inverse x) = (if x \<noteq> -\<infinity> then x else \<infinity>)"
   3.990 -  by (cases x) auto
   3.991 -
   3.992 -lemma extreal_inverse_minus[simp]:
   3.993 -  "inverse (- x) = (if x = 0 then \<infinity> else -inverse x)"
   3.994 -  by (cases x) simp_all
   3.995 -
   3.996 -lemma extreal_uminus_divide[simp]:
   3.997 -  fixes x y :: extreal shows "- x / y = - (x / y)"
   3.998 -  unfolding divide_extreal_def by simp
   3.999 -
  3.1000 -lemma extreal_divide_Infty[simp]:
  3.1001 -  "x / \<infinity> = 0" "x / -\<infinity> = 0"
  3.1002 -  unfolding divide_extreal_def by simp_all
  3.1003 -
  3.1004 -lemma extreal_divide_one[simp]:
  3.1005 -  "x / 1 = (x::extreal)"
  3.1006 -  unfolding divide_extreal_def by simp
  3.1007 -
  3.1008 -lemma extreal_divide_extreal[simp]:
  3.1009 -  "\<infinity> / extreal r = (if 0 \<le> r then \<infinity> else -\<infinity>)"
  3.1010 -  unfolding divide_extreal_def by simp
  3.1011 -
  3.1012 -lemma zero_le_divide_extreal[simp]:
  3.1013 -  fixes a :: extreal assumes "0 \<le> a" "0 \<le> b"
  3.1014 -  shows "0 \<le> a / b"
  3.1015 -  using assms by (cases rule: extreal2_cases[of a b]) (auto simp: zero_le_divide_iff)
  3.1016 -
  3.1017 -lemma extreal_le_divide_pos:
  3.1018 -  "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y \<le> z / x \<longleftrightarrow> x * y \<le> z"
  3.1019 -  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
  3.1020 -
  3.1021 -lemma extreal_divide_le_pos:
  3.1022 -  "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> z / x \<le> y \<longleftrightarrow> z \<le> x * y"
  3.1023 -  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
  3.1024 -
  3.1025 -lemma extreal_le_divide_neg:
  3.1026 -  "x < 0 \<Longrightarrow> x \<noteq> -\<infinity> \<Longrightarrow> y \<le> z / x \<longleftrightarrow> z \<le> x * y"
  3.1027 -  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
  3.1028 -
  3.1029 -lemma extreal_divide_le_neg:
  3.1030 -  "x < 0 \<Longrightarrow> x \<noteq> -\<infinity> \<Longrightarrow> z / x \<le> y \<longleftrightarrow> x * y \<le> z"
  3.1031 -  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
  3.1032 -
  3.1033 -lemma extreal_inverse_antimono_strict:
  3.1034 -  fixes x y :: extreal
  3.1035 -  shows "0 \<le> x \<Longrightarrow> x < y \<Longrightarrow> inverse y < inverse x"
  3.1036 -  by (cases rule: extreal2_cases[of x y]) auto
  3.1037 -
  3.1038 -lemma extreal_inverse_antimono:
  3.1039 -  fixes x y :: extreal
  3.1040 -  shows "0 \<le> x \<Longrightarrow> x <= y \<Longrightarrow> inverse y <= inverse x"
  3.1041 -  by (cases rule: extreal2_cases[of x y]) auto
  3.1042 -
  3.1043 -lemma inverse_inverse_Pinfty_iff[simp]:
  3.1044 -  "inverse x = \<infinity> \<longleftrightarrow> x = 0"
  3.1045 -  by (cases x) auto
  3.1046 -
  3.1047 -lemma extreal_inverse_eq_0:
  3.1048 -  "inverse x = 0 \<longleftrightarrow> x = \<infinity> \<or> x = -\<infinity>"
  3.1049 -  by (cases x) auto
  3.1050 -
  3.1051 -lemma extreal_0_gt_inverse:
  3.1052 -  fixes x :: extreal shows "0 < inverse x \<longleftrightarrow> x \<noteq> \<infinity> \<and> 0 \<le> x"
  3.1053 -  by (cases x) auto
  3.1054 -
  3.1055 -lemma extreal_mult_less_right:
  3.1056 -  assumes "b * a < c * a" "0 < a" "a < \<infinity>"
  3.1057 -  shows "b < c"
  3.1058 -  using assms
  3.1059 -  by (cases rule: extreal3_cases[of a b c])
  3.1060 -     (auto split: split_if_asm simp: zero_less_mult_iff zero_le_mult_iff)
  3.1061 -
  3.1062 -lemma extreal_power_divide:
  3.1063 -  "y \<noteq> 0 \<Longrightarrow> (x / y :: extreal) ^ n = x^n / y^n"
  3.1064 -  by (cases rule: extreal2_cases[of x y])
  3.1065 -     (auto simp: one_extreal_def zero_extreal_def power_divide not_le
  3.1066 -                 power_less_zero_eq zero_le_power_iff)
  3.1067 -
  3.1068 -lemma extreal_le_mult_one_interval:
  3.1069 -  fixes x y :: extreal
  3.1070 -  assumes y: "y \<noteq> -\<infinity>"
  3.1071 -  assumes z: "\<And>z. \<lbrakk> 0 < z ; z < 1 \<rbrakk> \<Longrightarrow> z * x \<le> y"
  3.1072 -  shows "x \<le> y"
  3.1073 -proof (cases x)
  3.1074 -  case PInf with z[of "1 / 2"] show "x \<le> y" by (simp add: one_extreal_def)
  3.1075 -next
  3.1076 -  case (real r) note r = this
  3.1077 -  show "x \<le> y"
  3.1078 -  proof (cases y)
  3.1079 -    case (real p) note p = this
  3.1080 -    have "r \<le> p"
  3.1081 -    proof (rule field_le_mult_one_interval)
  3.1082 -      fix z :: real assume "0 < z" and "z < 1"
  3.1083 -      with z[of "extreal z"]
  3.1084 -      show "z * r \<le> p" using p r by (auto simp: zero_le_mult_iff one_extreal_def)
  3.1085 -    qed
  3.1086 -    then show "x \<le> y" using p r by simp
  3.1087 -  qed (insert y, simp_all)
  3.1088 -qed simp
  3.1089 -
  3.1090 -subsection "Complete lattice"
  3.1091 -
  3.1092 -instantiation extreal :: lattice
  3.1093 -begin
  3.1094 -definition [simp]: "sup x y = (max x y :: extreal)"
  3.1095 -definition [simp]: "inf x y = (min x y :: extreal)"
  3.1096 -instance proof qed simp_all
  3.1097 -end
  3.1098 -
  3.1099 -instantiation extreal :: complete_lattice
  3.1100 -begin
  3.1101 -
  3.1102 -definition "bot = -\<infinity>"
  3.1103 -definition "top = \<infinity>"
  3.1104 -
  3.1105 -definition "Sup S = (LEAST z. ALL x:S. x <= z :: extreal)"
  3.1106 -definition "Inf S = (GREATEST z. ALL x:S. z <= x :: extreal)"
  3.1107 -
  3.1108 -lemma extreal_complete_Sup:
  3.1109 -  fixes S :: "extreal set" assumes "S \<noteq> {}"
  3.1110 -  shows "\<exists>x. (\<forall>y\<in>S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>S. y \<le> z) \<longrightarrow> x \<le> z)"
  3.1111 -proof cases
  3.1112 -  assume "\<exists>x. \<forall>a\<in>S. a \<le> extreal x"
  3.1113 -  then obtain y where y: "\<And>a. a\<in>S \<Longrightarrow> a \<le> extreal y" by auto
  3.1114 -  then have "\<infinity> \<notin> S" by force
  3.1115 -  show ?thesis
  3.1116 -  proof cases
  3.1117 -    assume "S = {-\<infinity>}"
  3.1118 -    then show ?thesis by (auto intro!: exI[of _ "-\<infinity>"])
  3.1119 -  next
  3.1120 -    assume "S \<noteq> {-\<infinity>}"
  3.1121 -    with `S \<noteq> {}` `\<infinity> \<notin> S` obtain x where "x \<in> S - {-\<infinity>}" "x \<noteq> \<infinity>" by auto
  3.1122 -    with y `\<infinity> \<notin> S` have "\<forall>z\<in>real ` (S - {-\<infinity>}). z \<le> y"
  3.1123 -      by (auto simp: real_of_extreal_ord_simps)
  3.1124 -    with reals_complete2[of "real ` (S - {-\<infinity>})"] `x \<in> S - {-\<infinity>}`
  3.1125 -    obtain s where s:
  3.1126 -       "\<forall>y\<in>S - {-\<infinity>}. real y \<le> s" "\<And>z. (\<forall>y\<in>S - {-\<infinity>}. real y \<le> z) \<Longrightarrow> s \<le> z"
  3.1127 -       by auto
  3.1128 -    show ?thesis
  3.1129 -    proof (safe intro!: exI[of _ "extreal s"])
  3.1130 -      fix z assume "z \<in> S" with `\<infinity> \<notin> S` show "z \<le> extreal s"
  3.1131 -      proof (cases z)
  3.1132 -        case (real r)
  3.1133 -        then show ?thesis
  3.1134 -          using s(1)[rule_format, of z] `z \<in> S` `z = extreal r` by auto
  3.1135 -      qed auto
  3.1136 -    next
  3.1137 -      fix z assume *: "\<forall>y\<in>S. y \<le> z"
  3.1138 -      with `S \<noteq> {-\<infinity>}` `S \<noteq> {}` show "extreal s \<le> z"
  3.1139 -      proof (cases z)
  3.1140 -        case (real u)
  3.1141 -        with * have "s \<le> u"
  3.1142 -          by (intro s(2)[of u]) (auto simp: real_of_extreal_ord_simps)
  3.1143 -        then show ?thesis using real by simp
  3.1144 -      qed auto
  3.1145 -    qed
  3.1146 -  qed
  3.1147 -next
  3.1148 -  assume *: "\<not> (\<exists>x. \<forall>a\<in>S. a \<le> extreal x)"
  3.1149 -  show ?thesis
  3.1150 -  proof (safe intro!: exI[of _ \<infinity>])
  3.1151 -    fix y assume **: "\<forall>z\<in>S. z \<le> y"
  3.1152 -    with * show "\<infinity> \<le> y"
  3.1153 -    proof (cases y)
  3.1154 -      case MInf with * ** show ?thesis by (force simp: not_le)
  3.1155 -    qed auto
  3.1156 -  qed simp
  3.1157 -qed
  3.1158 -
  3.1159 -lemma extreal_complete_Inf:
  3.1160 -  fixes S :: "extreal set" assumes "S ~= {}"
  3.1161 -  shows "EX x. (ALL y:S. x <= y) & (ALL z. (ALL y:S. z <= y) --> z <= x)"
  3.1162 -proof-
  3.1163 -def S1 == "uminus ` S"
  3.1164 -hence "S1 ~= {}" using assms by auto
  3.1165 -from this obtain x where x_def: "(ALL y:S1. y <= x) & (ALL z. (ALL y:S1. y <= z) --> x <= z)"
  3.1166 -   using extreal_complete_Sup[of S1] by auto
  3.1167 -{ fix z assume "ALL y:S. z <= y"
  3.1168 -  hence "ALL y:S1. y <= -z" unfolding S1_def by auto
  3.1169 -  hence "x <= -z" using x_def by auto
  3.1170 -  hence "z <= -x"
  3.1171 -    apply (subst extreal_uminus_uminus[symmetric])
  3.1172 -    unfolding extreal_minus_le_minus . }
  3.1173 -moreover have "(ALL y:S. -x <= y)"
  3.1174 -   using x_def unfolding S1_def
  3.1175 -   apply simp
  3.1176 -   apply (subst (3) extreal_uminus_uminus[symmetric])
  3.1177 -   unfolding extreal_minus_le_minus by simp
  3.1178 -ultimately show ?thesis by auto
  3.1179 -qed
  3.1180 -
  3.1181 -lemma extreal_complete_uminus_eq:
  3.1182 -  fixes S :: "extreal set"
  3.1183 -  shows "(\<forall>y\<in>uminus`S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>uminus`S. y \<le> z) \<longrightarrow> x \<le> z)
  3.1184 -     \<longleftrightarrow> (\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> -x)"
  3.1185 -  by simp (metis extreal_minus_le_minus extreal_uminus_uminus)
  3.1186 -
  3.1187 -lemma extreal_Sup_uminus_image_eq:
  3.1188 -  fixes S :: "extreal set"
  3.1189 -  shows "Sup (uminus ` S) = - Inf S"
  3.1190 -proof cases
  3.1191 -  assume "S = {}"
  3.1192 -  moreover have "(THE x. All (op \<le> x)) = (-\<infinity>::extreal)"
  3.1193 -    by (rule the_equality) (auto intro!: extreal_bot)
  3.1194 -  moreover have "(SOME x. \<forall>y. y \<le> x) = (\<infinity>::extreal)"
  3.1195 -    by (rule some_equality) (auto intro!: extreal_top)
  3.1196 -  ultimately show ?thesis unfolding Inf_extreal_def Sup_extreal_def
  3.1197 -    Least_def Greatest_def GreatestM_def by simp
  3.1198 -next
  3.1199 -  assume "S \<noteq> {}"
  3.1200 -  with extreal_complete_Sup[of "uminus`S"]
  3.1201 -  obtain x where x: "(\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> -x)"
  3.1202 -    unfolding extreal_complete_uminus_eq by auto
  3.1203 -  show "Sup (uminus ` S) = - Inf S"
  3.1204 -    unfolding Inf_extreal_def Greatest_def GreatestM_def
  3.1205 -  proof (intro someI2[of _ _ "\<lambda>x. Sup (uminus`S) = - x"])
  3.1206 -    show "(\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>y. (\<forall>z\<in>S. y \<le> z) \<longrightarrow> y \<le> -x)"
  3.1207 -      using x .
  3.1208 -    fix x' assume "(\<forall>y\<in>S. x' \<le> y) \<and> (\<forall>y. (\<forall>z\<in>S. y \<le> z) \<longrightarrow> y \<le> x')"
  3.1209 -    then have "(\<forall>y\<in>uminus`S. y \<le> - x') \<and> (\<forall>y. (\<forall>z\<in>uminus`S. z \<le> y) \<longrightarrow> - x' \<le> y)"
  3.1210 -      unfolding extreal_complete_uminus_eq by simp
  3.1211 -    then show "Sup (uminus ` S) = -x'"
  3.1212 -      unfolding Sup_extreal_def extreal_uminus_eq_iff
  3.1213 -      by (intro Least_equality) auto
  3.1214 -  qed
  3.1215 -qed
  3.1216 -
  3.1217 -instance
  3.1218 -proof
  3.1219 -  { fix x :: extreal and A
  3.1220 -    show "bot <= x" by (cases x) (simp_all add: bot_extreal_def)
  3.1221 -    show "x <= top" by (simp add: top_extreal_def) }
  3.1222 -
  3.1223 -  { fix x :: extreal and A assume "x : A"
  3.1224 -    with extreal_complete_Sup[of A]
  3.1225 -    obtain s where s: "\<forall>y\<in>A. y <= s" "\<forall>z. (\<forall>y\<in>A. y <= z) \<longrightarrow> s <= z" by auto
  3.1226 -    hence "x <= s" using `x : A` by auto
  3.1227 -    also have "... = Sup A" using s unfolding Sup_extreal_def
  3.1228 -      by (auto intro!: Least_equality[symmetric])
  3.1229 -    finally show "x <= Sup A" . }
  3.1230 -  note le_Sup = this
  3.1231 -
  3.1232 -  { fix x :: extreal and A assume *: "!!z. (z : A ==> z <= x)"
  3.1233 -    show "Sup A <= x"
  3.1234 -    proof (cases "A = {}")
  3.1235 -      case True
  3.1236 -      hence "Sup A = -\<infinity>" unfolding Sup_extreal_def
  3.1237 -        by (auto intro!: Least_equality)
  3.1238 -      thus "Sup A <= x" by simp
  3.1239 -    next
  3.1240 -      case False
  3.1241 -      with extreal_complete_Sup[of A]
  3.1242 -      obtain s where s: "\<forall>y\<in>A. y <= s" "\<forall>z. (\<forall>y\<in>A. y <= z) \<longrightarrow> s <= z" by auto
  3.1243 -      hence "Sup A = s"
  3.1244 -        unfolding Sup_extreal_def by (auto intro!: Least_equality)
  3.1245 -      also have "s <= x" using * s by auto
  3.1246 -      finally show "Sup A <= x" .
  3.1247 -    qed }
  3.1248 -  note Sup_le = this
  3.1249 -
  3.1250 -  { fix x :: extreal and A assume "x \<in> A"
  3.1251 -    with le_Sup[of "-x" "uminus`A"] show "Inf A \<le> x"
  3.1252 -      unfolding extreal_Sup_uminus_image_eq by simp }
  3.1253 -
  3.1254 -  { fix x :: extreal and A assume *: "!!z. (z : A ==> x <= z)"
  3.1255 -    with Sup_le[of "uminus`A" "-x"] show "x \<le> Inf A"
  3.1256 -      unfolding extreal_Sup_uminus_image_eq by force }
  3.1257 -qed
  3.1258 -end
  3.1259 -
  3.1260 -lemma extreal_SUPR_uminus:
  3.1261 -  fixes f :: "'a => extreal"
  3.1262 -  shows "(SUP i : R. -(f i)) = -(INF i : R. f i)"
  3.1263 -  unfolding SUPR_def INFI_def
  3.1264 -  using extreal_Sup_uminus_image_eq[of "f`R"]
  3.1265 -  by (simp add: image_image)
  3.1266 -
  3.1267 -lemma extreal_INFI_uminus:
  3.1268 -  fixes f :: "'a => extreal"
  3.1269 -  shows "(INF i : R. -(f i)) = -(SUP i : R. f i)"
  3.1270 -  using extreal_SUPR_uminus[of _ "\<lambda>x. - f x"] by simp
  3.1271 -
  3.1272 -lemma extreal_Inf_uminus_image_eq: "Inf (uminus ` S) = - Sup (S::extreal set)"
  3.1273 -  using extreal_Sup_uminus_image_eq[of "uminus ` S"] by (simp add: image_image)
  3.1274 -
  3.1275 -lemma extreal_inj_on_uminus[intro, simp]: "inj_on uminus (A :: extreal set)"
  3.1276 -  by (auto intro!: inj_onI)
  3.1277 -
  3.1278 -lemma extreal_image_uminus_shift:
  3.1279 -  fixes X Y :: "extreal set" shows "uminus ` X = Y \<longleftrightarrow> X = uminus ` Y"
  3.1280 -proof
  3.1281 -  assume "uminus ` X = Y"
  3.1282 -  then have "uminus ` uminus ` X = uminus ` Y"
  3.1283 -    by (simp add: inj_image_eq_iff)
  3.1284 -  then show "X = uminus ` Y" by (simp add: image_image)
  3.1285 -qed (simp add: image_image)
  3.1286 -
  3.1287 -lemma Inf_extreal_iff:
  3.1288 -  fixes z :: extreal
  3.1289 -  shows "(!!x. x:X ==> z <= x) ==> (EX x:X. x<y) <-> Inf X < y"
  3.1290 -  by (metis complete_lattice_class.Inf_greatest complete_lattice_class.Inf_lower less_le_not_le linear
  3.1291 -            order_less_le_trans)
  3.1292 -
  3.1293 -lemma Sup_eq_MInfty:
  3.1294 -  fixes S :: "extreal set" shows "Sup S = -\<infinity> \<longleftrightarrow> S = {} \<or> S = {-\<infinity>}"
  3.1295 -proof
  3.1296 -  assume a: "Sup S = -\<infinity>"
  3.1297 -  with complete_lattice_class.Sup_upper[of _ S]
  3.1298 -  show "S={} \<or> S={-\<infinity>}" by auto
  3.1299 -next
  3.1300 -  assume "S={} \<or> S={-\<infinity>}" then show "Sup S = -\<infinity>"
  3.1301 -    unfolding Sup_extreal_def by (auto intro!: Least_equality)
  3.1302 -qed
  3.1303 -
  3.1304 -lemma Inf_eq_PInfty:
  3.1305 -  fixes S :: "extreal set" shows "Inf S = \<infinity> \<longleftrightarrow> S = {} \<or> S = {\<infinity>}"
  3.1306 -  using Sup_eq_MInfty[of "uminus`S"]
  3.1307 -  unfolding extreal_Sup_uminus_image_eq extreal_image_uminus_shift by simp
  3.1308 -
  3.1309 -lemma Inf_eq_MInfty: "-\<infinity> : S ==> Inf S = -\<infinity>"
  3.1310 -  unfolding Inf_extreal_def
  3.1311 -  by (auto intro!: Greatest_equality)
  3.1312 -
  3.1313 -lemma Sup_eq_PInfty: "\<infinity> : S ==> Sup S = \<infinity>"
  3.1314 -  unfolding Sup_extreal_def
  3.1315 -  by (auto intro!: Least_equality)
  3.1316 -
  3.1317 -lemma extreal_SUPI:
  3.1318 -  fixes x :: extreal
  3.1319 -  assumes "!!i. i : A ==> f i <= x"
  3.1320 -  assumes "!!y. (!!i. i : A ==> f i <= y) ==> x <= y"
  3.1321 -  shows "(SUP i:A. f i) = x"
  3.1322 -  unfolding SUPR_def Sup_extreal_def
  3.1323 -  using assms by (auto intro!: Least_equality)
  3.1324 -
  3.1325 -lemma extreal_INFI:
  3.1326 -  fixes x :: extreal
  3.1327 -  assumes "!!i. i : A ==> f i >= x"
  3.1328 -  assumes "!!y. (!!i. i : A ==> f i >= y) ==> x >= y"
  3.1329 -  shows "(INF i:A. f i) = x"
  3.1330 -  unfolding INFI_def Inf_extreal_def
  3.1331 -  using assms by (auto intro!: Greatest_equality)
  3.1332 -
  3.1333 -lemma Sup_extreal_close:
  3.1334 -  fixes e :: extreal
  3.1335 -  assumes "0 < e" and S: "\<bar>Sup S\<bar> \<noteq> \<infinity>" "S \<noteq> {}"
  3.1336 -  shows "\<exists>x\<in>S. Sup S - e < x"
  3.1337 -  using assms by (cases e) (auto intro!: less_Sup_iff[THEN iffD1])
  3.1338 -
  3.1339 -lemma Inf_extreal_close:
  3.1340 -  fixes e :: extreal assumes "\<bar>Inf X\<bar> \<noteq> \<infinity>" "0 < e"
  3.1341 -  shows "\<exists>x\<in>X. x < Inf X + e"
  3.1342 -proof (rule Inf_less_iff[THEN iffD1])
  3.1343 -  show "Inf X < Inf X + e" using assms
  3.1344 -    by (cases e) auto
  3.1345 -qed
  3.1346 -
  3.1347 -lemma Sup_eq_top_iff:
  3.1348 -  fixes A :: "'a::{complete_lattice, linorder} set"
  3.1349 -  shows "Sup A = top \<longleftrightarrow> (\<forall>x<top. \<exists>i\<in>A. x < i)"
  3.1350 -proof
  3.1351 -  assume *: "Sup A = top"
  3.1352 -  show "(\<forall>x<top. \<exists>i\<in>A. x < i)" unfolding *[symmetric]
  3.1353 -  proof (intro allI impI)
  3.1354 -    fix x assume "x < Sup A" then show "\<exists>i\<in>A. x < i"
  3.1355 -      unfolding less_Sup_iff by auto
  3.1356 -  qed
  3.1357 -next
  3.1358 -  assume *: "\<forall>x<top. \<exists>i\<in>A. x < i"
  3.1359 -  show "Sup A = top"
  3.1360 -  proof (rule ccontr)
  3.1361 -    assume "Sup A \<noteq> top"
  3.1362 -    with top_greatest[of "Sup A"]
  3.1363 -    have "Sup A < top" unfolding le_less by auto
  3.1364 -    then have "Sup A < Sup A"
  3.1365 -      using * unfolding less_Sup_iff by auto
  3.1366 -    then show False by auto
  3.1367 -  qed
  3.1368 -qed
  3.1369 -
  3.1370 -lemma SUP_eq_top_iff:
  3.1371 -  fixes f :: "'a \<Rightarrow> 'b::{complete_lattice, linorder}"
  3.1372 -  shows "(SUP i:A. f i) = top \<longleftrightarrow> (\<forall>x<top. \<exists>i\<in>A. x < f i)"
  3.1373 -  unfolding SUPR_def Sup_eq_top_iff by auto
  3.1374 -
  3.1375 -lemma SUP_nat_Infty: "(SUP i::nat. extreal (real i)) = \<infinity>"
  3.1376 -proof -
  3.1377 -  { fix x assume "x \<noteq> \<infinity>"
  3.1378 -    then have "\<exists>k::nat. x < extreal (real k)"
  3.1379 -    proof (cases x)
  3.1380 -      case MInf then show ?thesis by (intro exI[of _ 0]) auto
  3.1381 -    next
  3.1382 -      case (real r)
  3.1383 -      moreover obtain k :: nat where "r < real k"
  3.1384 -        using ex_less_of_nat by (auto simp: real_eq_of_nat)
  3.1385 -      ultimately show ?thesis by auto
  3.1386 -    qed simp }
  3.1387 -  then show ?thesis
  3.1388 -    using SUP_eq_top_iff[of UNIV "\<lambda>n::nat. extreal (real n)"]
  3.1389 -    by (auto simp: top_extreal_def)
  3.1390 -qed
  3.1391 -
  3.1392 -lemma extreal_le_Sup:
  3.1393 -  fixes x :: extreal
  3.1394 -  shows "(x <= (SUP i:A. f i)) <-> (ALL y. y < x --> (EX i. i : A & y <= f i))"
  3.1395 -(is "?lhs <-> ?rhs")
  3.1396 -proof-
  3.1397 -{ assume "?rhs"
  3.1398 -  { assume "~(x <= (SUP i:A. f i))" hence "(SUP i:A. f i)<x" by (simp add: not_le)
  3.1399 -    from this obtain y where y_def: "(SUP i:A. f i)<y & y<x" using extreal_dense by auto
  3.1400 -    from this obtain i where "i : A & y <= f i" using `?rhs` by auto
  3.1401 -    hence "y <= (SUP i:A. f i)" using le_SUPI[of i A f] by auto
  3.1402 -    hence False using y_def by auto
  3.1403 -  } hence "?lhs" by auto
  3.1404 -}
  3.1405 -moreover
  3.1406 -{ assume "?lhs" hence "?rhs"
  3.1407 -  by (metis Collect_def Collect_mem_eq SUP_leI assms atLeastatMost_empty atLeastatMost_empty_iff
  3.1408 -      inf_sup_ord(4) linorder_le_cases sup_absorb1 xt1(8))
  3.1409 -} ultimately show ?thesis by auto
  3.1410 -qed
  3.1411 -
  3.1412 -lemma extreal_Inf_le:
  3.1413 -  fixes x :: extreal
  3.1414 -  shows "((INF i:A. f i) <= x) <-> (ALL y. x < y --> (EX i. i : A & f i <= y))"
  3.1415 -(is "?lhs <-> ?rhs")
  3.1416 -proof-
  3.1417 -{ assume "?rhs"
  3.1418 -  { assume "~((INF i:A. f i) <= x)" hence "x < (INF i:A. f i)" by (simp add: not_le)
  3.1419 -    from this obtain y where y_def: "x<y & y<(INF i:A. f i)" using extreal_dense by auto
  3.1420 -    from this obtain i where "i : A & f i <= y" using `?rhs` by auto
  3.1421 -    hence "(INF i:A. f i) <= y" using INF_leI[of i A f] by auto
  3.1422 -    hence False using y_def by auto
  3.1423 -  } hence "?lhs" by auto
  3.1424 -}
  3.1425 -moreover
  3.1426 -{ assume "?lhs" hence "?rhs"
  3.1427 -  by (metis Collect_def Collect_mem_eq le_INFI assms atLeastatMost_empty atLeastatMost_empty_iff
  3.1428 -      inf_sup_ord(4) linorder_le_cases sup_absorb1 xt1(8))
  3.1429 -} ultimately show ?thesis by auto
  3.1430 -qed
  3.1431 -
  3.1432 -lemma Inf_less:
  3.1433 -  fixes x :: extreal
  3.1434 -  assumes "(INF i:A. f i) < x"
  3.1435 -  shows "EX i. i : A & f i <= x"
  3.1436 -proof(rule ccontr)
  3.1437 -  assume "~ (EX i. i : A & f i <= x)"
  3.1438 -  hence "ALL i:A. f i > x" by auto
  3.1439 -  hence "(INF i:A. f i) >= x" apply (subst le_INFI) by auto
  3.1440 -  thus False using assms by auto
  3.1441 -qed
  3.1442 -
  3.1443 -lemma same_INF:
  3.1444 -  assumes "ALL e:A. f e = g e"
  3.1445 -  shows "(INF e:A. f e) = (INF e:A. g e)"
  3.1446 -proof-
  3.1447 -have "f ` A = g ` A" unfolding image_def using assms by auto
  3.1448 -thus ?thesis unfolding INFI_def by auto
  3.1449 -qed
  3.1450 -
  3.1451 -lemma same_SUP:
  3.1452 -  assumes "ALL e:A. f e = g e"
  3.1453 -  shows "(SUP e:A. f e) = (SUP e:A. g e)"
  3.1454 -proof-
  3.1455 -have "f ` A = g ` A" unfolding image_def using assms by auto
  3.1456 -thus ?thesis unfolding SUPR_def by auto
  3.1457 -qed
  3.1458 -
  3.1459 -lemma SUPR_eq:
  3.1460 -  assumes "\<forall>i\<in>A. \<exists>j\<in>B. f i \<le> g j"
  3.1461 -  assumes "\<forall>j\<in>B. \<exists>i\<in>A. g j \<le> f i"
  3.1462 -  shows "(SUP i:A. f i) = (SUP j:B. g j)"
  3.1463 -proof (intro antisym)
  3.1464 -  show "(SUP i:A. f i) \<le> (SUP j:B. g j)"
  3.1465 -    using assms by (metis SUP_leI le_SUPI2)
  3.1466 -  show "(SUP i:B. g i) \<le> (SUP j:A. f j)"
  3.1467 -    using assms by (metis SUP_leI le_SUPI2)
  3.1468 -qed
  3.1469 -
  3.1470 -lemma SUP_extreal_le_addI:
  3.1471 -  assumes "\<And>i. f i + y \<le> z" and "y \<noteq> -\<infinity>"
  3.1472 -  shows "SUPR UNIV f + y \<le> z"
  3.1473 -proof (cases y)
  3.1474 -  case (real r)
  3.1475 -  then have "\<And>i. f i \<le> z - y" using assms by (simp add: extreal_le_minus_iff)
  3.1476 -  then have "SUPR UNIV f \<le> z - y" by (rule SUP_leI)
  3.1477 -  then show ?thesis using real by (simp add: extreal_le_minus_iff)
  3.1478 -qed (insert assms, auto)
  3.1479 -
  3.1480 -lemma SUPR_extreal_add:
  3.1481 -  fixes f g :: "nat \<Rightarrow> extreal"
  3.1482 -  assumes "incseq f" "incseq g" and pos: "\<And>i. f i \<noteq> -\<infinity>" "\<And>i. g i \<noteq> -\<infinity>"
  3.1483 -  shows "(SUP i. f i + g i) = SUPR UNIV f + SUPR UNIV g"
  3.1484 -proof (rule extreal_SUPI)
  3.1485 -  fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> f i + g i \<le> y"
  3.1486 -  have f: "SUPR UNIV f \<noteq> -\<infinity>" using pos
  3.1487 -    unfolding SUPR_def Sup_eq_MInfty by (auto dest: image_eqD)
  3.1488 -  { fix j
  3.1489 -    { fix i
  3.1490 -      have "f i + g j \<le> f i + g (max i j)"
  3.1491 -        using `incseq g`[THEN incseqD] by (rule add_left_mono) auto
  3.1492 -      also have "\<dots> \<le> f (max i j) + g (max i j)"
  3.1493 -        using `incseq f`[THEN incseqD] by (rule add_right_mono) auto
  3.1494 -      also have "\<dots> \<le> y" using * by auto
  3.1495 -      finally have "f i + g j \<le> y" . }
  3.1496 -    then have "SUPR UNIV f + g j \<le> y"
  3.1497 -      using assms(4)[of j] by (intro SUP_extreal_le_addI) auto
  3.1498 -    then have "g j + SUPR UNIV f \<le> y" by (simp add: ac_simps) }
  3.1499 -  then have "SUPR UNIV g + SUPR UNIV f \<le> y"
  3.1500 -    using f by (rule SUP_extreal_le_addI)
  3.1501 -  then show "SUPR UNIV f + SUPR UNIV g \<le> y" by (simp add: ac_simps)
  3.1502 -qed (auto intro!: add_mono le_SUPI)
  3.1503 -
  3.1504 -lemma SUPR_extreal_add_pos:
  3.1505 -  fixes f g :: "nat \<Rightarrow> extreal"
  3.1506 -  assumes inc: "incseq f" "incseq g" and pos: "\<And>i. 0 \<le> f i" "\<And>i. 0 \<le> g i"
  3.1507 -  shows "(SUP i. f i + g i) = SUPR UNIV f + SUPR UNIV g"
  3.1508 -proof (intro SUPR_extreal_add inc)
  3.1509 -  fix i show "f i \<noteq> -\<infinity>" "g i \<noteq> -\<infinity>" using pos[of i] by auto
  3.1510 -qed
  3.1511 -
  3.1512 -lemma SUPR_extreal_setsum:
  3.1513 -  fixes f g :: "'a \<Rightarrow> nat \<Rightarrow> extreal"
  3.1514 -  assumes "\<And>n. n \<in> A \<Longrightarrow> incseq (f n)" and pos: "\<And>n i. n \<in> A \<Longrightarrow> 0 \<le> f n i"
  3.1515 -  shows "(SUP i. \<Sum>n\<in>A. f n i) = (\<Sum>n\<in>A. SUPR UNIV (f n))"
  3.1516 -proof cases
  3.1517 -  assume "finite A" then show ?thesis using assms
  3.1518 -    by induct (auto simp: incseq_setsumI2 setsum_nonneg SUPR_extreal_add_pos)
  3.1519 -qed simp
  3.1520 -
  3.1521 -lemma SUPR_extreal_cmult:
  3.1522 -  fixes f :: "nat \<Rightarrow> extreal" assumes "\<And>i. 0 \<le> f i" "0 \<le> c"
  3.1523 -  shows "(SUP i. c * f i) = c * SUPR UNIV f"
  3.1524 -proof (rule extreal_SUPI)
  3.1525 -  fix i have "f i \<le> SUPR UNIV f" by (rule le_SUPI) auto
  3.1526 -  then show "c * f i \<le> c * SUPR UNIV f"
  3.1527 -    using `0 \<le> c` by (rule extreal_mult_left_mono)
  3.1528 -next
  3.1529 -  fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> c * f i \<le> y"
  3.1530 -  show "c * SUPR UNIV f \<le> y"
  3.1531 -  proof cases
  3.1532 -    assume c: "0 < c \<and> c \<noteq> \<infinity>"
  3.1533 -    with * have "SUPR UNIV f \<le> y / c"
  3.1534 -      by (intro SUP_leI) (auto simp: extreal_le_divide_pos)
  3.1535 -    with c show ?thesis
  3.1536 -      by (auto simp: extreal_le_divide_pos)
  3.1537 -  next
  3.1538 -    { assume "c = \<infinity>" have ?thesis
  3.1539 -      proof cases
  3.1540 -        assume "\<forall>i. f i = 0"
  3.1541 -        moreover then have "range f = {0}" by auto
  3.1542 -        ultimately show "c * SUPR UNIV f \<le> y" using * by (auto simp: SUPR_def)
  3.1543 -      next
  3.1544 -        assume "\<not> (\<forall>i. f i = 0)"
  3.1545 -        then obtain i where "f i \<noteq> 0" by auto
  3.1546 -        with *[of i] `c = \<infinity>` `0 \<le> f i` show ?thesis by (auto split: split_if_asm)
  3.1547 -      qed }
  3.1548 -    moreover assume "\<not> (0 < c \<and> c \<noteq> \<infinity>)"
  3.1549 -    ultimately show ?thesis using * `0 \<le> c` by auto
  3.1550 -  qed
  3.1551 -qed
  3.1552 -
  3.1553 -lemma SUP_PInfty:
  3.1554 -  fixes f :: "'a \<Rightarrow> extreal"
  3.1555 -  assumes "\<And>n::nat. \<exists>i\<in>A. extreal (real n) \<le> f i"
  3.1556 -  shows "(SUP i:A. f i) = \<infinity>"
  3.1557 -  unfolding SUPR_def Sup_eq_top_iff[where 'a=extreal, unfolded top_extreal_def]
  3.1558 -  apply simp
  3.1559 -proof safe
  3.1560 -  fix x assume "x \<noteq> \<infinity>"
  3.1561 -  show "\<exists>i\<in>A. x < f i"
  3.1562 -  proof (cases x)
  3.1563 -    case PInf with `x \<noteq> \<infinity>` show ?thesis by simp
  3.1564 -  next
  3.1565 -    case MInf with assms[of "0"] show ?thesis by force
  3.1566 -  next
  3.1567 -    case (real r)
  3.1568 -    with less_PInf_Ex_of_nat[of x] obtain n :: nat where "x < extreal (real n)" by auto
  3.1569 -    moreover from assms[of n] guess i ..
  3.1570 -    ultimately show ?thesis
  3.1571 -      by (auto intro!: bexI[of _ i])
  3.1572 -  qed
  3.1573 -qed
  3.1574 -
  3.1575 -lemma Sup_countable_SUPR:
  3.1576 -  assumes "A \<noteq> {}"
  3.1577 -  shows "\<exists>f::nat \<Rightarrow> extreal. range f \<subseteq> A \<and> Sup A = SUPR UNIV f"
  3.1578 -proof (cases "Sup A")
  3.1579 -  case (real r)
  3.1580 -  have "\<forall>n::nat. \<exists>x. x \<in> A \<and> Sup A < x + 1 / extreal (real n)"
  3.1581 -  proof
  3.1582 -    fix n ::nat have "\<exists>x\<in>A. Sup A - 1 / extreal (real n) < x"
  3.1583 -      using assms real by (intro Sup_extreal_close) (auto simp: one_extreal_def)
  3.1584 -    then guess x ..
  3.1585 -    then show "\<exists>x. x \<in> A \<and> Sup A < x + 1 / extreal (real n)"
  3.1586 -      by (auto intro!: exI[of _ x] simp: extreal_minus_less_iff)
  3.1587 -  qed
  3.1588 -  from choice[OF this] guess f .. note f = this
  3.1589 -  have "SUPR UNIV f = Sup A"
  3.1590 -  proof (rule extreal_SUPI)
  3.1591 -    fix i show "f i \<le> Sup A" using f
  3.1592 -      by (auto intro!: complete_lattice_class.Sup_upper)
  3.1593 -  next
  3.1594 -    fix y assume bound: "\<And>i. i \<in> UNIV \<Longrightarrow> f i \<le> y"
  3.1595 -    show "Sup A \<le> y"
  3.1596 -    proof (rule extreal_le_epsilon, intro allI impI)
  3.1597 -      fix e :: extreal assume "0 < e"
  3.1598 -      show "Sup A \<le> y + e"
  3.1599 -      proof (cases e)
  3.1600 -        case (real r)
  3.1601 -        hence "0 < r" using `0 < e` by auto
  3.1602 -        then obtain n ::nat where *: "1 / real n < r" "0 < n"
  3.1603 -          using ex_inverse_of_nat_less by (auto simp: real_eq_of_nat inverse_eq_divide)
  3.1604 -        have "Sup A \<le> f n + 1 / extreal (real n)" using f[THEN spec, of n] by auto
  3.1605 -        also have "1 / extreal (real n) \<le> e" using real * by (auto simp: one_extreal_def )
  3.1606 -        with bound have "f n + 1 / extreal (real n) \<le> y + e" by (rule add_mono) simp
  3.1607 -        finally show "Sup A \<le> y + e" .
  3.1608 -      qed (insert `0 < e`, auto)
  3.1609 -    qed
  3.1610 -  qed
  3.1611 -  with f show ?thesis by (auto intro!: exI[of _ f])
  3.1612 -next
  3.1613 -  case PInf
  3.1614 -  from `A \<noteq> {}` obtain x where "x \<in> A" by auto
  3.1615 -  show ?thesis
  3.1616 -  proof cases
  3.1617 -    assume "\<infinity> \<in> A"
  3.1618 -    moreover then have "\<infinity> \<le> Sup A" by (intro complete_lattice_class.Sup_upper)
  3.1619 -    ultimately show ?thesis by (auto intro!: exI[of _ "\<lambda>x. \<infinity>"])
  3.1620 -  next
  3.1621 -    assume "\<infinity> \<notin> A"
  3.1622 -    have "\<exists>x\<in>A. 0 \<le> x"
  3.1623 -      by (metis Infty_neq_0 PInf complete_lattice_class.Sup_least extreal_infty_less_eq2 linorder_linear)
  3.1624 -    then obtain x where "x \<in> A" "0 \<le> x" by auto
  3.1625 -    have "\<forall>n::nat. \<exists>f. f \<in> A \<and> x + extreal (real n) \<le> f"
  3.1626 -    proof (rule ccontr)
  3.1627 -      assume "\<not> ?thesis"
  3.1628 -      then have "\<exists>n::nat. Sup A \<le> x + extreal (real n)"
  3.1629 -        by (simp add: Sup_le_iff not_le less_imp_le Ball_def) (metis less_imp_le)
  3.1630 -      then show False using `x \<in> A` `\<infinity> \<notin> A` PInf
  3.1631 -        by(cases x) auto
  3.1632 -    qed
  3.1633 -    from choice[OF this] guess f .. note f = this
  3.1634 -    have "SUPR UNIV f = \<infinity>"
  3.1635 -    proof (rule SUP_PInfty)
  3.1636 -      fix n :: nat show "\<exists>i\<in>UNIV. extreal (real n) \<le> f i"
  3.1637 -        using f[THEN spec, of n] `0 \<le> x`
  3.1638 -        by (cases rule: extreal2_cases[of "f n" x]) (auto intro!: exI[of _ n])
  3.1639 -    qed
  3.1640 -    then show ?thesis using f PInf by (auto intro!: exI[of _ f])
  3.1641 -  qed
  3.1642 -next
  3.1643 -  case MInf
  3.1644 -  with `A \<noteq> {}` have "A = {-\<infinity>}" by (auto simp: Sup_eq_MInfty)
  3.1645 -  then show ?thesis using MInf by (auto intro!: exI[of _ "\<lambda>x. -\<infinity>"])
  3.1646 -qed
  3.1647 -
  3.1648 -lemma SUPR_countable_SUPR:
  3.1649 -  "A \<noteq> {} \<Longrightarrow> \<exists>f::nat \<Rightarrow> extreal. range f \<subseteq> g`A \<and> SUPR A g = SUPR UNIV f"
  3.1650 -  using Sup_countable_SUPR[of "g`A"] by (auto simp: SUPR_def)
  3.1651 -
  3.1652 -
  3.1653 -lemma Sup_extreal_cadd:
  3.1654 -  fixes A :: "extreal set" assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
  3.1655 -  shows "Sup ((\<lambda>x. a + x) ` A) = a + Sup A"
  3.1656 -proof (rule antisym)
  3.1657 -  have *: "\<And>a::extreal. \<And>A. Sup ((\<lambda>x. a + x) ` A) \<le> a + Sup A"
  3.1658 -    by (auto intro!: add_mono complete_lattice_class.Sup_least complete_lattice_class.Sup_upper)
  3.1659 -  then show "Sup ((\<lambda>x. a + x) ` A) \<le> a + Sup A" .
  3.1660 -  show "a + Sup A \<le> Sup ((\<lambda>x. a + x) ` A)"
  3.1661 -  proof (cases a)
  3.1662 -    case PInf with `A \<noteq> {}` show ?thesis by (auto simp: image_constant)
  3.1663 -  next
  3.1664 -    case (real r)
  3.1665 -    then have **: "op + (- a) ` op + a ` A = A"
  3.1666 -      by (auto simp: image_iff ac_simps zero_extreal_def[symmetric])
  3.1667 -    from *[of "-a" "(\<lambda>x. a + x) ` A"] real show ?thesis unfolding **
  3.1668 -      by (cases rule: extreal2_cases[of "Sup A" "Sup (op + a ` A)"]) auto
  3.1669 -  qed (insert `a \<noteq> -\<infinity>`, auto)
  3.1670 -qed
  3.1671 -
  3.1672 -lemma Sup_extreal_cminus:
  3.1673 -  fixes A :: "extreal set" assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
  3.1674 -  shows "Sup ((\<lambda>x. a - x) ` A) = a - Inf A"
  3.1675 -  using Sup_extreal_cadd[of "uminus ` A" a] assms
  3.1676 -  by (simp add: comp_def image_image minus_extreal_def
  3.1677 -                 extreal_Sup_uminus_image_eq)
  3.1678 -
  3.1679 -lemma SUPR_extreal_cminus:
  3.1680 -  fixes A assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
  3.1681 -  shows "(SUP x:A. a - f x) = a - (INF x:A. f x)"
  3.1682 -  using Sup_extreal_cminus[of "f`A" a] assms
  3.1683 -  unfolding SUPR_def INFI_def image_image by auto
  3.1684 -
  3.1685 -lemma Inf_extreal_cminus:
  3.1686 -  fixes A :: "extreal set" assumes "A \<noteq> {}" and "\<bar>a\<bar> \<noteq> \<infinity>"
  3.1687 -  shows "Inf ((\<lambda>x. a - x) ` A) = a - Sup A"
  3.1688 -proof -
  3.1689 -  { fix x have "-a - -x = -(a - x)" using assms by (cases x) auto }
  3.1690 -  moreover then have "(\<lambda>x. -a - x)`uminus`A = uminus ` (\<lambda>x. a - x) ` A"
  3.1691 -    by (auto simp: image_image)
  3.1692 -  ultimately show ?thesis
  3.1693 -    using Sup_extreal_cminus[of "uminus ` A" "-a"] assms
  3.1694 -    by (auto simp add: extreal_Sup_uminus_image_eq extreal_Inf_uminus_image_eq)
  3.1695 -qed
  3.1696 -
  3.1697 -lemma INFI_extreal_cminus:
  3.1698 -  fixes A assumes "A \<noteq> {}" and "\<bar>a\<bar> \<noteq> \<infinity>"
  3.1699 -  shows "(INF x:A. a - f x) = a - (SUP x:A. f x)"
  3.1700 -  using Inf_extreal_cminus[of "f`A" a] assms
  3.1701 -  unfolding SUPR_def INFI_def image_image
  3.1702 -  by auto
  3.1703 -
  3.1704 -lemma uminus_extreal_add_uminus_uminus:
  3.1705 -  fixes a b :: extreal shows "a \<noteq> \<infinity> \<Longrightarrow> b \<noteq> \<infinity> \<Longrightarrow> - (- a + - b) = a + b"
  3.1706 -  by (cases rule: extreal2_cases[of a b]) auto
  3.1707 -
  3.1708 -lemma INFI_extreal_add:
  3.1709 -  assumes "decseq f" "decseq g" and fin: "\<And>i. f i \<noteq> \<infinity>" "\<And>i. g i \<noteq> \<infinity>"
  3.1710 -  shows "(INF i. f i + g i) = INFI UNIV f + INFI UNIV g"
  3.1711 -proof -
  3.1712 -  have INF_less: "(INF i. f i) < \<infinity>" "(INF i. g i) < \<infinity>"
  3.1713 -    using assms unfolding INF_less_iff by auto
  3.1714 -  { fix i from fin[of i] have "- ((- f i) + (- g i)) = f i + g i"
  3.1715 -      by (rule uminus_extreal_add_uminus_uminus) }
  3.1716 -  then have "(INF i. f i + g i) = (INF i. - ((- f i) + (- g i)))"
  3.1717 -    by simp
  3.1718 -  also have "\<dots> = INFI UNIV f + INFI UNIV g"
  3.1719 -    unfolding extreal_INFI_uminus
  3.1720 -    using assms INF_less
  3.1721 -    by (subst SUPR_extreal_add)
  3.1722 -       (auto simp: extreal_SUPR_uminus intro!: uminus_extreal_add_uminus_uminus)
  3.1723 -  finally show ?thesis .
  3.1724 -qed
  3.1725 -
  3.1726 -subsection "Limits on @{typ extreal}"
  3.1727 -
  3.1728 -subsubsection "Topological space"
  3.1729 -
  3.1730 -instantiation extreal :: topological_space
  3.1731 -begin
  3.1732 -
  3.1733 -definition "open A \<longleftrightarrow> open (extreal -` A)
  3.1734 -       \<and> (\<infinity> \<in> A \<longrightarrow> (\<exists>x. {extreal x <..} \<subseteq> A))
  3.1735 -       \<and> (-\<infinity> \<in> A \<longrightarrow> (\<exists>x. {..<extreal x} \<subseteq> A))"
  3.1736 -
  3.1737 -lemma open_PInfty: "open A \<Longrightarrow> \<infinity> \<in> A \<Longrightarrow> (\<exists>x. {extreal x<..} \<subseteq> A)"
  3.1738 -  unfolding open_extreal_def by auto
  3.1739 -
  3.1740 -lemma open_MInfty: "open A \<Longrightarrow> -\<infinity> \<in> A \<Longrightarrow> (\<exists>x. {..<extreal x} \<subseteq> A)"
  3.1741 -  unfolding open_extreal_def by auto
  3.1742 -
  3.1743 -lemma open_PInfty2: assumes "open A" "\<infinity> \<in> A" obtains x where "{extreal x<..} \<subseteq> A"
  3.1744 -  using open_PInfty[OF assms] by auto
  3.1745 -
  3.1746 -lemma open_MInfty2: assumes "open A" "-\<infinity> \<in> A" obtains x where "{..<extreal x} \<subseteq> A"
  3.1747 -  using open_MInfty[OF assms] by auto
  3.1748 -
  3.1749 -lemma extreal_openE: assumes "open A" obtains x y where
  3.1750 -  "open (extreal -` A)"
  3.1751 -  "\<infinity> \<in> A \<Longrightarrow> {extreal x<..} \<subseteq> A"
  3.1752 -  "-\<infinity> \<in> A \<Longrightarrow> {..<extreal y} \<subseteq> A"
  3.1753 -  using assms open_extreal_def by auto
  3.1754 -
  3.1755 -instance
  3.1756 -proof
  3.1757 -  let ?U = "UNIV::extreal set"
  3.1758 -  show "open ?U" unfolding open_extreal_def
  3.1759 -    by (auto intro!: exI[of _ 0])
  3.1760 -next
  3.1761 -  fix S T::"extreal set" assume "open S" and "open T"
  3.1762 -  from `open S`[THEN extreal_openE] guess xS yS .
  3.1763 -  moreover from `open T`[THEN extreal_openE] guess xT yT .
  3.1764 -  ultimately have
  3.1765 -    "open (extreal -` (S \<inter> T))"
  3.1766 -    "\<infinity> \<in> S \<inter> T \<Longrightarrow> {extreal (max xS xT) <..} \<subseteq> S \<inter> T"
  3.1767 -    "-\<infinity> \<in> S \<inter> T \<Longrightarrow> {..< extreal (min yS yT)} \<subseteq> S \<inter> T"
  3.1768 -    by auto
  3.1769 -  then show "open (S Int T)" unfolding open_extreal_def by blast
  3.1770 -next
  3.1771 -  fix K :: "extreal set set" assume "\<forall>S\<in>K. open S"
  3.1772 -  then have *: "\<forall>S. \<exists>x y. S \<in> K \<longrightarrow> open (extreal -` S) \<and>
  3.1773 -    (\<infinity> \<in> S \<longrightarrow> {extreal x <..} \<subseteq> S) \<and> (-\<infinity> \<in> S \<longrightarrow> {..< extreal y} \<subseteq> S)"
  3.1774 -    by (auto simp: open_extreal_def)
  3.1775 -  then show "open (Union K)" unfolding open_extreal_def
  3.1776 -  proof (intro conjI impI)
  3.1777 -    show "open (extreal -` \<Union>K)"
  3.1778 -      using *[THEN choice] by (auto simp: vimage_Union)
  3.1779 -  qed ((metis UnionE Union_upper subset_trans *)+)
  3.1780 -qed
  3.1781 -end
  3.1782 -
  3.1783 -lemma open_extreal: "open S \<Longrightarrow> open (extreal ` S)"
  3.1784 -  by (auto simp: inj_vimage_image_eq open_extreal_def)
  3.1785 -
  3.1786 -lemma open_extreal_vimage: "open S \<Longrightarrow> open (extreal -` S)"
  3.1787 -  unfolding open_extreal_def by auto
  3.1788 -
  3.1789 -lemma open_extreal_lessThan[intro, simp]: "open {..< a :: extreal}"
  3.1790 -proof -
  3.1791 -  have "\<And>x. extreal -` {..<extreal x} = {..< x}"
  3.1792 -    "extreal -` {..< \<infinity>} = UNIV" "extreal -` {..< -\<infinity>} = {}" by auto
  3.1793 -  then show ?thesis by (cases a) (auto simp: open_extreal_def)
  3.1794 -qed
  3.1795 -
  3.1796 -lemma open_extreal_greaterThan[intro, simp]:
  3.1797 -  "open {a :: extreal <..}"
  3.1798 -proof -
  3.1799 -  have "\<And>x. extreal -` {extreal x<..} = {x<..}"
  3.1800 -    "extreal -` {\<infinity><..} = {}" "extreal -` {-\<infinity><..} = UNIV" by auto
  3.1801 -  then show ?thesis by (cases a) (auto simp: open_extreal_def)
  3.1802 -qed
  3.1803 -
  3.1804 -lemma extreal_open_greaterThanLessThan[intro, simp]: "open {a::extreal <..< b}"
  3.1805 -  unfolding greaterThanLessThan_def by auto
  3.1806 -
  3.1807 -lemma closed_extreal_atLeast[simp, intro]: "closed {a :: extreal ..}"
  3.1808 -proof -
  3.1809 -  have "- {a ..} = {..< a}" by auto
  3.1810 -  then show "closed {a ..}"
  3.1811 -    unfolding closed_def using open_extreal_lessThan by auto
  3.1812 -qed
  3.1813 -
  3.1814 -lemma closed_extreal_atMost[simp, intro]: "closed {.. b :: extreal}"
  3.1815 -proof -
  3.1816 -  have "- {.. b} = {b <..}" by auto
  3.1817 -  then show "closed {.. b}"
  3.1818 -    unfolding closed_def using open_extreal_greaterThan by auto
  3.1819 -qed
  3.1820 -
  3.1821 -lemma closed_extreal_atLeastAtMost[simp, intro]:
  3.1822 -  shows "closed {a :: extreal .. b}"
  3.1823 -  unfolding atLeastAtMost_def by auto
  3.1824 -
  3.1825 -lemma closed_extreal_singleton:
  3.1826 -  "closed {a :: extreal}"
  3.1827 -by (metis atLeastAtMost_singleton closed_extreal_atLeastAtMost)
  3.1828 -
  3.1829 -lemma extreal_open_cont_interval:
  3.1830 -  assumes "open S" "x \<in> S" "\<bar>x\<bar> \<noteq> \<infinity>"
  3.1831 -  obtains e where "e>0" "{x-e <..< x+e} \<subseteq> S"
  3.1832 -proof-
  3.1833 -  from `open S` have "open (extreal -` S)" by (rule extreal_openE)
  3.1834 -  then obtain e where "0 < e" and e: "\<And>y. dist y (real x) < e \<Longrightarrow> extreal y \<in> S"
  3.1835 -    using assms unfolding open_dist by force
  3.1836 -  show thesis
  3.1837 -  proof (intro that subsetI)
  3.1838 -    show "0 < extreal e" using `0 < e` by auto
  3.1839 -    fix y assume "y \<in> {x - extreal e<..<x + extreal e}"
  3.1840 -    with assms obtain t where "y = extreal t" "dist t (real x) < e"
  3.1841 -      apply (cases y) by (auto simp: dist_real_def)
  3.1842 -    then show "y \<in> S" using e[of t] by auto
  3.1843 -  qed
  3.1844 -qed
  3.1845 -
  3.1846 -lemma extreal_open_cont_interval2:
  3.1847 -  assumes "open S" "x \<in> S" and x: "\<bar>x\<bar> \<noteq> \<infinity>"
  3.1848 -  obtains a b where "a < x" "x < b" "{a <..< b} \<subseteq> S"
  3.1849 -proof-
  3.1850 -  guess e using extreal_open_cont_interval[OF assms] .
  3.1851 -  with that[of "x-e" "x+e"] extreal_between[OF x, of e]
  3.1852 -  show thesis by auto
  3.1853 -qed
  3.1854 -
  3.1855 -instance extreal :: t2_space
  3.1856 -proof
  3.1857 -  fix x y :: extreal assume "x ~= y"
  3.1858 -  let "?P x (y::extreal)" = "EX U V. open U & open V & x : U & y : V & U Int V = {}"
  3.1859 -
  3.1860 -  { fix x y :: extreal assume "x < y"
  3.1861 -    from extreal_dense[OF this] obtain z where z: "x < z" "z < y" by auto
  3.1862 -    have "?P x y"
  3.1863 -      apply (rule exI[of _ "{..<z}"])
  3.1864 -      apply (rule exI[of _ "{z<..}"])
  3.1865 -      using z by auto }
  3.1866 -  note * = this
  3.1867 -
  3.1868 -  from `x ~= y`
  3.1869 -  show "EX U V. open U & open V & x : U & y : V & U Int V = {}"
  3.1870 -  proof (cases rule: linorder_cases)
  3.1871 -    assume "x = y" with `x ~= y` show ?thesis by simp
  3.1872 -  next assume "x < y" from *[OF this] show ?thesis by auto
  3.1873 -  next assume "y < x" from *[OF this] show ?thesis by auto
  3.1874 -  qed
  3.1875 -qed
  3.1876 -
  3.1877 -subsubsection {* Convergent sequences *}
  3.1878 -
  3.1879 -lemma lim_extreal[simp]:
  3.1880 -  "((\<lambda>n. extreal (f n)) ---> extreal x) net \<longleftrightarrow> (f ---> x) net" (is "?l = ?r")
  3.1881 -proof (intro iffI topological_tendstoI)
  3.1882 -  fix S assume "?l" "open S" "x \<in> S"
  3.1883 -  then show "eventually (\<lambda>x. f x \<in> S) net"
  3.1884 -    using `?l`[THEN topological_tendstoD, OF open_extreal, OF `open S`]
  3.1885 -    by (simp add: inj_image_mem_iff)
  3.1886 -next
  3.1887 -  fix S assume "?r" "open S" "extreal x \<in> S"
  3.1888 -  show "eventually (\<lambda>x. extreal (f x) \<in> S) net"
  3.1889 -    using `?r`[THEN topological_tendstoD, OF open_extreal_vimage, OF `open S`]
  3.1890 -    using `extreal x \<in> S` by auto
  3.1891 -qed
  3.1892 -
  3.1893 -lemma lim_real_of_extreal[simp]:
  3.1894 -  assumes lim: "(f ---> extreal x) net"
  3.1895 -  shows "((\<lambda>x. real (f x)) ---> x) net"
  3.1896 -proof (intro topological_tendstoI)
  3.1897 -  fix S assume "open S" "x \<in> S"
  3.1898 -  then have S: "open S" "extreal x \<in> extreal ` S"
  3.1899 -    by (simp_all add: inj_image_mem_iff)
  3.1900 -  have "\<forall>x. f x \<in> extreal ` S \<longrightarrow> real (f x) \<in> S" by auto
  3.1901 -  from this lim[THEN topological_tendstoD, OF open_extreal, OF S]
  3.1902 -  show "eventually (\<lambda>x. real (f x) \<in> S) net"
  3.1903 -    by (rule eventually_mono)
  3.1904 -qed
  3.1905 -
  3.1906 -lemma Lim_PInfty: "f ----> \<infinity> <-> (ALL B. EX N. ALL n>=N. f n >= extreal B)" (is "?l = ?r")
  3.1907 -proof assume ?r show ?l apply(rule topological_tendstoI)
  3.1908 -    unfolding eventually_sequentially
  3.1909 -  proof- fix S assume "open S" "\<infinity> : S"
  3.1910 -    from open_PInfty[OF this] guess B .. note B=this
  3.1911 -    from `?r`[rule_format,of "B+1"] guess N .. note N=this
  3.1912 -    show "EX N. ALL n>=N. f n : S" apply(rule_tac x=N in exI)
  3.1913 -    proof safe case goal1
  3.1914 -      have "extreal B < extreal (B + 1)" by auto
  3.1915 -      also have "... <= f n" using goal1 N by auto
  3.1916 -      finally show ?case using B by fastsimp
  3.1917 -    qed
  3.1918 -  qed
  3.1919 -next assume ?l show ?r
  3.1920 -  proof fix B::real have "open {extreal B<..}" "\<infinity> : {extreal B<..}" by auto
  3.1921 -    from topological_tendstoD[OF `?l` this,unfolded eventually_sequentially]
  3.1922 -    guess N .. note N=this
  3.1923 -    show "EX N. ALL n>=N. extreal B <= f n" apply(rule_tac x=N in exI) using N by auto
  3.1924 -  qed
  3.1925 -qed
  3.1926 -
  3.1927 -
  3.1928 -lemma Lim_MInfty: "f ----> (-\<infinity>) <-> (ALL B. EX N. ALL n>=N. f n <= extreal B)" (is "?l = ?r")
  3.1929 -proof assume ?r show ?l apply(rule topological_tendstoI)
  3.1930 -    unfolding eventually_sequentially
  3.1931 -  proof- fix S assume "open S" "(-\<infinity>) : S"
  3.1932 -    from open_MInfty[OF this] guess B .. note B=this
  3.1933 -    from `?r`[rule_format,of "B-(1::real)"] guess N .. note N=this
  3.1934 -    show "EX N. ALL n>=N. f n : S" apply(rule_tac x=N in exI)
  3.1935 -    proof safe case goal1
  3.1936 -      have "extreal (B - 1) >= f n" using goal1 N by auto
  3.1937 -      also have "... < extreal B" by auto
  3.1938 -      finally show ?case using B by fastsimp
  3.1939 -    qed
  3.1940 -  qed
  3.1941 -next assume ?l show ?r
  3.1942 -  proof fix B::real have "open {..<extreal B}" "(-\<infinity>) : {..<extreal B}" by auto
  3.1943 -    from topological_tendstoD[OF `?l` this,unfolded eventually_sequentially]
  3.1944 -    guess N .. note N=this
  3.1945 -    show "EX N. ALL n>=N. extreal B >= f n" apply(rule_tac x=N in exI) using N by auto
  3.1946 -  qed
  3.1947 -qed
  3.1948 -
  3.1949 -
  3.1950 -lemma Lim_bounded_PInfty: assumes lim:"f ----> l" and "!!n. f n <= extreal B" shows "l ~= \<infinity>"
  3.1951 -proof(rule ccontr,unfold not_not) let ?B = "B + 1" assume as:"l=\<infinity>"
  3.1952 -  from lim[unfolded this Lim_PInfty,rule_format,of "?B"]
  3.1953 -  guess N .. note N=this[rule_format,OF le_refl]
  3.1954 -  hence "extreal ?B <= extreal B" using assms(2)[of N] by(rule order_trans)
  3.1955 -  hence "extreal ?B < extreal ?B" apply (rule le_less_trans) by auto
  3.1956 -  thus False by auto
  3.1957 -qed
  3.1958 -
  3.1959 -
  3.1960 -lemma Lim_bounded_MInfty: assumes lim:"f ----> l" and "!!n. f n >= extreal B" shows "l ~= (-\<infinity>)"
  3.1961 -proof(rule ccontr,unfold not_not) let ?B = "B - 1" assume as:"l=(-\<infinity>)"
  3.1962 -  from lim[unfolded this Lim_MInfty,rule_format,of "?B"]
  3.1963 -  guess N .. note N=this[rule_format,OF le_refl]
  3.1964 -  hence "extreal B <= extreal ?B" using assms(2)[of N] order_trans[of "extreal B" "f N" "extreal(B - 1)"] by blast
  3.1965 -  thus False by auto
  3.1966 -qed
  3.1967 -
  3.1968 -
  3.1969 -lemma tendsto_explicit:
  3.1970 -  "f ----> f0 <-> (ALL S. open S --> f0 : S --> (EX N. ALL n>=N. f n : S))"
  3.1971 -  unfolding tendsto_def eventually_sequentially by auto
  3.1972 -
  3.1973 -
  3.1974 -lemma tendsto_obtains_N:
  3.1975 -  assumes "f ----> f0"
  3.1976 -  assumes "open S" "f0 : S"
  3.1977 -  obtains N where "ALL n>=N. f n : S"
  3.1978 -  using tendsto_explicit[of f f0] assms by auto
  3.1979 -
  3.1980 -
  3.1981 -lemma tail_same_limit:
  3.1982 -  fixes X Y N
  3.1983 -  assumes "X ----> L" "ALL n>=N. X n = Y n"
  3.1984 -  shows "Y ----> L"
  3.1985 -proof-
  3.1986 -{ fix S assume "open S" and "L:S"
  3.1987 -  from this obtain N1 where "ALL n>=N1. X n : S"
  3.1988 -     using assms unfolding tendsto_def eventually_sequentially by auto
  3.1989 -  hence "ALL n>=max N N1. Y n : S" using assms by auto
  3.1990 -  hence "EX N. ALL n>=N. Y n : S" apply(rule_tac x="max N N1" in exI) by auto
  3.1991 -}
  3.1992 -thus ?thesis using tendsto_explicit by auto
  3.1993 -qed
  3.1994 -
  3.1995 -
  3.1996 -lemma Lim_bounded_PInfty2:
  3.1997 -assumes lim:"f ----> l" and "ALL n>=N. f n <= extreal B"
  3.1998 -shows "l ~= \<infinity>"
  3.1999 -proof-
  3.2000 -  def g == "(%n. if n>=N then f n else extreal B)"
  3.2001 -  hence "g ----> l" using tail_same_limit[of f l N g] lim by auto
  3.2002 -  moreover have "!!n. g n <= extreal B" using g_def assms by auto
  3.2003 -  ultimately show ?thesis using  Lim_bounded_PInfty by auto
  3.2004 -qed
  3.2005 -
  3.2006 -lemma Lim_bounded_extreal:
  3.2007 -  assumes lim:"f ----> (l :: extreal)"
  3.2008 -  and "ALL n>=M. f n <= C"
  3.2009 -  shows "l<=C"
  3.2010 -proof-
  3.2011 -{ assume "l=(-\<infinity>)" hence ?thesis by auto }
  3.2012 -moreover
  3.2013 -{ assume "~(l=(-\<infinity>))"
  3.2014 -  { assume "C=\<infinity>" hence ?thesis by auto }
  3.2015 -  moreover
  3.2016 -  { assume "C=(-\<infinity>)" hence "ALL n>=M. f n = (-\<infinity>)" using assms by auto
  3.2017 -    hence "l=(-\<infinity>)" using assms
  3.2018 -       tendsto_unique[OF trivial_limit_sequentially] tail_same_limit[of "\<lambda>n. -\<infinity>" "-\<infinity>" M f, OF tendsto_const] by auto
  3.2019 -    hence ?thesis by auto }
  3.2020 -  moreover
  3.2021 -  { assume "EX B. C = extreal B"
  3.2022 -    from this obtain B where B_def: "C=extreal B" by auto
  3.2023 -    hence "~(l=\<infinity>)" using Lim_bounded_PInfty2 assms by auto
  3.2024 -    from this obtain m where m_def: "extreal m=l" using `~(l=(-\<infinity>))` by (cases l) auto
  3.2025 -    from this obtain N where N_def: "ALL n>=N. f n : {extreal(m - 1) <..< extreal(m+1)}"
  3.2026 -       apply (subst tendsto_obtains_N[of f l "{extreal(m - 1) <..< extreal(m+1)}"]) using assms by auto
  3.2027 -    { fix n assume "n>=N"
  3.2028 -      hence "EX r. extreal r = f n" using N_def by (cases "f n") auto
  3.2029 -    } from this obtain g where g_def: "ALL n>=N. extreal (g n) = f n" by metis
  3.2030 -    hence "(%n. extreal (g n)) ----> l" using tail_same_limit[of f l N] assms by auto
  3.2031 -    hence *: "(%n. g n) ----> m" using m_def by auto
  3.2032 -    { fix n assume "n>=max N M"
  3.2033 -      hence "extreal (g n) <= extreal B" using assms g_def B_def by auto
  3.2034 -      hence "g n <= B" by auto
  3.2035 -    } hence "EX N. ALL n>=N. g n <= B" by blast
  3.2036 -    hence "m<=B" using * LIMSEQ_le_const2[of g m B] by auto
  3.2037 -    hence ?thesis using m_def B_def by auto
  3.2038 -  } ultimately have ?thesis by (cases C) auto
  3.2039 -} ultimately show ?thesis by blast
  3.2040 -qed
  3.2041 -
  3.2042 -lemma real_of_extreal_mult[simp]:
  3.2043 -  fixes a b :: extreal shows "real (a * b) = real a * real b"
  3.2044 -  by (cases rule: extreal2_cases[of a b]) auto
  3.2045 -
  3.2046 -lemma real_of_extreal_eq_0:
  3.2047 -  "real x = 0 \<longleftrightarrow> x = \<infinity> \<or> x = -\<infinity> \<or> x = 0"
  3.2048 -  by (cases x) auto
  3.2049 -
  3.2050 -lemma tendsto_extreal_realD:
  3.2051 -  fixes f :: "'a \<Rightarrow> extreal"
  3.2052 -  assumes "x \<noteq> 0" and tendsto: "((\<lambda>x. extreal (real (f x))) ---> x) net"
  3.2053 -  shows "(f ---> x) net"
  3.2054 -proof (intro topological_tendstoI)
  3.2055 -  fix S assume S: "open S" "x \<in> S"
  3.2056 -  with `x \<noteq> 0` have "open (S - {0})" "x \<in> S - {0}" by auto
  3.2057 -  from tendsto[THEN topological_tendstoD, OF this]
  3.2058 -  show "eventually (\<lambda>x. f x \<in> S) net"
  3.2059 -    by (rule eventually_rev_mp) (auto simp: extreal_real real_of_extreal_0)
  3.2060 -qed
  3.2061 -
  3.2062 -lemma tendsto_extreal_realI:
  3.2063 -  fixes f :: "'a \<Rightarrow> extreal"
  3.2064 -  assumes x: "\<bar>x\<bar> \<noteq> \<infinity>" and tendsto: "(f ---> x) net"
  3.2065 -  shows "((\<lambda>x. extreal (real (f x))) ---> x) net"
  3.2066 -proof (intro topological_tendstoI)
  3.2067 -  fix S assume "open S" "x \<in> S"
  3.2068 -  with x have "open (S - {\<infinity>, -\<infinity>})" "x \<in> S - {\<infinity>, -\<infinity>}" by auto
  3.2069 -  from tendsto[THEN topological_tendstoD, OF this]
  3.2070 -  show "eventually (\<lambda>x. extreal (real (f x)) \<in> S) net"
  3.2071 -    by (elim eventually_elim1) (auto simp: extreal_real)
  3.2072 -qed
  3.2073 -
  3.2074 -lemma extreal_mult_cancel_left:
  3.2075 -  fixes a b c :: extreal shows "a * b = a * c \<longleftrightarrow>
  3.2076 -    ((\<bar>a\<bar> = \<infinity> \<and> 0 < b * c) \<or> a = 0 \<or> b = c)"
  3.2077 -  by (cases rule: extreal3_cases[of a b c])
  3.2078 -     (simp_all add: zero_less_mult_iff)
  3.2079 -
  3.2080 -lemma extreal_inj_affinity:
  3.2081 -  assumes "\<bar>m\<bar> \<noteq> \<infinity>" "m \<noteq> 0" "\<bar>t\<bar> \<noteq> \<infinity>"
  3.2082 -  shows "inj_on (\<lambda>x. m * x + t) A"
  3.2083 -  using assms
  3.2084 -  by (cases rule: extreal2_cases[of m t])
  3.2085 -     (auto intro!: inj_onI simp: extreal_add_cancel_right extreal_mult_cancel_left)
  3.2086 -
  3.2087 -lemma extreal_PInfty_eq_plus[simp]:
  3.2088 -  shows "\<infinity> = a + b \<longleftrightarrow> a = \<infinity> \<or> b = \<infinity>"
  3.2089 -  by (cases rule: extreal2_cases[of a b]) auto
  3.2090 -
  3.2091 -lemma extreal_MInfty_eq_plus[simp]:
  3.2092 -  shows "-\<infinity> = a + b \<longleftrightarrow> (a = -\<infinity> \<and> b \<noteq> \<infinity>) \<or> (b = -\<infinity> \<and> a \<noteq> \<infinity>)"
  3.2093 -  by (cases rule: extreal2_cases[of a b]) auto
  3.2094 -
  3.2095 -lemma extreal_less_divide_pos:
  3.2096 -  "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y < z / x \<longleftrightarrow> x * y < z"
  3.2097 -  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
  3.2098 -
  3.2099 -lemma extreal_divide_less_pos:
  3.2100 -  "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y / x < z \<longleftrightarrow> y < x * z"
  3.2101 -  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
  3.2102 -
  3.2103 -lemma extreal_divide_eq:
  3.2104 -  "b \<noteq> 0 \<Longrightarrow> \<bar>b\<bar> \<noteq> \<infinity> \<Longrightarrow> a / b = c \<longleftrightarrow> a = b * c"
  3.2105 -  by (cases rule: extreal3_cases[of a b c])
  3.2106 -     (simp_all add: field_simps)
  3.2107 -
  3.2108 -lemma extreal_inverse_not_MInfty[simp]: "inverse a \<noteq> -\<infinity>"
  3.2109 -  by (cases a) auto
  3.2110 -
  3.2111 -lemma extreal_mult_m1[simp]: "x * extreal (-1) = -x"
  3.2112 -  by (cases x) auto
  3.2113 -
  3.2114 -lemma extreal_LimI_finite:
  3.2115 -  assumes "\<bar>x\<bar> \<noteq> \<infinity>"
  3.2116 -  assumes "!!r. 0 < r ==> EX N. ALL n>=N. u n < x + r & x < u n + r"
  3.2117 -  shows "u ----> x"
  3.2118 -proof (rule topological_tendstoI, unfold eventually_sequentially)
  3.2119 -  obtain rx where rx_def: "x=extreal rx" using assms by (cases x) auto
  3.2120 -  fix S assume "open S" "x : S"
  3.2121 -  then have "open (extreal -` S)" unfolding open_extreal_def by auto
  3.2122 -  with `x \<in> S` obtain r where "0 < r" and dist: "!!y. dist y rx < r ==> extreal y \<in> S"
  3.2123 -    unfolding open_real_def rx_def by auto
  3.2124 -  then obtain n where
  3.2125 -    upper: "!!N. n <= N ==> u N < x + extreal r" and
  3.2126 -    lower: "!!N. n <= N ==> x < u N + extreal r" using assms(2)[of "extreal r"] by auto
  3.2127 -  show "EX N. ALL n>=N. u n : S"
  3.2128 -  proof (safe intro!: exI[of _ n])
  3.2129 -    fix N assume "n <= N"
  3.2130 -    from upper[OF this] lower[OF this] assms `0 < r`
  3.2131 -    have "u N ~: {\<infinity>,(-\<infinity>)}" by auto
  3.2132 -    from this obtain ra where ra_def: "(u N) = extreal ra" by (cases "u N") auto
  3.2133 -    hence "rx < ra + r" and "ra < rx + r"
  3.2134 -       using rx_def assms `0 < r` lower[OF `n <= N`] upper[OF `n <= N`] by auto
  3.2135 -    hence "dist (real (u N)) rx < r"
  3.2136 -      using rx_def ra_def
  3.2137 -      by (auto simp: dist_real_def abs_diff_less_iff field_simps)
  3.2138 -    from dist[OF this] show "u N : S" using `u N  ~: {\<infinity>, -\<infinity>}`
  3.2139 -      by (auto simp: extreal_real split: split_if_asm)
  3.2140 -  qed
  3.2141 -qed
  3.2142 -
  3.2143 -lemma extreal_LimI_finite_iff:
  3.2144 -  assumes "\<bar>x\<bar> \<noteq> \<infinity>"
  3.2145 -  shows "u ----> x <-> (ALL r. 0 < r --> (EX N. ALL n>=N. u n < x + r & x < u n + r))"
  3.2146 -  (is "?lhs <-> ?rhs")
  3.2147 -proof
  3.2148 -  assume lim: "u ----> x"
  3.2149 -  { fix r assume "(r::extreal)>0"
  3.2150 -    from this obtain N where N_def: "ALL n>=N. u n : {x - r <..< x + r}"
  3.2151 -       apply (subst tendsto_obtains_N[of u x "{x - r <..< x + r}"])
  3.2152 -       using lim extreal_between[of x r] assms `r>0` by auto
  3.2153 -    hence "EX N. ALL n>=N. u n < x + r & x < u n + r"
  3.2154 -      using extreal_minus_less[of r x] by (cases r) auto
  3.2155 -  } then show "?rhs" by auto
  3.2156 -next
  3.2157 -  assume ?rhs then show "u ----> x"
  3.2158 -    using extreal_LimI_finite[of x] assms by auto
  3.2159 -qed
  3.2160 -
  3.2161 -
  3.2162 -subsubsection {* @{text Liminf} and @{text Limsup} *}
  3.2163 -
  3.2164 -definition
  3.2165 -  "Liminf net f = (GREATEST l. \<forall>y<l. eventually (\<lambda>x. y < f x) net)"
  3.2166 -
  3.2167 -definition
  3.2168 -  "Limsup net f = (LEAST l. \<forall>y>l. eventually (\<lambda>x. f x < y) net)"
  3.2169 -
  3.2170 -lemma Liminf_Sup:
  3.2171 -  fixes f :: "'a => 'b::{complete_lattice, linorder}"
  3.2172 -  shows "Liminf net f = Sup {l. \<forall>y<l. eventually (\<lambda>x. y < f x) net}"
  3.2173 -  by (auto intro!: Greatest_equality complete_lattice_class.Sup_upper simp: less_Sup_iff Liminf_def)
  3.2174 -
  3.2175 -lemma Limsup_Inf:
  3.2176 -  fixes f :: "'a => 'b::{complete_lattice, linorder}"
  3.2177 -  shows "Limsup net f = Inf {l. \<forall>y>l. eventually (\<lambda>x. f x < y) net}"
  3.2178 -  by (auto intro!: Least_equality complete_lattice_class.Inf_lower simp: Inf_less_iff Limsup_def)
  3.2179 -
  3.2180 -lemma extreal_SupI:
  3.2181 -  fixes x :: extreal
  3.2182 -  assumes "\<And>y. y \<in> A \<Longrightarrow> y \<le> x"
  3.2183 -  assumes "\<And>y. (\<And>z. z \<in> A \<Longrightarrow> z \<le> y) \<Longrightarrow> x \<le> y"
  3.2184 -  shows "Sup A = x"
  3.2185 -  unfolding Sup_extreal_def
  3.2186 -  using assms by (auto intro!: Least_equality)
  3.2187 -
  3.2188 -lemma extreal_InfI:
  3.2189 -  fixes x :: extreal
  3.2190 -  assumes "\<And>i. i \<in> A \<Longrightarrow> x \<le> i"
  3.2191 -  assumes "\<And>y. (\<And>i. i \<in> A \<Longrightarrow> y \<le> i) \<Longrightarrow> y \<le> x"
  3.2192 -  shows "Inf A = x"
  3.2193 -  unfolding Inf_extreal_def
  3.2194 -  using assms by (auto intro!: Greatest_equality)
  3.2195 -
  3.2196 -lemma Limsup_const:
  3.2197 -  fixes c :: "'a::{complete_lattice, linorder}"
  3.2198 -  assumes ntriv: "\<not> trivial_limit net"
  3.2199 -  shows "Limsup net (\<lambda>x. c) = c"
  3.2200 -  unfolding Limsup_Inf
  3.2201 -proof (safe intro!: antisym complete_lattice_class.Inf_greatest complete_lattice_class.Inf_lower)
  3.2202 -  fix x assume *: "\<forall>y>x. eventually (\<lambda>_. c < y) net"
  3.2203 -  show "c \<le> x"
  3.2204 -  proof (rule ccontr)
  3.2205 -    assume "\<not> c \<le> x" then have "x < c" by auto
  3.2206 -    then show False using ntriv * by (auto simp: trivial_limit_def)
  3.2207 -  qed
  3.2208 -qed auto
  3.2209 -
  3.2210 -lemma Liminf_const:
  3.2211 -  fixes c :: "'a::{complete_lattice, linorder}"
  3.2212 -  assumes ntriv: "\<not> trivial_limit net"
  3.2213 -  shows "Liminf net (\<lambda>x. c) = c"
  3.2214 -  unfolding Liminf_Sup
  3.2215 -proof (safe intro!: antisym complete_lattice_class.Sup_least complete_lattice_class.Sup_upper)
  3.2216 -  fix x assume *: "\<forall>y<x. eventually (\<lambda>_. y < c) net"
  3.2217 -  show "x \<le> c"
  3.2218 -  proof (rule ccontr)
  3.2219 -    assume "\<not> x \<le> c" then have "c < x" by auto
  3.2220 -    then show False using ntriv * by (auto simp: trivial_limit_def)
  3.2221 -  qed
  3.2222 -qed auto
  3.2223 -
  3.2224 -lemma mono_set:
  3.2225 -  fixes S :: "('a::order) set"
  3.2226 -  shows "mono S \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> x \<in> S \<longrightarrow> y \<in> S)"
  3.2227 -  by (auto simp: mono_def mem_def)
  3.2228 -
  3.2229 -lemma mono_greaterThan[intro, simp]: "mono {B<..}" unfolding mono_set by auto
  3.2230 -lemma mono_atLeast[intro, simp]: "mono {B..}" unfolding mono_set by auto
  3.2231 -lemma mono_UNIV[intro, simp]: "mono UNIV" unfolding mono_set by auto
  3.2232 -lemma mono_empty[intro, simp]: "mono {}" unfolding mono_set by auto
  3.2233 -
  3.2234 -lemma mono_set_iff:
  3.2235 -  fixes S :: "'a::{linorder,complete_lattice} set"
  3.2236 -  defines "a \<equiv> Inf S"
  3.2237 -  shows "mono S \<longleftrightarrow> (S = {a <..} \<or> S = {a..})" (is "_ = ?c")
  3.2238 -proof
  3.2239 -  assume "mono S"
  3.2240 -  then have mono: "\<And>x y. x \<le> y \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S" by (auto simp: mono_set)
  3.2241 -  show ?c
  3.2242 -  proof cases
  3.2243 -    assume "a \<in> S"
  3.2244 -    show ?c
  3.2245 -      using mono[OF _ `a \<in> S`]
  3.2246 -      by (auto intro: complete_lattice_class.Inf_lower simp: a_def)
  3.2247 -  next
  3.2248 -    assume "a \<notin> S"
  3.2249 -    have "S = {a <..}"
  3.2250 -    proof safe
  3.2251 -      fix x assume "x \<in> S"
  3.2252 -      then have "a \<le> x" unfolding a_def by (rule complete_lattice_class.Inf_lower)
  3.2253 -      then show "a < x" using `x \<in> S` `a \<notin> S` by (cases "a = x") auto
  3.2254 -    next
  3.2255 -      fix x assume "a < x"
  3.2256 -      then obtain y where "y < x" "y \<in> S" unfolding a_def Inf_less_iff ..
  3.2257 -      with mono[of y x] show "x \<in> S" by auto
  3.2258 -    qed
  3.2259 -    then show ?c ..
  3.2260 -  qed
  3.2261 -qed auto
  3.2262 -
  3.2263 -lemma lim_imp_Liminf:
  3.2264 -  fixes f :: "'a \<Rightarrow> extreal"
  3.2265 -  assumes ntriv: "\<not> trivial_limit net"
  3.2266 -  assumes lim: "(f ---> f0) net"
  3.2267 -  shows "Liminf net f = f0"
  3.2268 -  unfolding Liminf_Sup
  3.2269 -proof (safe intro!: extreal_SupI)
  3.2270 -  fix y assume *: "\<forall>y'<y. eventually (\<lambda>x. y' < f x) net"
  3.2271 -  show "y \<le> f0"
  3.2272 -  proof (rule extreal_le_extreal)
  3.2273 -    fix B assume "B < y"
  3.2274 -    { assume "f0 < B"
  3.2275 -      then have "eventually (\<lambda>x. f x < B \<and> B < f x) net"
  3.2276 -         using topological_tendstoD[OF lim, of "{..<B}"] *[rule_format, OF `B < y`]
  3.2277 -         by (auto intro: eventually_conj)
  3.2278 -      also have "(\<lambda>x. f x < B \<and> B < f x) = (\<lambda>x. False)" by (auto simp: fun_eq_iff)
  3.2279 -      finally have False using ntriv[unfolded trivial_limit_def] by auto
  3.2280 -    } then show "B \<le> f0" by (metis linorder_le_less_linear)
  3.2281 -  qed
  3.2282 -next
  3.2283 -  fix y assume *: "\<forall>z. z \<in> {l. \<forall>y<l. eventually (\<lambda>x. y < f x) net} \<longrightarrow> z \<le> y"
  3.2284 -  show "f0 \<le> y"
  3.2285 -  proof (safe intro!: *[rule_format])
  3.2286 -    fix y assume "y < f0" then show "eventually (\<lambda>x. y < f x) net"
  3.2287 -      using lim[THEN topological_tendstoD, of "{y <..}"] by auto
  3.2288 -  qed
  3.2289 -qed
  3.2290 -
  3.2291 -lemma extreal_Liminf_le_Limsup:
  3.2292 -  fixes f :: "'a \<Rightarrow> extreal"
  3.2293 -  assumes ntriv: "\<not> trivial_limit net"
  3.2294 -  shows "Liminf net f \<le> Limsup net f"
  3.2295 -  unfolding Limsup_Inf Liminf_Sup
  3.2296 -proof (safe intro!: complete_lattice_class.Inf_greatest  complete_lattice_class.Sup_least)
  3.2297 -  fix u v assume *: "\<forall>y<u. eventually (\<lambda>x. y < f x) net" "\<forall>y>v. eventually (\<lambda>x. f x < y) net"
  3.2298 -  show "u \<le> v"
  3.2299 -  proof (rule ccontr)
  3.2300 -    assume "\<not> u \<le> v"
  3.2301 -    then obtain t where "t < u" "v < t"
  3.2302 -      using extreal_dense[of v u] by (auto simp: not_le)
  3.2303 -    then have "eventually (\<lambda>x. t < f x \<and> f x < t) net"
  3.2304 -      using * by (auto intro: eventually_conj)
  3.2305 -    also have "(\<lambda>x. t < f x \<and> f x < t) = (\<lambda>x. False)" by (auto simp: fun_eq_iff)
  3.2306 -    finally show False using ntriv by (auto simp: trivial_limit_def)
  3.2307 -  qed
  3.2308 -qed
  3.2309 -
  3.2310 -lemma Liminf_mono:
  3.2311 -  fixes f g :: "'a => extreal"
  3.2312 -  assumes ev: "eventually (\<lambda>x. f x \<le> g x) net"
  3.2313 -  shows "Liminf net f \<le> Liminf net g"
  3.2314 -  unfolding Liminf_Sup
  3.2315 -proof (safe intro!: Sup_mono bexI)
  3.2316 -  fix a y assume "\<forall>y<a. eventually (\<lambda>x. y < f x) net" and "y < a"
  3.2317 -  then have "eventually (\<lambda>x. y < f x) net" by auto
  3.2318 -  then show "eventually (\<lambda>x. y < g x) net"
  3.2319 -    by (rule eventually_rev_mp) (rule eventually_mono[OF _ ev], auto)
  3.2320 -qed simp
  3.2321 -
  3.2322 -lemma Liminf_eq:
  3.2323 -  fixes f g :: "'a \<Rightarrow> extreal"
  3.2324 -  assumes "eventually (\<lambda>x. f x = g x) net"
  3.2325 -  shows "Liminf net f = Liminf net g"
  3.2326 -  by (intro antisym Liminf_mono eventually_mono[OF _ assms]) auto
  3.2327 -
  3.2328 -lemma Liminf_mono_all:
  3.2329 -  fixes f g :: "'a \<Rightarrow> extreal"
  3.2330 -  assumes "\<And>x. f x \<le> g x"
  3.2331 -  shows "Liminf net f \<le> Liminf net g"
  3.2332 -  using assms by (intro Liminf_mono always_eventually) auto
  3.2333 -
  3.2334 -lemma Limsup_mono:
  3.2335 -  fixes f g :: "'a \<Rightarrow> extreal"
  3.2336 -  assumes ev: "eventually (\<lambda>x. f x \<le> g x) net"
  3.2337 -  shows "Limsup net f \<le> Limsup net g"
  3.2338 -  unfolding Limsup_Inf
  3.2339 -proof (safe intro!: Inf_mono bexI)
  3.2340 -  fix a y assume "\<forall>y>a. eventually (\<lambda>x. g x < y) net" and "a < y"
  3.2341 -  then have "eventually (\<lambda>x. g x < y) net" by auto
  3.2342 -  then show "eventually (\<lambda>x. f x < y) net"
  3.2343 -    by (rule eventually_rev_mp) (rule eventually_mono[OF _ ev], auto)
  3.2344 -qed simp
  3.2345 -
  3.2346 -lemma Limsup_mono_all:
  3.2347 -  fixes f g :: "'a \<Rightarrow> extreal"
  3.2348 -  assumes "\<And>x. f x \<le> g x"
  3.2349 -  shows "Limsup net f \<le> Limsup net g"
  3.2350 -  using assms by (intro Limsup_mono always_eventually) auto
  3.2351 -
  3.2352 -lemma Limsup_eq:
  3.2353 -  fixes f g :: "'a \<Rightarrow> extreal"
  3.2354 -  assumes "eventually (\<lambda>x. f x = g x) net"
  3.2355 -  shows "Limsup net f = Limsup net g"
  3.2356 -  by (intro antisym Limsup_mono eventually_mono[OF _ assms]) auto
  3.2357 -
  3.2358 -abbreviation "liminf \<equiv> Liminf sequentially"
  3.2359 -
  3.2360 -abbreviation "limsup \<equiv> Limsup sequentially"
  3.2361 -
  3.2362 -lemma (in complete_lattice) less_INFD:
  3.2363 -  assumes "y < INFI A f"" i \<in> A" shows "y < f i"
  3.2364 -proof -
  3.2365 -  note `y < INFI A f`
  3.2366 -  also have "INFI A f \<le> f i" using `i \<in> A` by (rule INF_leI)
  3.2367 -  finally show "y < f i" .
  3.2368 -qed
  3.2369 -
  3.2370 -lemma liminf_SUPR_INFI:
  3.2371 -  fixes f :: "nat \<Rightarrow> extreal"
  3.2372 -  shows "liminf f = (SUP n. INF m:{n..}. f m)"
  3.2373 -  unfolding Liminf_Sup eventually_sequentially
  3.2374 -proof (safe intro!: antisym complete_lattice_class.Sup_least)
  3.2375 -  fix x assume *: "\<forall>y<x. \<exists>N. \<forall>n\<ge>N. y < f n" show "x \<le> (SUP n. INF m:{n..}. f m)"
  3.2376 -  proof (rule extreal_le_extreal)
  3.2377 -    fix y assume "y < x"
  3.2378 -    with * obtain N where "\<And>n. N \<le> n \<Longrightarrow> y < f n" by auto
  3.2379 -    then have "y \<le> (INF m:{N..}. f m)" by (force simp: le_INF_iff)
  3.2380 -    also have "\<dots> \<le> (SUP n. INF m:{n..}. f m)" by (intro le_SUPI) auto
  3.2381 -    finally show "y \<le> (SUP n. INF m:{n..}. f m)" .
  3.2382 -  qed
  3.2383 -next
  3.2384 -  show "(SUP n. INF m:{n..}. f m) \<le> Sup {l. \<forall>y<l. \<exists>N. \<forall>n\<ge>N. y < f n}"
  3.2385 -  proof (unfold SUPR_def, safe intro!: Sup_mono bexI)
  3.2386 -    fix y n assume "y < INFI {n..} f"
  3.2387 -    from less_INFD[OF this] show "\<exists>N. \<forall>n\<ge>N. y < f n" by (intro exI[of _ n]) auto
  3.2388 -  qed (rule order_refl)
  3.2389 -qed
  3.2390 -
  3.2391 -lemma tail_same_limsup:
  3.2392 -  fixes X Y :: "nat => extreal"
  3.2393 -  assumes "\<And>n. N \<le> n \<Longrightarrow> X n = Y n"
  3.2394 -  shows "limsup X = limsup Y"
  3.2395 -  using Limsup_eq[of X Y sequentially] eventually_sequentially assms by auto
  3.2396 -
  3.2397 -lemma tail_same_liminf:
  3.2398 -  fixes X Y :: "nat => extreal"
  3.2399 -  assumes "\<And>n. N \<le> n \<Longrightarrow> X n = Y n"
  3.2400 -  shows "liminf X = liminf Y"
  3.2401 -  using Liminf_eq[of X Y sequentially] eventually_sequentially assms by auto
  3.2402 -
  3.2403 -lemma liminf_mono:
  3.2404 -  fixes X Y :: "nat \<Rightarrow> extreal"
  3.2405 -  assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= Y n"
  3.2406 -  shows "liminf X \<le> liminf Y"
  3.2407 -  using Liminf_mono[of X Y sequentially] eventually_sequentially assms by auto
  3.2408 -
  3.2409 -lemma limsup_mono:
  3.2410 -  fixes X Y :: "nat => extreal"
  3.2411 -  assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= Y n"
  3.2412 -  shows "limsup X \<le> limsup Y"
  3.2413 -  using Limsup_mono[of X Y sequentially] eventually_sequentially assms by auto
  3.2414 -
  3.2415 -declare trivial_limit_sequentially[simp]
  3.2416 -
  3.2417 -lemma
  3.2418 -  fixes X :: "nat \<Rightarrow> extreal"
  3.2419 -  shows extreal_incseq_uminus[simp]: "incseq (\<lambda>i. - X i) = decseq X"
  3.2420 -    and extreal_decseq_uminus[simp]: "decseq (\<lambda>i. - X i) = incseq X"
  3.2421 -  unfolding incseq_def decseq_def by auto
  3.2422 -
  3.2423 -lemma liminf_bounded:
  3.2424 -  fixes X Y :: "nat \<Rightarrow> extreal"
  3.2425 -  assumes "\<And>n. N \<le> n \<Longrightarrow> C \<le> X n"
  3.2426 -  shows "C \<le> liminf X"
  3.2427 -  using liminf_mono[of N "\<lambda>n. C" X] assms Liminf_const[of sequentially C] by simp
  3.2428 -
  3.2429 -lemma limsup_bounded:
  3.2430 -  fixes X Y :: "nat => extreal"
  3.2431 -  assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= C"
  3.2432 -  shows "limsup X \<le> C"
  3.2433 -  using limsup_mono[of N X "\<lambda>n. C"] assms Limsup_const[of sequentially C] by simp
  3.2434 -
  3.2435 -lemma liminf_bounded_iff:
  3.2436 -  fixes x :: "nat \<Rightarrow> extreal"
  3.2437 -  shows "C \<le> liminf x \<longleftrightarrow> (\<forall>B<C. \<exists>N. \<forall>n\<ge>N. B < x n)" (is "?lhs <-> ?rhs")
  3.2438 -proof safe
  3.2439 -  fix B assume "B < C" "C \<le> liminf x"
  3.2440 -  then have "B < liminf x" by auto
  3.2441 -  then obtain N where "B < (INF m:{N..}. x m)"
  3.2442 -    unfolding liminf_SUPR_INFI SUPR_def less_Sup_iff by auto
  3.2443 -  from less_INFD[OF this] show "\<exists>N. \<forall>n\<ge>N. B < x n" by auto
  3.2444 -next
  3.2445 -  assume *: "\<forall>B<C. \<exists>N. \<forall>n\<ge>N. B < x n"
  3.2446 -  { fix B assume "B<C"
  3.2447 -    then obtain N where "\<forall>n\<ge>N. B < x n" using `?rhs` by auto
  3.2448 -    hence "B \<le> (INF m:{N..}. x m)" by (intro le_INFI) auto
  3.2449 -    also have "... \<le> liminf x" unfolding liminf_SUPR_INFI by (intro le_SUPI) simp
  3.2450 -    finally have "B \<le> liminf x" .
  3.2451 -  } then show "?lhs" by (metis * leD liminf_bounded linorder_le_less_linear)
  3.2452 -qed
  3.2453 -
  3.2454 -lemma liminf_subseq_mono:
  3.2455 -  fixes X :: "nat \<Rightarrow> extreal"
  3.2456 -  assumes "subseq r"
  3.2457 -  shows "liminf X \<le> liminf (X \<circ> r) "
  3.2458 -proof-
  3.2459 -  have "\<And>n. (INF m:{n..}. X m) \<le> (INF m:{n..}. (X \<circ> r) m)"
  3.2460 -  proof (safe intro!: INF_mono)
  3.2461 -    fix n m :: nat assume "n \<le> m" then show "\<exists>ma\<in>{n..}. X ma \<le> (X \<circ> r) m"
  3.2462 -      using seq_suble[OF `subseq r`, of m] by (intro bexI[of _ "r m"]) auto
  3.2463 -  qed
  3.2464 -  then show ?thesis by (auto intro!: SUP_mono simp: liminf_SUPR_INFI comp_def)
  3.2465 -qed
  3.2466 -
  3.2467 -lemma extreal_real': assumes "\<bar>x\<bar> \<noteq> \<infinity>" shows "extreal (real x) = x"
  3.2468 -  using assms by auto
  3.2469 -
  3.2470 -lemma extreal_le_extreal_bounded:
  3.2471 -  fixes x y z :: extreal
  3.2472 -  assumes "z \<le> y"
  3.2473 -  assumes *: "\<And>B. z < B \<Longrightarrow> B < x \<Longrightarrow> B \<le> y"
  3.2474 -  shows "x \<le> y"
  3.2475 -proof (rule extreal_le_extreal)
  3.2476 -  fix B assume "B < x"
  3.2477 -  show "B \<le> y"
  3.2478 -  proof cases
  3.2479 -    assume "z < B" from *[OF this `B < x`] show "B \<le> y" .
  3.2480 -  next
  3.2481 -    assume "\<not> z < B" with `z \<le> y` show "B \<le> y" by auto
  3.2482 -  qed
  3.2483 -qed
  3.2484 -
  3.2485 -lemma fixes x y :: extreal
  3.2486 -  shows Sup_atMost[simp]: "Sup {.. y} = y"
  3.2487 -    and Sup_lessThan[simp]: "Sup {..< y} = y"
  3.2488 -    and Sup_atLeastAtMost[simp]: "x \<le> y \<Longrightarrow> Sup { x .. y} = y"
  3.2489 -    and Sup_greaterThanAtMost[simp]: "x < y \<Longrightarrow> Sup { x <.. y} = y"
  3.2490 -    and Sup_atLeastLessThan[simp]: "x < y \<Longrightarrow> Sup { x ..< y} = y"
  3.2491 -  by (auto simp: Sup_extreal_def intro!: Least_equality
  3.2492 -           intro: extreal_le_extreal extreal_le_extreal_bounded[of x])
  3.2493 -
  3.2494 -lemma Sup_greaterThanlessThan[simp]:
  3.2495 -  fixes x y :: extreal assumes "x < y" shows "Sup { x <..< y} = y"
  3.2496 -  unfolding Sup_extreal_def
  3.2497 -proof (intro Least_equality extreal_le_extreal_bounded[of _ _ y])
  3.2498 -  fix z assume z: "\<forall>u\<in>{x<..<y}. u \<le> z"
  3.2499 -  from extreal_dense[OF `x < y`] guess w .. note w = this
  3.2500 -  with z[THEN bspec, of w] show "x \<le> z" by auto
  3.2501 -qed auto
  3.2502 -
  3.2503 -lemma real_extreal_id: "real o extreal = id"
  3.2504 -proof-
  3.2505 -{ fix x have "(real o extreal) x = id x" by auto }
  3.2506 -from this show ?thesis using ext by blast
  3.2507 -qed
  3.2508 -
  3.2509 -lemma open_image_extreal: "open(UNIV-{\<infinity>,(-\<infinity>)})"
  3.2510 -by (metis range_extreal open_extreal open_UNIV)
  3.2511 -
  3.2512 -lemma extreal_le_distrib:
  3.2513 -  fixes a b c :: extreal shows "c * (a + b) \<le> c * a + c * b"
  3.2514 -  by (cases rule: extreal3_cases[of a b c])
  3.2515 -     (auto simp add: field_simps not_le mult_le_0_iff mult_less_0_iff)
  3.2516 -
  3.2517 -lemma extreal_pos_distrib:
  3.2518 -  fixes a b c :: extreal assumes "0 \<le> c" "c \<noteq> \<infinity>" shows "c * (a + b) = c * a + c * b"
  3.2519 -  using assms by (cases rule: extreal3_cases[of a b c])
  3.2520 -                 (auto simp add: field_simps not_le mult_le_0_iff mult_less_0_iff)
  3.2521 -
  3.2522 -lemma extreal_pos_le_distrib:
  3.2523 -fixes a b c :: extreal
  3.2524 -assumes "c>=0"
  3.2525 -shows "c * (a + b) <= c * a + c * b"
  3.2526 -  using assms by (cases rule: extreal3_cases[of a b c])
  3.2527 -                 (auto simp add: field_simps)
  3.2528 -
  3.2529 -lemma extreal_max_mono:
  3.2530 -  "[| (a::extreal) <= b; c <= d |] ==> max a c <= max b d"
  3.2531 -  by (metis sup_extreal_def sup_mono)
  3.2532 -
  3.2533 -
  3.2534 -lemma extreal_max_least:
  3.2535 -  "[| (a::extreal) <= x; c <= x |] ==> max a c <= x"
  3.2536 -  by (metis sup_extreal_def sup_least)
  3.2537 -
  3.2538 -end
     4.1 --- a/src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy	Tue Jul 19 14:35:44 2011 +0200
     4.2 +++ b/src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy	Tue Jul 19 14:36:12 2011 +0200
     4.3 @@ -8,87 +8,87 @@
     4.4  header {* Limits on the Extended real number line *}
     4.5  
     4.6  theory Extended_Real_Limits
     4.7 -  imports Topology_Euclidean_Space "~~/src/HOL/Library/Extended_Reals"
     4.8 +  imports Topology_Euclidean_Space "~~/src/HOL/Library/Extended_Real"
     4.9  begin
    4.10  
    4.11 -lemma continuous_on_extreal[intro, simp]: "continuous_on A extreal"
    4.12 -  unfolding continuous_on_topological open_extreal_def by auto
    4.13 +lemma continuous_on_ereal[intro, simp]: "continuous_on A ereal"
    4.14 +  unfolding continuous_on_topological open_ereal_def by auto
    4.15  
    4.16 -lemma continuous_at_extreal[intro, simp]: "continuous (at x) extreal"
    4.17 +lemma continuous_at_ereal[intro, simp]: "continuous (at x) ereal"
    4.18    using continuous_on_eq_continuous_at[of UNIV] by auto
    4.19  
    4.20 -lemma continuous_within_extreal[intro, simp]: "x \<in> A \<Longrightarrow> continuous (at x within A) extreal"
    4.21 +lemma continuous_within_ereal[intro, simp]: "x \<in> A \<Longrightarrow> continuous (at x within A) ereal"
    4.22    using continuous_on_eq_continuous_within[of A] by auto
    4.23  
    4.24 -lemma extreal_open_uminus:
    4.25 -  fixes S :: "extreal set"
    4.26 +lemma ereal_open_uminus:
    4.27 +  fixes S :: "ereal set"
    4.28    assumes "open S"
    4.29    shows "open (uminus ` S)"
    4.30 -  unfolding open_extreal_def
    4.31 +  unfolding open_ereal_def
    4.32  proof (intro conjI impI)
    4.33 -  obtain x y where S: "open (extreal -` S)"
    4.34 -    "\<infinity> \<in> S \<Longrightarrow> {extreal x<..} \<subseteq> S" "-\<infinity> \<in> S \<Longrightarrow> {..< extreal y} \<subseteq> S"
    4.35 -    using `open S` unfolding open_extreal_def by auto
    4.36 -  have "extreal -` uminus ` S = uminus ` (extreal -` S)"
    4.37 +  obtain x y where S: "open (ereal -` S)"
    4.38 +    "\<infinity> \<in> S \<Longrightarrow> {ereal x<..} \<subseteq> S" "-\<infinity> \<in> S \<Longrightarrow> {..< ereal y} \<subseteq> S"
    4.39 +    using `open S` unfolding open_ereal_def by auto
    4.40 +  have "ereal -` uminus ` S = uminus ` (ereal -` S)"
    4.41    proof safe
    4.42 -    fix x y assume "extreal x = - y" "y \<in> S"
    4.43 -    then show "x \<in> uminus ` extreal -` S" by (cases y) auto
    4.44 +    fix x y assume "ereal x = - y" "y \<in> S"
    4.45 +    then show "x \<in> uminus ` ereal -` S" by (cases y) auto
    4.46    next
    4.47 -    fix x assume "extreal x \<in> S"
    4.48 -    then show "- x \<in> extreal -` uminus ` S"
    4.49 -      by (auto intro: image_eqI[of _ _ "extreal x"])
    4.50 +    fix x assume "ereal x \<in> S"
    4.51 +    then show "- x \<in> ereal -` uminus ` S"
    4.52 +      by (auto intro: image_eqI[of _ _ "ereal x"])
    4.53    qed
    4.54 -  then show "open (extreal -` uminus ` S)"
    4.55 +  then show "open (ereal -` uminus ` S)"
    4.56      using S by (auto intro: open_negations)
    4.57    { assume "\<infinity> \<in> uminus ` S"
    4.58 -    then have "-\<infinity> \<in> S" by (metis image_iff extreal_uminus_uminus)
    4.59 -    then have "uminus ` {..<extreal y} \<subseteq> uminus ` S" using S by (intro image_mono) auto
    4.60 -    then show "\<exists>x. {extreal x<..} \<subseteq> uminus ` S" using extreal_uminus_lessThan by auto }
    4.61 +    then have "-\<infinity> \<in> S" by (metis image_iff ereal_uminus_uminus)
    4.62 +    then have "uminus ` {..<ereal y} \<subseteq> uminus ` S" using S by (intro image_mono) auto
    4.63 +    then show "\<exists>x. {ereal x<..} \<subseteq> uminus ` S" using ereal_uminus_lessThan by auto }
    4.64    { assume "-\<infinity> \<in> uminus ` S"
    4.65 -    then have "\<infinity> : S" by (metis image_iff extreal_uminus_uminus)
    4.66 -    then have "uminus ` {extreal x<..} <= uminus ` S" using S by (intro image_mono) auto
    4.67 -    then show "\<exists>y. {..<extreal y} <= uminus ` S" using extreal_uminus_greaterThan by auto }
    4.68 +    then have "\<infinity> : S" by (metis image_iff ereal_uminus_uminus)
    4.69 +    then have "uminus ` {ereal x<..} <= uminus ` S" using S by (intro image_mono) auto
    4.70 +    then show "\<exists>y. {..<ereal y} <= uminus ` S" using ereal_uminus_greaterThan by auto }
    4.71  qed
    4.72  
    4.73 -lemma extreal_uminus_complement:
    4.74 -  fixes S :: "extreal set"
    4.75 +lemma ereal_uminus_complement:
    4.76 +  fixes S :: "ereal set"
    4.77    shows "uminus ` (- S) = - uminus ` S"
    4.78    by (auto intro!: bij_image_Compl_eq surjI[of _ uminus] simp: bij_betw_def)
    4.79  
    4.80 -lemma extreal_closed_uminus:
    4.81 -  fixes S :: "extreal set"
    4.82 +lemma ereal_closed_uminus:
    4.83 +  fixes S :: "ereal set"
    4.84    assumes "closed S"
    4.85    shows "closed (uminus ` S)"
    4.86  using assms unfolding closed_def
    4.87 -using extreal_open_uminus[of "- S"] extreal_uminus_complement by auto
    4.88 +using ereal_open_uminus[of "- S"] ereal_uminus_complement by auto
    4.89  
    4.90 -lemma not_open_extreal_singleton:
    4.91 -  "\<not> (open {a :: extreal})"
    4.92 +lemma not_open_ereal_singleton:
    4.93 +  "\<not> (open {a :: ereal})"
    4.94  proof(rule ccontr)
    4.95    assume "\<not> \<not> open {a}" hence a: "open {a}" by auto
    4.96    show False
    4.97    proof (cases a)
    4.98      case MInf
    4.99 -    then obtain y where "{..<extreal y} <= {a}" using a open_MInfty2[of "{a}"] by auto
   4.100 -    hence "extreal(y - 1):{a}" apply (subst subsetD[of "{..<extreal y}"]) by auto
   4.101 +    then obtain y where "{..<ereal y} <= {a}" using a open_MInfty2[of "{a}"] by auto
   4.102 +    hence "ereal(y - 1):{a}" apply (subst subsetD[of "{..<ereal y}"]) by auto
   4.103      then show False using `a=(-\<infinity>)` by auto
   4.104    next
   4.105      case PInf
   4.106 -    then obtain y where "{extreal y<..} <= {a}" using a open_PInfty2[of "{a}"] by auto
   4.107 -    hence "extreal(y+1):{a}" apply (subst subsetD[of "{extreal y<..}"]) by auto
   4.108 +    then obtain y where "{ereal y<..} <= {a}" using a open_PInfty2[of "{a}"] by auto
   4.109 +    hence "ereal(y+1):{a}" apply (subst subsetD[of "{ereal y<..}"]) by auto
   4.110      then show False using `a=\<infinity>` by auto
   4.111    next
   4.112      case (real r) then have fin: "\<bar>a\<bar> \<noteq> \<infinity>" by simp
   4.113 -    from extreal_open_cont_interval[OF a singletonI this] guess e . note e = this
   4.114 +    from ereal_open_cont_interval[OF a singletonI this] guess e . note e = this
   4.115      then obtain b where b_def: "a<b & b<a+e"
   4.116 -      using fin extreal_between extreal_dense[of a "a+e"] by auto
   4.117 -    then have "b: {a-e <..< a+e}" using fin extreal_between[of a e] e by auto
   4.118 +      using fin ereal_between ereal_dense[of a "a+e"] by auto
   4.119 +    then have "b: {a-e <..< a+e}" using fin ereal_between[of a e] e by auto
   4.120      then show False using b_def e by auto
   4.121    qed
   4.122  qed
   4.123  
   4.124 -lemma extreal_closed_contains_Inf:
   4.125 -  fixes S :: "extreal set"
   4.126 +lemma ereal_closed_contains_Inf:
   4.127 +  fixes S :: "ereal set"
   4.128    assumes "closed S" "S ~= {}"
   4.129    shows "Inf S : S"
   4.130  proof(rule ccontr)
   4.131 @@ -96,8 +96,8 @@
   4.132    show False
   4.133    proof (cases "Inf S")
   4.134      case MInf hence "(-\<infinity>) : - S" using a by auto
   4.135 -    then obtain y where "{..<extreal y} <= (-S)" using a open_MInfty2[of "- S"] by auto
   4.136 -    hence "extreal y <= Inf S" by (metis Compl_anti_mono Compl_lessThan atLeast_iff
   4.137 +    then obtain y where "{..<ereal y} <= (-S)" using a open_MInfty2[of "- S"] by auto
   4.138 +    hence "ereal y <= Inf S" by (metis Compl_anti_mono Compl_lessThan atLeast_iff
   4.139        complete_lattice_class.Inf_greatest double_complement set_rev_mp)
   4.140      then show False using MInf by auto
   4.141    next
   4.142 @@ -105,9 +105,9 @@
   4.143      then show False by (metis `Inf S ~: S` insert_code mem_def PInf)
   4.144    next
   4.145      case (real r) then have fin: "\<bar>Inf S\<bar> \<noteq> \<infinity>" by simp
   4.146 -    from extreal_open_cont_interval[OF a this] guess e . note e = this
   4.147 +    from ereal_open_cont_interval[OF a this] guess e . note e = this
   4.148      { fix x assume "x:S" hence "x>=Inf S" by (rule complete_lattice_class.Inf_lower)
   4.149 -      hence *: "x>Inf S-e" using e by (metis fin extreal_between(1) order_less_le_trans)
   4.150 +      hence *: "x>Inf S-e" using e by (metis fin ereal_between(1) order_less_le_trans)
   4.151        { assume "x<Inf S+e" hence "x:{Inf S-e <..< Inf S+e}" using * by auto
   4.152          hence False using e `x:S` by auto
   4.153        } hence "x>=Inf S+e" by (metis linorder_le_less_linear)
   4.154 @@ -116,115 +116,115 @@
   4.155    qed
   4.156  qed
   4.157  
   4.158 -lemma extreal_closed_contains_Sup:
   4.159 -  fixes S :: "extreal set"
   4.160 +lemma ereal_closed_contains_Sup:
   4.161 +  fixes S :: "ereal set"
   4.162    assumes "closed S" "S ~= {}"
   4.163    shows "Sup S : S"
   4.164  proof-
   4.165 -  have "closed (uminus ` S)" by (metis assms(1) extreal_closed_uminus)
   4.166 -  hence "Inf (uminus ` S) : uminus ` S" using assms extreal_closed_contains_Inf[of "uminus ` S"] by auto
   4.167 -  hence "- Sup S : uminus ` S" using extreal_Sup_uminus_image_eq[of "uminus ` S"] by (auto simp: image_image)
   4.168 -  thus ?thesis by (metis imageI extreal_uminus_uminus extreal_minus_minus_image)
   4.169 +  have "closed (uminus ` S)" by (metis assms(1) ereal_closed_uminus)
   4.170 +  hence "Inf (uminus ` S) : uminus ` S" using assms ereal_closed_contains_Inf[of "uminus ` S"] by auto
   4.171 +  hence "- Sup S : uminus ` S" using ereal_Sup_uminus_image_eq[of "uminus ` S"] by (auto simp: image_image)
   4.172 +  thus ?thesis by (metis imageI ereal_uminus_uminus ereal_minus_minus_image)
   4.173  qed
   4.174  
   4.175 -lemma extreal_open_closed_aux:
   4.176 -  fixes S :: "extreal set"
   4.177 +lemma ereal_open_closed_aux:
   4.178 +  fixes S :: "ereal set"
   4.179    assumes "open S" "closed S"
   4.180    assumes S: "(-\<infinity>) ~: S"
   4.181    shows "S = {}"
   4.182  proof(rule ccontr)
   4.183    assume "S ~= {}"
   4.184 -  hence *: "(Inf S):S" by (metis assms(2) extreal_closed_contains_Inf)
   4.185 +  hence *: "(Inf S):S" by (metis assms(2) ereal_closed_contains_Inf)
   4.186    { assume "Inf S=(-\<infinity>)" hence False using * assms(3) by auto }
   4.187    moreover
   4.188    { assume "Inf S=\<infinity>" hence "S={\<infinity>}" by (metis Inf_eq_PInfty `S ~= {}`)
   4.189 -    hence False by (metis assms(1) not_open_extreal_singleton) }
   4.190 +    hence False by (metis assms(1) not_open_ereal_singleton) }
   4.191    moreover
   4.192    { assume fin: "\<bar>Inf S\<bar> \<noteq> \<infinity>"
   4.193 -    from extreal_open_cont_interval[OF assms(1) * fin] guess e . note e = this
   4.194 +    from ereal_open_cont_interval[OF assms(1) * fin] guess e . note e = this
   4.195      then obtain b where b_def: "Inf S-e<b & b<Inf S"
   4.196 -      using fin extreal_between[of "Inf S" e] extreal_dense[of "Inf S-e"] by auto
   4.197 -    hence "b: {Inf S-e <..< Inf S+e}" using e fin extreal_between[of "Inf S" e] by auto
   4.198 +      using fin ereal_between[of "Inf S" e] ereal_dense[of "Inf S-e"] by auto
   4.199 +    hence "b: {Inf S-e <..< Inf S+e}" using e fin ereal_between[of "Inf S" e] by auto
   4.200      hence "b:S" using e by auto
   4.201      hence False using b_def by (metis complete_lattice_class.Inf_lower leD)
   4.202    } ultimately show False by auto
   4.203  qed
   4.204  
   4.205 -lemma extreal_open_closed:
   4.206 -  fixes S :: "extreal set"
   4.207 +lemma ereal_open_closed:
   4.208 +  fixes S :: "ereal set"
   4.209    shows "(open S & closed S) <-> (S = {} | S = UNIV)"
   4.210  proof-
   4.211  { assume lhs: "open S & closed S"
   4.212 -  { assume "(-\<infinity>) ~: S" hence "S={}" using lhs extreal_open_closed_aux by auto }
   4.213 +  { assume "(-\<infinity>) ~: S" hence "S={}" using lhs ereal_open_closed_aux by auto }
   4.214    moreover
   4.215 -  { assume "(-\<infinity>) : S" hence "(- S)={}" using lhs extreal_open_closed_aux[of "-S"] by auto }
   4.216 +  { assume "(-\<infinity>) : S" hence "(- S)={}" using lhs ereal_open_closed_aux[of "-S"] by auto }
   4.217    ultimately have "S = {} | S = UNIV" by auto
   4.218  } thus ?thesis by auto
   4.219  qed
   4.220  
   4.221 -lemma extreal_open_affinity_pos:
   4.222 +lemma ereal_open_affinity_pos:
   4.223    assumes "open S" and m: "m \<noteq> \<infinity>" "0 < m" and t: "\<bar>t\<bar> \<noteq> \<infinity>"
   4.224    shows "open ((\<lambda>x. m * x + t) ` S)"
   4.225  proof -
   4.226 -  obtain r where r[simp]: "m = extreal r" using m by (cases m) auto
   4.227 -  obtain p where p[simp]: "t = extreal p" using t by auto
   4.228 +  obtain r where r[simp]: "m = ereal r" using m by (cases m) auto
   4.229 +  obtain p where p[simp]: "t = ereal p" using t by auto
   4.230    have "r \<noteq> 0" "0 < r" and m': "m \<noteq> \<infinity>" "m \<noteq> -\<infinity>" "m \<noteq> 0" using m by auto
   4.231 -  from `open S`[THEN extreal_openE] guess l u . note T = this
   4.232 +  from `open S`[THEN ereal_openE] guess l u . note T = this
   4.233    let ?f = "(\<lambda>x. m * x + t)"
   4.234 -  show ?thesis unfolding open_extreal_def
   4.235 +  show ?thesis unfolding open_ereal_def
   4.236    proof (intro conjI impI exI subsetI)
   4.237 -    have "extreal -` ?f ` S = (\<lambda>x. r * x + p) ` (extreal -` S)"
   4.238 +    have "ereal -` ?f ` S = (\<lambda>x. r * x + p) ` (ereal -` S)"
   4.239      proof safe
   4.240 -      fix x y assume "extreal y = m * x + t" "x \<in> S"
   4.241 -      then show "y \<in> (\<lambda>x. r * x + p) ` extreal -` S"
   4.242 +      fix x y assume "ereal y = m * x + t" "x \<in> S"
   4.243 +      then show "y \<in> (\<lambda>x. r * x + p) ` ereal -` S"
   4.244          using `r \<noteq> 0` by (cases x) (auto intro!: image_eqI[of _ _ "real x"] split: split_if_asm)
   4.245      qed force
   4.246 -    then show "open (extreal -` ?f ` S)"
   4.247 +    then show "open (ereal -` ?f ` S)"
   4.248        using open_affinity[OF T(1) `r \<noteq> 0`] by (auto simp: ac_simps)
   4.249    next
   4.250      assume "\<infinity> \<in> ?f`S" with `0 < r` have "\<infinity> \<in> S" by auto
   4.251 -    fix x assume "x \<in> {extreal (r * l + p)<..}"
   4.252 -    then have [simp]: "extreal (r * l + p) < x" by auto
   4.253 +    fix x assume "x \<in> {ereal (r * l + p)<..}"
   4.254 +    then have [simp]: "ereal (r * l + p) < x" by auto
   4.255      show "x \<in> ?f`S"
   4.256      proof (rule image_eqI)
   4.257        show "x = m * ((x - t) / m) + t"
   4.258 -        using m t by (cases rule: extreal3_cases[of m x t]) auto
   4.259 -      have "extreal l < (x - t)/m"
   4.260 -        using m t by (simp add: extreal_less_divide_pos extreal_less_minus)
   4.261 +        using m t by (cases rule: ereal3_cases[of m x t]) auto
   4.262 +      have "ereal l < (x - t)/m"
   4.263 +        using m t by (simp add: ereal_less_divide_pos ereal_less_minus)
   4.264        then show "(x - t)/m \<in> S" using T(2)[OF `\<infinity> \<in> S`] by auto
   4.265      qed
   4.266    next
   4.267      assume "-\<infinity> \<in> ?f`S" with `0 < r` have "-\<infinity> \<in> S" by auto
   4.268 -    fix x assume "x \<in> {..<extreal (r * u + p)}"
   4.269 -    then have [simp]: "x < extreal (r * u + p)" by auto
   4.270 +    fix x assume "x \<in> {..<ereal (r * u + p)}"
   4.271 +    then have [simp]: "x < ereal (r * u + p)" by auto
   4.272      show "x \<in> ?f`S"
   4.273      proof (rule image_eqI)
   4.274        show "x = m * ((x - t) / m) + t"
   4.275 -        using m t by (cases rule: extreal3_cases[of m x t]) auto
   4.276 -      have "(x - t)/m < extreal u"
   4.277 -        using m t by (simp add: extreal_divide_less_pos extreal_minus_less)
   4.278 +        using m t by (cases rule: ereal3_cases[of m x t]) auto
   4.279 +      have "(x - t)/m < ereal u"
   4.280 +        using m t by (simp add: ereal_divide_less_pos ereal_minus_less)
   4.281        then show "(x - t)/m \<in> S" using T(3)[OF `-\<infinity> \<in> S`] by auto
   4.282      qed
   4.283    qed
   4.284  qed
   4.285  
   4.286 -lemma extreal_open_affinity:
   4.287 +lemma ereal_open_affinity:
   4.288    assumes "open S" and m: "\<bar>m\<bar> \<noteq> \<infinity>" "m \<noteq> 0" and t: "\<bar>t\<bar> \<noteq> \<infinity>"
   4.289    shows "open ((\<lambda>x. m * x + t) ` S)"
   4.290  proof cases
   4.291    assume "0 < m" then show ?thesis
   4.292 -    using extreal_open_affinity_pos[OF `open S` _ _ t, of m] m by auto
   4.293 +    using ereal_open_affinity_pos[OF `open S` _ _ t, of m] m by auto
   4.294  next
   4.295    assume "\<not> 0 < m" then
   4.296    have "0 < -m" using `m \<noteq> 0` by (cases m) auto
   4.297    then have m: "-m \<noteq> \<infinity>" "0 < -m" using `\<bar>m\<bar> \<noteq> \<infinity>`
   4.298 -    by (auto simp: extreal_uminus_eq_reorder)
   4.299 -  from extreal_open_affinity_pos[OF extreal_open_uminus[OF `open S`] m t]
   4.300 +    by (auto simp: ereal_uminus_eq_reorder)
   4.301 +  from ereal_open_affinity_pos[OF ereal_open_uminus[OF `open S`] m t]
   4.302    show ?thesis unfolding image_image by simp
   4.303  qed
   4.304  
   4.305 -lemma extreal_lim_mult:
   4.306 -  fixes X :: "'a \<Rightarrow> extreal"
   4.307 +lemma ereal_lim_mult:
   4.308 +  fixes X :: "'a \<Rightarrow> ereal"
   4.309    assumes lim: "(X ---> L) net" and a: "\<bar>a\<bar> \<noteq> \<infinity>"
   4.310    shows "((\<lambda>i. a * X i) ---> a * L) net"
   4.311  proof cases
   4.312 @@ -233,73 +233,73 @@
   4.313    proof (rule topological_tendstoI)
   4.314      fix S assume "open S" "a * L \<in> S"
   4.315      have "a * L / a = L"
   4.316 -      using `a \<noteq> 0` a by (cases rule: extreal2_cases[of a L]) auto
   4.317 +      using `a \<noteq> 0` a by (cases rule: ereal2_cases[of a L]) auto
   4.318      then have L: "L \<in> ((\<lambda>x. x / a) ` S)"
   4.319        using `a * L \<in> S` by (force simp: image_iff)
   4.320      moreover have "open ((\<lambda>x. x / a) ` S)"
   4.321 -      using extreal_open_affinity[OF `open S`, of "inverse a" 0] `a \<noteq> 0` a
   4.322 -      by (auto simp: extreal_divide_eq extreal_inverse_eq_0 divide_extreal_def ac_simps)
   4.323 +      using ereal_open_affinity[OF `open S`, of "inverse a" 0] `a \<noteq> 0` a
   4.324 +      by (auto simp: ereal_divide_eq ereal_inverse_eq_0 divide_ereal_def ac_simps)
   4.325      note * = lim[THEN topological_tendstoD, OF this L]
   4.326      { fix x from a `a \<noteq> 0` have "a * (x / a) = x"
   4.327 -        by (cases rule: extreal2_cases[of a x]) auto }
   4.328 +        by (cases rule: ereal2_cases[of a x]) auto }
   4.329      note this[simp]
   4.330      show "eventually (\<lambda>x. a * X x \<in> S) net"
   4.331        by (rule eventually_mono[OF _ *]) auto
   4.332    qed
   4.333  qed auto
   4.334  
   4.335 -lemma extreal_lim_uminus:
   4.336 -  fixes X :: "'a \<Rightarrow> extreal" shows "((\<lambda>i. - X i) ---> -L) net \<longleftrightarrow> (X ---> L) net"
   4.337 -  using extreal_lim_mult[of X L net "extreal (-1)"]
   4.338 -        extreal_lim_mult[of "(\<lambda>i. - X i)" "-L" net "extreal (-1)"]
   4.339 +lemma ereal_lim_uminus:
   4.340 +  fixes X :: "'a \<Rightarrow> ereal" shows "((\<lambda>i. - X i) ---> -L) net \<longleftrightarrow> (X ---> L) net"
   4.341 +  using ereal_lim_mult[of X L net "ereal (-1)"]
   4.342 +        ereal_lim_mult[of "(\<lambda>i. - X i)" "-L" net "ereal (-1)"]
   4.343    by (auto simp add: algebra_simps)
   4.344  
   4.345 -lemma Lim_bounded2_extreal:
   4.346 -  assumes lim:"f ----> (l :: extreal)"
   4.347 +lemma Lim_bounded2_ereal:
   4.348 +  assumes lim:"f ----> (l :: ereal)"
   4.349    and ge: "ALL n>=N. f n >= C"
   4.350    shows "l>=C"
   4.351  proof-
   4.352  def g == "(%i. -(f i))"
   4.353 -{ fix n assume "n>=N" hence "g n <= -C" using assms extreal_minus_le_minus g_def by auto }
   4.354 +{ fix n assume "n>=N" hence "g n <= -C" using assms ereal_minus_le_minus g_def by auto }
   4.355  hence "ALL n>=N. g n <= -C" by auto
   4.356 -moreover have limg: "g ----> (-l)" using g_def extreal_lim_uminus lim by auto
   4.357 -ultimately have "-l <= -C" using Lim_bounded_extreal[of g "-l" _ "-C"] by auto
   4.358 -from this show ?thesis using extreal_minus_le_minus by auto
   4.359 +moreover have limg: "g ----> (-l)" using g_def ereal_lim_uminus lim by auto
   4.360 +ultimately have "-l <= -C" using Lim_bounded_ereal[of g "-l" _ "-C"] by auto
   4.361 +from this show ?thesis using ereal_minus_le_minus by auto
   4.362  qed
   4.363  
   4.364  
   4.365 -lemma extreal_open_atLeast: "open {x..} \<longleftrightarrow> x = -\<infinity>"
   4.366 +lemma ereal_open_atLeast: "open {x..} \<longleftrightarrow> x = -\<infinity>"
   4.367  proof
   4.368    assume "x = -\<infinity>" then have "{x..} = UNIV" by auto
   4.369    then show "open {x..}" by auto
   4.370  next
   4.371    assume "open {x..}"
   4.372    then have "open {x..} \<and> closed {x..}" by auto
   4.373 -  then have "{x..} = UNIV" unfolding extreal_open_closed by auto
   4.374 -  then show "x = -\<infinity>" by (simp add: bot_extreal_def atLeast_eq_UNIV_iff)
   4.375 +  then have "{x..} = UNIV" unfolding ereal_open_closed by auto
   4.376 +  then show "x = -\<infinity>" by (simp add: bot_ereal_def atLeast_eq_UNIV_iff)
   4.377  qed
   4.378  
   4.379 -lemma extreal_open_mono_set:
   4.380 -  fixes S :: "extreal set"
   4.381 +lemma ereal_open_mono_set:
   4.382 +  fixes S :: "ereal set"
   4.383    defines "a \<equiv> Inf S"
   4.384    shows "(open S \<and> mono S) \<longleftrightarrow> (S = UNIV \<or> S = {a <..})"
   4.385 -  by (metis Inf_UNIV a_def atLeast_eq_UNIV_iff extreal_open_atLeast
   4.386 -            extreal_open_closed mono_set_iff open_extreal_greaterThan)
   4.387 +  by (metis Inf_UNIV a_def atLeast_eq_UNIV_iff ereal_open_atLeast
   4.388 +            ereal_open_closed mono_set_iff open_ereal_greaterThan)
   4.389  
   4.390 -lemma extreal_closed_mono_set:
   4.391 -  fixes S :: "extreal set"
   4.392 +lemma ereal_closed_mono_set:
   4.393 +  fixes S :: "ereal set"
   4.394    shows "(closed S \<and> mono S) \<longleftrightarrow> (S = {} \<or> S = {Inf S ..})"
   4.395 -  by (metis Inf_UNIV atLeast_eq_UNIV_iff closed_extreal_atLeast
   4.396 -            extreal_open_closed mono_empty mono_set_iff open_extreal_greaterThan)
   4.397 +  by (metis Inf_UNIV atLeast_eq_UNIV_iff closed_ereal_atLeast
   4.398 +            ereal_open_closed mono_empty mono_set_iff open_ereal_greaterThan)
   4.399  
   4.400 -lemma extreal_Liminf_Sup_monoset:
   4.401 -  fixes f :: "'a => extreal"
   4.402 +lemma ereal_Liminf_Sup_monoset:
   4.403 +  fixes f :: "'a => ereal"
   4.404    shows "Liminf net f = Sup {l. \<forall>S. open S \<longrightarrow> mono S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) net}"
   4.405    unfolding Liminf_Sup
   4.406  proof (intro arg_cong[where f="\<lambda>P. Sup (Collect P)"] ext iffI allI impI)
   4.407    fix l S assume ev: "\<forall>y<l. eventually (\<lambda>x. y < f x) net" and "open S" "mono S" "l \<in> S"
   4.408    then have "S = UNIV \<or> S = {Inf S <..}"
   4.409 -    using extreal_open_mono_set[of S] by auto
   4.410 +    using ereal_open_mono_set[of S] by auto
   4.411    then show "eventually (\<lambda>x. f x \<in> S) net"
   4.412    proof
   4.413      assume S: "S = {Inf S<..}"
   4.414 @@ -314,15 +314,15 @@
   4.415    then show "eventually (\<lambda>x. y < f x) net" by auto
   4.416  qed
   4.417  
   4.418 -lemma extreal_Limsup_Inf_monoset:
   4.419 -  fixes f :: "'a => extreal"
   4.420 +lemma ereal_Limsup_Inf_monoset:
   4.421 +  fixes f :: "'a => ereal"
   4.422    shows "Limsup net f = Inf {l. \<forall>S. open S \<longrightarrow> mono (uminus ` S) \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) net}"
   4.423    unfolding Limsup_Inf
   4.424  proof (intro arg_cong[where f="\<lambda>P. Inf (Collect P)"] ext iffI allI impI)
   4.425    fix l S assume ev: "\<forall>y>l. eventually (\<lambda>x. f x < y) net" and "open S" "mono (uminus`S)" "l \<in> S"
   4.426 -  then have "open (uminus`S) \<and> mono (uminus`S)" by (simp add: extreal_open_uminus)
   4.427 +  then have "open (uminus`S) \<and> mono (uminus`S)" by (simp add: ereal_open_uminus)
   4.428    then have "S = UNIV \<or> S = {..< Sup S}"
   4.429 -    unfolding extreal_open_mono_set extreal_Inf_uminus_image_eq extreal_image_uminus_shift by simp
   4.430 +    unfolding ereal_open_mono_set ereal_Inf_uminus_image_eq ereal_image_uminus_shift by simp
   4.431    then show "eventually (\<lambda>x. f x \<in> S) net"
   4.432    proof
   4.433      assume S: "S = {..< Sup S}"
   4.434 @@ -338,70 +338,70 @@
   4.435  qed
   4.436  
   4.437  
   4.438 -lemma open_uminus_iff: "open (uminus ` S) \<longleftrightarrow> open (S::extreal set)"
   4.439 -  using extreal_open_uminus[of S] extreal_open_uminus[of "uminus`S"] by auto
   4.440 +lemma open_uminus_iff: "open (uminus ` S) \<longleftrightarrow> open (S::ereal set)"
   4.441 +  using ereal_open_uminus[of S] ereal_open_uminus[of "uminus`S"] by auto
   4.442  
   4.443 -lemma extreal_Limsup_uminus:
   4.444 -  fixes f :: "'a => extreal"
   4.445 +lemma ereal_Limsup_uminus:
   4.446 +  fixes f :: "'a => ereal"
   4.447    shows "Limsup net (\<lambda>x. - (f x)) = -(Liminf net f)"
   4.448  proof -
   4.449 -  { fix P l have "(\<exists>x. (l::extreal) = -x \<and> P x) \<longleftrightarrow> P (-l)" by (auto intro!: exI[of _ "-l"]) }
   4.450 +  { fix P l have "(\<exists>x. (l::ereal) = -x \<and> P x) \<longleftrightarrow> P (-l)" by (auto intro!: exI[of _ "-l"]) }
   4.451    note Ex_cancel = this
   4.452 -  { fix P :: "extreal set \<Rightarrow> bool" have "(\<forall>S. P S) \<longleftrightarrow> (\<forall>S. P (uminus`S))"
   4.453 +  { fix P :: "ereal set \<Rightarrow> bool" have "(\<forall>S. P S) \<longleftrightarrow> (\<forall>S. P (uminus`S))"
   4.454        apply auto by (erule_tac x="uminus`S" in allE) (auto simp: image_image) }
   4.455    note add_uminus_image = this
   4.456 -  { fix x S have "(x::extreal) \<in> uminus`S \<longleftrightarrow> -x\<in>S" by (auto intro!: image_eqI[of _ _ "-x"]) }
   4.457 +  { fix x S have "(x::ereal) \<in> uminus`S \<longleftrightarrow> -x\<in>S" by (auto intro!: image_eqI[of _ _ "-x"]) }
   4.458    note remove_uminus_image = this
   4.459    show ?thesis
   4.460 -    unfolding extreal_Limsup_Inf_monoset extreal_Liminf_Sup_monoset
   4.461 -    unfolding extreal_Inf_uminus_image_eq[symmetric] image_Collect Ex_cancel
   4.462 +    unfolding ereal_Limsup_Inf_monoset ereal_Liminf_Sup_monoset
   4.463 +    unfolding ereal_Inf_uminus_image_eq[symmetric] image_Collect Ex_cancel
   4.464      by (subst add_uminus_image) (simp add: open_uminus_iff remove_uminus_image)
   4.465  qed
   4.466  
   4.467 -lemma extreal_Liminf_uminus:
   4.468 -  fixes f :: "'a => extreal"
   4.469 +lemma ereal_Liminf_uminus:
   4.470 +  fixes f :: "'a => ereal"
   4.471    shows "Liminf net (\<lambda>x. - (f x)) = -(Limsup net f)"
   4.472 -  using extreal_Limsup_uminus[of _ "(\<lambda>x. - (f x))"] by auto
   4.473 +  using ereal_Limsup_uminus[of _ "(\<lambda>x. - (f x))"] by auto
   4.474  
   4.475 -lemma extreal_Lim_uminus:
   4.476 -  fixes f :: "'a \<Rightarrow> extreal" shows "(f ---> f0) net \<longleftrightarrow> ((\<lambda>x. - f x) ---> - f0) net"
   4.477 +lemma ereal_Lim_uminus:
   4.478 +  fixes f :: "'a \<Rightarrow> ereal" shows "(f ---> f0) net \<longleftrightarrow> ((\<lambda>x. - f x) ---> - f0) net"
   4.479    using
   4.480 -    extreal_lim_mult[of f f0 net "- 1"]
   4.481 -    extreal_lim_mult[of "\<lambda>x. - (f x)" "-f0" net "- 1"]
   4.482 -  by (auto simp: extreal_uminus_reorder)
   4.483 +    ereal_lim_mult[of f f0 net "- 1"]
   4.484 +    ereal_lim_mult[of "\<lambda>x. - (f x)" "-f0" net "- 1"]
   4.485 +  by (auto simp: ereal_uminus_reorder)
   4.486  
   4.487  lemma lim_imp_Limsup:
   4.488 -  fixes f :: "'a => extreal"
   4.489 +  fixes f :: "'a => ereal"
   4.490    assumes "\<not> trivial_limit net"
   4.491    assumes lim: "(f ---> f0) net"
   4.492    shows "Limsup net f = f0"
   4.493 -  using extreal_Lim_uminus[of f f0] lim_imp_Liminf[of net "(%x. -(f x))" "-f0"]
   4.494 -     extreal_Liminf_uminus[of net f] assms by simp
   4.495 +  using ereal_Lim_uminus[of f f0] lim_imp_Liminf[of net "(%x. -(f x))" "-f0"]
   4.496 +     ereal_Liminf_uminus[of net f] assms by simp
   4.497  
   4.498  lemma Liminf_PInfty:
   4.499 -  fixes f :: "'a \<Rightarrow> extreal"
   4.500 +  fixes f :: "'a \<Rightarrow> ereal"
   4.501    assumes "\<not> trivial_limit net"
   4.502    shows "(f ---> \<infinity>) net \<longleftrightarrow> Liminf net f = \<infinity>"
   4.503  proof (intro lim_imp_Liminf iffI assms)
   4.504    assume rhs: "Liminf net f = \<infinity>"
   4.505    { fix S assume "open S & \<infinity> : S"
   4.506 -    then obtain m where "{extreal m<..} <= S" using open_PInfty2 by auto
   4.507 -    moreover have "eventually (\<lambda>x. f x \<in> {extreal m<..}) net"
   4.508 -      using rhs unfolding Liminf_Sup top_extreal_def[symmetric] Sup_eq_top_iff
   4.509 -      by (auto elim!: allE[where x="extreal m"] simp: top_extreal_def)
   4.510 +    then obtain m where "{ereal m<..} <= S" using open_PInfty2 by auto
   4.511 +    moreover have "eventually (\<lambda>x. f x \<in> {ereal m<..}) net"
   4.512 +      using rhs unfolding Liminf_Sup top_ereal_def[symmetric] Sup_eq_top_iff
   4.513 +      by (auto elim!: allE[where x="ereal m"] simp: top_ereal_def)
   4.514      ultimately have "eventually (%x. f x : S) net" apply (subst eventually_mono) by auto
   4.515    } then show "(f ---> \<infinity>) net" unfolding tendsto_def by auto
   4.516  qed
   4.517  
   4.518  lemma Limsup_MInfty:
   4.519 -  fixes f :: "'a \<Rightarrow> extreal"
   4.520 +  fixes f :: "'a \<Rightarrow> ereal"
   4.521    assumes "\<not> trivial_limit net"
   4.522    shows "(f ---> -\<infinity>) net \<longleftrightarrow> Limsup net f = -\<infinity>"
   4.523 -  using assms extreal_Lim_uminus[of f "-\<infinity>"] Liminf_PInfty[of _ "\<lambda>x. - (f x)"]
   4.524 -        extreal_Liminf_uminus[of _ f] by (auto simp: extreal_uminus_eq_reorder)
   4.525 +  using assms ereal_Lim_uminus[of f "-\<infinity>"] Liminf_PInfty[of _ "\<lambda>x. - (f x)"]
   4.526 +        ereal_Liminf_uminus[of _ f] by (auto simp: ereal_uminus_eq_reorder)
   4.527  
   4.528 -lemma extreal_Liminf_eq_Limsup:
   4.529 -  fixes f :: "'a \<Rightarrow> extreal"
   4.530 +lemma ereal_Liminf_eq_Limsup:
   4.531 +  fixes f :: "'a \<Rightarrow> ereal"
   4.532    assumes ntriv: "\<not> trivial_limit net"
   4.533    assumes lim: "Liminf net f = f0" "Limsup net f = f0"
   4.534    shows "(f ---> f0) net"
   4.535 @@ -415,7 +415,7 @@
   4.536    proof (rule topological_tendstoI)
   4.537      fix S assume "open S""f0 \<in> S"
   4.538      then obtain a b where "a < Liminf net f" "Limsup net f < b" "{a<..<b} \<subseteq> S"
   4.539 -      using extreal_open_cont_interval2[of S f0] real lim by auto
   4.540 +      using ereal_open_cont_interval2[of S f0] real lim by auto
   4.541      then have "eventually (\<lambda>x. f x \<in> {a<..<b}) net"
   4.542        unfolding Liminf_Sup Limsup_Inf less_Sup_iff Inf_less_iff
   4.543        by (auto intro!: eventually_conj simp add: greaterThanLessThan_iff)
   4.544 @@ -424,62 +424,62 @@
   4.545    qed
   4.546  qed
   4.547  
   4.548 -lemma extreal_Liminf_eq_Limsup_iff:
   4.549 -  fixes f :: "'a \<Rightarrow> extreal"
   4.550 +lemma ereal_Liminf_eq_Limsup_iff:
   4.551 +  fixes f :: "'a \<Rightarrow> ereal"
   4.552    assumes "\<not> trivial_limit net"
   4.553    shows "(f ---> f0) net \<longleftrightarrow> Liminf net f = f0 \<and> Limsup net f = f0"
   4.554 -  by (metis assms extreal_Liminf_eq_Limsup lim_imp_Liminf lim_imp_Limsup)
   4.555 +  by (metis assms ereal_Liminf_eq_Limsup lim_imp_Liminf lim_imp_Limsup)
   4.556  
   4.557  lemma limsup_INFI_SUPR:
   4.558 -  fixes f :: "nat \<Rightarrow> extreal"
   4.559 +  fixes f :: "nat \<Rightarrow> ereal"
   4.560    shows "limsup f = (INF n. SUP m:{n..}. f m)"
   4.561 -  using extreal_Limsup_uminus[of sequentially "\<lambda>x. - f x"]
   4.562 -  by (simp add: liminf_SUPR_INFI extreal_INFI_uminus extreal_SUPR_uminus)
   4.563 +  using ereal_Limsup_uminus[of sequentially "\<lambda>x. - f x"]
   4.564 +  by (simp add: liminf_SUPR_INFI ereal_INFI_uminus ereal_SUPR_uminus)
   4.565  
   4.566  lemma liminf_PInfty:
   4.567 -  fixes X :: "nat => extreal"
   4.568 +  fixes X :: "nat => ereal"
   4.569    shows "X ----> \<infinity> <-> liminf X = \<infinity>"
   4.570  by (metis Liminf_PInfty trivial_limit_sequentially)
   4.571  
   4.572  lemma limsup_MInfty:
   4.573 -  fixes X :: "nat => extreal"
   4.574 +  fixes X :: "nat => ereal"
   4.575    shows "X ----> (-\<infinity>) <-> limsup X = (-\<infinity>)"
   4.576  by (metis Limsup_MInfty trivial_limit_sequentially)
   4.577  
   4.578 -lemma extreal_lim_mono:
   4.579 -  fixes X Y :: "nat => extreal"
   4.580 +lemma ereal_lim_mono:
   4.581 +  fixes X Y :: "nat => ereal"
   4.582    assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= Y n"
   4.583    assumes "X ----> x" "Y ----> y"
   4.584    shows "x <= y"
   4.585 -  by (metis extreal_Liminf_eq_Limsup_iff[OF trivial_limit_sequentially] assms liminf_mono)
   4.586 +  by (metis ereal_Liminf_eq_Limsup_iff[OF trivial_limit_sequentially] assms liminf_mono)
   4.587  
   4.588 -lemma incseq_le_extreal:
   4.589 -  fixes X :: "nat \<Rightarrow> extreal"
   4.590 +lemma incseq_le_ereal:
   4.591 +  fixes X :: "nat \<Rightarrow> ereal"
   4.592    assumes inc: "incseq X" and lim: "X ----> L"
   4.593    shows "X N \<le> L"
   4.594    using inc
   4.595 -  by (intro extreal_lim_mono[of N, OF _ Lim_const lim]) (simp add: incseq_def)
   4.596 +  by (intro ereal_lim_mono[of N, OF _ Lim_const lim]) (simp add: incseq_def)
   4.597  
   4.598 -lemma decseq_ge_extreal: assumes dec: "decseq X"
   4.599 -  and lim: "X ----> (L::extreal)" shows "X N >= L"
   4.600 +lemma decseq_ge_ereal: assumes dec: "decseq X"
   4.601 +  and lim: "X ----> (L::ereal)" shows "X N >= L"
   4.602    using dec
   4.603 -  by (intro extreal_lim_mono[of N, OF _ lim Lim_const]) (simp add: decseq_def)
   4.604 +  by (intro ereal_lim_mono[of N, OF _ lim Lim_const]) (simp add: decseq_def)
   4.605  
   4.606  lemma liminf_bounded_open:
   4.607 -  fixes x :: "nat \<Rightarrow> extreal"
   4.608 +  fixes x :: "nat \<Rightarrow> ereal"
   4.609    shows "x0 \<le> liminf x \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> mono S \<longrightarrow> x0 \<in> S \<longrightarrow> (\<exists>N. \<forall>n\<ge>N. x n \<in> S))" 
   4.610    (is "_ \<longleftrightarrow> ?P x0")
   4.611  proof
   4.612    assume "?P x0" then show "x0 \<le> liminf x"
   4.613 -    unfolding extreal_Liminf_Sup_monoset eventually_sequentially
   4.614 +    unfolding ereal_Liminf_Sup_monoset eventually_sequentially
   4.615      by (intro complete_lattice_class.Sup_upper) auto
   4.616  next
   4.617    assume "x0 \<le> liminf x"
   4.618 -  { fix S :: "extreal set" assume om: "open S & mono S & x0:S"
   4.619 +  { fix S :: "ereal set" assume om: "open S & mono S & x0:S"
   4.620      { assume "S = UNIV" hence "EX N. (ALL n>=N. x n : S)" by auto }
   4.621      moreover
   4.622      { assume "~(S=UNIV)"
   4.623 -      then obtain B where B_def: "S = {B<..}" using om extreal_open_mono_set by auto
   4.624 +      then obtain B where B_def: "S = {B<..}" using om ereal_open_mono_set by auto
   4.625        hence "B<x0" using om by auto
   4.626        hence "EX N. ALL n>=N. x n : S" unfolding B_def using `x0 \<le> liminf x` liminf_bounded_iff by auto
   4.627      } ultimately have "EX N. (ALL n>=N. x n : S)" by auto
   4.628 @@ -487,15 +487,15 @@
   4.629  qed
   4.630  
   4.631  lemma limsup_subseq_mono:
   4.632 -  fixes X :: "nat \<Rightarrow> extreal"
   4.633 +  fixes X :: "nat \<Rightarrow> ereal"
   4.634    assumes "subseq r"
   4.635    shows "limsup (X \<circ> r) \<le> limsup X"
   4.636  proof-
   4.637    have "(\<lambda>n. - X n) \<circ> r = (\<lambda>n. - (X \<circ> r) n)" by (simp add: fun_eq_iff)
   4.638    then have "- limsup X \<le> - limsup (X \<circ> r)"
   4.639       using liminf_subseq_mono[of r "(%n. - X n)"]
   4.640 -       extreal_Liminf_uminus[of sequentially X]
   4.641 -       extreal_Liminf_uminus[of sequentially "X o r"] assms by auto
   4.642 +       ereal_Liminf_uminus[of sequentially X]
   4.643 +       ereal_Liminf_uminus[of sequentially "X o r"] assms by auto
   4.644    then show ?thesis by auto
   4.645  qed
   4.646  
   4.647 @@ -514,8 +514,8 @@
   4.648  from this show ?thesis apply(rule Topology_Euclidean_Space.bounded_increasing_convergent)
   4.649     using assms by auto
   4.650  qed
   4.651 -lemma lim_extreal_increasing: assumes "\<And>n m. n >= m \<Longrightarrow> f n >= f m"
   4.652 -  obtains l where "f ----> (l::extreal)"
   4.653 +lemma lim_ereal_increasing: assumes "\<And>n m. n >= m \<Longrightarrow> f n >= f m"
   4.654 +  obtains l where "f ----> (l::ereal)"
   4.655  proof(cases "f = (\<lambda>x. - \<infinity>)")
   4.656    case True then show thesis using Lim_const[of "- \<infinity>" sequentially] by (intro that[of "-\<infinity>"]) auto
   4.657  next
   4.658 @@ -527,18 +527,18 @@
   4.659    hence incy: "!!n m. n>=m ==> Y n >= Y m" using assms by auto
   4.660    from minf have minfy: "ALL n. Y n ~= (-\<infinity>)" using Y_def by auto
   4.661    show thesis
   4.662 -  proof(cases "EX B. ALL n. f n < extreal B")
   4.663 +  proof(cases "EX B. ALL n. f n < ereal B")
   4.664      case False thus thesis apply- apply(rule that[of \<infinity>]) unfolding Lim_PInfty not_ex not_all
   4.665      apply safe apply(erule_tac x=B in allE,safe) apply(rule_tac x=x in exI,safe)
   4.666      apply(rule order_trans[OF _ assms[rule_format]]) by auto
   4.667    next case True then guess B ..
   4.668 -    hence "ALL n. Y n < extreal B" using Y_def by auto note B = this[rule_format]
   4.669 +    hence "ALL n. Y n < ereal B" using Y_def by auto note B = this[rule_format]
   4.670      { fix n have "Y n < \<infinity>" using B[of n] apply (subst less_le_trans) by auto
   4.671        hence "Y n ~= \<infinity> & Y n ~= (-\<infinity>)" using minfy by auto
   4.672      } hence *: "ALL n. \<bar>Y n\<bar> \<noteq> \<infinity>" by auto
   4.673      { fix n have "real (Y n) < B" proof- case goal1 thus ?case
   4.674 -        using B[of n] apply-apply(subst(asm) extreal_real'[THEN sym]) defer defer
   4.675 -        unfolding extreal_less using * by auto
   4.676 +        using B[of n] apply-apply(subst(asm) ereal_real'[THEN sym]) defer defer
   4.677 +        unfolding ereal_less using * by auto
   4.678        qed
   4.679      }
   4.680      hence B': "ALL n. (real (Y n) <= B)" using less_imp_le by auto
   4.681 @@ -546,29 +546,29 @@
   4.682        apply(rule bounded_increasing_convergent2)
   4.683      proof safe show "!!n. real (Y n) <= B" using B' by auto
   4.684        fix n m::nat assume "n<=m"
   4.685 -      hence "extreal (real (Y n)) <= extreal (real (Y m))"
   4.686 -        using incy[rule_format,of n m] apply(subst extreal_real)+
   4.687 +      hence "ereal (real (Y n)) <= ereal (real (Y m))"
   4.688 +        using incy[rule_format,of n m] apply(subst ereal_real)+
   4.689          using *[rule_format, of n] *[rule_format, of m] by auto
   4.690        thus "real (Y n) <= real (Y m)" by auto
   4.691      qed then guess l .. note l=this
   4.692 -    have "Y ----> extreal l" using l apply-apply(subst(asm) lim_extreal[THEN sym])
   4.693 -    unfolding extreal_real using * by auto
   4.694 -    thus thesis apply-apply(rule that[of "extreal l"])
   4.695 +    have "Y ----> ereal l" using l apply-apply(subst(asm) lim_ereal[THEN sym])
   4.696 +    unfolding ereal_real using * by auto
   4.697 +    thus thesis apply-apply(rule that[of "ereal l"])
   4.698         apply (subst tail_same_limit[of Y _ N]) using Y_def by auto
   4.699    qed
   4.700  qed
   4.701  
   4.702 -lemma lim_extreal_decreasing: assumes "\<And>n m. n >= m \<Longrightarrow> f n <= f m"
   4.703 -  obtains l where "f ----> (l::extreal)"
   4.704 +lemma lim_ereal_decreasing: assumes "\<And>n m. n >= m \<Longrightarrow> f n <= f m"
   4.705 +  obtains l where "f ----> (l::ereal)"
   4.706  proof -
   4.707 -  from lim_extreal_increasing[of "\<lambda>x. - f x"] assms
   4.708 +  from lim_ereal_increasing[of "\<lambda>x. - f x"] assms
   4.709    obtain l where "(\<lambda>x. - f x) ----> l" by auto
   4.710 -  from extreal_lim_mult[OF this, of "- 1"] show thesis
   4.711 -    by (intro that[of "-l"]) (simp add: extreal_uminus_eq_reorder)
   4.712 +  from ereal_lim_mult[OF this, of "- 1"] show thesis
   4.713 +    by (intro that[of "-l"]) (simp add: ereal_uminus_eq_reorder)
   4.714  qed
   4.715  
   4.716 -lemma compact_extreal:
   4.717 -  fixes X :: "nat \<Rightarrow> extreal"
   4.718 +lemma compact_ereal:
   4.719 +  fixes X :: "nat \<Rightarrow> ereal"
   4.720    shows "\<exists>l r. subseq r \<and> (X \<circ> r) ----> l"
   4.721  proof -
   4.722    obtain r where "subseq r" and mono: "monoseq (X \<circ> r)"
   4.723 @@ -576,66 +576,66 @@
   4.724    then have "(\<forall>n m. m \<le> n \<longrightarrow> (X \<circ> r) m \<le> (X \<circ> r) n) \<or> (\<forall>n m. m \<le> n \<longrightarrow> (X \<circ> r) n \<le> (X \<circ> r) m)"
   4.725      by (auto simp add: monoseq_def)
   4.726    then obtain l where "(X\<circ>r) ----> l"
   4.727 -     using lim_extreal_increasing[of "X \<circ> r"] lim_extreal_decreasing[of "X \<circ> r"] by auto
   4.728 +     using lim_ereal_increasing[of "X \<circ> r"] lim_ereal_decreasing[of "X \<circ> r"] by auto
   4.729    then show ?thesis using `subseq r` by auto
   4.730  qed
   4.731  
   4.732 -lemma extreal_Sup_lim:
   4.733 -  assumes "\<And>n. b n \<in> s" "b ----> (a::extreal)"
   4.734 +lemma ereal_Sup_lim:
   4.735 +  assumes "\<And>n. b n \<in> s" "b ----> (a::ereal)"
   4.736    shows "a \<le> Sup s"
   4.737 -by (metis Lim_bounded_extreal assms complete_lattice_class.Sup_upper)
   4.738 +by (metis Lim_bounded_ereal assms complete_lattice_class.Sup_upper)
   4.739  
   4.740 -lemma extreal_Inf_lim:
   4.741 -  assumes "\<And>n. b n \<in> s" "b ----> (a::extreal)"
   4.742 +lemma ereal_Inf_lim:
   4.743 +  assumes "\<And>n. b n \<in> s" "b ----> (a::ereal)"
   4.744    shows "Inf s \<le> a"
   4.745 -by (metis Lim_bounded2_extreal assms complete_lattice_class.Inf_lower)
   4.746 +by (metis Lim_bounded2_ereal assms complete_lattice_class.Inf_lower)
   4.747  
   4.748 -lemma SUP_Lim_extreal:
   4.749 -  fixes X :: "nat \<Rightarrow> extreal" assumes "incseq X" "X ----> l" shows "(SUP n. X n) = l"
   4.750 -proof (rule extreal_SUPI)
   4.751 +lemma SUP_Lim_ereal:
   4.752 +  fixes X :: "nat \<Rightarrow> ereal" assumes "incseq X" "X ----> l" shows "(SUP n. X n) = l"
   4.753 +proof (rule ereal_SUPI)
   4.754    fix n from assms show "X n \<le> l"
   4.755 -    by (intro incseq_le_extreal) (simp add: incseq_def)
   4.756 +    by (intro incseq_le_ereal) (simp add: incseq_def)
   4.757  next
   4.758    fix y assume "\<And>n. n \<in> UNIV \<Longrightarrow> X n \<le> y"
   4.759 -  with extreal_Sup_lim[OF _ `X ----> l`, of "{..y}"]
   4.760 +  with ereal_Sup_lim[OF _ `X ----> l`, of "{..y}"]
   4.761    show "l \<le> y" by auto
   4.762  qed
   4.763  
   4.764 -lemma LIMSEQ_extreal_SUPR:
   4.765 -  fixes X :: "nat \<Rightarrow> extreal" assumes "incseq X" shows "X ----> (SUP n. X n)"
   4.766 -proof (rule lim_extreal_increasing)
   4.767 +lemma LIMSEQ_ereal_SUPR:
   4.768 +  fixes X :: "nat \<Rightarrow> ereal" assumes "incseq X" shows "X ----> (SUP n. X n)"
   4.769 +proof (rule lim_ereal_increasing)
   4.770    fix n m :: nat assume "m \<le> n" then show "X m \<le> X n"
   4.771      using `incseq X` by (simp add: incseq_def)
   4.772  next
   4.773    fix l assume "X ----> l"
   4.774 -  with SUP_Lim_extreal[of X, OF assms this] show ?thesis by simp
   4.775 +  with SUP_Lim_ereal[of X, OF assms this] show ?thesis by simp
   4.776  qed
   4.777  
   4.778 -lemma INF_Lim_extreal: "decseq X \<Longrightarrow> X ----> l \<Longrightarrow> (INF n. X n) = (l::extreal)"
   4.779 -  using SUP_Lim_extreal[of "\<lambda>i. - X i" "- l"]
   4.780 -  by (simp add: extreal_SUPR_uminus extreal_lim_uminus)
   4.781 +lemma INF_Lim_ereal: "decseq X \<Longrightarrow> X ----> l \<Longrightarrow> (INF n. X n) = (l::ereal)"
   4.782 +  using SUP_Lim_ereal[of "\<lambda>i. - X i" "- l"]
   4.783 +  by (simp add: ereal_SUPR_uminus ereal_lim_uminus)
   4.784  
   4.785 -lemma LIMSEQ_extreal_INFI: "decseq X \<Longrightarrow> X ----> (INF n. X n :: extreal)"
   4.786 -  using LIMSEQ_extreal_SUPR[of "\<lambda>i. - X i"]
   4.787 -  by (simp add: extreal_SUPR_uminus extreal_lim_uminus)
   4.788 +lemma LIMSEQ_ereal_INFI: "decseq X \<Longrightarrow> X ----> (INF n. X n :: ereal)"
   4.789 +  using LIMSEQ_ereal_SUPR[of "\<lambda>i. - X i"]
   4.790 +  by (simp add: ereal_SUPR_uminus ereal_lim_uminus)
   4.791  
   4.792  lemma SUP_eq_LIMSEQ:
   4.793    assumes "mono f"
   4.794 -  shows "(SUP n. extreal (f n)) = extreal x \<longleftrightarrow> f ----> x"
   4.795 +  shows "(SUP n. ereal (f n)) = ereal x \<longleftrightarrow> f ----> x"
   4.796  proof
   4.797 -  have inc: "incseq (\<lambda>i. extreal (f i))"
   4.798 +  have inc: "incseq (\<lambda>i. ereal (f i))"
   4.799      using `mono f` unfolding mono_def incseq_def by auto
   4.800    { assume "f ----> x"
   4.801 -   then have "(\<lambda>i. extreal (f i)) ----> extreal x" by auto
   4.802 -   from SUP_Lim_extreal[OF inc this]
   4.803 -   show "(SUP n. extreal (f n)) = extreal x" . }
   4.804 -  { assume "(SUP n. extreal (f n)) = extreal x"
   4.805 -    with LIMSEQ_extreal_SUPR[OF inc]
   4.806 +   then have "(\<lambda>i. ereal (f i)) ----> ereal x" by auto
   4.807 +   from SUP_Lim_ereal[OF inc this]
   4.808 +   show "(SUP n. ereal (f n)) = ereal x" . }
   4.809 +  { assume "(SUP n. ereal (f n)) = ereal x"
   4.810 +    with LIMSEQ_ereal_SUPR[OF inc]
   4.811      show "f ----> x" by auto }
   4.812  qed
   4.813  
   4.814  lemma Liminf_within:
   4.815 -  fixes f :: "'a::metric_space => extreal"
   4.816 +  fixes f :: "'a::metric_space => ereal"
   4.817    shows "Liminf (at x within S) f = (SUP e:{0<..}. INF y:(S Int ball x e - {x}). f y)"
   4.818  proof-
   4.819  let ?l="(SUP e:{0<..}. INF y:(S Int ball x e - {x}). f y)"
   4.820 @@ -645,7 +645,7 @@
   4.821    { assume "T=UNIV" hence ?thesis by (simp add: gt_ex) }
   4.822    moreover
   4.823    { assume "~(T=UNIV)"
   4.824 -    then obtain B where "T={B<..}" using T_def extreal_open_mono_set[of T] by auto
   4.825 +    then obtain B where "T={B<..}" using T_def ereal_open_mono_set[of T] by auto
   4.826      hence "B<?l" using T_def by auto
   4.827      then obtain d where d_def: "0<d & B<(INF y:(S Int ball x d - {x}). f y)"
   4.828        unfolding less_SUP_iff by auto
   4.829 @@ -670,14 +670,14 @@
   4.830      } hence "B <= INFI (S Int ball x d - {x}) f" apply (subst le_INFI) by auto
   4.831      also have "...<=?l" apply (subst le_SUPI) using d_def by auto
   4.832      finally have "B<=?l" by auto
   4.833 -  } hence "z <= ?l" using extreal_le_extreal[of z "?l"] by auto
   4.834 +  } hence "z <= ?l" using ereal_le_ereal[of z "?l"] by auto
   4.835  }
   4.836 -ultimately show ?thesis unfolding extreal_Liminf_Sup_monoset eventually_within
   4.837 -   apply (subst extreal_SupI[of _ "(SUP e:{0<..}. INFI (S Int ball x e - {x}) f)"]) by auto
   4.838 +ultimately show ?thesis unfolding ereal_Liminf_Sup_monoset eventually_within
   4.839 +   apply (subst ereal_SupI[of _ "(SUP e:{0<..}. INFI (S Int ball x e - {x}) f)"]) by auto
   4.840  qed
   4.841  
   4.842  lemma Limsup_within:
   4.843 -  fixes f :: "'a::metric_space => extreal"
   4.844 +  fixes f :: "'a::metric_space => ereal"
   4.845    shows "Limsup (at x within S) f = (INF e:{0<..}. SUP y:(S Int ball x e - {x}). f y)"
   4.846  proof-
   4.847  let ?l="(INF e:{0<..}. SUP y:(S Int ball x e - {x}). f y)"
   4.848 @@ -687,12 +687,12 @@
   4.849    { assume "T=UNIV" hence ?thesis by (simp add: gt_ex) }
   4.850    moreover
   4.851    { assume "~(T=UNIV)" hence "~(uminus ` T = UNIV)"
   4.852 -       by (metis Int_UNIV_right Int_absorb1 image_mono extreal_minus_minus_image subset_UNIV)
   4.853 -    hence "uminus ` T = {Inf (uminus ` T)<..}" using T_def extreal_open_mono_set[of "uminus ` T"]
   4.854 -       extreal_open_uminus[of T] by auto
   4.855 +       by (metis Int_UNIV_right Int_absorb1 image_mono ereal_minus_minus_image subset_UNIV)
   4.856 +    hence "uminus ` T = {Inf (uminus ` T)<..}" using T_def ereal_open_mono_set[of "uminus ` T"]
   4.857 +       ereal_open_uminus[of T] by auto
   4.858      then obtain B where "T={..<B}"
   4.859 -      unfolding extreal_Inf_uminus_image_eq extreal_uminus_lessThan[symmetric]
   4.860 -      unfolding inj_image_eq_iff[OF extreal_inj_on_uminus] by simp
   4.861 +      unfolding ereal_Inf_uminus_image_eq ereal_uminus_lessThan[symmetric]
   4.862 +      unfolding inj_image_eq_iff[OF ereal_inj_on_uminus] by simp
   4.863      hence "?l<B" using T_def by auto
   4.864      then obtain d where d_def: "0<d & (SUP y:(S Int ball x d - {x}). f y)<B"
   4.865        unfolding INF_less_iff by auto
   4.866 @@ -717,33 +717,33 @@
   4.867      } hence "SUPR (S Int ball x d - {x}) f <= B" apply (subst SUP_leI) by auto
   4.868      moreover have "?l<=SUPR (S Int ball x d - {x}) f" apply (subst INF_leI) using d_def by auto
   4.869      ultimately have "?l<=B" by auto
   4.870 -  } hence "?l <= z" using extreal_ge_extreal[of z "?l"] by auto
   4.871 +  } hence "?l <= z" using ereal_ge_ereal[of z "?l"] by auto
   4.872  }
   4.873 -ultimately show ?thesis unfolding extreal_Limsup_Inf_monoset eventually_within
   4.874 -   apply (subst extreal_InfI) by auto
   4.875 +ultimately show ?thesis unfolding ereal_Limsup_Inf_monoset eventually_within
   4.876 +   apply (subst ereal_InfI) by auto
   4.877  qed
   4.878  
   4.879  
   4.880  lemma Liminf_within_UNIV:
   4.881 -  fixes f :: "'a::metric_space => extreal"
   4.882 +  fixes f :: "'a::metric_space => ereal"
   4.883    shows "Liminf (at x) f = Liminf (at x within UNIV) f"
   4.884  by (metis within_UNIV)
   4.885  
   4.886  
   4.887  lemma Liminf_at:
   4.888 -  fixes f :: "'a::metric_space => extreal"
   4.889 +  fixes f :: "'a::metric_space => ereal"
   4.890    shows "Liminf (at x) f = (SUP e:{0<..}. INF y:(ball x e - {x}). f y)"
   4.891  using Liminf_within[of x UNIV f] Liminf_within_UNIV[of x f] by auto
   4.892  
   4.893  
   4.894  lemma Limsup_within_UNIV:
   4.895 -  fixes f :: "'a::metric_space => extreal"
   4.896 +  fixes f :: "'a::metric_space => ereal"
   4.897    shows "Limsup (at x) f = Limsup (at x within UNIV) f"
   4.898  by (metis within_UNIV)
   4.899  
   4.900  
   4.901  lemma Limsup_at:
   4.902 -  fixes f :: "'a::metric_space => extreal"
   4.903 +  fixes f :: "'a::metric_space => ereal"
   4.904    shows "Limsup (at x) f = (INF e:{0<..}. SUP y:(ball x e - {x}). f y)"
   4.905  using Limsup_within[of x UNIV f] Limsup_within_UNIV[of x f] by auto
   4.906  
   4.907 @@ -755,14 +755,14 @@
   4.908  by (metis assms(1) linorder_le_less_linear n_not_Suc_n real_of_nat_le_zero_cancel_iff)
   4.909  
   4.910  lemma Liminf_within_constant:
   4.911 -  fixes f :: "'a::metric_space => extreal"
   4.912 +  fixes f :: "'a::metric_space => ereal"
   4.913    assumes "ALL y:S. f y = C"
   4.914    assumes "~trivial_limit (at x within S)"
   4.915    shows "Liminf (at x within S) f = C"
   4.916  by (metis Lim_within_constant assms lim_imp_Liminf)
   4.917  
   4.918  lemma Limsup_within_constant:
   4.919 -  fixes f :: "'a::metric_space => extreal"
   4.920 +  fixes f :: "'a::metric_space => ereal"
   4.921    assumes "ALL y:S. f y = C"
   4.922    assumes "~trivial_limit (at x within S)"
   4.923    shows "Limsup (at x within S) f = C"
   4.924 @@ -805,17 +805,17 @@
   4.925  } ultimately show ?thesis by auto
   4.926  qed
   4.927  
   4.928 -lemma liminf_extreal_cminus:
   4.929 -  fixes f :: "nat \<Rightarrow> extreal" assumes "c \<noteq> -\<infinity>"
   4.930 +lemma liminf_ereal_cminus:
   4.931 +  fixes f :: "nat \<Rightarrow> ereal" assumes "c \<noteq> -\<infinity>"
   4.932    shows "liminf (\<lambda>x. c - f x) = c - limsup f"
   4.933  proof (cases c)
   4.934    case PInf then show ?thesis by (simp add: Liminf_const)
   4.935  next
   4.936    case (real r) then show ?thesis
   4.937      unfolding liminf_SUPR_INFI limsup_INFI_SUPR
   4.938 -    apply (subst INFI_extreal_cminus)
   4.939 +    apply (subst INFI_ereal_cminus)
   4.940      apply auto
   4.941 -    apply (subst SUPR_extreal_cminus)
   4.942 +    apply (subst SUPR_ereal_cminus)
   4.943      apply auto
   4.944      done
   4.945  qed (insert `c \<noteq> -\<infinity>`, simp)
   4.946 @@ -853,77 +853,77 @@
   4.947  from this show ?thesis using continuous_imp_tendsto by auto
   4.948  qed
   4.949  
   4.950 -lemma continuous_at_of_extreal:
   4.951 -  fixes x0 :: extreal
   4.952 +lemma continuous_at_of_ereal:
   4.953 +  fixes x0 :: ereal
   4.954    assumes "\<bar>x0\<bar> \<noteq> \<infinity>"
   4.955    shows "continuous (at x0) real"
   4.956  proof-
   4.957  { fix T assume T_def: "open T & real x0 : T"
   4.958 -  def S == "extreal ` T"
   4.959 -  hence "extreal (real x0) : S" using T_def by auto
   4.960 -  hence "x0 : S" using assms extreal_real by auto
   4.961 -  moreover have "open S" using open_extreal S_def T_def by auto
   4.962 +  def S == "ereal ` T"
   4.963 +  hence "ereal (real x0) : S" using T_def by auto
   4.964 +  hence "x0 : S" using assms ereal_real by auto
   4.965 +  moreover have "open S" using open_ereal S_def T_def by auto
   4.966    moreover have "ALL y:S. real y : T" using S_def T_def by auto
   4.967    ultimately have "EX S. x0 : S & open S & (ALL y:S. real y : T)" by auto
   4.968  } from this show ?thesis unfolding continuous_at_open by blast
   4.969  qed
   4.970  
   4.971  
   4.972 -lemma continuous_at_iff_extreal:
   4.973 +lemma continuous_at_iff_ereal:
   4.974  fixes f :: "'a::t2_space => real"
   4.975 -shows "continuous (at x0) f <-> continuous (at x0) (extreal o f)"
   4.976 +shows "continuous (at x0) f <-> continuous (at x0) (ereal o f)"
   4.977  proof-
   4.978 -{ assume "continuous (at x0) f" hence "continuous (at x0) (extreal o f)"
   4.979 -     using continuous_at_extreal continuous_at_compose[of x0 f extreal] by auto
   4.980 +{ assume "continuous (at x0) f" hence "continuous (at x0) (ereal o f)"
   4.981 +     using continuous_at_ereal continuous_at_compose[of x0 f ereal] by auto
   4.982  }
   4.983  moreover
   4.984 -{ assume "continuous (at x0) (extreal o f)"
   4.985 -  hence "continuous (at x0) (real o (extreal o f))"
   4.986 -     using continuous_at_of_extreal by (intro continuous_at_compose[of x0 "extreal o f"]) auto
   4.987 -  moreover have "real o (extreal o f) = f" using real_extreal_id by (simp add: o_assoc)
   4.988 +{ assume "continuous (at x0) (ereal o f)"
   4.989 +  hence "continuous (at x0) (real o (ereal o f))"
   4.990 +     using continuous_at_of_ereal by (intro continuous_at_compose[of x0 "ereal o f"]) auto
   4.991 +  moreover have "real o (ereal o f) = f" using real_ereal_id by (simp add: o_assoc)
   4.992    ultimately have "continuous (at x0) f" by auto
   4.993  } ultimately show ?thesis by auto
   4.994  qed
   4.995  
   4.996  
   4.997 -lemma continuous_on_iff_extreal:
   4.998 +lemma continuous_on_iff_ereal:
   4.999  fixes f :: "'a::t2_space => real"
  4.1000  fixes A assumes "open A"
  4.1001 -shows "continuous_on A f <-> continuous_on A (extreal o f)"
  4.1002 -   using continuous_at_iff_extreal assms by (auto simp add: continuous_on_eq_continuous_at)
  4.1003 +shows "continuous_on A f <-> continuous_on A (ereal o f)"
  4.1004 +   using continuous_at_iff_ereal assms by (auto simp add: continuous_on_eq_continuous_at)
  4.1005  
  4.1006  
  4.1007  lemma continuous_on_real: "continuous_on (UNIV-{\<infinity>,(-\<infinity>)}) real"
  4.1008 -   using continuous_at_of_extreal continuous_on_eq_continuous_at open_image_extreal by auto
  4.1009 +   using continuous_at_of_ereal continuous_on_eq_continuous_at open_image_ereal by auto
  4.1010  
  4.1011  
  4.1012  lemma continuous_on_iff_real:
  4.1013 -  fixes f :: "'a::t2_space => extreal"
  4.1014 +  fixes f :: "'a::t2_space => ereal"
  4.1015    assumes "\<And>x. x \<in> A \<Longrightarrow> \<bar>f x\<bar> \<noteq> \<infinity>"
  4.1016    shows "continuous_on A f \<longleftrightarrow> continuous_on A (real \<circ> f)"
  4.1017  proof-
  4.1018    have "f ` A <= UNIV-{\<infinity>,(-\<infinity>)}" using assms by force
  4.1019    hence *: "continuous_on (f ` A) real"
  4.1020       using continuous_on_real by (simp add: continuous_on_subset)
  4.1021 -have **: "continuous_on ((real o f) ` A) extreal"
  4.1022 -   using continuous_on_extreal continuous_on_subset[of "UNIV" "extreal" "(real o f) ` A"] by blast
  4.1023 +have **: "continuous_on ((real o f) ` A) ereal"
  4.1024 +   using continuous_on_ereal continuous_on_subset[of "UNIV" "ereal" "(real o f) ` A"] by blast
  4.1025  { assume "continuous_on A f" hence "continuous_on A (real o f)"
  4.1026    apply (subst continuous_on_compose) using * by auto
  4.1027  }
  4.1028  moreover
  4.1029  { assume "continuous_on A (real o f)"
  4.1030 -  hence "continuous_on A (extreal o (real o f))"
  4.1031 +  hence "continuous_on A (ereal o (real o f))"
  4.1032       apply (subst continuous_on_compose) using ** by auto
  4.1033    hence "continuous_on A f"
  4.1034 -     apply (subst continuous_on_eq[of A "extreal o (real o f)" f])
  4.1035 -     using assms extreal_real by auto
  4.1036 +     apply (subst continuous_on_eq[of A "ereal o (real o f)" f])
  4.1037 +     using assms ereal_real by auto
  4.1038  }
  4.1039  ultimately show ?thesis by auto
  4.1040  qed
  4.1041  
  4.1042  
  4.1043  lemma continuous_at_const:
  4.1044 -  fixes f :: "'a::t2_space => extreal"
  4.1045 +  fixes f :: "'a::t2_space => ereal"
  4.1046    assumes "ALL x. (f x = C)"
  4.1047    shows "ALL x. continuous (at x) f"
  4.1048  unfolding continuous_at_open using assms t1_space by auto
  4.1049 @@ -977,11 +977,11 @@
  4.1050  qed
  4.1051  
  4.1052  
  4.1053 -lemma mono_closed_extreal:
  4.1054 +lemma mono_closed_ereal:
  4.1055    fixes S :: "real set"
  4.1056    assumes mono: "ALL y z. y:S & y<=z --> z:S"
  4.1057    assumes "closed S"
  4.1058 -  shows "EX a. S = {x. a <= extreal x}"
  4.1059 +  shows "EX a. S = {x. a <= ereal x}"
  4.1060  proof-
  4.1061  { assume "S = {}" hence ?thesis apply(rule_tac x=PInfty in exI) by auto }
  4.1062  moreover
  4.1063 @@ -989,14 +989,14 @@
  4.1064  moreover
  4.1065  { assume "EX a. S = {a ..}"
  4.1066    from this obtain a where "S={a ..}" by auto
  4.1067 -  hence ?thesis apply(rule_tac x="extreal a" in exI) by auto
  4.1068 +  hence ?thesis apply(rule_tac x="ereal a" in exI) by auto
  4.1069  } ultimately show ?thesis using mono_closed_real[of S] assms by auto
  4.1070  qed
  4.1071  
  4.1072  subsection {* Sums *}
  4.1073  
  4.1074 -lemma setsum_extreal[simp]:
  4.1075 -  "(\<Sum>x\<in>A. extreal (f x)) = extreal (\<Sum>x\<in>A. f x)"
  4.1076 +lemma setsum_ereal[simp]:
  4.1077 +  "(\<Sum>x\<in>A. ereal (f x)) = ereal (\<Sum>x\<in>A. f x)"
  4.1078  proof cases
  4.1079    assume "finite A" then show ?thesis by induct auto
  4.1080  qed simp
  4.1081 @@ -1029,9 +1029,9 @@
  4.1082    have "finite A" by (rule ccontr) (insert *, auto)
  4.1083    moreover have "\<exists>i\<in>A. \<bar>f i\<bar> = \<infinity>"
  4.1084    proof (rule ccontr)
  4.1085 -    assume "\<not> ?thesis" then have "\<forall>i\<in>A. \<exists>r. f i = extreal r" by auto
  4.1086 +    assume "\<not> ?thesis" then have "\<forall>i\<in>A. \<exists>r. f i = ereal r" by auto
  4.1087      from bchoice[OF this] guess r ..
  4.1088 -    with * show False by (auto simp: setsum_extreal)
  4.1089 +    with * show False by (auto simp: setsum_ereal)
  4.1090    qed
  4.1091    ultimately show "finite A \<and> (\<exists>i\<in>A. \<bar>f i\<bar> = \<infinity>)" by auto
  4.1092  next
  4.1093 @@ -1040,72 +1040,72 @@
  4.1094    then show "\<bar>setsum f A\<bar> = \<infinity>"
  4.1095    proof induct
  4.1096      case (insert j A) then show ?case
  4.1097 -      by (cases rule: extreal3_cases[of "f i" "f j" "setsum f A"]) auto
  4.1098 +      by (cases rule: ereal3_cases[of "f i" "f j" "setsum f A"]) auto
  4.1099    qed simp
  4.1100  qed
  4.1101  
  4.1102 -lemma setsum_real_of_extreal:
  4.1103 +lemma setsum_real_of_ereal:
  4.1104    assumes "\<And>x. x \<in> S \<Longrightarrow> \<bar>f x\<bar> \<noteq> \<infinity>"
  4.1105    shows "(\<Sum>x\<in>S. real (f x)) = real (setsum f S)"
  4.1106  proof -
  4.1107 -  have "\<forall>x\<in>S. \<exists>r. f x = extreal r"
  4.1108 +  have "\<forall>x\<in>S. \<exists>r. f x = ereal r"
  4.1109    proof
  4.1110      fix x assume "x \<in> S"
  4.1111 -    from assms[OF this] show "\<exists>r. f x = extreal r" by (cases "f x") auto
  4.1112 +    from assms[OF this] show "\<exists>r. f x = ereal r" by (cases "f x") auto
  4.1113    qed
  4.1114    from bchoice[OF this] guess r ..
  4.1115    then show ?thesis by simp
  4.1116  qed
  4.1117  
  4.1118 -lemma setsum_extreal_0:
  4.1119 -  fixes f :: "'a \<Rightarrow> extreal" assumes "finite A" "\<And>i. i \<in> A \<Longrightarrow> 0 \<le> f i"
  4.1120 +lemma setsum_ereal_0:
  4.1121 +  fixes f :: "'a \<Rightarrow> ereal" assumes "finite A" "\<And>i. i \<in> A \<Longrightarrow> 0 \<le> f i"
  4.1122    shows "(\<Sum>x\<in>A. f x) = 0 \<longleftrightarrow> (\<forall>i\<in>A. f i = 0)"
  4.1123  proof
  4.1124    assume *: "(\<Sum>x\<in>A. f x) = 0"
  4.1125    then have "(\<Sum>x\<in>A. f x) \<noteq> \<infinity>" by auto
  4.1126    then have "\<forall>i\<in>A. \<bar>f i\<bar> \<noteq> \<infinity>" using assms by (force simp: setsum_Pinfty)
  4.1127 -  then have "\<forall>i\<in>A. \<exists>r. f i = extreal r" by auto
  4.1128 +  then have "\<forall>i\<in>A. \<exists>r. f i = ereal r" by auto
  4.1129    from bchoice[OF this] * assms show "\<forall>i\<in>A. f i = 0"
  4.1130      using setsum_nonneg_eq_0_iff[of A "\<lambda>i. real (f i)"] by auto
  4.1131  qed (rule setsum_0')
  4.1132  
  4.1133  
  4.1134 -lemma setsum_extreal_right_distrib:
  4.1135 -  fixes f :: "'a \<Rightarrow> extreal" assumes "\<And>i. i \<in> A \<Longrightarrow> 0 \<le> f i"
  4.1136 +lemma setsum_ereal_right_distrib:
  4.1137 +  fixes f :: "'a \<Rightarrow> ereal" assumes "\<And>i. i \<in> A \<Longrightarrow> 0 \<le> f i"
  4.1138    shows "r * setsum f A = (\<Sum>n\<in>A. r * f n)"
  4.1139  proof cases
  4.1140    assume "finite A" then show ?thesis using assms
  4.1141 -    by induct (auto simp: extreal_right_distrib setsum_nonneg)
  4.1142 +    by induct (auto simp: ereal_right_distrib setsum_nonneg)
  4.1143  qed simp
  4.1144  
  4.1145 -lemma sums_extreal_positive:
  4.1146 -  fixes f :: "nat \<Rightarrow> extreal" assumes "\<And>i. 0 \<le> f i" shows "f sums (SUP n. \<Sum>i<n. f i)"
  4.1147 +lemma sums_ereal_positive:
  4.1148 +  fixes f :: "nat \<Rightarrow> ereal" assumes "\<And>i. 0 \<le> f i" shows "f sums (SUP n. \<Sum>i<n. f i)"
  4.1149  proof -
  4.1150    have "incseq (\<lambda>i. \<Sum>j=0..<i. f j)"
  4.1151 -    using extreal_add_mono[OF _ assms] by (auto intro!: incseq_SucI)
  4.1152 -  from LIMSEQ_extreal_SUPR[OF this]
  4.1153 +    using ereal_add_mono[OF _ assms] by (auto intro!: incseq_SucI)
  4.1154 +  from LIMSEQ_ereal_SUPR[OF this]
  4.1155    show ?thesis unfolding sums_def by (simp add: atLeast0LessThan)
  4.1156  qed
  4.1157  
  4.1158 -lemma summable_extreal_pos:
  4.1159 -  fixes f :: "nat \<Rightarrow> extreal" assumes "\<And>i. 0 \<le> f i" shows "summable f"
  4.1160 -  using sums_extreal_positive[of f, OF assms] unfolding summable_def by auto
  4.1161 +lemma summable_ereal_pos:
  4.1162 +  fixes f :: "nat \<Rightarrow> ereal" assumes "\<And>i. 0 \<le> f i" shows "summable f"
  4.1163 +  using sums_ereal_positive[of f, OF assms] unfolding summable_def by auto
  4.1164  
  4.1165 -lemma suminf_extreal_eq_SUPR:
  4.1166 -  fixes f :: "nat \<Rightarrow> extreal" assumes "\<And>i. 0 \<le> f i"
  4.1167 +lemma suminf_ereal_eq_SUPR:
  4.1168 +  fixes f :: "nat \<Rightarrow> ereal" assumes "\<And>i. 0 \<le> f i"
  4.1169    shows "(\<Sum>x. f x) = (SUP n. \<Sum>i<n. f i)"
  4.1170 -  using sums_extreal_positive[of f, OF assms, THEN sums_unique] by simp
  4.1171 +  using sums_ereal_positive[of f, OF assms, THEN sums_unique] by simp
  4.1172  
  4.1173 -lemma sums_extreal:
  4.1174 -  "(\<lambda>x. extreal (f x)) sums extreal x \<longleftrightarrow> f sums x"
  4.1175 +lemma sums_ereal:
  4.1176 +  "(\<lambda>x. ereal (f x)) sums ereal x \<longleftrightarrow> f sums x"
  4.1177    unfolding sums_def by simp
  4.1178  
  4.1179  lemma suminf_bound:
  4.1180 -  fixes f :: "nat \<Rightarrow> extreal"
  4.1181 +  fixes f :: "nat \<Rightarrow> ereal"
  4.1182    assumes "\<forall>N. (\<Sum>n<N. f n) \<le> x" and pos: "\<And>n. 0 \<le> f n"
  4.1183    shows "suminf f \<le> x"
  4.1184 -proof (rule Lim_bounded_extreal)
  4.1185 -  have "summable f" using pos[THEN summable_extreal_pos] .
  4.1186 +proof (rule Lim_bounded_ereal)
  4.1187 +  have "summable f" using pos[THEN summable_ereal_pos] .
  4.1188    then show "(\<lambda>N. \<Sum>n<N. f n) ----> suminf f"
  4.1189      by (auto dest!: summable_sums simp: sums_def atLeast0LessThan)
  4.1190    show "\<forall>n\<ge>0. setsum f {..<n} \<le> x"
  4.1191 @@ -1113,15 +1113,15 @@
  4.1192  qed
  4.1193  
  4.1194  lemma suminf_bound_add:
  4.1195 -  fixes f :: "nat \<Rightarrow> extreal"
  4.1196 +  fixes f :: "nat \<Rightarrow> ereal"
  4.1197    assumes "\<forall>N. (\<Sum>n<N. f n) + y \<le> x" and pos: "\<And>n. 0 \<le> f n" and "y \<noteq> -\<infinity>"
  4.1198    shows "suminf f + y \<le> x"
  4.1199  proof (cases y)
  4.1200    case (real r) then have "\<forall>N. (\<Sum>n<N. f n) \<le> x - y"
  4.1201 -    using assms by (simp add: extreal_le_minus)
  4.1202 +    using assms by (simp add: ereal_le_minus)
  4.1203    then have "(\<Sum> n. f n) \<le> x - y" using pos by (rule suminf_bound)
  4.1204    then show "(\<Sum> n. f n) + y \<le> x"
  4.1205 -    using assms real by (simp add: extreal_le_minus)
  4.1206 +    using assms real by (simp add: ereal_le_minus)
  4.1207  qed (insert assms, auto)
  4.1208  
  4.1209  lemma sums_finite:
  4.1210 @@ -1140,22 +1140,22 @@
  4.1211    shows "suminf f = (\<Sum>N<n. f N)"
  4.1212    using sums_finite[OF assms, THEN sums_unique] by simp
  4.1213  
  4.1214 -lemma suminf_extreal_0[simp]: "(\<Sum>i. 0) = (0::'a::{comm_monoid_add,t2_space})"
  4.1215 +lemma suminf_ereal_0[simp]: "(\<Sum>i. 0) = (0::'a::{comm_monoid_add,t2_space})"
  4.1216    using suminf_finite[of 0 "\<lambda>x. 0"] by simp
  4.1217  
  4.1218  lemma suminf_upper:
  4.1219 -  fixes f :: "nat \<Rightarrow> extreal" assumes "\<And>n. 0 \<le> f n"
  4.1220 +  fixes f :: "nat \<Rightarrow> ereal" assumes "\<And>n. 0 \<le> f n"
  4.1221    shows "(\<Sum>n<N. f n) \<le> (\<Sum>n. f n)"
  4.1222 -  unfolding suminf_extreal_eq_SUPR[OF assms] SUPR_def
  4.1223 +  unfolding suminf_ereal_eq_SUPR[OF assms] SUPR_def
  4.1224    by (auto intro: complete_lattice_class.Sup_upper image_eqI)
  4.1225  
  4.1226  lemma suminf_0_le:
  4.1227 -  fixes f :: "nat \<Rightarrow> extreal" assumes "\<And>n. 0 \<le> f n"
  4.1228 +  fixes f :: "nat \<Rightarrow> ereal" assumes "\<And>n. 0 \<le> f n"
  4.1229    shows "0 \<le> (\<Sum>n. f n)"
  4.1230    using suminf_upper[of f 0, OF assms] by simp
  4.1231  
  4.1232  lemma suminf_le_pos:
  4.1233 -  fixes f g :: "nat \<Rightarrow> extreal"
  4.1234 +  fixes f g :: "nat \<Rightarrow> ereal"
  4.1235    assumes "\<And>N. f N \<le> g N" "\<And>N. 0 \<le> f N"
  4.1236    shows "suminf f \<le> suminf g"
  4.1237  proof (safe intro!: suminf_bound)
  4.1238 @@ -1165,25 +1165,25 @@
  4.1239    finally show "setsum f {..<n} \<le> suminf g" .
  4.1240  qed (rule assms(2))
  4.1241  
  4.1242 -lemma suminf_half_series_extreal: "(\<Sum>n. (1/2 :: extreal)^Suc n) = 1"
  4.1243 -  using sums_extreal[THEN iffD2, OF power_half_series, THEN sums_unique, symmetric]
  4.1244 -  by (simp add: one_extreal_def)
  4.1245 +lemma suminf_half_series_ereal: "(\<Sum>n. (1/2 :: ereal)^Suc n) = 1"
  4.1246 +  using sums_ereal[THEN iffD2, OF power_half_series, THEN sums_unique, symmetric]
  4.1247 +  by (simp add: one_ereal_def)
  4.1248  
  4.1249 -lemma suminf_add_extreal:
  4.1250 -  fixes f g :: "nat \<Rightarrow> extreal"
  4.1251 +lemma suminf_add_ereal:
  4.1252 +  fixes f g :: "nat \<Rightarrow> ereal"
  4.1253    assumes "\<And>i. 0 \<le> f i" "\<And>i. 0 \<le> g i"
  4.1254    shows "(\<Sum>i. f i + g i) = suminf f + suminf g"
  4.1255 -  apply (subst (1 2 3) suminf_extreal_eq_SUPR)
  4.1256 +  apply (subst (1 2 3) suminf_ereal_eq_SUPR)
  4.1257    unfolding setsum_addf
  4.1258 -  by (intro assms extreal_add_nonneg_nonneg SUPR_extreal_add_pos incseq_setsumI setsum_nonneg ballI)+
  4.1259 +  by (intro assms ereal_add_nonneg_nonneg SUPR_ereal_add_pos incseq_setsumI setsum_nonneg ballI)+
  4.1260  
  4.1261 -lemma suminf_cmult_extreal:
  4.1262 -  fixes f g :: "nat \<Rightarrow> extreal"
  4.1263 +lemma suminf_cmult_ereal:
  4.1264 +  fixes f g :: "nat \<Rightarrow> ereal"
  4.1265    assumes "\<And>i. 0 \<le> f i" "0 \<le> a"
  4.1266    shows "(\<Sum>i. a * f i) = a * suminf f"
  4.1267 -  by (auto simp: setsum_extreal_right_distrib[symmetric] assms
  4.1268 -                 extreal_zero_le_0_iff setsum_nonneg suminf_extreal_eq_SUPR
  4.1269 -           intro!: SUPR_extreal_cmult )
  4.1270 +  by (auto simp: setsum_ereal_right_distrib[symmetric] assms
  4.1271 +                 ereal_zero_le_0_iff setsum_nonneg suminf_ereal_eq_SUPR
  4.1272 +           intro!: SUPR_ereal_cmult )
  4.1273  
  4.1274  lemma suminf_PInfty:
  4.1275    assumes "\<And>i. 0 \<le> f i" "suminf f \<noteq> \<infinity>"
  4.1276 @@ -1197,43 +1197,43 @@
  4.1277  
  4.1278  lemma suminf_PInfty_fun:
  4.1279    assumes "\<And>i. 0 \<le> f i" "suminf f \<noteq> \<infinity>"
  4.1280 -  shows "\<exists>f'. f = (\<lambda>x. extreal (f' x))"
  4.1281 +  shows "\<exists>f'. f = (\<lambda>x. ereal (f' x))"
  4.1282  proof -
  4.1283 -  have "\<forall>i. \<exists>r. f i = extreal r"
  4.1284 +  have "\<forall>i. \<exists>r. f i = ereal r"
  4.1285    proof
  4.1286 -    fix i show "\<exists>r. f i = extreal r"
  4.1287 +    fix i show "\<exists>r. f i = ereal r"
  4.1288        using suminf_PInfty[OF assms] assms(1)[of i] by (cases "f i") auto
  4.1289    qed
  4.1290    from choice[OF this] show ?thesis by auto
  4.1291  qed
  4.1292  
  4.1293 -lemma summable_extreal:
  4.1294 -  assumes "\<And>i. 0 \<le> f i" "(\<Sum>i. extreal (f i)) \<noteq> \<infinity>"
  4.1295 +lemma summable_ereal:
  4.1296 +  assumes "\<And>i. 0 \<le> f i" "(\<Sum>i. ereal (f i)) \<noteq> \<infinity>"
  4.1297    shows "summable f"
  4.1298  proof -
  4.1299 -  have "0 \<le> (\<Sum>i. extreal (f i))"
  4.1300 +  have "0 \<le> (\<Sum>i. ereal (f i))"
  4.1301      using assms by (intro suminf_0_le) auto
  4.1302 -  with assms obtain r where r: "(\<Sum>i. extreal (f i)) = extreal r"
  4.1303 -    by (cases "\<Sum>i. extreal (f i)") auto
  4.1304 -  from summable_extreal_pos[of "\<lambda>x. extreal (f x)"]
  4.1305 -  have "summable (\<lambda>x. extreal (f x))" using assms by auto
  4.1306 +  with assms obtain r where r: "(\<Sum>i. ereal (f i)) = ereal r"
  4.1307 +    by (cases "\<Sum>i. ereal (f i)") auto
  4.1308 +  from summable_ereal_pos[of "\<lambda>x. ereal (f x)"]
  4.1309 +  have "summable (\<lambda>x. ereal (f x))" using assms by auto
  4.1310    from summable_sums[OF this]
  4.1311 -  have "(\<lambda>x. extreal (f x)) sums (\<Sum>x. extreal (f x))" by auto
  4.1312 +  have "(\<lambda>x. ereal (f x)) sums (\<Sum>x. ereal (f x))" by auto
  4.1313    then show "summable f"
  4.1314 -    unfolding r sums_extreal summable_def ..
  4.1315 +    unfolding r sums_ereal summable_def ..
  4.1316  qed
  4.1317  
  4.1318 -lemma suminf_extreal:
  4.1319 -  assumes "\<And>i. 0 \<le> f i" "(\<Sum>i. extreal (f i)) \<noteq> \<infinity>"
  4.1320 -  shows "(\<Sum>i. extreal (f i)) = extreal (suminf f)"
  4.1321 +lemma suminf_ereal:
  4.1322 +  assumes "\<And>i. 0 \<le> f i" "(\<Sum>i. ereal (f i)) \<noteq> \<infinity>"
  4.1323 +  shows "(\<Sum>i. ereal (f i)) = ereal (suminf f)"
  4.1324  proof (rule sums_unique[symmetric])
  4.1325 -  from summable_extreal[OF assms]
  4.1326 -  show "(\<lambda>x. extreal (f x)) sums (extreal (suminf f))"
  4.1327 -    unfolding sums_extreal using assms by (intro summable_sums summable_extreal)
  4.1328 +  from summable_ereal[OF assms]
  4.1329 +  show "(\<lambda>x. ereal (f x)) sums (ereal (suminf f))"
  4.1330 +    unfolding sums_ereal using assms by (intro summable_sums summable_ereal)
  4.1331  qed
  4.1332  
  4.1333 -lemma suminf_extreal_minus:
  4.1334 -  fixes f g :: "nat \<Rightarrow> extreal"
  4.1335 +lemma suminf_ereal_minus:
  4.1336 +  fixes f g :: "nat \<Rightarrow> ereal"
  4.1337    assumes ord: "\<And>i. g i \<le> f i" "\<And>i. 0 \<le> g i" and fin: "suminf f \<noteq> \<infinity>" "suminf g \<noteq> \<infinity>"
  4.1338    shows "(\<Sum>i. f i - g i) = suminf f - suminf g"
  4.1339  proof -
  4.1340 @@ -1241,50 +1241,50 @@
  4.1341    moreover
  4.1342    from suminf_PInfty_fun[OF `\<And>i. 0 \<le> f i` fin(1)] guess f' .. note this[simp]
  4.1343    from suminf_PInfty_fun[OF `\<And>i. 0 \<le> g i` fin(2)] guess g' .. note this[simp]
  4.1344 -  { fix i have "0 \<le> f i - g i" using ord[of i] by (auto simp: extreal_le_minus_iff) }
  4.1345 +  { fix i have "0 \<le> f i - g i" using ord[of i] by (auto simp: ereal_le_minus_iff) }
  4.1346    moreover
  4.1347    have "suminf (\<lambda>i. f i - g i) \<le> suminf f"
  4.1348      using assms by (auto intro!: suminf_le_pos simp: field_simps)
  4.1349    then have "suminf (\<lambda>i. f i - g i) \<noteq> \<infinity>" using fin by auto
  4.1350    ultimately show ?thesis using assms `\<And>i. 0 \<le> f i`
  4.1351      apply simp
  4.1352 -    by (subst (1 2 3) suminf_extreal)
  4.1353 -       (auto intro!: suminf_diff[symmetric] summable_extreal)
  4.1354 +    by (subst (1 2 3) suminf_ereal)
  4.1355 +       (auto intro!: suminf_diff[symmetric] summable_ereal)
  4.1356  qed
  4.1357  
  4.1358 -lemma suminf_extreal_PInf[simp]:
  4.1359 +lemma suminf_ereal_PInf[simp]:
  4.1360    "(\<Sum>x. \<infinity>) = \<infinity>"
  4.1361  proof -
  4.1362    have "(\<Sum>i<Suc 0. \<infinity>) \<le> (\<Sum>x. \<infinity>)" by (rule suminf_upper) auto
  4.1363    then show ?thesis by simp
  4.1364  qed
  4.1365  
  4.1366 -lemma summable_real_of_extreal:
  4.1367 +lemma summable_real_of_ereal:
  4.1368    assumes f: "\<And>i. 0 \<le> f i" and fin: "(\<Sum>i. f i) \<noteq> \<infinity>"
  4.1369    shows "summable (\<lambda>i. real (f i))"
  4.1370  proof (rule summable_def[THEN iffD2])
  4.1371    have "0 \<le> (\<Sum>i. f i)" using assms by (auto intro: suminf_0_le)
  4.1372 -  with fin obtain r where r: "extreal r = (\<Sum>i. f i)" by (cases "(\<Sum>i. f i)") auto
  4.1373 +  with fin obtain r where r: "ereal r = (\<Sum>i. f i)" by (cases "(\<Sum>i. f i)") auto
  4.1374    { fix i have "f i \<noteq> \<infinity>" using f by (intro suminf_PInfty[OF _ fin]) auto
  4.1375      then have "\<bar>f i\<bar> \<noteq> \<infinity>" using f[of i] by auto }
  4.1376    note fin = this
  4.1377 -  have "(\<lambda>i. extreal (real (f i))) sums (\<Sum>i. extreal (real (f i)))"
  4.1378 -    using f by (auto intro!: summable_extreal_pos summable_sums simp: extreal_le_real_iff zero_extreal_def)
  4.1379 -  also have "\<dots> = extreal r" using fin r by (auto simp: extreal_real)
  4.1380 -  finally show "\<exists>r. (\<lambda>i. real (f i)) sums r" by (auto simp: sums_extreal)
  4.1381 +  have "(\<lambda>i. ereal (real (f i))) sums (\<Sum>i. ereal (real (f i)))"
  4.1382 +    using f by (auto intro!: summable_ereal_pos summable_sums simp: ereal_le_real_iff zero_ereal_def)
  4.1383 +  also have "\<dots> = ereal r" using fin r by (auto simp: ereal_real)
  4.1384 +  finally show "\<exists>r. (\<lambda>i. real (f i)) sums r" by (auto simp: sums_ereal)
  4.1385  qed
  4.1386  
  4.1387  lemma suminf_SUP_eq:
  4.1388 -  fixes f :: "nat \<Rightarrow> nat \<Rightarrow> extreal"
  4.1389 +  fixes f :: "nat \<Rightarrow> nat \<Rightarrow> ereal"
  4.1390    assumes "\<And>i. incseq (\<lambda>n. f n i)" "\<And>n i. 0 \<le> f n i"
  4.1391    shows "(\<Sum>i. SUP n. f n i) = (SUP n. \<Sum>i. f n i)"
  4.1392  proof -
  4.1393    { fix n :: nat
  4.1394      have "(\<Sum>i<n. SUP k. f k i) = (SUP k. \<Sum>i<n. f k i)"
  4.1395 -      using assms by (auto intro!: SUPR_extreal_setsum[symmetric]) }
  4.1396 +      using assms by (auto intro!: SUPR_ereal_setsum[symmetric]) }
  4.1397    note * = this
  4.1398    show ?thesis using assms
  4.1399 -    apply (subst (1 2) suminf_extreal_eq_SUPR)
  4.1400 +    apply (subst (1 2) suminf_ereal_eq_SUPR)
  4.1401      unfolding *
  4.1402      apply (auto intro!: le_SUPI2)
  4.1403      apply (subst SUP_commute) ..
     5.1 --- a/src/HOL/Probability/Binary_Product_Measure.thy	Tue Jul 19 14:35:44 2011 +0200
     5.2 +++ b/src/HOL/Probability/Binary_Product_Measure.thy	Tue Jul 19 14:36:12 2011 +0200
     5.3 @@ -291,7 +291,7 @@
     5.4          (if x \<in> space M1 then measure M2 (space M2) - ?s A x else 0)"
     5.5        by (auto intro!: M2.measure_compl simp: vimage_Diff)
     5.6      with `A \<in> sets ?D` top show "space ?D - A \<in> sets ?D"
     5.7 -      by (auto intro!: Diff M1.measurable_If M1.borel_measurable_extreal_diff)
     5.8 +      by (auto intro!: Diff M1.measurable_If M1.borel_measurable_ereal_diff)
     5.9    next
    5.10      fix F :: "nat \<Rightarrow> ('a\<times>'b) set" assume "disjoint_family F" "range F \<subseteq> sets ?D"
    5.11      moreover then have "\<And>x. measure M2 (\<Union>i. Pair x -` F i) = (\<Sum>i. ?s (F i) x)"
    5.12 @@ -401,7 +401,7 @@
    5.13    apply (simp add: pair_measure_def pair_measure_generator_def)
    5.14  proof (rule M1.positive_integral_cong)
    5.15    fix x assume "x \<in> space M1"
    5.16 -  have *: "\<And>y. indicator A (x, y) = (indicator (Pair x -` A) y :: extreal)"
    5.17 +  have *: "\<And>y. indicator A (x, y) = (indicator (Pair x -` A) y :: ereal)"
    5.18      unfolding indicator_def by auto
    5.19    show "(\<integral>\<^isup>+ y. indicator A (x, y) \<partial>M2) = measure M2 (Pair x -` A)"
    5.20      unfolding *
    5.21 @@ -656,7 +656,7 @@
    5.22    show "(\<lambda>x. \<integral>\<^isup>+y. f (x, y) \<partial>M2) \<in> borel_measurable M1"
    5.23      and "(\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>P P f" using f(2)
    5.24      by (auto simp del: vimage_Int cong: measurable_cong
    5.25 -             intro!: M1.borel_measurable_extreal_setsum setsum_cong
    5.26 +             intro!: M1.borel_measurable_ereal_setsum setsum_cong
    5.27               simp add: M1.positive_integral_setsum simple_integral_def
    5.28                         M1.positive_integral_cmult
    5.29                         M1.positive_integral_cong[OF eq]
    5.30 @@ -760,7 +760,7 @@
    5.31      show "M1.\<mu> {x\<in>space M1. M2.\<mu> (Pair x -` N) \<noteq> 0} = 0"
    5.32        by (auto simp: pair_measure_alt M1.positive_integral_0_iff)
    5.33      show "{x \<in> space M1. M2.\<mu> (Pair x -` N) \<noteq> 0} \<in> sets M1"
    5.34 -      by (intro M1.borel_measurable_extreal_neq_const measure_cut_measurable_fst N)
    5.35 +      by (intro M1.borel_measurable_ereal_neq_const measure_cut_measurable_fst N)
    5.36      { fix x assume "x \<in> space M1" "M2.\<mu> (Pair x -` N) = 0"
    5.37        have "M2.almost_everywhere (\<lambda>y. Q (x, y))"
    5.38        proof (rule M2.AE_I)
    5.39 @@ -822,45 +822,45 @@
    5.40    shows "M1.almost_everywhere (\<lambda>x. integrable M2 (\<lambda> y. f (x, y)))" (is "?AE")
    5.41      and "(\<integral>x. (\<integral>y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^isup>L P f" (is "?INT")
    5.42  proof -
    5.43 -  let "?pf x" = "extreal (f x)" and "?nf x" = "extreal (- f x)"
    5.44 +  let "?pf x" = "ereal (f x)" and "?nf x" = "ereal (- f x)"
    5.45    have
    5.46      borel: "?nf \<in> borel_measurable P""?pf \<in> borel_measurable P" and
    5.47      int: "integral\<^isup>P P ?nf \<noteq> \<infinity>" "integral\<^isup>P P ?pf \<noteq> \<infinity>"
    5.48      using assms by auto
    5.49 -  have "(\<integral>\<^isup>+x. (\<integral>\<^isup>+y. extreal (f (x, y)) \<partial>M2) \<partial>M1) \<noteq> \<infinity>"
    5.50 -     "(\<integral>\<^isup>+x. (\<integral>\<^isup>+y. extreal (- f (x, y)) \<partial>M2) \<partial>M1) \<noteq> \<infinity>"
    5.51 +  have "(\<integral>\<^isup>+x. (\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2) \<partial>M1) \<noteq> \<infinity>"
    5.52 +     "(\<integral>\<^isup>+x. (\<integral>\<^isup>+y. ereal (- f (x, y)) \<partial>M2) \<partial>M1) \<noteq> \<infinity>"
    5.53      using borel[THEN positive_integral_fst_measurable(1)] int
    5.54      unfolding borel[THEN positive_integral_fst_measurable(2)] by simp_all
    5.55    with borel[THEN positive_integral_fst_measurable(1)]
    5.56 -  have AE_pos: "AE x in M1. (\<integral>\<^isup>+y. extreal (f (x, y)) \<partial>M2) \<noteq> \<infinity>"
    5.57 -    "AE x in M1. (\<integral>\<^isup>+y. extreal (- f (x, y)) \<partial>M2) \<noteq> \<infinity>"
    5.58 +  have AE_pos: "AE x in M1. (\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2) \<noteq> \<infinity>"
    5.59 +    "AE x in M1. (\<integral>\<^isup>+y. ereal (- f (x, y)) \<partial>M2) \<noteq> \<infinity>"
    5.60      by (auto intro!: M1.positive_integral_PInf_AE )
    5.61 -  then have AE: "AE x in M1. \<bar>\<integral>\<^isup>+y. extreal (f (x, y)) \<partial>M2\<bar> \<noteq> \<infinity>"
    5.62 -    "AE x in M1. \<bar>\<integral>\<^isup>+y. extreal (- f (x, y)) \<partial>M2\<bar> \<noteq> \<infinity>"
    5.63 +  then have AE: "AE x in M1. \<bar>\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2\<bar> \<noteq> \<infinity>"
    5.64 +    "AE x in M1. \<bar>\<integral>\<^isup>+y. ereal (- f (x, y)) \<partial>M2\<bar> \<noteq> \<infinity>"
    5.65      by (auto simp: M2.positive_integral_positive)
    5.66    from AE_pos show ?AE using assms
    5.67      by (simp add: measurable_pair_image_snd integrable_def)
    5.68 -  { fix f have "(\<integral>\<^isup>+ x. - \<integral>\<^isup>+ y. extreal (f x y) \<partial>M2 \<partial>M1) = (\<integral>\<^isup>+x. 0 \<partial>M1)"
    5.69 +  { fix f have "(\<integral>\<^isup>+ x. - \<integral>\<^isup>+ y. ereal (f x y) \<partial>M2 \<partial>M1) = (\<integral>\<^isup>+x. 0 \<partial>M1)"
    5.70        using M2.positive_integral_positive
    5.71 -      by (intro M1.positive_integral_cong_pos) (auto simp: extreal_uminus_le_reorder)
    5.72 -    then have "(\<integral>\<^isup>+ x. - \<integral>\<^isup>+ y. extreal (f x y) \<partial>M2 \<partial>M1) = 0" by simp }
    5.73 +      by (intro M1.positive_integral_cong_pos) (auto simp: ereal_uminus_le_reorder)
    5.74 +    then have "(\<integral>\<^isup>+ x. - \<integral>\<^isup>+ y. ereal (f x y) \<partial>M2 \<partial>M1) = 0" by simp }
    5.75    note this[simp]
    5.76 -  { fix f assume borel: "(\<lambda>x. extreal (f x)) \<in> borel_measurable P"
    5.77 -      and int: "integral\<^isup>P P (\<lambda>x. extreal (f x)) \<noteq> \<infinity>"
    5.78 -      and AE: "M1.almost_everywhere (\<lambda>x. (\<integral>\<^isup>+y. extreal (f (x, y)) \<partial>M2) \<noteq> \<infinity>)"
    5.79 -    have "integrable M1 (\<lambda>x. real (\<integral>\<^isup>+y. extreal (f (x, y)) \<partial>M2))" (is "integrable M1 ?f")
    5.80 +  { fix f assume borel: "(\<lambda>x. ereal (f x)) \<in> borel_measurable P"
    5.81 +      and int: "integral\<^isup>P P (\<lambda>x. ereal (f x)) \<noteq> \<infinity>"
    5.82 +      and AE: "M1.almost_everywhere (\<lambda>x. (\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2) \<noteq> \<infinity>)"
    5.83 +    have "integrable M1 (\<lambda>x. real (\<integral>\<^isup>+y. ereal (f (x, y)) \<partial>M2))" (is "integrable M1 ?f")
    5.84      proof (intro integrable_def[THEN iffD2] conjI)
    5.85        show "?f \<in> borel_measurable M1"
    5.86 -        using borel by (auto intro!: M1.borel_measurable_real_of_extreal positive_integral_fst_measurable)
    5.87 -      have "(\<integral>\<^isup>+x. extreal (?f x) \<partial>M1) = (\<integral>\<^isup>+x. (\<integral>\<^isup>+y. extreal (f (x, y))  \<partial>M2) \<partial>M1)"
    5.88 +        using borel by (auto intro!: M1.borel_measurable_real_of_ereal positive_integral_fst_measurable)
    5.89 +      have "(\<integral>\<^isup>+x. ereal (?f x) \<partial>M1) = (\<integral>\<^isup>+x. (\<integral>\<^isup>+y. ereal (f (x, y))  \<partial>M2) \<partial>M1)"
    5.90          using AE M2.positive_integral_positive
    5.91 -        by (auto intro!: M1.positive_integral_cong_AE simp: extreal_real)
    5.92 -      then show "(\<integral>\<^isup>+x. extreal (?f x) \<partial>M1) \<noteq> \<infinity>"
    5.93 +        by (auto intro!: M1.positive_integral_cong_AE simp: ereal_real)
    5.94 +      then show "(\<integral>\<^isup>+x. ereal (?f x) \<partial>M1) \<noteq> \<infinity>"
    5.95          using positive_integral_fst_measurable[OF borel] int by simp
    5.96 -      have "(\<integral>\<^isup>+x. extreal (- ?f x) \<partial>M1) = (\<integral>\<^isup>+x. 0 \<partial>M1)"
    5.97 +      have "(\<integral>\<^isup>+x. ereal (- ?f x) \<partial>M1) = (\<integral>\<^isup>+x. 0 \<partial>M1)"
    5.98          by (intro M1.positive_integral_cong_pos)
    5.99 -           (simp add: M2.positive_integral_positive real_of_extreal_pos)
   5.100 -      then show "(\<integral>\<^isup>+x. extreal (- ?f x) \<partial>M1) \<noteq> \<infinity>" by simp
   5.101 +           (simp add: M2.positive_integral_positive real_of_ereal_pos)
   5.102 +      then show "(\<integral>\<^isup>+x. ereal (- ?f x) \<partial>M1) \<noteq> \<infinity>" by simp
   5.103      qed }
   5.104    with this[OF borel(1) int(1) AE_pos(2)] this[OF borel(2) int(2) AE_pos(1)]
   5.105    show ?INT
     6.1 --- a/src/HOL/Probability/Borel_Space.thy	Tue Jul 19 14:35:44 2011 +0200
     6.2 +++ b/src/HOL/Probability/Borel_Space.thy	Tue Jul 19 14:36:12 2011 +0200
     6.3 @@ -112,7 +112,7 @@
     6.4  qed
     6.5  
     6.6  lemma (in sigma_algebra) borel_measurable_restricted:
     6.7 -  fixes f :: "'a \<Rightarrow> extreal" assumes "A \<in> sets M"
     6.8 +  fixes f :: "'a \<Rightarrow> ereal" assumes "A \<in> sets M"
     6.9    shows "f \<in> borel_measurable (restricted_space A) \<longleftrightarrow>
    6.10      (\<lambda>x. f x * indicator A x) \<in> borel_measurable M"
    6.11      (is "f \<in> borel_measurable ?R \<longleftrightarrow> ?f \<in> borel_measurable M")
    6.12 @@ -123,7 +123,7 @@
    6.13    show ?thesis unfolding *
    6.14      unfolding in_borel_measurable_borel
    6.15    proof (simp, safe)
    6.16 -    fix S :: "extreal set" assume "S \<in> sets borel"
    6.17 +    fix S :: "ereal set" assume "S \<in> sets borel"
    6.18        "\<forall>S\<in>sets borel. ?f -` S \<inter> A \<in> op \<inter> A ` sets M"
    6.19      then have "?f -` S \<inter> A \<in> op \<inter> A ` sets M" by auto
    6.20      then have f: "?f -` S \<inter> A \<in> sets M"
    6.21 @@ -142,7 +142,7 @@
    6.22        then show ?thesis using f by auto
    6.23      qed
    6.24    next
    6.25 -    fix S :: "extreal set" assume "S \<in> sets borel"
    6.26 +    fix S :: "ereal set" assume "S \<in> sets borel"
    6.27        "\<forall>S\<in>sets borel. ?f -` S \<inter> space M \<in> sets M"
    6.28      then have f: "?f -` S \<inter> space M \<in> sets M" by auto
    6.29      then show "?f -` S \<inter> A \<in> op \<inter> A ` sets M"
    6.30 @@ -1095,70 +1095,70 @@
    6.31  
    6.32  subsection "Borel space on the extended reals"
    6.33  
    6.34 -lemma borel_measurable_extreal_borel:
    6.35 -  "extreal \<in> borel_measurable borel"
    6.36 -  unfolding borel_def[where 'a=extreal]
    6.37 +lemma borel_measurable_ereal_borel:
    6.38 +  "ereal \<in> borel_measurable borel"
    6.39 +  unfolding borel_def[where 'a=ereal]
    6.40  proof (rule borel.measurable_sigma)
    6.41 -  fix X :: "extreal set" assume "X \<in> sets \<lparr>space = UNIV, sets = open \<rparr>"
    6.42 +  fix X :: "ereal set" assume "X \<in> sets \<lparr>space = UNIV, sets = open \<rparr>"
    6.43    then have "open X" by (auto simp: mem_def)
    6.44 -  then have "open (extreal -` X \<inter> space borel)"
    6.45 -    by (simp add: open_extreal_vimage)
    6.46 -  then show "extreal -` X \<inter> space borel \<in> sets borel" by auto
    6.47 +  then have "open (ereal -` X \<inter> space borel)"
    6.48 +    by (simp add: open_ereal_vimage)
    6.49 +  then show "ereal -` X \<inter> space borel \<in> sets borel" by auto
    6.50  qed auto
    6.51  
    6.52 -lemma (in sigma_algebra) borel_measurable_extreal[simp, intro]:
    6.53 -  assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. extreal (f x)) \<in> borel_measurable M"
    6.54 -  using measurable_comp[OF f borel_measurable_extreal_borel] unfolding comp_def .
    6.55 +lemma (in sigma_algebra) borel_measurable_ereal[simp, intro]:
    6.56 +  assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
    6.57 +  using measurable_comp[OF f borel_measurable_ereal_borel] unfolding comp_def .
    6.58  
    6.59 -lemma borel_measurable_real_of_extreal_borel:
    6.60 -  "(real :: extreal \<Rightarrow> real) \<in> borel_measurable borel"
    6.61 +lemma borel_measurable_real_of_ereal_borel:
    6.62 +  "(real :: ereal \<Rightarrow> real) \<in> borel_measurable borel"
    6.63    unfolding borel_def[where 'a=real]
    6.64  proof (rule borel.measurable_sigma)
    6.65    fix B :: "real set" assume "B \<in> sets \<lparr>space = UNIV, sets = open \<rparr>"
    6.66    then have "open B" by (auto simp: mem_def)
    6.67 -  have *: "extreal -` real -` (B - {0}) = B - {0}" by auto
    6.68 -  have open_real: "open (real -` (B - {0}) :: extreal set)"
    6.69 -    unfolding open_extreal_def * using `open B` by auto
    6.70 -  show "(real -` B \<inter> space borel :: extreal set) \<in> sets borel"
    6.71 +  have *: "ereal -` real -` (B - {0}) = B - {0}" by auto
    6.72 +  have open_real: "open (real -` (B - {0}) :: ereal set)"
    6.73 +    unfolding open_ereal_def * using `open B` by auto
    6.74 +  show "(real -` B \<inter> space borel :: ereal set) \<in> sets borel"
    6.75    proof cases
    6.76      assume "0 \<in> B"
    6.77      then have *: "real -` B = real -` (B - {0}) \<union> {-\<infinity>, \<infinity>, 0}"
    6.78 -      by (auto simp add: real_of_extreal_eq_0)
    6.79 -    then show "(real -` B :: extreal set) \<inter> space borel \<in> sets borel"
    6.80 +      by (auto simp add: real_of_ereal_eq_0)
    6.81 +    then show "(real -` B :: ereal set) \<inter> space borel \<in> sets borel"
    6.82        using open_real by auto
    6.83    next
    6.84      assume "0 \<notin> B"
    6.85 -    then have *: "(real -` B :: extreal set) = real -` (B - {0})"
    6.86 -      by (auto simp add: real_of_extreal_eq_0)
    6.87 -    then show "(real -` B :: extreal set) \<inter> space borel \<in> sets borel"
    6.88 +    then have *: "(real -` B :: ereal set) = real -` (B - {0})"
    6.89 +      by (auto simp add: real_of_ereal_eq_0)
    6.90 +    then show "(real -` B :: ereal set) \<inter> space borel \<in> sets borel"
    6.91        using open_real by auto
    6.92    qed
    6.93  qed auto
    6.94  
    6.95 -lemma (in sigma_algebra) borel_measurable_real_of_extreal[simp, intro]:
    6.96 -  assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. real (f x :: extreal)) \<in> borel_measurable M"
    6.97 -  using measurable_comp[OF f borel_measurable_real_of_extreal_borel] unfolding comp_def .
    6.98 +lemma (in sigma_algebra) borel_measurable_real_of_ereal[simp, intro]:
    6.99 +  assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. real (f x :: ereal)) \<in> borel_measurable M"
   6.100 +  using measurable_comp[OF f borel_measurable_real_of_ereal_borel] unfolding comp_def .
   6.101  
   6.102 -lemma (in sigma_algebra) borel_measurable_extreal_iff:
   6.103 -  shows "(\<lambda>x. extreal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
   6.104 +lemma (in sigma_algebra) borel_measurable_ereal_iff:
   6.105 +  shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
   6.106  proof
   6.107 -  assume "(\<lambda>x. extreal (f x)) \<in> borel_measurable M"
   6.108 -  from borel_measurable_real_of_extreal[OF this]
   6.109 +  assume "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
   6.110 +  from borel_measurable_real_of_ereal[OF this]
   6.111    show "f \<in> borel_measurable M" by auto
   6.112  qed auto
   6.113  
   6.114 -lemma (in sigma_algebra) borel_measurable_extreal_iff_real:
   6.115 +lemma (in sigma_algebra) borel_measurable_ereal_iff_real:
   6.116    "f \<in> borel_measurable M \<longleftrightarrow>
   6.117      ((\<lambda>x. real (f x)) \<in> borel_measurable M \<and> f -` {\<infinity>} \<inter> space M \<in> sets M \<and> f -` {-\<infinity>} \<inter> space M \<in> sets M)"
   6.118  proof safe
   6.119    assume *: "(\<lambda>x. real (f x)) \<in> borel_measurable M" "f -` {\<infinity>} \<inter> space M \<in> sets M" "f -` {-\<infinity>} \<inter> space M \<in> sets M"
   6.120    have "f -` {\<infinity>} \<inter> space M = {x\<in>space M. f x = \<infinity>}" "f -` {-\<infinity>} \<inter> space M = {x\<in>space M. f x = -\<infinity>}" by auto
   6.121    with * have **: "{x\<in>space M. f x = \<infinity>} \<in> sets M" "{x\<in>space M. f x = -\<infinity>} \<in> sets M" by simp_all
   6.122 -  let "?f x" = "if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else extreal (real (f x))"
   6.123 +  let "?f x" = "if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else ereal (real (f x))"
   6.124    have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
   6.125 -  also have "?f = f" by (auto simp: fun_eq_iff extreal_real)
   6.126 +  also have "?f = f" by (auto simp: fun_eq_iff ereal_real)
   6.127    finally show "f \<in> borel_measurable M" .
   6.128 -qed (auto intro: measurable_sets borel_measurable_real_of_extreal)
   6.129 +qed (auto intro: measurable_sets borel_measurable_real_of_ereal)
   6.130  
   6.131  lemma (in sigma_algebra) less_eq_ge_measurable:
   6.132    fixes f :: "'a \<Rightarrow> 'c::linorder"
   6.133 @@ -1186,40 +1186,40 @@
   6.134    ultimately show "f -` {a ..} \<inter> space M \<in> sets M" by auto
   6.135  qed
   6.136  
   6.137 -lemma (in sigma_algebra) borel_measurable_uminus_borel_extreal:
   6.138 -  "(uminus :: extreal \<Rightarrow> extreal) \<in> borel_measurable borel"
   6.139 +lemma (in sigma_algebra) borel_measurable_uminus_borel_ereal:
   6.140 +  "(uminus :: ereal \<Rightarrow> ereal) \<in> borel_measurable borel"
   6.141  proof (subst borel_def, rule borel.measurable_sigma)
   6.142 -  fix X :: "extreal set" assume "X \<in> sets \<lparr>space = UNIV, sets = open\<rparr>"
   6.143 +  fix X :: "ereal set" assume "X \<in> sets \<lparr>space = UNIV, sets = open\<rparr>"
   6.144    then have "open X" by (simp add: mem_def)
   6.145    have "uminus -` X = uminus ` X" by (force simp: image_iff)
   6.146 -  then have "open (uminus -` X)" using `open X` extreal_open_uminus by auto
   6.147 +  then have "open (uminus -` X)" using `open X` ereal_open_uminus by auto
   6.148    then show "uminus -` X \<inter> space borel \<in> sets borel" by auto
   6.149  qed auto
   6.150  
   6.151 -lemma (in sigma_algebra) borel_measurable_uminus_extreal[intro]:
   6.152 +lemma (in sigma_algebra) borel_measurable_uminus_ereal[intro]:
   6.153    assumes "f \<in> borel_measurable M"
   6.154 -  shows "(\<lambda>x. - f x :: extreal) \<in> borel_measurable M"
   6.155 -  using measurable_comp[OF assms borel_measurable_uminus_borel_extreal] by (simp add: comp_def)
   6.156 +  shows "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
   6.157 +  using measurable_comp[OF assms borel_measurable_uminus_borel_ereal] by (simp add: comp_def)
   6.158  
   6.159 -lemma (in sigma_algebra) borel_measurable_uminus_eq_extreal[simp]:
   6.160 -  "(\<lambda>x. - f x :: extreal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
   6.161 +lemma (in sigma_algebra) borel_measurable_uminus_eq_ereal[simp]:
   6.162 +  "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
   6.163  proof
   6.164 -  assume ?l from borel_measurable_uminus_extreal[OF this] show ?r by simp
   6.165 +  assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
   6.166  qed auto
   6.167  
   6.168 -lemma (in sigma_algebra) borel_measurable_eq_atMost_extreal:
   6.169 -  "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
   6.170 +lemma (in sigma_algebra) borel_measurable_eq_atMost_ereal:
   6.171 +  "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
   6.172  proof (intro iffI allI)
   6.173    assume pos[rule_format]: "\<forall>a. f -` {..a} \<inter> space M \<in> sets M"
   6.174    show "f \<in> borel_measurable M"
   6.175 -    unfolding borel_measurable_extreal_iff_real borel_measurable_iff_le
   6.176 +    unfolding borel_measurable_ereal_iff_real borel_measurable_iff_le
   6.177    proof (intro conjI allI)
   6.178      fix a :: real
   6.179 -    { fix x :: extreal assume *: "\<forall>i::nat. real i < x"
   6.180 +    { fix x :: ereal assume *: "\<forall>i::nat. real i < x"
   6.181        have "x = \<infinity>"
   6.182 -      proof (rule extreal_top)
   6.183 +      proof (rule ereal_top)
   6.184          fix B from real_arch_lt[of B] guess n ..
   6.185 -        then have "extreal B < real n" by auto
   6.186 +        then have "ereal B < real n" by auto
   6.187          with * show "B \<le> x" by (metis less_trans less_imp_le)
   6.188        qed }
   6.189      then have "f -` {\<infinity>} \<inter> space M = space M - (\<Union>i::nat. f -` {.. real i} \<inter> space M)"
   6.190 @@ -1228,53 +1228,53 @@
   6.191      moreover
   6.192      have "{-\<infinity>} = {..-\<infinity>}" by auto
   6.193      then show "f -` {-\<infinity>} \<inter> space M \<in> sets M" using pos by auto
   6.194 -    moreover have "{x\<in>space M. f x \<le> extreal a} \<in> sets M"
   6.195 -      using pos[of "extreal a"] by (simp add: vimage_def Int_def conj_commute)
   6.196 +    moreover have "{x\<in>space M. f x \<le> ereal a} \<in> sets M"
   6.197 +      using pos[of "ereal a"] by (simp add: vimage_def Int_def conj_commute)
   6.198      moreover have "{w \<in> space M. real (f w) \<le> a} =
   6.199 -      (if a < 0 then {w \<in> space M. f w \<le> extreal a} - f -` {-\<infinity>} \<inter> space M
   6.200 -      else {w \<in> space M. f w \<le> extreal a} \<union> (f -` {\<infinity>} \<inter> space M) \<union> (f -` {-\<infinity>} \<inter> space M))" (is "?l = ?r")
   6.201 +      (if a < 0 then {w \<in> space M. f w \<le> ereal a} - f -` {-\<infinity>} \<inter> space M
   6.202 +      else {w \<in> space M. f w \<le> ereal a} \<union> (f -` {\<infinity>} \<inter> space M) \<union> (f -` {-\<infinity>} \<inter> space M))" (is "?l = ?r")
   6.203        proof (intro set_eqI) fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases "f x") auto qed
   6.204      ultimately show "{w \<in> space M. real (f w) \<le> a} \<in> sets M" by auto
   6.205    qed
   6.206  qed (simp add: measurable_sets)
   6.207  
   6.208 -lemma (in sigma_algebra) borel_measurable_eq_atLeast_extreal:
   6.209 -  "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
   6.210 +lemma (in sigma_algebra) borel_measurable_eq_atLeast_ereal:
   6.211 +  "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
   6.212  proof
   6.213    assume pos: "\<forall>a. f -` {a..} \<inter> space M \<in> sets M"
   6.214    moreover have "\<And>a. (\<lambda>x. - f x) -` {..a} = f -` {-a ..}"
   6.215 -    by (auto simp: extreal_uminus_le_reorder)
   6.216 +    by (auto simp: ereal_uminus_le_reorder)
   6.217    ultimately have "(\<lambda>x. - f x) \<in> borel_measurable M"
   6.218 -    unfolding borel_measurable_eq_atMost_extreal by auto
   6.219 +    unfolding borel_measurable_eq_atMost_ereal by auto
   6.220    then show "f \<in> borel_measurable M" by simp
   6.221  qed (simp add: measurable_sets)
   6.222  
   6.223 -lemma (in sigma_algebra) borel_measurable_extreal_iff_less:
   6.224 -  "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
   6.225 -  unfolding borel_measurable_eq_atLeast_extreal greater_eq_le_measurable ..
   6.226 +lemma (in sigma_algebra) borel_measurable_ereal_iff_less:
   6.227 +  "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
   6.228 +  unfolding borel_measurable_eq_atLeast_ereal greater_eq_le_measurable ..
   6.229  
   6.230 -lemma (in sigma_algebra) borel_measurable_extreal_iff_ge:
   6.231 -  "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
   6.232 -  unfolding borel_measurable_eq_atMost_extreal less_eq_ge_measurable ..
   6.233 +lemma (in sigma_algebra) borel_measurable_ereal_iff_ge:
   6.234 +  "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
   6.235 +  unfolding borel_measurable_eq_atMost_ereal less_eq_ge_measurable ..
   6.236  
   6.237 -lemma (in sigma_algebra) borel_measurable_extreal_eq_const:
   6.238 -  fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M"
   6.239 +lemma (in sigma_algebra) borel_measurable_ereal_eq_const:
   6.240 +  fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M"
   6.241    shows "{x\<in>space M. f x = c} \<in> sets M"
   6.242  proof -
   6.243    have "{x\<in>space M. f x = c} = (f -` {c} \<inter> space M)" by auto
   6.244    then show ?thesis using assms by (auto intro!: measurable_sets)
   6.245  qed
   6.246  
   6.247 -lemma (in sigma_algebra) borel_measurable_extreal_neq_const:
   6.248 -  fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M"
   6.249 +lemma (in sigma_algebra) borel_measurable_ereal_neq_const:
   6.250 +  fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M"
   6.251    shows "{x\<in>space M. f x \<noteq> c} \<in> sets M"
   6.252  proof -
   6.253    have "{x\<in>space M. f x \<noteq> c} = space M - (f -` {c} \<inter> space M)" by auto
   6.254    then show ?thesis using assms by (auto intro!: measurable_sets)
   6.255  qed
   6.256  
   6.257 -lemma (in sigma_algebra) borel_measurable_extreal_le[intro,simp]:
   6.258 -  fixes f g :: "'a \<Rightarrow> extreal"
   6.259 +lemma (in sigma_algebra) borel_measurable_ereal_le[intro,simp]:
   6.260 +  fixes f g :: "'a \<Rightarrow> ereal"
   6.261    assumes f: "f \<in> borel_measurable M"
   6.262    assumes g: "g \<in> borel_measurable M"
   6.263    shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
   6.264 @@ -1283,13 +1283,13 @@
   6.265      {x \<in> space M. real (f x) \<le> real (g x)} - (f -` {\<infinity>, -\<infinity>} \<inter> space M \<union> g -` {\<infinity>, -\<infinity>} \<inter> space M) \<union>
   6.266      f -` {-\<infinity>} \<inter> space M \<union> g -` {\<infinity>} \<inter> space M" (is "?l = ?r")
   6.267    proof (intro set_eqI)
   6.268 -    fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases rule: extreal2_cases[of "f x" "g x"]) auto
   6.269 +    fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases rule: ereal2_cases[of "f x" "g x"]) auto
   6.270    qed
   6.271    with f g show ?thesis by (auto intro!: Un simp: measurable_sets)
   6.272  qed
   6.273  
   6.274 -lemma (in sigma_algebra) borel_measurable_extreal_less[intro,simp]:
   6.275 -  fixes f :: "'a \<Rightarrow> extreal"
   6.276 +lemma (in sigma_algebra) borel_measurable_ereal_less[intro,simp]:
   6.277 +  fixes f :: "'a \<Rightarrow> ereal"
   6.278    assumes f: "f \<in> borel_measurable M"
   6.279    assumes g: "g \<in> borel_measurable M"
   6.280    shows "{x \<in> space M. f x < g x} \<in> sets M"
   6.281 @@ -1298,8 +1298,8 @@
   6.282    then show ?thesis using g f by auto
   6.283  qed
   6.284  
   6.285 -lemma (in sigma_algebra) borel_measurable_extreal_eq[intro,simp]:
   6.286 -  fixes f :: "'a \<Rightarrow> extreal"
   6.287 +lemma (in sigma_algebra) borel_measurable_ereal_eq[intro,simp]:
   6.288 +  fixes f :: "'a \<Rightarrow> ereal"
   6.289    assumes f: "f \<in> borel_measurable M"
   6.290    assumes g: "g \<in> borel_measurable M"
   6.291    shows "{w \<in> space M. f w = g w} \<in> sets M"
   6.292 @@ -1308,8 +1308,8 @@
   6.293    then show ?thesis using g f by auto
   6.294  qed
   6.295  
   6.296 -lemma (in sigma_algebra) borel_measurable_extreal_neq[intro,simp]:
   6.297 -  fixes f :: "'a \<Rightarrow> extreal"
   6.298 +lemma (in sigma_algebra) borel_measurable_ereal_neq[intro,simp]:
   6.299 +  fixes f :: "'a \<Rightarrow> ereal"
   6.300    assumes f: "f \<in> borel_measurable M"
   6.301    assumes g: "g \<in> borel_measurable M"
   6.302    shows "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
   6.303 @@ -1323,23 +1323,23 @@
   6.304    "{x\<in>space M. P x \<and> Q x} = {x\<in>space M. P x} \<inter> {x\<in>space M. Q x}"
   6.305    by auto
   6.306  
   6.307 -lemma (in sigma_algebra) borel_measurable_extreal_add[intro, simp]:
   6.308 -  fixes f :: "'a \<Rightarrow> extreal"
   6.309 +lemma (in sigma_algebra) borel_measurable_ereal_add[intro, simp]:
   6.310 +  fixes f :: "'a \<Rightarrow> ereal"
   6.311    assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   6.312    shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   6.313  proof -
   6.314    { fix x assume "x \<in> space M" then have "f x + g x =
   6.315        (if f x = \<infinity> \<or> g x = \<infinity> then \<infinity>
   6.316          else if f x = -\<infinity> \<or> g x = -\<infinity> then -\<infinity>
   6.317 -        else extreal (real (f x) + real (g x)))"
   6.318 -      by (cases rule: extreal2_cases[of "f x" "g x"]) auto }
   6.319 +        else ereal (real (f x) + real (g x)))"
   6.320 +      by (cases rule: ereal2_cases[of "f x" "g x"]) auto }
   6.321    with assms show ?thesis
   6.322      by (auto cong: measurable_cong simp: split_sets
   6.323               intro!: Un measurable_If measurable_sets)
   6.324  qed
   6.325  
   6.326 -lemma (in sigma_algebra) borel_measurable_extreal_setsum[simp, intro]:
   6.327 -  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> extreal"
   6.328 +lemma (in sigma_algebra) borel_measurable_ereal_setsum[simp, intro]:
   6.329 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
   6.330    assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   6.331    shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   6.332  proof cases
   6.333 @@ -1348,25 +1348,25 @@
   6.334      by induct auto
   6.335  qed (simp add: borel_measurable_const)
   6.336  
   6.337 -lemma (in sigma_algebra) borel_measurable_extreal_abs[intro, simp]:
   6.338 -  fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M"
   6.339 +lemma (in sigma_algebra) borel_measurable_ereal_abs[intro, simp]:
   6.340 +  fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M"
   6.341    shows "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
   6.342  proof -
   6.343    { fix x have "\<bar>f x\<bar> = (if 0 \<le> f x then f x else - f x)" by auto }
   6.344    then show ?thesis using assms by (auto intro!: measurable_If)
   6.345  qed
   6.346  
   6.347 -lemma (in sigma_algebra) borel_measurable_extreal_times[intro, simp]:
   6.348 -  fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   6.349 +lemma (in sigma_algebra) borel_measurable_ereal_times[intro, simp]:
   6.350 +  fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   6.351    shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   6.352  proof -
   6.353 -  { fix f g :: "'a \<Rightarrow> extreal"
   6.354 +  { fix f g :: "'a \<Rightarrow> ereal"
   6.355      assume b: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   6.356        and pos: "\<And>x. 0 \<le> f x" "\<And>x. 0 \<le> g x"
   6.357      { fix x have *: "f x * g x = (if f x = 0 \<or> g x = 0 then 0
   6.358          else if f x = \<infinity> \<or> g x = \<infinity> then \<infinity>
   6.359 -        else extreal (real (f x) * real (g x)))"
   6.360 -      apply (cases rule: extreal2_cases[of "f x" "g x"])
   6.361 +        else ereal (real (f x) * real (g x)))"
   6.362 +      apply (cases rule: ereal2_cases[of "f x" "g x"])
   6.363        using pos[of x] by auto }
   6.364      with b have "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   6.365        by (auto cong: measurable_cong simp: split_sets
   6.366 @@ -1376,12 +1376,12 @@
   6.367      (\<lambda>x. if 0 \<le> f x \<and> 0 \<le> g x \<or> f x < 0 \<and> g x < 0 then \<bar>f x\<bar> * \<bar>g x\<bar> else - (\<bar>f x\<bar> * \<bar>g x\<bar>))"
   6.368      by (auto simp: fun_eq_iff)
   6.369    show ?thesis using assms unfolding *
   6.370 -    by (intro measurable_If pos_times borel_measurable_uminus_extreal)
   6.371 +    by (intro measurable_If pos_times borel_measurable_uminus_ereal)
   6.372         (auto simp: split_sets intro!: Int)
   6.373  qed
   6.374  
   6.375 -lemma (in sigma_algebra) borel_measurable_extreal_setprod[simp, intro]:
   6.376 -  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> extreal"
   6.377 +lemma (in sigma_algebra) borel_measurable_ereal_setprod[simp, intro]:
   6.378 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
   6.379    assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   6.380    shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
   6.381  proof cases
   6.382 @@ -1389,25 +1389,25 @@
   6.383    thus ?thesis using assms by induct auto
   6.384  qed simp
   6.385  
   6.386 -lemma (in sigma_algebra) borel_measurable_extreal_min[simp, intro]:
   6.387 -  fixes f g :: "'a \<Rightarrow> extreal"
   6.388 +lemma (in sigma_algebra) borel_measurable_ereal_min[simp, intro]:
   6.389 +  fixes f g :: "'a \<Rightarrow> ereal"
   6.390    assumes "f \<in> borel_measurable M"
   6.391    assumes "g \<in> borel_measurable M"
   6.392    shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
   6.393    using assms unfolding min_def by (auto intro!: measurable_If)
   6.394  
   6.395 -lemma (in sigma_algebra) borel_measurable_extreal_max[simp, intro]:
   6.396 -  fixes f g :: "'a \<Rightarrow> extreal"
   6.397 +lemma (in sigma_algebra) borel_measurable_ereal_max[simp, intro]:
   6.398 +  fixes f g :: "'a \<Rightarrow> ereal"
   6.399    assumes "f \<in> borel_measurable M"
   6.400    and "g \<in> borel_measurable M"
   6.401    shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
   6.402    using assms unfolding max_def by (auto intro!: measurable_If)
   6.403  
   6.404  lemma (in sigma_algebra) borel_measurable_SUP[simp, intro]:
   6.405 -  fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> extreal"
   6.406 +  fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> ereal"
   6.407    assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
   6.408    shows "(\<lambda>x. SUP i : A. f i x) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
   6.409 -  unfolding borel_measurable_extreal_iff_ge
   6.410 +  unfolding borel_measurable_ereal_iff_ge
   6.411  proof
   6.412    fix a
   6.413    have "?sup -` {a<..} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
   6.414 @@ -1417,10 +1417,10 @@
   6.415  qed
   6.416  
   6.417  lemma (in sigma_algebra) borel_measurable_INF[simp, intro]:
   6.418 -  fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> extreal"
   6.419 +  fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> ereal"
   6.420    assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
   6.421    shows "(\<lambda>x. INF i : A. f i x) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
   6.422 -  unfolding borel_measurable_extreal_iff_less
   6.423 +  unfolding borel_measurable_ereal_iff_less
   6.424  proof
   6.425    fix a
   6.426    have "?inf -` {..<a} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
   6.427 @@ -1430,30 +1430,30 @@
   6.428  qed
   6.429  
   6.430  lemma (in sigma_algebra) borel_measurable_liminf[simp, intro]:
   6.431 -  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
   6.432 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
   6.433    assumes "\<And>i. f i \<in> borel_measurable M"
   6.434    shows "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
   6.435    unfolding liminf_SUPR_INFI using assms by auto
   6.436  
   6.437  lemma (in sigma_algebra) borel_measurable_limsup[simp, intro]:
   6.438 -  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
   6.439 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
   6.440    assumes "\<And>i. f i \<in> borel_measurable M"
   6.441    shows "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
   6.442    unfolding limsup_INFI_SUPR using assms by auto
   6.443  
   6.444 -lemma (in sigma_algebra) borel_measurable_extreal_diff[simp, intro]:
   6.445 -  fixes f g :: "'a \<Rightarrow> extreal"
   6.446 +lemma (in sigma_algebra) borel_measurable_ereal_diff[simp, intro]:
   6.447 +  fixes f g :: "'a \<Rightarrow> ereal"
   6.448    assumes "f \<in> borel_measurable M"
   6.449    assumes "g \<in> borel_measurable M"
   6.450    shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   6.451 -  unfolding minus_extreal_def using assms by auto
   6.452 +  unfolding minus_ereal_def using assms by auto
   6.453  
   6.454  lemma (in sigma_algebra) borel_measurable_psuminf[simp, intro]:
   6.455 -  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
   6.456 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
   6.457    assumes "\<And>i. f i \<in> borel_measurable M" and pos: "\<And>i x. x \<in> space M \<Longrightarrow> 0 \<le> f i x"
   6.458    shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
   6.459    apply (subst measurable_cong)
   6.460 -  apply (subst suminf_extreal_eq_SUPR)
   6.461 +  apply (subst suminf_ereal_eq_SUPR)
   6.462    apply (rule pos)
   6.463    using assms by auto
   6.464  
   6.465 @@ -1465,11 +1465,11 @@
   6.466    and u: "\<And>i. u i \<in> borel_measurable M"
   6.467    shows "u' \<in> borel_measurable M"
   6.468  proof -
   6.469 -  have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. extreal (u n x)) = extreal (u' x)"
   6.470 -    using u' by (simp add: lim_imp_Liminf trivial_limit_sequentially lim_extreal)
   6.471 -  moreover from u have "(\<lambda>x. liminf (\<lambda>n. extreal (u n x))) \<in> borel_measurable M"
   6.472 +  have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. ereal (u n x)) = ereal (u' x)"
   6.473 +    using u' by (simp add: lim_imp_Liminf trivial_limit_sequentially lim_ereal)
   6.474 +  moreover from u have "(\<lambda>x. liminf (\<lambda>n. ereal (u n x))) \<in> borel_measurable M"
   6.475      by auto
   6.476 -  ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_extreal_iff)
   6.477 +  ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_ereal_iff)
   6.478  qed
   6.479  
   6.480  end
     7.1 --- a/src/HOL/Probability/Caratheodory.thy	Tue Jul 19 14:35:44 2011 +0200
     7.2 +++ b/src/HOL/Probability/Caratheodory.thy	Tue Jul 19 14:36:12 2011 +0200
     7.3 @@ -19,8 +19,8 @@
     7.4    Originally from the Hurd/Coble measure theory development, translated by Lawrence Paulson.
     7.5  *}
     7.6  
     7.7 -lemma suminf_extreal_2dimen:
     7.8 -  fixes f:: "nat \<times> nat \<Rightarrow> extreal"
     7.9 +lemma suminf_ereal_2dimen:
    7.10 +  fixes f:: "nat \<times> nat \<Rightarrow> ereal"
    7.11    assumes pos: "\<And>p. 0 \<le> f p"
    7.12    assumes "\<And>m. g m = (\<Sum>n. f (m,n))"
    7.13    shows "(\<Sum>i. f (prod_decode i)) = suminf g"
    7.14 @@ -47,21 +47,21 @@
    7.15    ultimately
    7.16    show ?thesis unfolding g_def using pos
    7.17      by (auto intro!: SUPR_eq  simp: setsum_cartesian_product reindex le_SUPI2
    7.18 -                     setsum_nonneg suminf_extreal_eq_SUPR SUPR_pair
    7.19 -                     SUPR_extreal_setsum[symmetric] incseq_setsumI setsum_nonneg)
    7.20 +                     setsum_nonneg suminf_ereal_eq_SUPR SUPR_pair
    7.21 +                     SUPR_ereal_setsum[symmetric] incseq_setsumI setsum_nonneg)
    7.22  qed
    7.23  
    7.24  subsection {* Measure Spaces *}
    7.25  
    7.26  record 'a measure_space = "'a algebra" +
    7.27 -  measure :: "'a set \<Rightarrow> extreal"
    7.28 +  measure :: "'a set \<Rightarrow> ereal"
    7.29  
    7.30 -definition positive where "positive M f \<longleftrightarrow> f {} = (0::extreal) \<and> (\<forall>A\<in>sets M. 0 \<le> f A)"
    7.31 +definition positive where "positive M f \<longleftrightarrow> f {} = (0::ereal) \<and> (\<forall>A\<in>sets M. 0 \<le> f A)"
    7.32  
    7.33  definition additive where "additive M f \<longleftrightarrow>
    7.34    (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<inter> y = {} \<longrightarrow> f (x \<union> y) = f x + f y)"
    7.35  
    7.36 -definition countably_additive :: "('a, 'b) algebra_scheme \<Rightarrow> ('a set \<Rightarrow> extreal) \<Rightarrow> bool" where
    7.37 +definition countably_additive :: "('a, 'b) algebra_scheme \<Rightarrow> ('a set \<Rightarrow> ereal) \<Rightarrow> bool" where
    7.38    "countably_additive M f \<longleftrightarrow> (\<forall>A. range A \<subseteq> sets M \<longrightarrow> disjoint_family A \<longrightarrow> (\<Union>i. A i) \<in> sets M \<longrightarrow>
    7.39      (\<Sum>i. f (A i)) = f (\<Union>i. A i))"
    7.40  
    7.41 @@ -168,7 +168,7 @@
    7.42    by (simp add: lambda_system_def)
    7.43  
    7.44  lemma (in algebra) lambda_system_Compl:
    7.45 -  fixes f:: "'a set \<Rightarrow> extreal"
    7.46 +  fixes f:: "'a set \<Rightarrow> ereal"
    7.47    assumes x: "x \<in> lambda_system M f"
    7.48    shows "space M - x \<in> lambda_system M f"
    7.49  proof -
    7.50 @@ -181,7 +181,7 @@
    7.51  qed
    7.52  
    7.53  lemma (in algebra) lambda_system_Int:
    7.54 -  fixes f:: "'a set \<Rightarrow> extreal"
    7.55 +  fixes f:: "'a set \<Rightarrow> ereal"
    7.56    assumes xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
    7.57    shows "x \<inter> y \<in> lambda_system M f"
    7.58  proof -
    7.59 @@ -215,7 +215,7 @@
    7.60  qed
    7.61  
    7.62  lemma (in algebra) lambda_system_Un:
    7.63 -  fixes f:: "'a set \<Rightarrow> extreal"
    7.64 +  fixes f:: "'a set \<Rightarrow> ereal"
    7.65    assumes xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
    7.66    shows "x \<union> y \<in> lambda_system M f"
    7.67  proof -
    7.68 @@ -321,7 +321,7 @@
    7.69  qed
    7.70  
    7.71  lemma (in algebra) increasing_additive_bound:
    7.72 -  fixes A:: "nat \<Rightarrow> 'a set" and  f :: "'a set \<Rightarrow> extreal"
    7.73 +  fixes A:: "nat \<Rightarrow> 'a set" and  f :: "'a set \<Rightarrow> ereal"
    7.74    assumes f: "positive M f" and ad: "additive M f"
    7.75        and inc: "increasing M f"
    7.76        and A: "range A \<subseteq> sets M"
    7.77 @@ -346,7 +346,7 @@
    7.78    by (simp add: positive_def lambda_system_def)
    7.79  
    7.80  lemma (in algebra) lambda_system_strong_sum:
    7.81 -  fixes A:: "nat \<Rightarrow> 'a set" and f :: "'a set \<Rightarrow> extreal"
    7.82 +  fixes A:: "nat \<Rightarrow> 'a set" and f :: "'a set \<Rightarrow> ereal"
    7.83    assumes f: "positive M f" and a: "a \<in> sets M"
    7.84        and A: "range A \<subseteq> lambda_system M f"
    7.85        and disj: "disjoint_family A"
    7.86 @@ -537,7 +537,7 @@
    7.87    assumes posf: "positive M f" and ca: "countably_additive M f"
    7.88        and s: "s \<in> sets M"
    7.89    shows "Inf (measure_set M f s) = f s"
    7.90 -  unfolding Inf_extreal_def
    7.91 +  unfolding Inf_ereal_def
    7.92  proof (safe intro!: Greatest_equality)
    7.93    fix z
    7.94    assume z: "z \<in> measure_set M f s"
    7.95 @@ -648,7 +648,7 @@
    7.96  qed
    7.97  
    7.98  lemma (in ring_of_sets) inf_measure_close:
    7.99 -  fixes e :: extreal
   7.100 +  fixes e :: ereal
   7.101    assumes posf: "positive M f" and e: "0 < e" and ss: "s \<subseteq> (space M)" and "Inf (measure_set M f s) \<noteq> \<infinity>"
   7.102    shows "\<exists>A. range A \<subseteq> sets M \<and> disjoint_family A \<and> s \<subseteq> (\<Union>i. A i) \<and>
   7.103                 (\<Sum>i. f (A i)) \<le> Inf (measure_set M f s) + e"
   7.104 @@ -656,7 +656,7 @@