merged
authorhoelzl
Mon, 14 Mar 2011 15:29:10 +0100
changeset 41982 96cbc6379e5a
parent 41968 7f5c9bd991be (current diff)
parent 41981 cdf7693bbe08 (diff)
child 41983 2dc6e382a58b
merged
src/HOL/Probability/Positive_Extended_Real.thy
--- a/src/HOL/Complete_Lattice.thy	Mon Mar 14 15:17:10 2011 +0100
+++ b/src/HOL/Complete_Lattice.thy	Mon Mar 14 15:29:10 2011 +0100
@@ -89,25 +89,45 @@
   by (auto intro: Sup_least dest: Sup_upper)
 
 lemma Inf_mono:
-  assumes "\<And>b. b \<in> B \<Longrightarrow> \<exists>a\<in>A. a \<le> b"
-  shows "Inf A \<le> Inf B"
+  assumes "\<And>b. b \<in> B \<Longrightarrow> \<exists>a\<in>A. a \<sqsubseteq> b"
+  shows "Inf A \<sqsubseteq> Inf B"
 proof (rule Inf_greatest)
   fix b assume "b \<in> B"
-  with assms obtain a where "a \<in> A" and "a \<le> b" by blast
-  from `a \<in> A` have "Inf A \<le> a" by (rule Inf_lower)
-  with `a \<le> b` show "Inf A \<le> b" by auto
+  with assms obtain a where "a \<in> A" and "a \<sqsubseteq> b" by blast
+  from `a \<in> A` have "Inf A \<sqsubseteq> a" by (rule Inf_lower)
+  with `a \<sqsubseteq> b` show "Inf A \<sqsubseteq> b" by auto
 qed
 
 lemma Sup_mono:
-  assumes "\<And>a. a \<in> A \<Longrightarrow> \<exists>b\<in>B. a \<le> b"
-  shows "Sup A \<le> Sup B"
+  assumes "\<And>a. a \<in> A \<Longrightarrow> \<exists>b\<in>B. a \<sqsubseteq> b"
+  shows "Sup A \<sqsubseteq> Sup B"
 proof (rule Sup_least)
   fix a assume "a \<in> A"
-  with assms obtain b where "b \<in> B" and "a \<le> b" by blast
-  from `b \<in> B` have "b \<le> Sup B" by (rule Sup_upper)
-  with `a \<le> b` show "a \<le> Sup B" by auto
+  with assms obtain b where "b \<in> B" and "a \<sqsubseteq> b" by blast
+  from `b \<in> B` have "b \<sqsubseteq> Sup B" by (rule Sup_upper)
+  with `a \<sqsubseteq> b` show "a \<sqsubseteq> Sup B" by auto
 qed
 
+lemma top_le:
+  "top \<sqsubseteq> x \<Longrightarrow> x = top"
+  by (rule antisym) auto
+
+lemma le_bot:
+  "x \<sqsubseteq> bot \<Longrightarrow> x = bot"
+  by (rule antisym) auto
+
+lemma not_less_bot[simp]: "\<not> (x \<sqsubset> bot)"
+  using bot_least[of x] by (auto simp: le_less)
+
+lemma not_top_less[simp]: "\<not> (top \<sqsubset> x)"
+  using top_greatest[of x] by (auto simp: le_less)
+
+lemma Sup_upper2: "u \<in> A \<Longrightarrow> v \<sqsubseteq> u \<Longrightarrow> v \<sqsubseteq> Sup A"
+  using Sup_upper[of u A] by auto
+
+lemma Inf_lower2: "u \<in> A \<Longrightarrow> u \<sqsubseteq> v \<Longrightarrow> Inf A \<sqsubseteq> v"
+  using Inf_lower[of u A] by auto
+
 definition INFI :: "'b set \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'a" where
   "INFI A f = \<Sqinter> (f ` A)"
 
@@ -146,15 +166,27 @@
 context complete_lattice
 begin
 
+lemma SUP_cong: "(\<And>x. x \<in> A \<Longrightarrow> f x = g x) \<Longrightarrow> SUPR A f = SUPR A g"
+  by (simp add: SUPR_def cong: image_cong)
+
+lemma INF_cong: "(\<And>x. x \<in> A \<Longrightarrow> f x = g x) \<Longrightarrow> INFI A f = INFI A g"
+  by (simp add: INFI_def cong: image_cong)
+
 lemma le_SUPI: "i : A \<Longrightarrow> M i \<sqsubseteq> (SUP i:A. M i)"
   by (auto simp add: SUPR_def intro: Sup_upper)
 
+lemma le_SUPI2: "i \<in> A \<Longrightarrow> u \<sqsubseteq> M i \<Longrightarrow> u \<sqsubseteq> (SUP i:A. M i)"
+  using le_SUPI[of i A M] by auto
+
 lemma SUP_leI: "(\<And>i. i : A \<Longrightarrow> M i \<sqsubseteq> u) \<Longrightarrow> (SUP i:A. M i) \<sqsubseteq> u"
   by (auto simp add: SUPR_def intro: Sup_least)
 
 lemma INF_leI: "i : A \<Longrightarrow> (INF i:A. M i) \<sqsubseteq> M i"
   by (auto simp add: INFI_def intro: Inf_lower)
 
+lemma INF_leI2: "i \<in> A \<Longrightarrow> M i \<sqsubseteq> u \<Longrightarrow> (INF i:A. M i) \<sqsubseteq> u"
+  using INF_leI[of i A M] by auto
+
 lemma le_INFI: "(\<And>i. i : A \<Longrightarrow> u \<sqsubseteq> M i) \<Longrightarrow> u \<sqsubseteq> (INF i:A. M i)"
   by (auto simp add: INFI_def intro: Inf_greatest)
 
--- a/src/HOL/IsaMakefile	Mon Mar 14 15:17:10 2011 +0100
+++ b/src/HOL/IsaMakefile	Mon Mar 14 15:29:10 2011 +0100
@@ -437,10 +437,10 @@
   Library/ContNotDenum.thy Library/Continuity.thy Library/Convex.thy	\
   Library/Countable.thy Library/Diagonalize.thy Library/Dlist.thy	\
   Library/Efficient_Nat.thy Library/Eval_Witness.thy 			\
-  Library/Executable_Set.thy Library/Float.thy				\
-  Library/Formal_Power_Series.thy Library/Fraction_Field.thy		\
-  Library/FrechetDeriv.thy Library/Cset.thy Library/FuncSet.thy		\
-  Library/Function_Algebras.thy						\
+  Library/Executable_Set.thy Library/Extended_Reals.thy			\
+  Library/Float.thy Library/Formal_Power_Series.thy			\
+  Library/Fraction_Field.thy Library/FrechetDeriv.thy Library/Cset.thy	\
+  Library/FuncSet.thy Library/Function_Algebras.thy			\
   Library/Fundamental_Theorem_Algebra.thy Library/Glbs.thy		\
   Library/Indicator_Function.thy Library/Infinite_Set.thy		\
   Library/Inner_Product.thy Library/Kleene_Algebra.thy			\
@@ -619,7 +619,7 @@
   Number_Theory/UniqueFactorization.thy  \
   Number_Theory/ROOT.ML
 	@$(ISABELLE_TOOL) usedir -g true $(OUT)/HOL Number_Theory
-                                     
+
 
 ## HOL-Old_Number_Theory
 
@@ -1154,6 +1154,7 @@
   Multivariate_Analysis/Derivative.thy					\
   Multivariate_Analysis/Determinants.thy				\
   Multivariate_Analysis/Euclidean_Space.thy				\
+  Multivariate_Analysis/Extended_Real_Limits.thy			\
   Multivariate_Analysis/Fashoda.thy					\
   Multivariate_Analysis/Finite_Cartesian_Product.thy			\
   Multivariate_Analysis/Integration.certs				\
@@ -1167,9 +1168,10 @@
   Multivariate_Analysis/Topology_Euclidean_Space.thy			\
   Multivariate_Analysis/document/root.tex				\
   Multivariate_Analysis/normarith.ML Library/Glbs.thy			\
-  Library/Indicator_Function.thy Library/Inner_Product.thy		\
-  Library/Numeral_Type.thy Library/Convex.thy Library/FrechetDeriv.thy	\
-  Library/Product_Vector.thy Library/Product_plus.thy
+  Library/Extended_Reals.thy Library/Indicator_Function.thy		\
+  Library/Inner_Product.thy Library/Numeral_Type.thy Library/Convex.thy	\
+  Library/FrechetDeriv.thy Library/Product_Vector.thy			\
+  Library/Product_plus.thy
 	@cd Multivariate_Analysis; $(ISABELLE_TOOL) usedir -b -g true $(OUT)/HOL HOL-Multivariate_Analysis
 
 
@@ -1184,7 +1186,6 @@
   Probability/ex/Koepf_Duermuth_Countermeasure.thy			\
   Probability/Information.thy Probability/Lebesgue_Integration.thy	\
   Probability/Lebesgue_Measure.thy Probability/Measure.thy		\
-  Probability/Positive_Extended_Real.thy				\
   Probability/Probability_Space.thy Probability/Probability.thy		\
   Probability/Product_Measure.thy Probability/Radon_Nikodym.thy		\
   Probability/ROOT.ML Probability/Sigma_Algebra.thy			\
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Library/Extended_Reals.thy	Mon Mar 14 15:29:10 2011 +0100
@@ -0,0 +1,2438 @@
+(* Title: src/HOL/Library/Extended_Reals.thy
+   Author: Johannes Hölzl; TU München
+   Author: Robert Himmelmann; TU München
+   Author: Armin Heller; TU München
+   Author: Bogdan Grechuk; University of Edinburgh *)
+
+header {* Extended real number line *}
+
+theory Extended_Reals
+  imports Complex_Main
+begin
+
+text {*
+
+For more lemmas about the extended real numbers go to
+  @{text "src/HOL/Multivaraite_Analysis/Extended_Real_Limits.thy"}
+
+*}
+
+lemma (in complete_lattice) atLeast_eq_UNIV_iff: "{x..} = UNIV \<longleftrightarrow> x = bot"
+proof
+  assume "{x..} = UNIV"
+  show "x = bot"
+  proof (rule ccontr)
+    assume "x \<noteq> bot" then have "bot \<notin> {x..}" by (simp add: le_less)
+    then show False using `{x..} = UNIV` by simp
+  qed
+qed auto
+
+lemma SUPR_pair:
+  "(SUP i : A. SUP j : B. f i j) = (SUP p : A \<times> B. f (fst p) (snd p))"
+  by (rule antisym) (auto intro!: SUP_leI le_SUPI2)
+
+lemma INFI_pair:
+  "(INF i : A. INF j : B. f i j) = (INF p : A \<times> B. f (fst p) (snd p))"
+  by (rule antisym) (auto intro!: le_INFI INF_leI2)
+
+subsection {* Definition and basic properties *}
+
+datatype extreal = extreal real | PInfty | MInfty
+
+notation (xsymbols)
+  PInfty  ("\<infinity>")
+
+notation (HTML output)
+  PInfty  ("\<infinity>")
+
+declare [[coercion "extreal :: real \<Rightarrow> extreal"]]
+
+instantiation extreal :: uminus
+begin
+  fun uminus_extreal where
+    "- (extreal r) = extreal (- r)"
+  | "- \<infinity> = MInfty"
+  | "- MInfty = \<infinity>"
+  instance ..
+end
+
+lemma inj_extreal[simp]: "inj_on extreal A"
+  unfolding inj_on_def by auto
+
+lemma MInfty_neq_PInfty[simp]:
+  "\<infinity> \<noteq> - \<infinity>" "- \<infinity> \<noteq> \<infinity>" by simp_all
+
+lemma MInfty_neq_extreal[simp]:
+  "extreal r \<noteq> - \<infinity>" "- \<infinity> \<noteq> extreal r" by simp_all
+
+lemma MInfinity_cases[simp]:
+  "(case - \<infinity> of extreal r \<Rightarrow> f r | \<infinity> \<Rightarrow> y | MInfinity \<Rightarrow> z) = z"
+  by simp
+
+lemma extreal_uminus_uminus[simp]:
+  fixes a :: extreal shows "- (- a) = a"
+  by (cases a) simp_all
+
+lemma MInfty_eq[simp]:
+  "MInfty = - \<infinity>" by simp
+
+declare uminus_extreal.simps(2)[simp del]
+
+lemma extreal_cases[case_names real PInf MInf, cases type: extreal]:
+  assumes "\<And>r. x = extreal r \<Longrightarrow> P"
+  assumes "x = \<infinity> \<Longrightarrow> P"
+  assumes "x = -\<infinity> \<Longrightarrow> P"
+  shows P
+  using assms by (cases x) auto
+
+lemmas extreal2_cases = extreal_cases[case_product extreal_cases]
+lemmas extreal3_cases = extreal2_cases[case_product extreal_cases]
+
+lemma extreal_uminus_eq_iff[simp]:
+  fixes a b :: extreal shows "-a = -b \<longleftrightarrow> a = b"
+  by (cases rule: extreal2_cases[of a b]) simp_all
+
+function of_extreal :: "extreal \<Rightarrow> real" where
+"of_extreal (extreal r) = r" |
+"of_extreal \<infinity> = 0" |
+"of_extreal (-\<infinity>) = 0"
+  by (auto intro: extreal_cases)
+termination proof qed (rule wf_empty)
+
+defs (overloaded)
+  real_of_extreal_def [code_unfold]: "real \<equiv> of_extreal"
+
+lemma real_of_extreal[simp]:
+    "real (- x :: extreal) = - (real x)"
+    "real (extreal r) = r"
+    "real \<infinity> = 0"
+  by (cases x) (simp_all add: real_of_extreal_def)
+
+lemma range_extreal[simp]: "range extreal = UNIV - {\<infinity>, -\<infinity>}"
+proof safe
+  fix x assume "x \<notin> range extreal" "x \<noteq> \<infinity>"
+  then show "x = -\<infinity>" by (cases x) auto
+qed auto
+
+lemma extreal_range_uminus[simp]: "range uminus = (UNIV::extreal set)"
+proof safe
+  fix x :: extreal show "x \<in> range uminus" by (intro image_eqI[of _ _ "-x"]) auto
+qed auto
+
+instantiation extreal :: number
+begin
+definition [simp]: "number_of x = extreal (number_of x)"
+instance proof qed
+end
+
+instantiation extreal :: abs
+begin
+  function abs_extreal where
+    "\<bar>extreal r\<bar> = extreal \<bar>r\<bar>"
+  | "\<bar>-\<infinity>\<bar> = \<infinity>"
+  | "\<bar>\<infinity>\<bar> = \<infinity>"
+  by (auto intro: extreal_cases)
+  termination proof qed (rule wf_empty)
+  instance ..
+end
+
+lemma abs_eq_infinity_cases[elim!]: "\<lbrakk> \<bar>x\<bar> = \<infinity> ; x = \<infinity> \<Longrightarrow> P ; x = -\<infinity> \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
+  by (cases x) auto
+
+lemma abs_neq_infinity_cases[elim!]: "\<lbrakk> \<bar>x\<bar> \<noteq> \<infinity> ; \<And>r. x = extreal r \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
+  by (cases x) auto
+
+lemma abs_extreal_uminus[simp]: "\<bar>- x\<bar> = \<bar>x::extreal\<bar>"
+  by (cases x) auto
+
+subsubsection "Addition"
+
+instantiation extreal :: comm_monoid_add
+begin
+
+definition "0 = extreal 0"
+
+function plus_extreal where
+"extreal r + extreal p = extreal (r + p)" |
+"\<infinity> + a = \<infinity>" |
+"a + \<infinity> = \<infinity>" |
+"extreal r + -\<infinity> = - \<infinity>" |
+"-\<infinity> + extreal p = -\<infinity>" |
+"-\<infinity> + -\<infinity> = -\<infinity>"
+proof -
+  case (goal1 P x)
+  moreover then obtain a b where "x = (a, b)" by (cases x) auto
+  ultimately show P
+   by (cases rule: extreal2_cases[of a b]) auto
+qed auto
+termination proof qed (rule wf_empty)
+
+lemma Infty_neq_0[simp]:
+  "\<infinity> \<noteq> 0" "0 \<noteq> \<infinity>"
+  "-\<infinity> \<noteq> 0" "0 \<noteq> -\<infinity>"
+  by (simp_all add: zero_extreal_def)
+
+lemma extreal_eq_0[simp]:
+  "extreal r = 0 \<longleftrightarrow> r = 0"
+  "0 = extreal r \<longleftrightarrow> r = 0"
+  unfolding zero_extreal_def by simp_all
+
+instance
+proof
+  fix a :: extreal show "0 + a = a"
+    by (cases a) (simp_all add: zero_extreal_def)
+  fix b :: extreal show "a + b = b + a"
+    by (cases rule: extreal2_cases[of a b]) simp_all
+  fix c :: extreal show "a + b + c = a + (b + c)"
+    by (cases rule: extreal3_cases[of a b c]) simp_all
+qed
+end
+
+lemma abs_extreal_zero[simp]: "\<bar>0\<bar> = (0::extreal)"
+  unfolding zero_extreal_def abs_extreal.simps by simp
+
+lemma extreal_uminus_zero[simp]:
+  "- 0 = (0::extreal)"
+  by (simp add: zero_extreal_def)
+
+lemma extreal_uminus_zero_iff[simp]:
+  fixes a :: extreal shows "-a = 0 \<longleftrightarrow> a = 0"
+  by (cases a) simp_all
+
+lemma extreal_plus_eq_PInfty[simp]:
+  shows "a + b = \<infinity> \<longleftrightarrow> a = \<infinity> \<or> b = \<infinity>"
+  by (cases rule: extreal2_cases[of a b]) auto
+
+lemma extreal_plus_eq_MInfty[simp]:
+  shows "a + b = -\<infinity> \<longleftrightarrow>
+    (a = -\<infinity> \<or> b = -\<infinity>) \<and> a \<noteq> \<infinity> \<and> b \<noteq> \<infinity>"
+  by (cases rule: extreal2_cases[of a b]) auto
+
+lemma extreal_add_cancel_left:
+  assumes "a \<noteq> -\<infinity>"
+  shows "a + b = a + c \<longleftrightarrow> (a = \<infinity> \<or> b = c)"
+  using assms by (cases rule: extreal3_cases[of a b c]) auto
+
+lemma extreal_add_cancel_right:
+  assumes "a \<noteq> -\<infinity>"
+  shows "b + a = c + a \<longleftrightarrow> (a = \<infinity> \<or> b = c)"
+  using assms by (cases rule: extreal3_cases[of a b c]) auto
+
+lemma extreal_real:
+  "extreal (real x) = (if \<bar>x\<bar> = \<infinity> then 0 else x)"
+  by (cases x) simp_all
+
+lemma real_of_extreal_add:
+  fixes a b :: extreal
+  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)"
+  by (cases rule: extreal2_cases[of a b]) auto
+
+subsubsection "Linear order on @{typ extreal}"
+
+instantiation extreal :: linorder
+begin
+
+function less_extreal where
+"extreal x < extreal y \<longleftrightarrow> x < y" |
+"        \<infinity> < a         \<longleftrightarrow> False" |
+"        a < -\<infinity>        \<longleftrightarrow> False" |
+"extreal x < \<infinity>         \<longleftrightarrow> True" |
+"       -\<infinity> < extreal r \<longleftrightarrow> True" |
+"       -\<infinity> < \<infinity>         \<longleftrightarrow> True"
+proof -
+  case (goal1 P x)
+  moreover then obtain a b where "x = (a,b)" by (cases x) auto
+  ultimately show P by (cases rule: extreal2_cases[of a b]) auto
+qed simp_all
+termination by (relation "{}") simp
+
+definition "x \<le> (y::extreal) \<longleftrightarrow> x < y \<or> x = y"
+
+lemma extreal_infty_less[simp]:
+  "x < \<infinity> \<longleftrightarrow> (x \<noteq> \<infinity>)"
+  "-\<infinity> < x \<longleftrightarrow> (x \<noteq> -\<infinity>)"
+  by (cases x, simp_all) (cases x, simp_all)
+
+lemma extreal_infty_less_eq[simp]:
+  "\<infinity> \<le> x \<longleftrightarrow> x = \<infinity>"
+  "x \<le> -\<infinity> \<longleftrightarrow> x = -\<infinity>"
+  by (auto simp add: less_eq_extreal_def)
+
+lemma extreal_less[simp]:
+  "extreal r < 0 \<longleftrightarrow> (r < 0)"
+  "0 < extreal r \<longleftrightarrow> (0 < r)"
+  "0 < \<infinity>"
+  "-\<infinity> < 0"
+  by (simp_all add: zero_extreal_def)
+
+lemma extreal_less_eq[simp]:
+  "x \<le> \<infinity>"
+  "-\<infinity> \<le> x"
+  "extreal r \<le> extreal p \<longleftrightarrow> r \<le> p"
+  "extreal r \<le> 0 \<longleftrightarrow> r \<le> 0"
+  "0 \<le> extreal r \<longleftrightarrow> 0 \<le> r"
+  by (auto simp add: less_eq_extreal_def zero_extreal_def)
+
+lemma extreal_infty_less_eq2:
+  "a \<le> b \<Longrightarrow> a = \<infinity> \<Longrightarrow> b = \<infinity>"
+  "a \<le> b \<Longrightarrow> b = -\<infinity> \<Longrightarrow> a = -\<infinity>"
+  by simp_all
+
+instance
+proof
+  fix x :: extreal show "x \<le> x"
+    by (cases x) simp_all
+  fix y :: extreal show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
+    by (cases rule: extreal2_cases[of x y]) auto
+  show "x \<le> y \<or> y \<le> x "
+    by (cases rule: extreal2_cases[of x y]) auto
+  { assume "x \<le> y" "y \<le> x" then show "x = y"
+    by (cases rule: extreal2_cases[of x y]) auto }
+  { fix z assume "x \<le> y" "y \<le> z" then show "x \<le> z"
+    by (cases rule: extreal3_cases[of x y z]) auto }
+qed
+end
+
+instance extreal :: ordered_ab_semigroup_add
+proof
+  fix a b c :: extreal assume "a \<le> b" then show "c + a \<le> c + b"
+    by (cases rule: extreal3_cases[of a b c]) auto
+qed
+
+lemma extreal_MInfty_lessI[intro, simp]:
+  "a \<noteq> -\<infinity> \<Longrightarrow> -\<infinity> < a"
+  by (cases a) auto
+
+lemma extreal_less_PInfty[intro, simp]:
+  "a \<noteq> \<infinity> \<Longrightarrow> a < \<infinity>"
+  by (cases a) auto
+
+lemma extreal_less_extreal_Ex:
+  fixes a b :: extreal
+  shows "x < extreal r \<longleftrightarrow> x = -\<infinity> \<or> (\<exists>p. p < r \<and> x = extreal p)"
+  by (cases x) auto
+
+lemma less_PInf_Ex_of_nat: "x \<noteq> \<infinity> \<longleftrightarrow> (\<exists>n::nat. x < extreal (real n))"
+proof (cases x)
+  case (real r) then show ?thesis
+    using reals_Archimedean2[of r] by simp
+qed simp_all
+
+lemma extreal_add_mono:
+  fixes a b c d :: extreal assumes "a \<le> b" "c \<le> d" shows "a + c \<le> b + d"
+  using assms
+  apply (cases a)
+  apply (cases rule: extreal3_cases[of b c d], auto)
+  apply (cases rule: extreal3_cases[of b c d], auto)
+  done
+
+lemma extreal_minus_le_minus[simp]:
+  fixes a b :: extreal shows "- a \<le> - b \<longleftrightarrow> b \<le> a"
+  by (cases rule: extreal2_cases[of a b]) auto
+
+lemma extreal_minus_less_minus[simp]:
+  fixes a b :: extreal shows "- a < - b \<longleftrightarrow> b < a"
+  by (cases rule: extreal2_cases[of a b]) auto
+
+lemma extreal_le_real_iff:
+  "x \<le> real y \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> extreal x \<le> y) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> x \<le> 0))"
+  by (cases y) auto
+
+lemma real_le_extreal_iff:
+  "real y \<le> x \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> y \<le> extreal x) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> 0 \<le> x))"
+  by (cases y) auto
+
+lemma extreal_less_real_iff:
+  "x < real y \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> extreal x < y) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> x < 0))"
+  by (cases y) auto
+
+lemma real_less_extreal_iff:
+  "real y < x \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> y < extreal x) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> 0 < x))"
+  by (cases y) auto
+
+lemma real_of_extreal_positive_mono:
+  assumes "x \<noteq> \<infinity>" "y \<noteq> \<infinity>" "0 \<le> x" "x \<le> y"
+  shows "real x \<le> real y"
+  using assms by (cases rule: extreal2_cases[of x y]) auto
+
+lemma real_of_extreal_pos:
+  fixes x :: extreal shows "0 \<le> x \<Longrightarrow> 0 \<le> real x" by (cases x) auto
+
+lemmas real_of_extreal_ord_simps =
+  extreal_le_real_iff real_le_extreal_iff extreal_less_real_iff real_less_extreal_iff
+
+lemma extreal_dense:
+  fixes x y :: extreal assumes "x < y"
+  shows "EX z. x < z & z < y"
+proof -
+{ assume a: "x = (-\<infinity>)"
+  { assume "y = \<infinity>" hence ?thesis using a by (auto intro!: exI[of _ "0"]) }
+  moreover
+  { assume "y ~= \<infinity>"
+    with `x < y` obtain r where r: "y = extreal r" by (cases y) auto
+    hence ?thesis using `x < y` a by (auto intro!: exI[of _ "extreal (r - 1)"])
+  } ultimately have ?thesis by auto
+}
+moreover
+{ assume "x ~= (-\<infinity>)"
+  with `x < y` obtain p where p: "x = extreal p" by (cases x) auto
+  { assume "y = \<infinity>" hence ?thesis using `x < y` p
+       by (auto intro!: exI[of _ "extreal (p + 1)"]) }
+  moreover
+  { assume "y ~= \<infinity>"
+    with `x < y` obtain r where r: "y = extreal r" by (cases y) auto
+    with p `x < y` have "p < r" by auto
+    with dense obtain z where "p < z" "z < r" by auto
+    hence ?thesis using r p by (auto intro!: exI[of _ "extreal z"])
+  } ultimately have ?thesis by auto
+} ultimately show ?thesis by auto
+qed
+
+lemma extreal_dense2:
+  fixes x y :: extreal assumes "x < y"
+  shows "EX z. x < extreal z & extreal z < y"
+  by (metis extreal_dense[OF `x < y`] extreal_cases less_extreal.simps(2,3))
+
+lemma extreal_add_strict_mono:
+  fixes a b c d :: extreal
+  assumes "a = b" "0 \<le> a" "a \<noteq> \<infinity>" "c < d"
+  shows "a + c < b + d"
+  using assms by (cases rule: extreal3_cases[case_product extreal_cases, of a b c d]) auto
+
+lemma extreal_less_add: "\<bar>a\<bar> \<noteq> \<infinity> \<Longrightarrow> c < b \<Longrightarrow> a + c < a + b"
+  by (cases rule: extreal2_cases[of b c]) auto
+
+lemma extreal_uminus_eq_reorder: "- a = b \<longleftrightarrow> a = (-b::extreal)" by auto
+
+lemma extreal_uminus_less_reorder: "- a < b \<longleftrightarrow> -b < (a::extreal)"
+  by (subst (3) extreal_uminus_uminus[symmetric]) (simp only: extreal_minus_less_minus)
+
+lemma extreal_uminus_le_reorder: "- a \<le> b \<longleftrightarrow> -b \<le> (a::extreal)"
+  by (subst (3) extreal_uminus_uminus[symmetric]) (simp only: extreal_minus_le_minus)
+
+lemmas extreal_uminus_reorder =
+  extreal_uminus_eq_reorder extreal_uminus_less_reorder extreal_uminus_le_reorder
+
+lemma extreal_bot:
+  fixes x :: extreal assumes "\<And>B. x \<le> extreal B" shows "x = - \<infinity>"
+proof (cases x)
+  case (real r) with assms[of "r - 1"] show ?thesis by auto
+next case PInf with assms[of 0] show ?thesis by auto
+next case MInf then show ?thesis by simp
+qed
+
+lemma extreal_top:
+  fixes x :: extreal assumes "\<And>B. x \<ge> extreal B" shows "x = \<infinity>"
+proof (cases x)
+  case (real r) with assms[of "r + 1"] show ?thesis by auto
+next case MInf with assms[of 0] show ?thesis by auto
+next case PInf then show ?thesis by simp
+qed
+
+lemma
+  shows extreal_max[simp]: "extreal (max x y) = max (extreal x) (extreal y)"
+    and extreal_min[simp]: "extreal (min x y) = min (extreal x) (extreal y)"
+  by (simp_all add: min_def max_def)
+
+lemma extreal_max_0: "max 0 (extreal r) = extreal (max 0 r)"
+  by (auto simp: zero_extreal_def)
+
+lemma
+  fixes f :: "nat \<Rightarrow> extreal"
+  shows incseq_uminus[simp]: "incseq (\<lambda>x. - f x) \<longleftrightarrow> decseq f"
+  and decseq_uminus[simp]: "decseq (\<lambda>x. - f x) \<longleftrightarrow> incseq f"
+  unfolding decseq_def incseq_def by auto
+
+lemma extreal_add_nonneg_nonneg:
+  fixes a b :: extreal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a + b"
+  using add_mono[of 0 a 0 b] by simp
+
+lemma image_eqD: "f ` A = B \<Longrightarrow> (\<forall>x\<in>A. f x \<in> B)"
+  by auto
+
+lemma incseq_setsumI:
+  fixes f :: "nat \<Rightarrow> 'a::{comm_monoid_add, ordered_ab_semigroup_add}"
+  assumes "\<And>i. 0 \<le> f i"
+  shows "incseq (\<lambda>i. setsum f {..< i})"
+proof (intro incseq_SucI)
+  fix n have "setsum f {..< n} + 0 \<le> setsum f {..<n} + f n"
+    using assms by (rule add_left_mono)
+  then show "setsum f {..< n} \<le> setsum f {..< Suc n}"
+    by auto
+qed
+
+lemma incseq_setsumI2:
+  fixes f :: "'i \<Rightarrow> nat \<Rightarrow> 'a::{comm_monoid_add, ordered_ab_semigroup_add}"
+  assumes "\<And>n. n \<in> A \<Longrightarrow> incseq (f n)"
+  shows "incseq (\<lambda>i. \<Sum>n\<in>A. f n i)"
+  using assms unfolding incseq_def by (auto intro: setsum_mono)
+
+subsubsection "Multiplication"
+
+instantiation extreal :: "{comm_monoid_mult, sgn}"
+begin
+
+definition "1 = extreal 1"
+
+function sgn_extreal where
+  "sgn (extreal r) = extreal (sgn r)"
+| "sgn \<infinity> = 1"
+| "sgn (-\<infinity>) = -1"
+by (auto intro: extreal_cases)
+termination proof qed (rule wf_empty)
+
+function times_extreal where
+"extreal r * extreal p = extreal (r * p)" |
+"extreal r * \<infinity> = (if r = 0 then 0 else if r > 0 then \<infinity> else -\<infinity>)" |
+"\<infinity> * extreal r = (if r = 0 then 0 else if r > 0 then \<infinity> else -\<infinity>)" |
+"extreal r * -\<infinity> = (if r = 0 then 0 else if r > 0 then -\<infinity> else \<infinity>)" |
+"-\<infinity> * extreal r = (if r = 0 then 0 else if r > 0 then -\<infinity> else \<infinity>)" |
+"\<infinity> * \<infinity> = \<infinity>" |
+"-\<infinity> * \<infinity> = -\<infinity>" |
+"\<infinity> * -\<infinity> = -\<infinity>" |
+"-\<infinity> * -\<infinity> = \<infinity>"
+proof -
+  case (goal1 P x)
+  moreover then obtain a b where "x = (a, b)" by (cases x) auto
+  ultimately show P by (cases rule: extreal2_cases[of a b]) auto
+qed simp_all
+termination by (relation "{}") simp
+
+instance
+proof
+  fix a :: extreal show "1 * a = a"
+    by (cases a) (simp_all add: one_extreal_def)
+  fix b :: extreal show "a * b = b * a"
+    by (cases rule: extreal2_cases[of a b]) simp_all
+  fix c :: extreal show "a * b * c = a * (b * c)"
+    by (cases rule: extreal3_cases[of a b c])
+       (simp_all add: zero_extreal_def zero_less_mult_iff)
+qed
+end
+
+lemma abs_extreal_one[simp]: "\<bar>1\<bar> = (1::extreal)"
+  unfolding one_extreal_def by simp
+
+lemma extreal_mult_zero[simp]:
+  fixes a :: extreal shows "a * 0 = 0"
+  by (cases a) (simp_all add: zero_extreal_def)
+
+lemma extreal_zero_mult[simp]:
+  fixes a :: extreal shows "0 * a = 0"
+  by (cases a) (simp_all add: zero_extreal_def)
+
+lemma extreal_m1_less_0[simp]:
+  "-(1::extreal) < 0"
+  by (simp add: zero_extreal_def one_extreal_def)
+
+lemma extreal_zero_m1[simp]:
+  "1 \<noteq> (0::extreal)"
+  by (simp add: zero_extreal_def one_extreal_def)
+
+lemma extreal_times_0[simp]:
+  fixes x :: extreal shows "0 * x = 0"
+  by (cases x) (auto simp: zero_extreal_def)
+
+lemma extreal_times[simp]:
+  "1 \<noteq> \<infinity>" "\<infinity> \<noteq> 1"
+  "1 \<noteq> -\<infinity>" "-\<infinity> \<noteq> 1"
+  by (auto simp add: times_extreal_def one_extreal_def)
+
+lemma extreal_plus_1[simp]:
+  "1 + extreal r = extreal (r + 1)" "extreal r + 1 = extreal (r + 1)"
+  "1 + -\<infinity> = -\<infinity>" "-\<infinity> + 1 = -\<infinity>"
+  unfolding one_extreal_def by auto
+
+lemma extreal_zero_times[simp]:
+  fixes a b :: extreal shows "a * b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
+  by (cases rule: extreal2_cases[of a b]) auto
+
+lemma extreal_mult_eq_PInfty[simp]:
+  shows "a * b = \<infinity> \<longleftrightarrow>
+    (a = \<infinity> \<and> b > 0) \<or> (a > 0 \<and> b = \<infinity>) \<or> (a = -\<infinity> \<and> b < 0) \<or> (a < 0 \<and> b = -\<infinity>)"
+  by (cases rule: extreal2_cases[of a b]) auto
+
+lemma extreal_mult_eq_MInfty[simp]:
+  shows "a * b = -\<infinity> \<longleftrightarrow>
+    (a = \<infinity> \<and> b < 0) \<or> (a < 0 \<and> b = \<infinity>) \<or> (a = -\<infinity> \<and> b > 0) \<or> (a > 0 \<and> b = -\<infinity>)"
+  by (cases rule: extreal2_cases[of a b]) auto
+
+lemma extreal_0_less_1[simp]: "0 < (1::extreal)"
+  by (simp_all add: zero_extreal_def one_extreal_def)
+
+lemma extreal_zero_one[simp]: "0 \<noteq> (1::extreal)"
+  by (simp_all add: zero_extreal_def one_extreal_def)
+
+lemma extreal_mult_minus_left[simp]:
+  fixes a b :: extreal shows "-a * b = - (a * b)"
+  by (cases rule: extreal2_cases[of a b]) auto
+
+lemma extreal_mult_minus_right[simp]:
+  fixes a b :: extreal shows "a * -b = - (a * b)"
+  by (cases rule: extreal2_cases[of a b]) auto
+
+lemma extreal_mult_infty[simp]:
+  "a * \<infinity> = (if a = 0 then 0 else if 0 < a then \<infinity> else - \<infinity>)"
+  by (cases a) auto
+
+lemma extreal_infty_mult[simp]:
+  "\<infinity> * a = (if a = 0 then 0 else if 0 < a then \<infinity> else - \<infinity>)"
+  by (cases a) auto
+
+lemma extreal_mult_strict_right_mono:
+  assumes "a < b" and "0 < c" "c < \<infinity>"
+  shows "a * c < b * c"
+  using assms
+  by (cases rule: extreal3_cases[of a b c])
+     (auto simp: zero_le_mult_iff extreal_less_PInfty)
+
+lemma extreal_mult_strict_left_mono:
+  "\<lbrakk> a < b ; 0 < c ; c < \<infinity>\<rbrakk> \<Longrightarrow> c * a < c * b"
+  using extreal_mult_strict_right_mono by (simp add: mult_commute[of c])
+
+lemma extreal_mult_right_mono:
+  fixes a b c :: extreal shows "\<lbrakk>a \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> a*c \<le> b*c"
+  using assms
+  apply (cases "c = 0") apply simp
+  by (cases rule: extreal3_cases[of a b c])
+     (auto simp: zero_le_mult_iff extreal_less_PInfty)
+
+lemma extreal_mult_left_mono:
+  fixes a b c :: extreal shows "\<lbrakk>a \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> c * a \<le> c * b"
+  using extreal_mult_right_mono by (simp add: mult_commute[of c])
+
+lemma zero_less_one_extreal[simp]: "0 \<le> (1::extreal)"
+  by (simp add: one_extreal_def zero_extreal_def)
+
+lemma extreal_0_le_mult[simp]: "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a * (b :: extreal)"
+  by (cases rule: extreal2_cases[of a b]) (auto simp: mult_nonneg_nonneg)
+
+lemma extreal_right_distrib:
+  fixes r a b :: extreal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> r * (a + b) = r * a + r * b"
+  by (cases rule: extreal3_cases[of r a b]) (simp_all add: field_simps)
+
+lemma extreal_left_distrib:
+  fixes r a b :: extreal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> (a + b) * r = a * r + b * r"
+  by (cases rule: extreal3_cases[of r a b]) (simp_all add: field_simps)
+
+lemma extreal_mult_le_0_iff:
+  fixes a b :: extreal
+  shows "a * b \<le> 0 \<longleftrightarrow> (0 \<le> a \<and> b \<le> 0) \<or> (a \<le> 0 \<and> 0 \<le> b)"
+  by (cases rule: extreal2_cases[of a b]) (simp_all add: mult_le_0_iff)
+
+lemma extreal_zero_le_0_iff:
+  fixes a b :: extreal
+  shows "0 \<le> a * b \<longleftrightarrow> (0 \<le> a \<and> 0 \<le> b) \<or> (a \<le> 0 \<and> b \<le> 0)"
+  by (cases rule: extreal2_cases[of a b]) (simp_all add: zero_le_mult_iff)
+
+lemma extreal_mult_less_0_iff:
+  fixes a b :: extreal
+  shows "a * b < 0 \<longleftrightarrow> (0 < a \<and> b < 0) \<or> (a < 0 \<and> 0 < b)"
+  by (cases rule: extreal2_cases[of a b]) (simp_all add: mult_less_0_iff)
+
+lemma extreal_zero_less_0_iff:
+  fixes a b :: extreal
+  shows "0 < a * b \<longleftrightarrow> (0 < a \<and> 0 < b) \<or> (a < 0 \<and> b < 0)"
+  by (cases rule: extreal2_cases[of a b]) (simp_all add: zero_less_mult_iff)
+
+lemma extreal_distrib:
+  fixes a b c :: extreal
+  assumes "a \<noteq> \<infinity> \<or> b \<noteq> -\<infinity>" "a \<noteq> -\<infinity> \<or> b \<noteq> \<infinity>" "\<bar>c\<bar> \<noteq> \<infinity>"
+  shows "(a + b) * c = a * c + b * c"
+  using assms
+  by (cases rule: extreal3_cases[of a b c]) (simp_all add: field_simps)
+
+lemma extreal_le_epsilon:
+  fixes x y :: extreal
+  assumes "ALL e. 0 < e --> x <= y + e"
+  shows "x <= y"
+proof-
+{ assume a: "EX r. y = extreal r"
+  from this obtain r where r_def: "y = extreal r" by auto
+  { assume "x=(-\<infinity>)" hence ?thesis by auto }
+  moreover
+  { assume "~(x=(-\<infinity>))"
+    from this obtain p where p_def: "x = extreal p"
+    using a assms[rule_format, of 1] by (cases x) auto
+    { fix e have "0 < e --> p <= r + e"
+      using assms[rule_format, of "extreal e"] p_def r_def by auto }
+    hence "p <= r" apply (subst field_le_epsilon) by auto
+    hence ?thesis using r_def p_def by auto
+  } ultimately have ?thesis by blast
+}
+moreover
+{ assume "y=(-\<infinity>) | y=\<infinity>" hence ?thesis
+    using assms[rule_format, of 1] by (cases x) auto
+} ultimately show ?thesis by (cases y) auto
+qed
+
+
+lemma extreal_le_epsilon2:
+  fixes x y :: extreal
+  assumes "ALL e. 0 < e --> x <= y + extreal e"
+  shows "x <= y"
+proof-
+{ fix e :: extreal assume "e>0"
+  { assume "e=\<infinity>" hence "x<=y+e" by auto }
+  moreover
+  { assume "e~=\<infinity>"
+    from this obtain r where "e = extreal r" using `e>0` apply (cases e) by auto
+    hence "x<=y+e" using assms[rule_format, of r] `e>0` by auto
+  } ultimately have "x<=y+e" by blast
+} from this show ?thesis using extreal_le_epsilon by auto
+qed
+
+lemma extreal_le_real:
+  fixes x y :: extreal
+  assumes "ALL z. x <= extreal z --> y <= extreal z"
+  shows "y <= x"
+by (metis assms extreal.exhaust extreal_bot extreal_less_eq(1)
+          extreal_less_eq(2) order_refl uminus_extreal.simps(2))
+
+lemma extreal_le_extreal:
+  fixes x y :: extreal
+  assumes "\<And>B. B < x \<Longrightarrow> B <= y"
+  shows "x <= y"
+by (metis assms extreal_dense leD linorder_le_less_linear)
+
+lemma extreal_ge_extreal:
+  fixes x y :: extreal
+  assumes "ALL B. B>x --> B >= y"
+  shows "x >= y"
+by (metis assms extreal_dense leD linorder_le_less_linear)
+
+subsubsection {* Power *}
+
+instantiation extreal :: power
+begin
+primrec power_extreal where
+  "power_extreal x 0 = 1" |
+  "power_extreal x (Suc n) = x * x ^ n"
+instance ..
+end
+
+lemma extreal_power[simp]: "(extreal x) ^ n = extreal (x^n)"
+  by (induct n) (auto simp: one_extreal_def)
+
+lemma extreal_power_PInf[simp]: "\<infinity> ^ n = (if n = 0 then 1 else \<infinity>)"
+  by (induct n) (auto simp: one_extreal_def)
+
+lemma extreal_power_uminus[simp]:
+  fixes x :: extreal
+  shows "(- x) ^ n = (if even n then x ^ n else - (x^n))"
+  by (induct n) (auto simp: one_extreal_def)
+
+lemma extreal_power_number_of[simp]:
+  "(number_of num :: extreal) ^ n = extreal (number_of num ^ n)"
+  by (induct n) (auto simp: one_extreal_def)
+
+lemma zero_le_power_extreal[simp]:
+  fixes a :: extreal assumes "0 \<le> a"
+  shows "0 \<le> a ^ n"
+  using assms by (induct n) (auto simp: extreal_zero_le_0_iff)
+
+subsubsection {* Subtraction *}
+
+lemma extreal_minus_minus_image[simp]:
+  fixes S :: "extreal set"
+  shows "uminus ` uminus ` S = S"
+  by (auto simp: image_iff)
+
+lemma extreal_uminus_lessThan[simp]:
+  fixes a :: extreal shows "uminus ` {..<a} = {-a<..}"
+proof (safe intro!: image_eqI)
+  fix x assume "-a < x"
+  then have "- x < - (- a)" by (simp del: extreal_uminus_uminus)
+  then show "- x < a" by simp
+qed auto
+
+lemma extreal_uminus_greaterThan[simp]:
+  "uminus ` {(a::extreal)<..} = {..<-a}"
+  by (metis extreal_uminus_lessThan extreal_uminus_uminus
+            extreal_minus_minus_image)
+
+instantiation extreal :: minus
+begin
+definition "x - y = x + -(y::extreal)"
+instance ..
+end
+
+lemma extreal_minus[simp]:
+  "extreal r - extreal p = extreal (r - p)"
+  "-\<infinity> - extreal r = -\<infinity>"
+  "extreal r - \<infinity> = -\<infinity>"
+  "\<infinity> - x = \<infinity>"
+  "-\<infinity> - \<infinity> = -\<infinity>"
+  "x - -y = x + y"
+  "x - 0 = x"
+  "0 - x = -x"
+  by (simp_all add: minus_extreal_def)
+
+lemma extreal_x_minus_x[simp]:
+  "x - x = (if \<bar>x\<bar> = \<infinity> then \<infinity> else 0)"
+  by (cases x) simp_all
+
+lemma extreal_eq_minus_iff:
+  fixes x y z :: extreal
+  shows "x = z - y \<longleftrightarrow>
+    (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x + y = z) \<and>
+    (y = -\<infinity> \<longrightarrow> x = \<infinity>) \<and>
+    (y = \<infinity> \<longrightarrow> z = \<infinity> \<longrightarrow> x = \<infinity>) \<and>
+    (y = \<infinity> \<longrightarrow> z \<noteq> \<infinity> \<longrightarrow> x = -\<infinity>)"
+  by (cases rule: extreal3_cases[of x y z]) auto
+
+lemma extreal_eq_minus:
+  fixes x y z :: extreal
+  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x = z - y \<longleftrightarrow> x + y = z"
+  by (auto simp: extreal_eq_minus_iff)
+
+lemma extreal_less_minus_iff:
+  fixes x y z :: extreal
+  shows "x < z - y \<longleftrightarrow>
+    (y = \<infinity> \<longrightarrow> z = \<infinity> \<and> x \<noteq> \<infinity>) \<and>
+    (y = -\<infinity> \<longrightarrow> x \<noteq> \<infinity>) \<and>
+    (\<bar>y\<bar> \<noteq> \<infinity>\<longrightarrow> x + y < z)"
+  by (cases rule: extreal3_cases[of x y z]) auto
+
+lemma extreal_less_minus:
+  fixes x y z :: extreal
+  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x < z - y \<longleftrightarrow> x + y < z"
+  by (auto simp: extreal_less_minus_iff)
+
+lemma extreal_le_minus_iff:
+  fixes x y z :: extreal
+  shows "x \<le> z - y \<longleftrightarrow>
+    (y = \<infinity> \<longrightarrow> z \<noteq> \<infinity> \<longrightarrow> x = -\<infinity>) \<and>
+    (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x + y \<le> z)"
+  by (cases rule: extreal3_cases[of x y z]) auto
+
+lemma extreal_le_minus:
+  fixes x y z :: extreal
+  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x \<le> z - y \<longleftrightarrow> x + y \<le> z"
+  by (auto simp: extreal_le_minus_iff)
+
+lemma extreal_minus_less_iff:
+  fixes x y z :: extreal
+  shows "x - y < z \<longleftrightarrow>
+    y \<noteq> -\<infinity> \<and> (y = \<infinity> \<longrightarrow> x \<noteq> \<infinity> \<and> z \<noteq> -\<infinity>) \<and>
+    (y \<noteq> \<infinity> \<longrightarrow> x < z + y)"
+  by (cases rule: extreal3_cases[of x y z]) auto
+
+lemma extreal_minus_less:
+  fixes x y z :: extreal
+  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x - y < z \<longleftrightarrow> x < z + y"
+  by (auto simp: extreal_minus_less_iff)
+
+lemma extreal_minus_le_iff:
+  fixes x y z :: extreal
+  shows "x - y \<le> z \<longleftrightarrow>
+    (y = -\<infinity> \<longrightarrow> z = \<infinity>) \<and>
+    (y = \<infinity> \<longrightarrow> x = \<infinity> \<longrightarrow> z = \<infinity>) \<and>
+    (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x \<le> z + y)"
+  by (cases rule: extreal3_cases[of x y z]) auto
+
+lemma extreal_minus_le:
+  fixes x y z :: extreal
+  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x - y \<le> z \<longleftrightarrow> x \<le> z + y"
+  by (auto simp: extreal_minus_le_iff)
+
+lemma extreal_minus_eq_minus_iff:
+  fixes a b c :: extreal
+  shows "a - b = a - c \<longleftrightarrow>
+    b = c \<or> a = \<infinity> \<or> (a = -\<infinity> \<and> b \<noteq> -\<infinity> \<and> c \<noteq> -\<infinity>)"
+  by (cases rule: extreal3_cases[of a b c]) auto
+
+lemma extreal_add_le_add_iff:
+  "c + a \<le> c + b \<longleftrightarrow>
+    a \<le> b \<or> c = \<infinity> \<or> (c = -\<infinity> \<and> a \<noteq> \<infinity> \<and> b \<noteq> \<infinity>)"
+  by (cases rule: extreal3_cases[of a b c]) (simp_all add: field_simps)
+
+lemma extreal_mult_le_mult_iff:
+  "\<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)"
+  by (cases rule: extreal3_cases[of a b c]) (simp_all add: mult_le_cancel_left)
+
+lemma extreal_minus_mono:
+  fixes A B C D :: extreal assumes "A \<le> B" "D \<le> C"
+  shows "A - C \<le> B - D"
+  using assms
+  by (cases rule: extreal3_cases[case_product extreal_cases, of A B C D]) simp_all
+
+lemma real_of_extreal_minus:
+  "real (a - b) = (if \<bar>a\<bar> = \<infinity> \<or> \<bar>b\<bar> = \<infinity> then 0 else real a - real b)"
+  by (cases rule: extreal2_cases[of a b]) auto
+
+lemma extreal_diff_positive:
+  fixes a b :: extreal shows "a \<le> b \<Longrightarrow> 0 \<le> b - a"
+  by (cases rule: extreal2_cases[of a b]) auto
+
+lemma extreal_between:
+  fixes x e :: extreal
+  assumes "\<bar>x\<bar> \<noteq> \<infinity>" "0 < e"
+  shows "x - e < x" "x < x + e"
+using assms apply (cases x, cases e) apply auto
+using assms by (cases x, cases e) auto
+
+subsubsection {* Division *}
+
+instantiation extreal :: inverse
+begin
+
+function inverse_extreal where
+"inverse (extreal r) = (if r = 0 then \<infinity> else extreal (inverse r))" |
+"inverse \<infinity> = 0" |
+"inverse (-\<infinity>) = 0"
+  by (auto intro: extreal_cases)
+termination by (relation "{}") simp
+
+definition "x / y = x * inverse (y :: extreal)"
+
+instance proof qed
+end
+
+lemma extreal_inverse[simp]:
+  "inverse 0 = \<infinity>"
+  "inverse (1::extreal) = 1"
+  by (simp_all add: one_extreal_def zero_extreal_def)
+
+lemma extreal_divide[simp]:
+  "extreal r / extreal p = (if p = 0 then extreal r * \<infinity> else extreal (r / p))"
+  unfolding divide_extreal_def by (auto simp: divide_real_def)
+
+lemma extreal_divide_same[simp]:
+  "x / x = (if \<bar>x\<bar> = \<infinity> \<or> x = 0 then 0 else 1)"
+  by (cases x)
+     (simp_all add: divide_real_def divide_extreal_def one_extreal_def)
+
+lemma extreal_inv_inv[simp]:
+  "inverse (inverse x) = (if x \<noteq> -\<infinity> then x else \<infinity>)"
+  by (cases x) auto
+
+lemma extreal_inverse_minus[simp]:
+  "inverse (- x) = (if x = 0 then \<infinity> else -inverse x)"
+  by (cases x) simp_all
+
+lemma extreal_uminus_divide[simp]:
+  fixes x y :: extreal shows "- x / y = - (x / y)"
+  unfolding divide_extreal_def by simp
+
+lemma extreal_divide_Infty[simp]:
+  "x / \<infinity> = 0" "x / -\<infinity> = 0"
+  unfolding divide_extreal_def by simp_all
+
+lemma extreal_divide_one[simp]:
+  "x / 1 = (x::extreal)"
+  unfolding divide_extreal_def by simp
+
+lemma extreal_divide_extreal[simp]:
+  "\<infinity> / extreal r = (if 0 \<le> r then \<infinity> else -\<infinity>)"
+  unfolding divide_extreal_def by simp
+
+lemma zero_le_divide_extreal[simp]:
+  fixes a :: extreal assumes "0 \<le> a" "0 \<le> b"
+  shows "0 \<le> a / b"
+  using assms by (cases rule: extreal2_cases[of a b]) (auto simp: zero_le_divide_iff)
+
+lemma extreal_le_divide_pos:
+  "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y \<le> z / x \<longleftrightarrow> x * y \<le> z"
+  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
+
+lemma extreal_divide_le_pos:
+  "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> z / x \<le> y \<longleftrightarrow> z \<le> x * y"
+  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
+
+lemma extreal_le_divide_neg:
+  "x < 0 \<Longrightarrow> x \<noteq> -\<infinity> \<Longrightarrow> y \<le> z / x \<longleftrightarrow> z \<le> x * y"
+  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
+
+lemma extreal_divide_le_neg:
+  "x < 0 \<Longrightarrow> x \<noteq> -\<infinity> \<Longrightarrow> z / x \<le> y \<longleftrightarrow> x * y \<le> z"
+  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
+
+lemma extreal_inverse_antimono_strict:
+  fixes x y :: extreal
+  shows "0 \<le> x \<Longrightarrow> x < y \<Longrightarrow> inverse y < inverse x"
+  by (cases rule: extreal2_cases[of x y]) auto
+
+lemma extreal_inverse_antimono:
+  fixes x y :: extreal
+  shows "0 \<le> x \<Longrightarrow> x <= y \<Longrightarrow> inverse y <= inverse x"
+  by (cases rule: extreal2_cases[of x y]) auto
+
+lemma inverse_inverse_Pinfty_iff[simp]:
+  "inverse x = \<infinity> \<longleftrightarrow> x = 0"
+  by (cases x) auto
+
+lemma extreal_inverse_eq_0:
+  "inverse x = 0 \<longleftrightarrow> x = \<infinity> \<or> x = -\<infinity>"
+  by (cases x) auto
+
+lemma extreal_0_gt_inverse:
+  fixes x :: extreal shows "0 < inverse x \<longleftrightarrow> x \<noteq> \<infinity> \<and> 0 \<le> x"
+  by (cases x) auto
+
+lemma extreal_mult_less_right:
+  assumes "b * a < c * a" "0 < a" "a < \<infinity>"
+  shows "b < c"
+  using assms
+  by (cases rule: extreal3_cases[of a b c])
+     (auto split: split_if_asm simp: zero_less_mult_iff zero_le_mult_iff)
+
+lemma extreal_power_divide:
+  "y \<noteq> 0 \<Longrightarrow> (x / y :: extreal) ^ n = x^n / y^n"
+  by (cases rule: extreal2_cases[of x y])
+     (auto simp: one_extreal_def zero_extreal_def power_divide not_le
+                 power_less_zero_eq zero_le_power_iff)
+
+lemma extreal_le_mult_one_interval:
+  fixes x y :: extreal
+  assumes y: "y \<noteq> -\<infinity>"
+  assumes z: "\<And>z. \<lbrakk> 0 < z ; z < 1 \<rbrakk> \<Longrightarrow> z * x \<le> y"
+  shows "x \<le> y"
+proof (cases x)
+  case PInf with z[of "1 / 2"] show "x \<le> y" by (simp add: one_extreal_def)
+next
+  case (real r) note r = this
+  show "x \<le> y"
+  proof (cases y)
+    case (real p) note p = this
+    have "r \<le> p"
+    proof (rule field_le_mult_one_interval)
+      fix z :: real assume "0 < z" and "z < 1"
+      with z[of "extreal z"]
+      show "z * r \<le> p" using p r by (auto simp: zero_le_mult_iff one_extreal_def)
+    qed
+    then show "x \<le> y" using p r by simp
+  qed (insert y, simp_all)
+qed simp
+
+subsection "Complete lattice"
+
+instantiation extreal :: lattice
+begin
+definition [simp]: "sup x y = (max x y :: extreal)"
+definition [simp]: "inf x y = (min x y :: extreal)"
+instance proof qed simp_all
+end
+
+instantiation extreal :: complete_lattice
+begin
+
+definition "bot = -\<infinity>"
+definition "top = \<infinity>"
+
+definition "Sup S = (LEAST z. ALL x:S. x <= z :: extreal)"
+definition "Inf S = (GREATEST z. ALL x:S. z <= x :: extreal)"
+
+lemma extreal_complete_Sup:
+  fixes S :: "extreal set" assumes "S \<noteq> {}"
+  shows "\<exists>x. (\<forall>y\<in>S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>S. y \<le> z) \<longrightarrow> x \<le> z)"
+proof cases
+  assume "\<exists>x. \<forall>a\<in>S. a \<le> extreal x"
+  then obtain y where y: "\<And>a. a\<in>S \<Longrightarrow> a \<le> extreal y" by auto
+  then have "\<infinity> \<notin> S" by force
+  show ?thesis
+  proof cases
+    assume "S = {-\<infinity>}"
+    then show ?thesis by (auto intro!: exI[of _ "-\<infinity>"])
+  next
+    assume "S \<noteq> {-\<infinity>}"
+    with `S \<noteq> {}` `\<infinity> \<notin> S` obtain x where "x \<in> S - {-\<infinity>}" "x \<noteq> \<infinity>" by auto
+    with y `\<infinity> \<notin> S` have "\<forall>z\<in>real ` (S - {-\<infinity>}). z \<le> y"
+      by (auto simp: real_of_extreal_ord_simps)
+    with reals_complete2[of "real ` (S - {-\<infinity>})"] `x \<in> S - {-\<infinity>}`
+    obtain s where s:
+       "\<forall>y\<in>S - {-\<infinity>}. real y \<le> s" "\<And>z. (\<forall>y\<in>S - {-\<infinity>}. real y \<le> z) \<Longrightarrow> s \<le> z"
+       by auto
+    show ?thesis
+    proof (safe intro!: exI[of _ "extreal s"])
+      fix z assume "z \<in> S" with `\<infinity> \<notin> S` show "z \<le> extreal s"
+      proof (cases z)
+        case (real r)
+        then show ?thesis
+          using s(1)[rule_format, of z] `z \<in> S` `z = extreal r` by auto
+      qed auto
+    next
+      fix z assume *: "\<forall>y\<in>S. y \<le> z"
+      with `S \<noteq> {-\<infinity>}` `S \<noteq> {}` show "extreal s \<le> z"
+      proof (cases z)
+        case (real u)
+        with * have "s \<le> u"
+          by (intro s(2)[of u]) (auto simp: real_of_extreal_ord_simps)
+        then show ?thesis using real by simp
+      qed auto
+    qed
+  qed
+next
+  assume *: "\<not> (\<exists>x. \<forall>a\<in>S. a \<le> extreal x)"
+  show ?thesis
+  proof (safe intro!: exI[of _ \<infinity>])
+    fix y assume **: "\<forall>z\<in>S. z \<le> y"
+    with * show "\<infinity> \<le> y"
+    proof (cases y)
+      case MInf with * ** show ?thesis by (force simp: not_le)
+    qed auto
+  qed simp
+qed
+
+lemma extreal_complete_Inf:
+  fixes S :: "extreal set" assumes "S ~= {}"
+  shows "EX x. (ALL y:S. x <= y) & (ALL z. (ALL y:S. z <= y) --> z <= x)"
+proof-
+def S1 == "uminus ` S"
+hence "S1 ~= {}" using assms by auto
+from this obtain x where x_def: "(ALL y:S1. y <= x) & (ALL z. (ALL y:S1. y <= z) --> x <= z)"
+   using extreal_complete_Sup[of S1] by auto
+{ fix z assume "ALL y:S. z <= y"
+  hence "ALL y:S1. y <= -z" unfolding S1_def by auto
+  hence "x <= -z" using x_def by auto
+  hence "z <= -x"
+    apply (subst extreal_uminus_uminus[symmetric])
+    unfolding extreal_minus_le_minus . }
+moreover have "(ALL y:S. -x <= y)"
+   using x_def unfolding S1_def
+   apply simp
+   apply (subst (3) extreal_uminus_uminus[symmetric])
+   unfolding extreal_minus_le_minus by simp
+ultimately show ?thesis by auto
+qed
+
+lemma extreal_complete_uminus_eq:
+  fixes S :: "extreal set"
+  shows "(\<forall>y\<in>uminus`S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>uminus`S. y \<le> z) \<longrightarrow> x \<le> z)
+     \<longleftrightarrow> (\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> -x)"
+  by simp (metis extreal_minus_le_minus extreal_uminus_uminus)
+
+lemma extreal_Sup_uminus_image_eq:
+  fixes S :: "extreal set"
+  shows "Sup (uminus ` S) = - Inf S"
+proof cases
+  assume "S = {}"
+  moreover have "(THE x. All (op \<le> x)) = (-\<infinity>::extreal)"
+    by (rule the_equality) (auto intro!: extreal_bot)
+  moreover have "(SOME x. \<forall>y. y \<le> x) = (\<infinity>::extreal)"
+    by (rule some_equality) (auto intro!: extreal_top)
+  ultimately show ?thesis unfolding Inf_extreal_def Sup_extreal_def
+    Least_def Greatest_def GreatestM_def by simp
+next
+  assume "S \<noteq> {}"
+  with extreal_complete_Sup[of "uminus`S"]
+  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)"
+    unfolding extreal_complete_uminus_eq by auto
+  show "Sup (uminus ` S) = - Inf S"
+    unfolding Inf_extreal_def Greatest_def GreatestM_def
+  proof (intro someI2[of _ _ "\<lambda>x. Sup (uminus`S) = - x"])
+    show "(\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>y. (\<forall>z\<in>S. y \<le> z) \<longrightarrow> y \<le> -x)"
+      using x .
+    fix x' assume "(\<forall>y\<in>S. x' \<le> y) \<and> (\<forall>y. (\<forall>z\<in>S. y \<le> z) \<longrightarrow> y \<le> x')"
+    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)"
+      unfolding extreal_complete_uminus_eq by simp
+    then show "Sup (uminus ` S) = -x'"
+      unfolding Sup_extreal_def extreal_uminus_eq_iff
+      by (intro Least_equality) auto
+  qed
+qed
+
+instance
+proof
+  { fix x :: extreal and A
+    show "bot <= x" by (cases x) (simp_all add: bot_extreal_def)
+    show "x <= top" by (simp add: top_extreal_def) }
+
+  { fix x :: extreal and A assume "x : A"
+    with extreal_complete_Sup[of A]
+    obtain s where s: "\<forall>y\<in>A. y <= s" "\<forall>z. (\<forall>y\<in>A. y <= z) \<longrightarrow> s <= z" by auto
+    hence "x <= s" using `x : A` by auto
+    also have "... = Sup A" using s unfolding Sup_extreal_def
+      by (auto intro!: Least_equality[symmetric])
+    finally show "x <= Sup A" . }
+  note le_Sup = this
+
+  { fix x :: extreal and A assume *: "!!z. (z : A ==> z <= x)"
+    show "Sup A <= x"
+    proof (cases "A = {}")
+      case True
+      hence "Sup A = -\<infinity>" unfolding Sup_extreal_def
+        by (auto intro!: Least_equality)
+      thus "Sup A <= x" by simp
+    next
+      case False
+      with extreal_complete_Sup[of A]
+      obtain s where s: "\<forall>y\<in>A. y <= s" "\<forall>z. (\<forall>y\<in>A. y <= z) \<longrightarrow> s <= z" by auto
+      hence "Sup A = s"
+        unfolding Sup_extreal_def by (auto intro!: Least_equality)
+      also have "s <= x" using * s by auto
+      finally show "Sup A <= x" .
+    qed }
+  note Sup_le = this
+
+  { fix x :: extreal and A assume "x \<in> A"
+    with le_Sup[of "-x" "uminus`A"] show "Inf A \<le> x"
+      unfolding extreal_Sup_uminus_image_eq by simp }
+
+  { fix x :: extreal and A assume *: "!!z. (z : A ==> x <= z)"
+    with Sup_le[of "uminus`A" "-x"] show "x \<le> Inf A"
+      unfolding extreal_Sup_uminus_image_eq by force }
+qed
+end
+
+lemma extreal_SUPR_uminus:
+  fixes f :: "'a => extreal"
+  shows "(SUP i : R. -(f i)) = -(INF i : R. f i)"
+  unfolding SUPR_def INFI_def
+  using extreal_Sup_uminus_image_eq[of "f`R"]
+  by (simp add: image_image)
+
+lemma extreal_INFI_uminus:
+  fixes f :: "'a => extreal"
+  shows "(INF i : R. -(f i)) = -(SUP i : R. f i)"
+  using extreal_SUPR_uminus[of _ "\<lambda>x. - f x"] by simp
+
+lemma extreal_Inf_uminus_image_eq: "Inf (uminus ` S) = - Sup (S::extreal set)"
+  using extreal_Sup_uminus_image_eq[of "uminus ` S"] by (simp add: image_image)
+
+lemma extreal_inj_on_uminus[intro, simp]: "inj_on uminus (A :: extreal set)"
+  by (auto intro!: inj_onI)
+
+lemma extreal_image_uminus_shift:
+  fixes X Y :: "extreal set" shows "uminus ` X = Y \<longleftrightarrow> X = uminus ` Y"
+proof
+  assume "uminus ` X = Y"
+  then have "uminus ` uminus ` X = uminus ` Y"
+    by (simp add: inj_image_eq_iff)
+  then show "X = uminus ` Y" by (simp add: image_image)
+qed (simp add: image_image)
+
+lemma Inf_extreal_iff:
+  fixes z :: extreal
+  shows "(!!x. x:X ==> z <= x) ==> (EX x:X. x<y) <-> Inf X < y"
+  by (metis complete_lattice_class.Inf_greatest complete_lattice_class.Inf_lower less_le_not_le linear
+            order_less_le_trans)
+
+lemma Sup_eq_MInfty:
+  fixes S :: "extreal set" shows "Sup S = -\<infinity> \<longleftrightarrow> S = {} \<or> S = {-\<infinity>}"
+proof
+  assume a: "Sup S = -\<infinity>"
+  with complete_lattice_class.Sup_upper[of _ S]
+  show "S={} \<or> S={-\<infinity>}" by auto
+next
+  assume "S={} \<or> S={-\<infinity>}" then show "Sup S = -\<infinity>"
+    unfolding Sup_extreal_def by (auto intro!: Least_equality)
+qed
+
+lemma Inf_eq_PInfty:
+  fixes S :: "extreal set" shows "Inf S = \<infinity> \<longleftrightarrow> S = {} \<or> S = {\<infinity>}"
+  using Sup_eq_MInfty[of "uminus`S"]
+  unfolding extreal_Sup_uminus_image_eq extreal_image_uminus_shift by simp
+
+lemma Inf_eq_MInfty: "-\<infinity> : S ==> Inf S = -\<infinity>"
+  unfolding Inf_extreal_def
+  by (auto intro!: Greatest_equality)
+
+lemma Sup_eq_PInfty: "\<infinity> : S ==> Sup S = \<infinity>"
+  unfolding Sup_extreal_def
+  by (auto intro!: Least_equality)
+
+lemma extreal_SUPI:
+  fixes x :: extreal
+  assumes "!!i. i : A ==> f i <= x"
+  assumes "!!y. (!!i. i : A ==> f i <= y) ==> x <= y"
+  shows "(SUP i:A. f i) = x"
+  unfolding SUPR_def Sup_extreal_def
+  using assms by (auto intro!: Least_equality)
+
+lemma extreal_INFI:
+  fixes x :: extreal
+  assumes "!!i. i : A ==> f i >= x"
+  assumes "!!y. (!!i. i : A ==> f i >= y) ==> x >= y"
+  shows "(INF i:A. f i) = x"
+  unfolding INFI_def Inf_extreal_def
+  using assms by (auto intro!: Greatest_equality)
+
+lemma Sup_extreal_close:
+  fixes e :: extreal
+  assumes "0 < e" and S: "\<bar>Sup S\<bar> \<noteq> \<infinity>" "S \<noteq> {}"
+  shows "\<exists>x\<in>S. Sup S - e < x"
+  using assms by (cases e) (auto intro!: less_Sup_iff[THEN iffD1])
+
+lemma Inf_extreal_close:
+  fixes e :: extreal assumes "\<bar>Inf X\<bar> \<noteq> \<infinity>" "0 < e"
+  shows "\<exists>x\<in>X. x < Inf X + e"
+proof (rule Inf_less_iff[THEN iffD1])
+  show "Inf X < Inf X + e" using assms
+    by (cases e) auto
+qed
+
+lemma Sup_eq_top_iff:
+  fixes A :: "'a::{complete_lattice, linorder} set"
+  shows "Sup A = top \<longleftrightarrow> (\<forall>x<top. \<exists>i\<in>A. x < i)"
+proof
+  assume *: "Sup A = top"
+  show "(\<forall>x<top. \<exists>i\<in>A. x < i)" unfolding *[symmetric]
+  proof (intro allI impI)
+    fix x assume "x < Sup A" then show "\<exists>i\<in>A. x < i"
+      unfolding less_Sup_iff by auto
+  qed
+next
+  assume *: "\<forall>x<top. \<exists>i\<in>A. x < i"
+  show "Sup A = top"
+  proof (rule ccontr)
+    assume "Sup A \<noteq> top"
+    with top_greatest[of "Sup A"]
+    have "Sup A < top" unfolding le_less by auto
+    then have "Sup A < Sup A"
+      using * unfolding less_Sup_iff by auto
+    then show False by auto
+  qed
+qed
+
+lemma SUP_eq_top_iff:
+  fixes f :: "'a \<Rightarrow> 'b::{complete_lattice, linorder}"
+  shows "(SUP i:A. f i) = top \<longleftrightarrow> (\<forall>x<top. \<exists>i\<in>A. x < f i)"
+  unfolding SUPR_def Sup_eq_top_iff by auto
+
+lemma SUP_nat_Infty: "(SUP i::nat. extreal (real i)) = \<infinity>"
+proof -
+  { fix x assume "x \<noteq> \<infinity>"
+    then have "\<exists>k::nat. x < extreal (real k)"
+    proof (cases x)
+      case MInf then show ?thesis by (intro exI[of _ 0]) auto
+    next
+      case (real r)
+      moreover obtain k :: nat where "r < real k"
+        using ex_less_of_nat by (auto simp: real_eq_of_nat)
+      ultimately show ?thesis by auto
+    qed simp }
+  then show ?thesis
+    using SUP_eq_top_iff[of UNIV "\<lambda>n::nat. extreal (real n)"]
+    by (auto simp: top_extreal_def)
+qed
+
+lemma extreal_le_Sup:
+  fixes x :: extreal
+  shows "(x <= (SUP i:A. f i)) <-> (ALL y. y < x --> (EX i. i : A & y <= f i))"
+(is "?lhs <-> ?rhs")
+proof-
+{ assume "?rhs"
+  { assume "~(x <= (SUP i:A. f i))" hence "(SUP i:A. f i)<x" by (simp add: not_le)
+    from this obtain y where y_def: "(SUP i:A. f i)<y & y<x" using extreal_dense by auto
+    from this obtain i where "i : A & y <= f i" using `?rhs` by auto
+    hence "y <= (SUP i:A. f i)" using le_SUPI[of i A f] by auto
+    hence False using y_def by auto
+  } hence "?lhs" by auto
+}
+moreover
+{ assume "?lhs" hence "?rhs"
+  by (metis Collect_def Collect_mem_eq SUP_leI assms atLeastatMost_empty atLeastatMost_empty_iff
+      inf_sup_ord(4) linorder_le_cases sup_absorb1 xt1(8))
+} ultimately show ?thesis by auto
+qed
+
+lemma extreal_Inf_le:
+  fixes x :: extreal
+  shows "((INF i:A. f i) <= x) <-> (ALL y. x < y --> (EX i. i : A & f i <= y))"
+(is "?lhs <-> ?rhs")
+proof-
+{ assume "?rhs"
+  { assume "~((INF i:A. f i) <= x)" hence "x < (INF i:A. f i)" by (simp add: not_le)
+    from this obtain y where y_def: "x<y & y<(INF i:A. f i)" using extreal_dense by auto
+    from this obtain i where "i : A & f i <= y" using `?rhs` by auto
+    hence "(INF i:A. f i) <= y" using INF_leI[of i A f] by auto
+    hence False using y_def by auto
+  } hence "?lhs" by auto
+}
+moreover
+{ assume "?lhs" hence "?rhs"
+  by (metis Collect_def Collect_mem_eq le_INFI assms atLeastatMost_empty atLeastatMost_empty_iff
+      inf_sup_ord(4) linorder_le_cases sup_absorb1 xt1(8))
+} ultimately show ?thesis by auto
+qed
+
+lemma Inf_less:
+  fixes x :: extreal
+  assumes "(INF i:A. f i) < x"
+  shows "EX i. i : A & f i <= x"
+proof(rule ccontr)
+  assume "~ (EX i. i : A & f i <= x)"
+  hence "ALL i:A. f i > x" by auto
+  hence "(INF i:A. f i) >= x" apply (subst le_INFI) by auto
+  thus False using assms by auto
+qed
+
+lemma same_INF:
+  assumes "ALL e:A. f e = g e"
+  shows "(INF e:A. f e) = (INF e:A. g e)"
+proof-
+have "f ` A = g ` A" unfolding image_def using assms by auto
+thus ?thesis unfolding INFI_def by auto
+qed
+
+lemma same_SUP:
+  assumes "ALL e:A. f e = g e"
+  shows "(SUP e:A. f e) = (SUP e:A. g e)"
+proof-
+have "f ` A = g ` A" unfolding image_def using assms by auto
+thus ?thesis unfolding SUPR_def by auto
+qed
+
+lemma SUPR_eq:
+  assumes "\<forall>i\<in>A. \<exists>j\<in>B. f i \<le> g j"
+  assumes "\<forall>j\<in>B. \<exists>i\<in>A. g j \<le> f i"
+  shows "(SUP i:A. f i) = (SUP j:B. g j)"
+proof (intro antisym)
+  show "(SUP i:A. f i) \<le> (SUP j:B. g j)"
+    using assms by (metis SUP_leI le_SUPI2)
+  show "(SUP i:B. g i) \<le> (SUP j:A. f j)"
+    using assms by (metis SUP_leI le_SUPI2)
+qed
+
+lemma SUP_extreal_le_addI:
+  assumes "\<And>i. f i + y \<le> z" and "y \<noteq> -\<infinity>"
+  shows "SUPR UNIV f + y \<le> z"
+proof (cases y)
+  case (real r)
+  then have "\<And>i. f i \<le> z - y" using assms by (simp add: extreal_le_minus_iff)
+  then have "SUPR UNIV f \<le> z - y" by (rule SUP_leI)
+  then show ?thesis using real by (simp add: extreal_le_minus_iff)
+qed (insert assms, auto)
+
+lemma SUPR_extreal_add:
+  fixes f g :: "nat \<Rightarrow> extreal"
+  assumes "incseq f" "incseq g" and pos: "\<And>i. f i \<noteq> -\<infinity>" "\<And>i. g i \<noteq> -\<infinity>"
+  shows "(SUP i. f i + g i) = SUPR UNIV f + SUPR UNIV g"
+proof (rule extreal_SUPI)
+  fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> f i + g i \<le> y"
+  have f: "SUPR UNIV f \<noteq> -\<infinity>" using pos
+    unfolding SUPR_def Sup_eq_MInfty by (auto dest: image_eqD)
+  { fix j
+    { fix i
+      have "f i + g j \<le> f i + g (max i j)"
+        using `incseq g`[THEN incseqD] by (rule add_left_mono) auto
+      also have "\<dots> \<le> f (max i j) + g (max i j)"
+        using `incseq f`[THEN incseqD] by (rule add_right_mono) auto
+      also have "\<dots> \<le> y" using * by auto
+      finally have "f i + g j \<le> y" . }
+    then have "SUPR UNIV f + g j \<le> y"
+      using assms(4)[of j] by (intro SUP_extreal_le_addI) auto
+    then have "g j + SUPR UNIV f \<le> y" by (simp add: ac_simps) }
+  then have "SUPR UNIV g + SUPR UNIV f \<le> y"
+    using f by (rule SUP_extreal_le_addI)
+  then show "SUPR UNIV f + SUPR UNIV g \<le> y" by (simp add: ac_simps)
+qed (auto intro!: add_mono le_SUPI)
+
+lemma SUPR_extreal_add_pos:
+  fixes f g :: "nat \<Rightarrow> extreal"
+  assumes inc: "incseq f" "incseq g" and pos: "\<And>i. 0 \<le> f i" "\<And>i. 0 \<le> g i"
+  shows "(SUP i. f i + g i) = SUPR UNIV f + SUPR UNIV g"
+proof (intro SUPR_extreal_add inc)
+  fix i show "f i \<noteq> -\<infinity>" "g i \<noteq> -\<infinity>" using pos[of i] by auto
+qed
+
+lemma SUPR_extreal_setsum:
+  fixes f g :: "'a \<Rightarrow> nat \<Rightarrow> extreal"
+  assumes "\<And>n. n \<in> A \<Longrightarrow> incseq (f n)" and pos: "\<And>n i. n \<in> A \<Longrightarrow> 0 \<le> f n i"
+  shows "(SUP i. \<Sum>n\<in>A. f n i) = (\<Sum>n\<in>A. SUPR UNIV (f n))"
+proof cases
+  assume "finite A" then show ?thesis using assms
+    by induct (auto simp: incseq_setsumI2 setsum_nonneg SUPR_extreal_add_pos)
+qed simp
+
+lemma SUPR_extreal_cmult:
+  fixes f :: "nat \<Rightarrow> extreal" assumes "\<And>i. 0 \<le> f i" "0 \<le> c"
+  shows "(SUP i. c * f i) = c * SUPR UNIV f"
+proof (rule extreal_SUPI)
+  fix i have "f i \<le> SUPR UNIV f" by (rule le_SUPI) auto
+  then show "c * f i \<le> c * SUPR UNIV f"
+    using `0 \<le> c` by (rule extreal_mult_left_mono)
+next
+  fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> c * f i \<le> y"
+  show "c * SUPR UNIV f \<le> y"
+  proof cases
+    assume c: "0 < c \<and> c \<noteq> \<infinity>"
+    with * have "SUPR UNIV f \<le> y / c"
+      by (intro SUP_leI) (auto simp: extreal_le_divide_pos)
+    with c show ?thesis
+      by (auto simp: extreal_le_divide_pos)
+  next
+    { assume "c = \<infinity>" have ?thesis
+      proof cases
+        assume "\<forall>i. f i = 0"
+        moreover then have "range f = {0}" by auto
+        ultimately show "c * SUPR UNIV f \<le> y" using * by (auto simp: SUPR_def)
+      next
+        assume "\<not> (\<forall>i. f i = 0)"
+        then obtain i where "f i \<noteq> 0" by auto
+        with *[of i] `c = \<infinity>` `0 \<le> f i` show ?thesis by (auto split: split_if_asm)
+      qed }
+    moreover assume "\<not> (0 < c \<and> c \<noteq> \<infinity>)"
+    ultimately show ?thesis using * `0 \<le> c` by auto
+  qed
+qed
+
+lemma SUP_PInfty:
+  fixes f :: "'a \<Rightarrow> extreal"
+  assumes "\<And>n::nat. \<exists>i\<in>A. extreal (real n) \<le> f i"
+  shows "(SUP i:A. f i) = \<infinity>"
+  unfolding SUPR_def Sup_eq_top_iff[where 'a=extreal, unfolded top_extreal_def]
+  apply simp
+proof safe
+  fix x assume "x \<noteq> \<infinity>"
+  show "\<exists>i\<in>A. x < f i"
+  proof (cases x)
+    case PInf with `x \<noteq> \<infinity>` show ?thesis by simp
+  next
+    case MInf with assms[of "0"] show ?thesis by force
+  next
+    case (real r)
+    with less_PInf_Ex_of_nat[of x] obtain n :: nat where "x < extreal (real n)" by auto
+    moreover from assms[of n] guess i ..
+    ultimately show ?thesis
+      by (auto intro!: bexI[of _ i])
+  qed
+qed
+
+lemma Sup_countable_SUPR:
+  assumes "A \<noteq> {}"
+  shows "\<exists>f::nat \<Rightarrow> extreal. range f \<subseteq> A \<and> Sup A = SUPR UNIV f"
+proof (cases "Sup A")
+  case (real r)
+  have "\<forall>n::nat. \<exists>x. x \<in> A \<and> Sup A < x + 1 / extreal (real n)"
+  proof
+    fix n ::nat have "\<exists>x\<in>A. Sup A - 1 / extreal (real n) < x"
+      using assms real by (intro Sup_extreal_close) (auto simp: one_extreal_def)
+    then guess x ..
+    then show "\<exists>x. x \<in> A \<and> Sup A < x + 1 / extreal (real n)"
+      by (auto intro!: exI[of _ x] simp: extreal_minus_less_iff)
+  qed
+  from choice[OF this] guess f .. note f = this
+  have "SUPR UNIV f = Sup A"
+  proof (rule extreal_SUPI)
+    fix i show "f i \<le> Sup A" using f
+      by (auto intro!: complete_lattice_class.Sup_upper)
+  next
+    fix y assume bound: "\<And>i. i \<in> UNIV \<Longrightarrow> f i \<le> y"
+    show "Sup A \<le> y"
+    proof (rule extreal_le_epsilon, intro allI impI)
+      fix e :: extreal assume "0 < e"
+      show "Sup A \<le> y + e"
+      proof (cases e)
+        case (real r)
+        hence "0 < r" using `0 < e` by auto
+        then obtain n ::nat where *: "1 / real n < r" "0 < n"
+          using ex_inverse_of_nat_less by (auto simp: real_eq_of_nat inverse_eq_divide)
+        have "Sup A \<le> f n + 1 / extreal (real n)" using f[THEN spec, of n] by auto
+        also have "1 / extreal (real n) \<le> e" using real * by (auto simp: one_extreal_def )
+        with bound have "f n + 1 / extreal (real n) \<le> y + e" by (rule add_mono) simp
+        finally show "Sup A \<le> y + e" .
+      qed (insert `0 < e`, auto)
+    qed
+  qed
+  with f show ?thesis by (auto intro!: exI[of _ f])
+next
+  case PInf
+  from `A \<noteq> {}` obtain x where "x \<in> A" by auto
+  show ?thesis
+  proof cases
+    assume "\<infinity> \<in> A"
+    moreover then have "\<infinity> \<le> Sup A" by (intro complete_lattice_class.Sup_upper)
+    ultimately show ?thesis by (auto intro!: exI[of _ "\<lambda>x. \<infinity>"])
+  next
+    assume "\<infinity> \<notin> A"
+    have "\<exists>x\<in>A. 0 \<le> x"
+      by (metis Infty_neq_0 PInf complete_lattice_class.Sup_least extreal_infty_less_eq2 linorder_linear)
+    then obtain x where "x \<in> A" "0 \<le> x" by auto
+    have "\<forall>n::nat. \<exists>f. f \<in> A \<and> x + extreal (real n) \<le> f"
+    proof (rule ccontr)
+      assume "\<not> ?thesis"
+      then have "\<exists>n::nat. Sup A \<le> x + extreal (real n)"
+        by (simp add: Sup_le_iff not_le less_imp_le Ball_def) (metis less_imp_le)
+      then show False using `x \<in> A` `\<infinity> \<notin> A` PInf
+        by(cases x) auto
+    qed
+    from choice[OF this] guess f .. note f = this
+    have "SUPR UNIV f = \<infinity>"
+    proof (rule SUP_PInfty)
+      fix n :: nat show "\<exists>i\<in>UNIV. extreal (real n) \<le> f i"
+        using f[THEN spec, of n] `0 \<le> x`
+        by (cases rule: extreal2_cases[of "f n" x]) (auto intro!: exI[of _ n])
+    qed
+    then show ?thesis using f PInf by (auto intro!: exI[of _ f])
+  qed
+next
+  case MInf
+  with `A \<noteq> {}` have "A = {-\<infinity>}" by (auto simp: Sup_eq_MInfty)
+  then show ?thesis using MInf by (auto intro!: exI[of _ "\<lambda>x. -\<infinity>"])
+qed
+
+lemma SUPR_countable_SUPR:
+  "A \<noteq> {} \<Longrightarrow> \<exists>f::nat \<Rightarrow> extreal. range f \<subseteq> g`A \<and> SUPR A g = SUPR UNIV f"
+  using Sup_countable_SUPR[of "g`A"] by (auto simp: SUPR_def)
+
+
+lemma Sup_extreal_cadd:
+  fixes A :: "extreal set" assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
+  shows "Sup ((\<lambda>x. a + x) ` A) = a + Sup A"
+proof (rule antisym)
+  have *: "\<And>a::extreal. \<And>A. Sup ((\<lambda>x. a + x) ` A) \<le> a + Sup A"
+    by (auto intro!: add_mono complete_lattice_class.Sup_least complete_lattice_class.Sup_upper)
+  then show "Sup ((\<lambda>x. a + x) ` A) \<le> a + Sup A" .
+  show "a + Sup A \<le> Sup ((\<lambda>x. a + x) ` A)"
+  proof (cases a)
+    case PInf with `A \<noteq> {}` show ?thesis by (auto simp: image_constant)
+  next
+    case (real r)
+    then have **: "op + (- a) ` op + a ` A = A"
+      by (auto simp: image_iff ac_simps zero_extreal_def[symmetric])
+    from *[of "-a" "(\<lambda>x. a + x) ` A"] real show ?thesis unfolding **
+      by (cases rule: extreal2_cases[of "Sup A" "Sup (op + a ` A)"]) auto
+  qed (insert `a \<noteq> -\<infinity>`, auto)
+qed
+
+lemma Sup_extreal_cminus:
+  fixes A :: "extreal set" assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
+  shows "Sup ((\<lambda>x. a - x) ` A) = a - Inf A"
+  using Sup_extreal_cadd[of "uminus ` A" a] assms
+  by (simp add: comp_def image_image minus_extreal_def
+                 extreal_Sup_uminus_image_eq)
+
+lemma SUPR_extreal_cminus:
+  fixes A assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
+  shows "(SUP x:A. a - f x) = a - (INF x:A. f x)"
+  using Sup_extreal_cminus[of "f`A" a] assms
+  unfolding SUPR_def INFI_def image_image by auto
+
+lemma Inf_extreal_cminus:
+  fixes A :: "extreal set" assumes "A \<noteq> {}" and "\<bar>a\<bar> \<noteq> \<infinity>"
+  shows "Inf ((\<lambda>x. a - x) ` A) = a - Sup A"
+proof -
+  { fix x have "-a - -x = -(a - x)" using assms by (cases x) auto }
+  moreover then have "(\<lambda>x. -a - x)`uminus`A = uminus ` (\<lambda>x. a - x) ` A"
+    by (auto simp: image_image)
+  ultimately show ?thesis
+    using Sup_extreal_cminus[of "uminus ` A" "-a"] assms
+    by (auto simp add: extreal_Sup_uminus_image_eq extreal_Inf_uminus_image_eq)
+qed
+
+lemma INFI_extreal_cminus:
+  fixes A assumes "A \<noteq> {}" and "\<bar>a\<bar> \<noteq> \<infinity>"
+  shows "(INF x:A. a - f x) = a - (SUP x:A. f x)"
+  using Inf_extreal_cminus[of "f`A" a] assms
+  unfolding SUPR_def INFI_def image_image
+  by auto
+
+subsection "Limits on @{typ extreal}"
+
+subsubsection "Topological space"
+
+instantiation extreal :: topological_space
+begin
+
+definition "open A \<longleftrightarrow> open (extreal -` A)
+       \<and> (\<infinity> \<in> A \<longrightarrow> (\<exists>x. {extreal x <..} \<subseteq> A))
+       \<and> (-\<infinity> \<in> A \<longrightarrow> (\<exists>x. {..<extreal x} \<subseteq> A))"
+
+lemma open_PInfty: "open A \<Longrightarrow> \<infinity> \<in> A \<Longrightarrow> (\<exists>x. {extreal x<..} \<subseteq> A)"
+  unfolding open_extreal_def by auto
+
+lemma open_MInfty: "open A \<Longrightarrow> -\<infinity> \<in> A \<Longrightarrow> (\<exists>x. {..<extreal x} \<subseteq> A)"
+  unfolding open_extreal_def by auto
+
+lemma open_PInfty2: assumes "open A" "\<infinity> \<in> A" obtains x where "{extreal x<..} \<subseteq> A"
+  using open_PInfty[OF assms] by auto
+
+lemma open_MInfty2: assumes "open A" "-\<infinity> \<in> A" obtains x where "{..<extreal x} \<subseteq> A"
+  using open_MInfty[OF assms] by auto
+
+lemma extreal_openE: assumes "open A" obtains x y where
+  "open (extreal -` A)"
+  "\<infinity> \<in> A \<Longrightarrow> {extreal x<..} \<subseteq> A"
+  "-\<infinity> \<in> A \<Longrightarrow> {..<extreal y} \<subseteq> A"
+  using assms open_extreal_def by auto
+
+instance
+proof
+  let ?U = "UNIV::extreal set"
+  show "open ?U" unfolding open_extreal_def
+    by (auto intro!: exI[of _ 0])
+next
+  fix S T::"extreal set" assume "open S" and "open T"
+  from `open S`[THEN extreal_openE] guess xS yS .
+  moreover from `open T`[THEN extreal_openE] guess xT yT .
+  ultimately have
+    "open (extreal -` (S \<inter> T))"
+    "\<infinity> \<in> S \<inter> T \<Longrightarrow> {extreal (max xS xT) <..} \<subseteq> S \<inter> T"
+    "-\<infinity> \<in> S \<inter> T \<Longrightarrow> {..< extreal (min yS yT)} \<subseteq> S \<inter> T"
+    by auto
+  then show "open (S Int T)" unfolding open_extreal_def by blast
+next
+  fix K :: "extreal set set" assume "\<forall>S\<in>K. open S"
+  then have *: "\<forall>S. \<exists>x y. S \<in> K \<longrightarrow> open (extreal -` S) \<and>
+    (\<infinity> \<in> S \<longrightarrow> {extreal x <..} \<subseteq> S) \<and> (-\<infinity> \<in> S \<longrightarrow> {..< extreal y} \<subseteq> S)"
+    by (auto simp: open_extreal_def)
+  then show "open (Union K)" unfolding open_extreal_def
+  proof (intro conjI impI)
+    show "open (extreal -` \<Union>K)"
+      using *[THEN choice] by (auto simp: vimage_Union)
+  qed ((metis UnionE Union_upper subset_trans *)+)
+qed
+end
+
+lemma open_extreal: "open S \<Longrightarrow> open (extreal ` S)"
+  by (auto simp: inj_vimage_image_eq open_extreal_def)
+
+lemma open_extreal_vimage: "open S \<Longrightarrow> open (extreal -` S)"
+  unfolding open_extreal_def by auto
+
+lemma open_extreal_lessThan[intro, simp]: "open {..< a :: extreal}"
+proof -
+  have "\<And>x. extreal -` {..<extreal x} = {..< x}"
+    "extreal -` {..< \<infinity>} = UNIV" "extreal -` {..< -\<infinity>} = {}" by auto
+  then show ?thesis by (cases a) (auto simp: open_extreal_def)
+qed
+
+lemma open_extreal_greaterThan[intro, simp]:
+  "open {a :: extreal <..}"
+proof -
+  have "\<And>x. extreal -` {extreal x<..} = {x<..}"
+    "extreal -` {\<infinity><..} = {}" "extreal -` {-\<infinity><..} = UNIV" by auto
+  then show ?thesis by (cases a) (auto simp: open_extreal_def)
+qed
+
+lemma extreal_open_greaterThanLessThan[intro, simp]: "open {a::extreal <..< b}"
+  unfolding greaterThanLessThan_def by auto
+
+lemma closed_extreal_atLeast[simp, intro]: "closed {a :: extreal ..}"
+proof -
+  have "- {a ..} = {..< a}" by auto
+  then show "closed {a ..}"
+    unfolding closed_def using open_extreal_lessThan by auto
+qed
+
+lemma closed_extreal_atMost[simp, intro]: "closed {.. b :: extreal}"
+proof -
+  have "- {.. b} = {b <..}" by auto
+  then show "closed {.. b}"
+    unfolding closed_def using open_extreal_greaterThan by auto
+qed
+
+lemma closed_extreal_atLeastAtMost[simp, intro]:
+  shows "closed {a :: extreal .. b}"
+  unfolding atLeastAtMost_def by auto
+
+lemma closed_extreal_singleton:
+  "closed {a :: extreal}"
+by (metis atLeastAtMost_singleton closed_extreal_atLeastAtMost)
+
+lemma extreal_open_cont_interval:
+  assumes "open S" "x \<in> S" "\<bar>x\<bar> \<noteq> \<infinity>"
+  obtains e where "e>0" "{x-e <..< x+e} \<subseteq> S"
+proof-
+  from `open S` have "open (extreal -` S)" by (rule extreal_openE)
+  then obtain e where "0 < e" and e: "\<And>y. dist y (real x) < e \<Longrightarrow> extreal y \<in> S"
+    using assms unfolding open_dist by force
+  show thesis
+  proof (intro that subsetI)
+    show "0 < extreal e" using `0 < e` by auto
+    fix y assume "y \<in> {x - extreal e<..<x + extreal e}"
+    with assms obtain t where "y = extreal t" "dist t (real x) < e"
+      apply (cases y) by (auto simp: dist_real_def)
+    then show "y \<in> S" using e[of t] by auto
+  qed
+qed
+
+lemma extreal_open_cont_interval2:
+  assumes "open S" "x \<in> S" and x: "\<bar>x\<bar> \<noteq> \<infinity>"
+  obtains a b where "a < x" "x < b" "{a <..< b} \<subseteq> S"
+proof-
+  guess e using extreal_open_cont_interval[OF assms] .
+  with that[of "x-e" "x+e"] extreal_between[OF x, of e]
+  show thesis by auto
+qed
+
+instance extreal :: t2_space
+proof
+  fix x y :: extreal assume "x ~= y"
+  let "?P x (y::extreal)" = "EX U V. open U & open V & x : U & y : V & U Int V = {}"
+
+  { fix x y :: extreal assume "x < y"
+    from extreal_dense[OF this] obtain z where z: "x < z" "z < y" by auto
+    have "?P x y"
+      apply (rule exI[of _ "{..<z}"])
+      apply (rule exI[of _ "{z<..}"])
+      using z by auto }
+  note * = this
+
+  from `x ~= y`
+  show "EX U V. open U & open V & x : U & y : V & U Int V = {}"
+  proof (cases rule: linorder_cases)
+    assume "x = y" with `x ~= y` show ?thesis by simp
+  next assume "x < y" from *[OF this] show ?thesis by auto
+  next assume "y < x" from *[OF this] show ?thesis by auto
+  qed
+qed
+
+subsubsection {* Convergent sequences *}
+
+lemma lim_extreal[simp]:
+  "((\<lambda>n. extreal (f n)) ---> extreal x) net \<longleftrightarrow> (f ---> x) net" (is "?l = ?r")
+proof (intro iffI topological_tendstoI)
+  fix S assume "?l" "open S" "x \<in> S"
+  then show "eventually (\<lambda>x. f x \<in> S) net"
+    using `?l`[THEN topological_tendstoD, OF open_extreal, OF `open S`]
+    by (simp add: inj_image_mem_iff)
+next
+  fix S assume "?r" "open S" "extreal x \<in> S"
+  show "eventually (\<lambda>x. extreal (f x) \<in> S) net"
+    using `?r`[THEN topological_tendstoD, OF open_extreal_vimage, OF `open S`]
+    using `extreal x \<in> S` by auto
+qed
+
+lemma lim_real_of_extreal[simp]:
+  assumes lim: "(f ---> extreal x) net"
+  shows "((\<lambda>x. real (f x)) ---> x) net"
+proof (intro topological_tendstoI)
+  fix S assume "open S" "x \<in> S"
+  then have S: "open S" "extreal x \<in> extreal ` S"
+    by (simp_all add: inj_image_mem_iff)
+  have "\<forall>x. f x \<in> extreal ` S \<longrightarrow> real (f x) \<in> S" by auto
+  from this lim[THEN topological_tendstoD, OF open_extreal, OF S]
+  show "eventually (\<lambda>x. real (f x) \<in> S) net"
+    by (rule eventually_mono)
+qed
+
+lemma Lim_PInfty: "f ----> \<infinity> <-> (ALL B. EX N. ALL n>=N. f n >= extreal B)" (is "?l = ?r")
+proof assume ?r show ?l apply(rule topological_tendstoI)
+    unfolding eventually_sequentially
+  proof- fix S assume "open S" "\<infinity> : S"
+    from open_PInfty[OF this] guess B .. note B=this
+    from `?r`[rule_format,of "B+1"] guess N .. note N=this
+    show "EX N. ALL n>=N. f n : S" apply(rule_tac x=N in exI)
+    proof safe case goal1
+      have "extreal B < extreal (B + 1)" by auto
+      also have "... <= f n" using goal1 N by auto
+      finally show ?case using B by fastsimp
+    qed
+  qed
+next assume ?l show ?r
+  proof fix B::real have "open {extreal B<..}" "\<infinity> : {extreal B<..}" by auto
+    from topological_tendstoD[OF `?l` this,unfolded eventually_sequentially]
+    guess N .. note N=this
+    show "EX N. ALL n>=N. extreal B <= f n" apply(rule_tac x=N in exI) using N by auto
+  qed
+qed
+
+
+lemma Lim_MInfty: "f ----> (-\<infinity>) <-> (ALL B. EX N. ALL n>=N. f n <= extreal B)" (is "?l = ?r")
+proof assume ?r show ?l apply(rule topological_tendstoI)
+    unfolding eventually_sequentially
+  proof- fix S assume "open S" "(-\<infinity>) : S"
+    from open_MInfty[OF this] guess B .. note B=this
+    from `?r`[rule_format,of "B-(1::real)"] guess N .. note N=this
+    show "EX N. ALL n>=N. f n : S" apply(rule_tac x=N in exI)
+    proof safe case goal1
+      have "extreal (B - 1) >= f n" using goal1 N by auto
+      also have "... < extreal B" by auto
+      finally show ?case using B by fastsimp
+    qed
+  qed
+next assume ?l show ?r
+  proof fix B::real have "open {..<extreal B}" "(-\<infinity>) : {..<extreal B}" by auto
+    from topological_tendstoD[OF `?l` this,unfolded eventually_sequentially]
+    guess N .. note N=this
+    show "EX N. ALL n>=N. extreal B >= f n" apply(rule_tac x=N in exI) using N by auto
+  qed
+qed
+
+
+lemma Lim_bounded_PInfty: assumes lim:"f ----> l" and "!!n. f n <= extreal B" shows "l ~= \<infinity>"
+proof(rule ccontr,unfold not_not) let ?B = "B + 1" assume as:"l=\<infinity>"
+  from lim[unfolded this Lim_PInfty,rule_format,of "?B"]
+  guess N .. note N=this[rule_format,OF le_refl]
+  hence "extreal ?B <= extreal B" using assms(2)[of N] by(rule order_trans)
+  hence "extreal ?B < extreal ?B" apply (rule le_less_trans) by auto
+  thus False by auto
+qed
+
+
+lemma Lim_bounded_MInfty: assumes lim:"f ----> l" and "!!n. f n >= extreal B" shows "l ~= (-\<infinity>)"
+proof(rule ccontr,unfold not_not) let ?B = "B - 1" assume as:"l=(-\<infinity>)"
+  from lim[unfolded this Lim_MInfty,rule_format,of "?B"]
+  guess N .. note N=this[rule_format,OF le_refl]
+  hence "extreal B <= extreal ?B" using assms(2)[of N] order_trans[of "extreal B" "f N" "extreal(B - 1)"] by blast
+  thus False by auto
+qed
+
+
+lemma tendsto_explicit:
+  "f ----> f0 <-> (ALL S. open S --> f0 : S --> (EX N. ALL n>=N. f n : S))"
+  unfolding tendsto_def eventually_sequentially by auto
+
+
+lemma tendsto_obtains_N:
+  assumes "f ----> f0"
+  assumes "open S" "f0 : S"
+  obtains N where "ALL n>=N. f n : S"
+  using tendsto_explicit[of f f0] assms by auto
+
+
+lemma tail_same_limit:
+  fixes X Y N
+  assumes "X ----> L" "ALL n>=N. X n = Y n"
+  shows "Y ----> L"
+proof-
+{ fix S assume "open S" and "L:S"
+  from this obtain N1 where "ALL n>=N1. X n : S"
+     using assms unfolding tendsto_def eventually_sequentially by auto
+  hence "ALL n>=max N N1. Y n : S" using assms by auto
+  hence "EX N. ALL n>=N. Y n : S" apply(rule_tac x="max N N1" in exI) by auto
+}
+thus ?thesis using tendsto_explicit by auto
+qed
+
+
+lemma Lim_bounded_PInfty2:
+assumes lim:"f ----> l" and "ALL n>=N. f n <= extreal B"
+shows "l ~= \<infinity>"
+proof-
+  def g == "(%n. if n>=N then f n else extreal B)"
+  hence "g ----> l" using tail_same_limit[of f l N g] lim by auto
+  moreover have "!!n. g n <= extreal B" using g_def assms by auto
+  ultimately show ?thesis using  Lim_bounded_PInfty by auto
+qed
+
+lemma Lim_bounded_extreal:
+  assumes lim:"f ----> (l :: extreal)"
+  and "ALL n>=M. f n <= C"
+  shows "l<=C"
+proof-
+{ assume "l=(-\<infinity>)" hence ?thesis by auto }
+moreover
+{ assume "~(l=(-\<infinity>))"
+  { assume "C=\<infinity>" hence ?thesis by auto }
+  moreover
+  { assume "C=(-\<infinity>)" hence "ALL n>=M. f n = (-\<infinity>)" using assms by auto
+    hence "l=(-\<infinity>)" using assms
+       tendsto_unique[OF trivial_limit_sequentially] tail_same_limit[of "\<lambda>n. -\<infinity>" "-\<infinity>" M f, OF tendsto_const] by auto
+    hence ?thesis by auto }
+  moreover
+  { assume "EX B. C = extreal B"
+    from this obtain B where B_def: "C=extreal B" by auto
+    hence "~(l=\<infinity>)" using Lim_bounded_PInfty2 assms by auto
+    from this obtain m where m_def: "extreal m=l" using `~(l=(-\<infinity>))` by (cases l) auto
+    from this obtain N where N_def: "ALL n>=N. f n : {extreal(m - 1) <..< extreal(m+1)}"
+       apply (subst tendsto_obtains_N[of f l "{extreal(m - 1) <..< extreal(m+1)}"]) using assms by auto
+    { fix n assume "n>=N"
+      hence "EX r. extreal r = f n" using N_def by (cases "f n") auto
+    } from this obtain g where g_def: "ALL n>=N. extreal (g n) = f n" by metis
+    hence "(%n. extreal (g n)) ----> l" using tail_same_limit[of f l N] assms by auto
+    hence *: "(%n. g n) ----> m" using m_def by auto
+    { fix n assume "n>=max N M"
+      hence "extreal (g n) <= extreal B" using assms g_def B_def by auto
+      hence "g n <= B" by auto
+    } hence "EX N. ALL n>=N. g n <= B" by blast
+    hence "m<=B" using * LIMSEQ_le_const2[of g m B] by auto
+    hence ?thesis using m_def B_def by auto
+  } ultimately have ?thesis by (cases C) auto
+} ultimately show ?thesis by blast
+qed
+
+lemma real_of_extreal_0[simp]: "real (0::extreal) = 0"
+  unfolding real_of_extreal_def zero_extreal_def by simp
+
+lemma real_of_extreal_mult[simp]:
+  fixes a b :: extreal shows "real (a * b) = real a * real b"
+  by (cases rule: extreal2_cases[of a b]) auto
+
+lemma real_of_extreal_eq_0:
+  "real x = 0 \<longleftrightarrow> x = \<infinity> \<or> x = -\<infinity> \<or> x = 0"
+  by (cases x) auto
+
+lemma tendsto_extreal_realD:
+  fixes f :: "'a \<Rightarrow> extreal"
+  assumes "x \<noteq> 0" and tendsto: "((\<lambda>x. extreal (real (f x))) ---> x) net"
+  shows "(f ---> x) net"
+proof (intro topological_tendstoI)
+  fix S assume S: "open S" "x \<in> S"
+  with `x \<noteq> 0` have "open (S - {0})" "x \<in> S - {0}" by auto
+  from tendsto[THEN topological_tendstoD, OF this]
+  show "eventually (\<lambda>x. f x \<in> S) net"
+    by (rule eventually_rev_mp) (auto simp: extreal_real real_of_extreal_0)
+qed
+
+lemma tendsto_extreal_realI:
+  fixes f :: "'a \<Rightarrow> extreal"
+  assumes x: "\<bar>x\<bar> \<noteq> \<infinity>" and tendsto: "(f ---> x) net"
+  shows "((\<lambda>x. extreal (real (f x))) ---> x) net"
+proof (intro topological_tendstoI)
+  fix S assume "open S" "x \<in> S"
+  with x have "open (S - {\<infinity>, -\<infinity>})" "x \<in> S - {\<infinity>, -\<infinity>}" by auto
+  from tendsto[THEN topological_tendstoD, OF this]
+  show "eventually (\<lambda>x. extreal (real (f x)) \<in> S) net"
+    by (elim eventually_elim1) (auto simp: extreal_real)
+qed
+
+lemma extreal_mult_cancel_left:
+  fixes a b c :: extreal shows "a * b = a * c \<longleftrightarrow>
+    ((\<bar>a\<bar> = \<infinity> \<and> 0 < b * c) \<or> a = 0 \<or> b = c)"
+  by (cases rule: extreal3_cases[of a b c])
+     (simp_all add: zero_less_mult_iff)
+
+lemma extreal_inj_affinity:
+  assumes "\<bar>m\<bar> \<noteq> \<infinity>" "m \<noteq> 0" "\<bar>t\<bar> \<noteq> \<infinity>"
+  shows "inj_on (\<lambda>x. m * x + t) A"
+  using assms
+  by (cases rule: extreal2_cases[of m t])
+     (auto intro!: inj_onI simp: extreal_add_cancel_right extreal_mult_cancel_left)
+
+lemma extreal_PInfty_eq_plus[simp]:
+  shows "\<infinity> = a + b \<longleftrightarrow> a = \<infinity> \<or> b = \<infinity>"
+  by (cases rule: extreal2_cases[of a b]) auto
+
+lemma extreal_MInfty_eq_plus[simp]:
+  shows "-\<infinity> = a + b \<longleftrightarrow> (a = -\<infinity> \<and> b \<noteq> \<infinity>) \<or> (b = -\<infinity> \<and> a \<noteq> \<infinity>)"
+  by (cases rule: extreal2_cases[of a b]) auto
+
+lemma extreal_less_divide_pos:
+  "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y < z / x \<longleftrightarrow> x * y < z"
+  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
+
+lemma extreal_divide_less_pos:
+  "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y / x < z \<longleftrightarrow> y < x * z"
+  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
+
+lemma extreal_divide_eq:
+  "b \<noteq> 0 \<Longrightarrow> \<bar>b\<bar> \<noteq> \<infinity> \<Longrightarrow> a / b = c \<longleftrightarrow> a = b * c"
+  by (cases rule: extreal3_cases[of a b c])
+     (simp_all add: field_simps)
+
+lemma extreal_inverse_not_MInfty[simp]: "inverse a \<noteq> -\<infinity>"
+  by (cases a) auto
+
+lemma extreal_mult_m1[simp]: "x * extreal (-1) = -x"
+  by (cases x) auto
+
+lemma extreal_LimI_finite:
+  assumes "\<bar>x\<bar> \<noteq> \<infinity>"
+  assumes "!!r. 0 < r ==> EX N. ALL n>=N. u n < x + r & x < u n + r"
+  shows "u ----> x"
+proof (rule topological_tendstoI, unfold eventually_sequentially)
+  obtain rx where rx_def: "x=extreal rx" using assms by (cases x) auto
+  fix S assume "open S" "x : S"
+  then have "open (extreal -` S)" unfolding open_extreal_def by auto
+  with `x \<in> S` obtain r where "0 < r" and dist: "!!y. dist y rx < r ==> extreal y \<in> S"
+    unfolding open_real_def rx_def by auto
+  then obtain n where
+    upper: "!!N. n <= N ==> u N < x + extreal r" and
+    lower: "!!N. n <= N ==> x < u N + extreal r" using assms(2)[of "extreal r"] by auto
+  show "EX N. ALL n>=N. u n : S"
+  proof (safe intro!: exI[of _ n])
+    fix N assume "n <= N"
+    from upper[OF this] lower[OF this] assms `0 < r`
+    have "u N ~: {\<infinity>,(-\<infinity>)}" by auto
+    from this obtain ra where ra_def: "(u N) = extreal ra" by (cases "u N") auto
+    hence "rx < ra + r" and "ra < rx + r"
+       using rx_def assms `0 < r` lower[OF `n <= N`] upper[OF `n <= N`] by auto
+    hence "dist (real (u N)) rx < r"
+      using rx_def ra_def
+      by (auto simp: dist_real_def abs_diff_less_iff field_simps)
+    from dist[OF this] show "u N : S" using `u N  ~: {\<infinity>, -\<infinity>}`
+      by (auto simp: extreal_real split: split_if_asm)
+  qed
+qed
+
+lemma extreal_LimI_finite_iff:
+  assumes "\<bar>x\<bar> \<noteq> \<infinity>"
+  shows "u ----> x <-> (ALL r. 0 < r --> (EX N. ALL n>=N. u n < x + r & x < u n + r))"
+  (is "?lhs <-> ?rhs")
+proof
+  assume lim: "u ----> x"
+  { fix r assume "(r::extreal)>0"
+    from this obtain N where N_def: "ALL n>=N. u n : {x - r <..< x + r}"
+       apply (subst tendsto_obtains_N[of u x "{x - r <..< x + r}"])
+       using lim extreal_between[of x r] assms `r>0` by auto
+    hence "EX N. ALL n>=N. u n < x + r & x < u n + r"
+      using extreal_minus_less[of r x] by (cases r) auto
+  } then show "?rhs" by auto
+next
+  assume ?rhs then show "u ----> x"
+    using extreal_LimI_finite[of x] assms by auto
+qed
+
+
+subsubsection {* @{text Liminf} and @{text Limsup} *}
+
+definition
+  "Liminf net f = (GREATEST l. \<forall>y<l. eventually (\<lambda>x. y < f x) net)"
+
+definition
+  "Limsup net f = (LEAST l. \<forall>y>l. eventually (\<lambda>x. f x < y) net)"
+
+lemma Liminf_Sup:
+  fixes f :: "'a => 'b::{complete_lattice, linorder}"
+  shows "Liminf net f = Sup {l. \<forall>y<l. eventually (\<lambda>x. y < f x) net}"
+  by (auto intro!: Greatest_equality complete_lattice_class.Sup_upper simp: less_Sup_iff Liminf_def)
+
+lemma Limsup_Inf:
+  fixes f :: "'a => 'b::{complete_lattice, linorder}"
+  shows "Limsup net f = Inf {l. \<forall>y>l. eventually (\<lambda>x. f x < y) net}"
+  by (auto intro!: Least_equality complete_lattice_class.Inf_lower simp: Inf_less_iff Limsup_def)
+
+lemma extreal_SupI:
+  fixes x :: extreal
+  assumes "\<And>y. y \<in> A \<Longrightarrow> y \<le> x"
+  assumes "\<And>y. (\<And>z. z \<in> A \<Longrightarrow> z \<le> y) \<Longrightarrow> x \<le> y"
+  shows "Sup A = x"
+  unfolding Sup_extreal_def
+  using assms by (auto intro!: Least_equality)
+
+lemma extreal_InfI:
+  fixes x :: extreal
+  assumes "\<And>i. i \<in> A \<Longrightarrow> x \<le> i"
+  assumes "\<And>y. (\<And>i. i \<in> A \<Longrightarrow> y \<le> i) \<Longrightarrow> y \<le> x"
+  shows "Inf A = x"
+  unfolding Inf_extreal_def
+  using assms by (auto intro!: Greatest_equality)
+
+lemma Limsup_const:
+  fixes c :: "'a::{complete_lattice, linorder}"
+  assumes ntriv: "\<not> trivial_limit net"
+  shows "Limsup net (\<lambda>x. c) = c"
+  unfolding Limsup_Inf
+proof (safe intro!: antisym complete_lattice_class.Inf_greatest complete_lattice_class.Inf_lower)
+  fix x assume *: "\<forall>y>x. eventually (\<lambda>_. c < y) net"
+  show "c \<le> x"
+  proof (rule ccontr)
+    assume "\<not> c \<le> x" then have "x < c" by auto
+    then show False using ntriv * by (auto simp: trivial_limit_def)
+  qed
+qed auto
+
+lemma Liminf_const:
+  fixes c :: "'a::{complete_lattice, linorder}"
+  assumes ntriv: "\<not> trivial_limit net"
+  shows "Liminf net (\<lambda>x. c) = c"
+  unfolding Liminf_Sup
+proof (safe intro!: antisym complete_lattice_class.Sup_least complete_lattice_class.Sup_upper)
+  fix x assume *: "\<forall>y<x. eventually (\<lambda>_. y < c) net"
+  show "x \<le> c"
+  proof (rule ccontr)
+    assume "\<not> x \<le> c" then have "c < x" by auto
+    then show False using ntriv * by (auto simp: trivial_limit_def)
+  qed
+qed auto
+
+lemma mono_set:
+  fixes S :: "('a::order) set"
+  shows "mono S \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> x \<in> S \<longrightarrow> y \<in> S)"
+  by (auto simp: mono_def mem_def)
+
+lemma mono_greaterThan[intro, simp]: "mono {B<..}" unfolding mono_set by auto
+lemma mono_atLeast[intro, simp]: "mono {B..}" unfolding mono_set by auto
+lemma mono_UNIV[intro, simp]: "mono UNIV" unfolding mono_set by auto
+lemma mono_empty[intro, simp]: "mono {}" unfolding mono_set by auto
+
+lemma mono_set_iff:
+  fixes S :: "'a::{linorder,complete_lattice} set"
+  defines "a \<equiv> Inf S"
+  shows "mono S \<longleftrightarrow> (S = {a <..} \<or> S = {a..})" (is "_ = ?c")
+proof
+  assume "mono S"
+  then have mono: "\<And>x y. x \<le> y \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S" by (auto simp: mono_set)
+  show ?c
+  proof cases
+    assume "a \<in> S"
+    show ?c
+      using mono[OF _ `a \<in> S`]
+      by (auto intro: complete_lattice_class.Inf_lower simp: a_def)
+  next
+    assume "a \<notin> S"
+    have "S = {a <..}"
+    proof safe
+      fix x assume "x \<in> S"
+      then have "a \<le> x" unfolding a_def by (rule complete_lattice_class.Inf_lower)
+      then show "a < x" using `x \<in> S` `a \<notin> S` by (cases "a = x") auto
+    next
+      fix x assume "a < x"
+      then obtain y where "y < x" "y \<in> S" unfolding a_def Inf_less_iff ..
+      with mono[of y x] show "x \<in> S" by auto
+    qed
+    then show ?c ..
+  qed
+qed auto
+
+lemma lim_imp_Liminf:
+  fixes f :: "'a \<Rightarrow> extreal"
+  assumes ntriv: "\<not> trivial_limit net"
+  assumes lim: "(f ---> f0) net"
+  shows "Liminf net f = f0"
+  unfolding Liminf_Sup
+proof (safe intro!: extreal_SupI)
+  fix y assume *: "\<forall>y'<y. eventually (\<lambda>x. y' < f x) net"
+  show "y \<le> f0"
+  proof (rule extreal_le_extreal)
+    fix B assume "B < y"
+    { assume "f0 < B"
+      then have "eventually (\<lambda>x. f x < B \<and> B < f x) net"
+         using topological_tendstoD[OF lim, of "{..<B}"] *[rule_format, OF `B < y`]
+         by (auto intro: eventually_conj)
+      also have "(\<lambda>x. f x < B \<and> B < f x) = (\<lambda>x. False)" by (auto simp: fun_eq_iff)
+      finally have False using ntriv[unfolded trivial_limit_def] by auto
+    } then show "B \<le> f0" by (metis linorder_le_less_linear)
+  qed
+next
+  fix y assume *: "\<forall>z. z \<in> {l. \<forall>y<l. eventually (\<lambda>x. y < f x) net} \<longrightarrow> z \<le> y"
+  show "f0 \<le> y"
+  proof (safe intro!: *[rule_format])
+    fix y assume "y < f0" then show "eventually (\<lambda>x. y < f x) net"
+      using lim[THEN topological_tendstoD, of "{y <..}"] by auto
+  qed
+qed
+
+lemma extreal_Liminf_le_Limsup:
+  fixes f :: "'a \<Rightarrow> extreal"
+  assumes ntriv: "\<not> trivial_limit net"
+  shows "Liminf net f \<le> Limsup net f"
+  unfolding Limsup_Inf Liminf_Sup
+proof (safe intro!: complete_lattice_class.Inf_greatest  complete_lattice_class.Sup_least)
+  fix u v assume *: "\<forall>y<u. eventually (\<lambda>x. y < f x) net" "\<forall>y>v. eventually (\<lambda>x. f x < y) net"
+  show "u \<le> v"
+  proof (rule ccontr)
+    assume "\<not> u \<le> v"
+    then obtain t where "t < u" "v < t"
+      using extreal_dense[of v u] by (auto simp: not_le)
+    then have "eventually (\<lambda>x. t < f x \<and> f x < t) net"
+      using * by (auto intro: eventually_conj)
+    also have "(\<lambda>x. t < f x \<and> f x < t) = (\<lambda>x. False)" by (auto simp: fun_eq_iff)
+    finally show False using ntriv by (auto simp: trivial_limit_def)
+  qed
+qed
+
+lemma Liminf_mono:
+  fixes f g :: "'a => extreal"
+  assumes ev: "eventually (\<lambda>x. f x \<le> g x) net"
+  shows "Liminf net f \<le> Liminf net g"
+  unfolding Liminf_Sup
+proof (safe intro!: Sup_mono bexI)
+  fix a y assume "\<forall>y<a. eventually (\<lambda>x. y < f x) net" and "y < a"
+  then have "eventually (\<lambda>x. y < f x) net" by auto
+  then show "eventually (\<lambda>x. y < g x) net"
+    by (rule eventually_rev_mp) (rule eventually_mono[OF _ ev], auto)
+qed simp
+
+lemma Liminf_eq:
+  fixes f g :: "'a \<Rightarrow> extreal"
+  assumes "eventually (\<lambda>x. f x = g x) net"
+  shows "Liminf net f = Liminf net g"
+  by (intro antisym Liminf_mono eventually_mono[OF _ assms]) auto
+
+lemma Liminf_mono_all:
+  fixes f g :: "'a \<Rightarrow> extreal"
+  assumes "\<And>x. f x \<le> g x"
+  shows "Liminf net f \<le> Liminf net g"
+  using assms by (intro Liminf_mono always_eventually) auto
+
+lemma Limsup_mono:
+  fixes f g :: "'a \<Rightarrow> extreal"
+  assumes ev: "eventually (\<lambda>x. f x \<le> g x) net"
+  shows "Limsup net f \<le> Limsup net g"
+  unfolding Limsup_Inf
+proof (safe intro!: Inf_mono bexI)
+  fix a y assume "\<forall>y>a. eventually (\<lambda>x. g x < y) net" and "a < y"
+  then have "eventually (\<lambda>x. g x < y) net" by auto
+  then show "eventually (\<lambda>x. f x < y) net"
+    by (rule eventually_rev_mp) (rule eventually_mono[OF _ ev], auto)
+qed simp
+
+lemma Limsup_mono_all:
+  fixes f g :: "'a \<Rightarrow> extreal"
+  assumes "\<And>x. f x \<le> g x"
+  shows "Limsup net f \<le> Limsup net g"
+  using assms by (intro Limsup_mono always_eventually) auto
+
+lemma Limsup_eq:
+  fixes f g :: "'a \<Rightarrow> extreal"
+  assumes "eventually (\<lambda>x. f x = g x) net"
+  shows "Limsup net f = Limsup net g"
+  by (intro antisym Limsup_mono eventually_mono[OF _ assms]) auto
+
+abbreviation "liminf \<equiv> Liminf sequentially"
+
+abbreviation "limsup \<equiv> Limsup sequentially"
+
+lemma (in complete_lattice) less_INFD:
+  assumes "y < INFI A f"" i \<in> A" shows "y < f i"
+proof -
+  note `y < INFI A f`
+  also have "INFI A f \<le> f i" using `i \<in> A` by (rule INF_leI)
+  finally show "y < f i" .
+qed
+
+lemma liminf_SUPR_INFI:
+  fixes f :: "nat \<Rightarrow> extreal"
+  shows "liminf f = (SUP n. INF m:{n..}. f m)"
+  unfolding Liminf_Sup eventually_sequentially
+proof (safe intro!: antisym complete_lattice_class.Sup_least)
+  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)"
+  proof (rule extreal_le_extreal)
+    fix y assume "y < x"
+    with * obtain N where "\<And>n. N \<le> n \<Longrightarrow> y < f n" by auto
+    then have "y \<le> (INF m:{N..}. f m)" by (force simp: le_INF_iff)
+    also have "\<dots> \<le> (SUP n. INF m:{n..}. f m)" by (intro le_SUPI) auto
+    finally show "y \<le> (SUP n. INF m:{n..}. f m)" .
+  qed
+next
+  show "(SUP n. INF m:{n..}. f m) \<le> Sup {l. \<forall>y<l. \<exists>N. \<forall>n\<ge>N. y < f n}"
+  proof (unfold SUPR_def, safe intro!: Sup_mono bexI)
+    fix y n assume "y < INFI {n..} f"
+    from less_INFD[OF this] show "\<exists>N. \<forall>n\<ge>N. y < f n" by (intro exI[of _ n]) auto
+  qed (rule order_refl)
+qed
+
+lemma tail_same_limsup:
+  fixes X Y :: "nat => extreal"
+  assumes "\<And>n. N \<le> n \<Longrightarrow> X n = Y n"
+  shows "limsup X = limsup Y"
+  using Limsup_eq[of X Y sequentially] eventually_sequentially assms by auto
+
+lemma tail_same_liminf:
+  fixes X Y :: "nat => extreal"
+  assumes "\<And>n. N \<le> n \<Longrightarrow> X n = Y n"
+  shows "liminf X = liminf Y"
+  using Liminf_eq[of X Y sequentially] eventually_sequentially assms by auto
+
+lemma liminf_mono:
+  fixes X Y :: "nat \<Rightarrow> extreal"
+  assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= Y n"
+  shows "liminf X \<le> liminf Y"
+  using Liminf_mono[of X Y sequentially] eventually_sequentially assms by auto
+
+lemma limsup_mono:
+  fixes X Y :: "nat => extreal"
+  assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= Y n"
+  shows "limsup X \<le> limsup Y"
+  using Limsup_mono[of X Y sequentially] eventually_sequentially assms by auto
+
+declare trivial_limit_sequentially[simp]
+
+lemma
+  fixes X :: "nat \<Rightarrow> extreal"
+  shows extreal_incseq_uminus[simp]: "incseq (\<lambda>i. - X i) = decseq X"
+    and extreal_decseq_uminus[simp]: "decseq (\<lambda>i. - X i) = incseq X"
+  unfolding incseq_def decseq_def by auto
+
+lemma liminf_bounded:
+  fixes X Y :: "nat \<Rightarrow> extreal"
+  assumes "\<And>n. N \<le> n \<Longrightarrow> C \<le> X n"
+  shows "C \<le> liminf X"
+  using liminf_mono[of N "\<lambda>n. C" X] assms Liminf_const[of sequentially C] by simp
+
+lemma limsup_bounded:
+  fixes X Y :: "nat => extreal"
+  assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= C"
+  shows "limsup X \<le> C"
+  using limsup_mono[of N X "\<lambda>n. C"] assms Limsup_const[of sequentially C] by simp
+
+lemma liminf_bounded_iff:
+  fixes x :: "nat \<Rightarrow> extreal"
+  shows "C \<le> liminf x \<longleftrightarrow> (\<forall>B<C. \<exists>N. \<forall>n\<ge>N. B < x n)" (is "?lhs <-> ?rhs")
+proof safe
+  fix B assume "B < C" "C \<le> liminf x"
+  then have "B < liminf x" by auto
+  then obtain N where "B < (INF m:{N..}. x m)"
+    unfolding liminf_SUPR_INFI SUPR_def less_Sup_iff by auto
+  from less_INFD[OF this] show "\<exists>N. \<forall>n\<ge>N. B < x n" by auto
+next
+  assume *: "\<forall>B<C. \<exists>N. \<forall>n\<ge>N. B < x n"
+  { fix B assume "B<C"
+    then obtain N where "\<forall>n\<ge>N. B < x n" using `?rhs` by auto
+    hence "B \<le> (INF m:{N..}. x m)" by (intro le_INFI) auto
+    also have "... \<le> liminf x" unfolding liminf_SUPR_INFI by (intro le_SUPI) simp
+    finally have "B \<le> liminf x" .
+  } then show "?lhs" by (metis * leD liminf_bounded linorder_le_less_linear)
+qed
+
+lemma liminf_subseq_mono:
+  fixes X :: "nat \<Rightarrow> extreal"
+  assumes "subseq r"
+  shows "liminf X \<le> liminf (X \<circ> r) "
+proof-
+  have "\<And>n. (INF m:{n..}. X m) \<le> (INF m:{n..}. (X \<circ> r) m)"
+  proof (safe intro!: INF_mono)
+    fix n m :: nat assume "n \<le> m" then show "\<exists>ma\<in>{n..}. X ma \<le> (X \<circ> r) m"
+      using seq_suble[OF `subseq r`, of m] by (intro bexI[of _ "r m"]) auto
+  qed
+  then show ?thesis by (auto intro!: SUP_mono simp: liminf_SUPR_INFI comp_def)
+qed
+
+lemma extreal_real': assumes "\<bar>x\<bar> \<noteq> \<infinity>" shows "extreal (real x) = x"
+  using assms by auto
+
+lemma extreal_le_extreal_bounded:
+  fixes x y z :: extreal
+  assumes "z \<le> y"
+  assumes *: "\<And>B. z < B \<Longrightarrow> B < x \<Longrightarrow> B \<le> y"
+  shows "x \<le> y"
+proof (rule extreal_le_extreal)
+  fix B assume "B < x"
+  show "B \<le> y"
+  proof cases
+    assume "z < B" from *[OF this `B < x`] show "B \<le> y" .
+  next
+    assume "\<not> z < B" with `z \<le> y` show "B \<le> y" by auto
+  qed
+qed
+
+lemma fixes x y :: extreal
+  shows Sup_atMost[simp]: "Sup {.. y} = y"
+    and Sup_lessThan[simp]: "Sup {..< y} = y"
+    and Sup_atLeastAtMost[simp]: "x \<le> y \<Longrightarrow> Sup { x .. y} = y"
+    and Sup_greaterThanAtMost[simp]: "x < y \<Longrightarrow> Sup { x <.. y} = y"
+    and Sup_atLeastLessThan[simp]: "x < y \<Longrightarrow> Sup { x ..< y} = y"
+  by (auto simp: Sup_extreal_def intro!: Least_equality
+           intro: extreal_le_extreal extreal_le_extreal_bounded[of x])
+
+lemma Sup_greaterThanlessThan[simp]:
+  fixes x y :: extreal assumes "x < y" shows "Sup { x <..< y} = y"
+  unfolding Sup_extreal_def
+proof (intro Least_equality extreal_le_extreal_bounded[of _ _ y])
+  fix z assume z: "\<forall>u\<in>{x<..<y}. u \<le> z"
+  from extreal_dense[OF `x < y`] guess w .. note w = this
+  with z[THEN bspec, of w] show "x \<le> z" by auto
+qed auto
+
+lemma real_extreal_id: "real o extreal = id"
+proof-
+{ fix x have "(real o extreal) x = id x" by auto }
+from this show ?thesis using ext by blast
+qed
+
+
+lemma open_image_extreal: "open(UNIV-{\<infinity>,(-\<infinity>)})"
+by (metis range_extreal open_extreal open_UNIV)
+
+lemma extreal_le_distrib:
+  fixes a b c :: extreal shows "c * (a + b) \<le> c * a + c * b"
+  by (cases rule: extreal3_cases[of a b c])
+     (auto simp add: field_simps not_le mult_le_0_iff mult_less_0_iff)
+
+lemma extreal_pos_distrib:
+  fixes a b c :: extreal assumes "0 \<le> c" "c \<noteq> \<infinity>" shows "c * (a + b) = c * a + c * b"
+  using assms by (cases rule: extreal3_cases[of a b c])
+                 (auto simp add: field_simps not_le mult_le_0_iff mult_less_0_iff)
+
+lemma extreal_pos_le_distrib:
+fixes a b c :: extreal
+assumes "c>=0"
+shows "c * (a + b) <= c * a + c * b"
+  using assms by (cases rule: extreal3_cases[of a b c])
+                 (auto simp add: field_simps)
+
+lemma extreal_max_mono:
+  "[| (a::extreal) <= b; c <= d |] ==> max a c <= max b d"
+  by (metis sup_extreal_def sup_mono)
+
+
+lemma extreal_max_least:
+  "[| (a::extreal) <= x; c <= x |] ==> max a c <= x"
+  by (metis sup_extreal_def sup_least)
+
+end
--- a/src/HOL/Limits.thy	Mon Mar 14 15:17:10 2011 +0100
+++ b/src/HOL/Limits.thy	Mon Mar 14 15:29:10 2011 +0100
@@ -103,7 +103,6 @@
   shows "eventually (\<lambda>i. R i) net"
 using assms by (auto elim!: eventually_rev_mp)
 
-
 subsection {* Finer-than relation *}
 
 text {* @{term "net \<le> net'"} means that @{term net} is finer than
@@ -231,7 +230,6 @@
   "eventually (\<lambda>x. False) net \<longleftrightarrow> net = bot"
 unfolding expand_net_eq by (auto elim: eventually_rev_mp)
 
-
 subsection {* Map function for nets *}
 
 definition netmap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a net \<Rightarrow> 'b net" where
@@ -287,6 +285,13 @@
 by (safe, fast, drule_tac x=N in spec, auto elim: eventually_rev_mp)
 
 
+definition
+  trivial_limit :: "'a net \<Rightarrow> bool" where
+  "trivial_limit net \<longleftrightarrow> eventually (\<lambda>x. False) net"
+
+lemma trivial_limit_sequentially[intro]: "\<not> trivial_limit sequentially"
+  by (auto simp add: trivial_limit_def eventually_sequentially)
+
 subsection {* Standard Nets *}
 
 definition within :: "'a net \<Rightarrow> 'a set \<Rightarrow> 'a net" (infixr "within" 70) where
@@ -827,4 +832,29 @@
     \<Longrightarrow> ((\<lambda>x. f x / g x) ---> a / b) net"
 by (simp add: mult.tendsto tendsto_inverse divide_inverse)
 
+lemma tendsto_unique:
+  fixes f :: "'a \<Rightarrow> 'b::t2_space"
+  assumes "\<not> trivial_limit net"  "(f ---> l) net"  "(f ---> l') net"
+  shows "l = l'"
+proof (rule ccontr)
+  assume "l \<noteq> l'"
+  obtain U V where "open U" "open V" "l \<in> U" "l' \<in> V" "U \<inter> V = {}"
+    using hausdorff [OF `l \<noteq> l'`] by fast
+  have "eventually (\<lambda>x. f x \<in> U) net"
+    using `(f ---> l) net` `open U` `l \<in> U` by (rule topological_tendstoD)
+  moreover
+  have "eventually (\<lambda>x. f x \<in> V) net"
+    using `(f ---> l') net` `open V` `l' \<in> V` by (rule topological_tendstoD)
+  ultimately
+  have "eventually (\<lambda>x. False) net"
+  proof (rule eventually_elim2)
+    fix x
+    assume "f x \<in> U" "f x \<in> V"
+    hence "f x \<in> U \<inter> V" by simp
+    with `U \<inter> V = {}` show "False" by simp
+  qed
+  with `\<not> trivial_limit net` show "False"
+    by (simp add: trivial_limit_def)
+qed
+
 end
--- a/src/HOL/Multivariate_Analysis/Derivative.thy	Mon Mar 14 15:17:10 2011 +0100
+++ b/src/HOL/Multivariate_Analysis/Derivative.thy	Mon Mar 14 15:29:10 2011 +0100
@@ -1129,11 +1129,11 @@
     show "bounded_linear (g' x)" unfolding linear_linear linear_def apply(rule,rule,rule) defer proof(rule,rule)
       fix x' y z::"'m" and c::real
       note lin = assms(2)[rule_format,OF `x\<in>s`,THEN derivative_linear]
-      show "g' x (c *\<^sub>R x') = c *\<^sub>R g' x x'" apply(rule Lim_unique[OF trivial_limit_sequentially])
+      show "g' x (c *\<^sub>R x') = c *\<^sub>R g' x x'" apply(rule tendsto_unique[OF trivial_limit_sequentially])
         apply(rule lem3[rule_format])
         unfolding lin[unfolded bounded_linear_def bounded_linear_axioms_def,THEN conjunct2,THEN conjunct1,rule_format]
         apply(rule Lim_cmul) by(rule lem3[rule_format])
-      show "g' x (y + z) = g' x y + g' x z" apply(rule Lim_unique[OF trivial_limit_sequentially])
+      show "g' x (y + z) = g' x y + g' x z" apply(rule tendsto_unique[OF trivial_limit_sequentially])
         apply(rule lem3[rule_format]) unfolding lin[unfolded bounded_linear_def additive_def,THEN conjunct1,rule_format]
         apply(rule Lim_add) by(rule lem3[rule_format])+ qed 
     show "\<forall>e>0. \<exists>d>0. \<forall>y\<in>s. norm (y - x) < d \<longrightarrow> norm (g y - g x - g' x (y - x)) \<le> e * norm (y - x)" proof(rule,rule) case goal1
--- a/src/HOL/Multivariate_Analysis/Euclidean_Space.thy	Mon Mar 14 15:17:10 2011 +0100
+++ b/src/HOL/Multivariate_Analysis/Euclidean_Space.thy	Mon Mar 14 15:29:10 2011 +0100
@@ -326,42 +326,6 @@
 
 text{* Hence more metric properties. *}
 
-lemma dist_triangle_alt:
-  fixes x y z :: "'a::metric_space"
-  shows "dist y z <= dist x y + dist x z"
-by (rule dist_triangle3)
-
-lemma dist_pos_lt:
-  fixes x y :: "'a::metric_space"
-  shows "x \<noteq> y ==> 0 < dist x y"
-by (simp add: zero_less_dist_iff)
-
-lemma dist_nz:
-  fixes x y :: "'a::metric_space"
-  shows "x \<noteq> y \<longleftrightarrow> 0 < dist x y"
-by (simp add: zero_less_dist_iff)
-
-lemma dist_triangle_le:
-  fixes x y z :: "'a::metric_space"
-  shows "dist x z + dist y z <= e \<Longrightarrow> dist x y <= e"
-by (rule order_trans [OF dist_triangle2])
-
-lemma dist_triangle_lt:
-  fixes x y z :: "'a::metric_space"
-  shows "dist x z + dist y z < e ==> dist x y < e"
-by (rule le_less_trans [OF dist_triangle2])
-
-lemma dist_triangle_half_l:
-  fixes x1 x2 y :: "'a::metric_space"
-  shows "dist x1 y < e / 2 \<Longrightarrow> dist x2 y < e / 2 \<Longrightarrow> dist x1 x2 < e"
-by (rule dist_triangle_lt [where z=y], simp)
-
-lemma dist_triangle_half_r:
-  fixes x1 x2 y :: "'a::metric_space"
-  shows "dist y x1 < e / 2 \<Longrightarrow> dist y x2 < e / 2 \<Longrightarrow> dist x1 x2 < e"
-by (rule dist_triangle_half_l, simp_all add: dist_commute)
-
-
 lemma norm_triangle_half_r:
   shows "norm (y - x1) < e / 2 \<Longrightarrow> norm (y - x2) < e / 2 \<Longrightarrow> norm (x1 - x2) < e"
   using dist_triangle_half_r unfolding dist_norm[THEN sym] by auto
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy	Mon Mar 14 15:29:10 2011 +0100
@@ -0,0 +1,1261 @@
+(* Title: src/HOL/Multivariate_Analysis/Extended_Reals.thy
+   Author: Johannes Hölzl; TU München
+   Author: Robert Himmelmann; TU München
+   Author: Armin Heller; TU München
+   Author: Bogdan Grechuk; University of Edinburgh *)
+
+header {* Limits on the Extended real number line *}
+
+theory Extended_Real_Limits
+  imports Topology_Euclidean_Space "~~/src/HOL/Library/Extended_Reals"
+begin
+
+lemma continuous_on_extreal[intro, simp]: "continuous_on A extreal"
+  unfolding continuous_on_topological open_extreal_def by auto
+
+lemma continuous_at_extreal[intro, simp]: "continuous (at x) extreal"
+  using continuous_on_eq_continuous_at[of UNIV] by auto
+
+lemma continuous_within_extreal[intro, simp]: "x \<in> A \<Longrightarrow> continuous (at x within A) extreal"
+  using continuous_on_eq_continuous_within[of A] by auto
+
+lemma extreal_open_uminus:
+  fixes S :: "extreal set"
+  assumes "open S"
+  shows "open (uminus ` S)"
+  unfolding open_extreal_def
+proof (intro conjI impI)
+  obtain x y where S: "open (extreal -` S)"
+    "\<infinity> \<in> S \<Longrightarrow> {extreal x<..} \<subseteq> S" "-\<infinity> \<in> S \<Longrightarrow> {..< extreal y} \<subseteq> S"
+    using `open S` unfolding open_extreal_def by auto
+  have "extreal -` uminus ` S = uminus ` (extreal -` S)"
+  proof safe
+    fix x y assume "extreal x = - y" "y \<in> S"
+    then show "x \<in> uminus ` extreal -` S" by (cases y) auto
+  next
+    fix x assume "extreal x \<in> S"
+    then show "- x \<in> extreal -` uminus ` S"
+      by (auto intro: image_eqI[of _ _ "extreal x"])
+  qed
+  then show "open (extreal -` uminus ` S)"
+    using S by (auto intro: open_negations)
+  { assume "\<infinity> \<in> uminus ` S"
+    then have "-\<infinity> \<in> S" by (metis image_iff extreal_uminus_uminus)
+    then have "uminus ` {..<extreal y} \<subseteq> uminus ` S" using S by (intro image_mono) auto
+    then show "\<exists>x. {extreal x<..} \<subseteq> uminus ` S" using extreal_uminus_lessThan by auto }
+  { assume "-\<infinity> \<in> uminus ` S"
+    then have "\<infinity> : S" by (metis image_iff extreal_uminus_uminus)
+    then have "uminus ` {extreal x<..} <= uminus ` S" using S by (intro image_mono) auto
+    then show "\<exists>y. {..<extreal y} <= uminus ` S" using extreal_uminus_greaterThan by auto }
+qed
+
+lemma extreal_uminus_complement:
+  fixes S :: "extreal set"
+  shows "uminus ` (- S) = - uminus ` S"
+  by (auto intro!: bij_image_Compl_eq surjI[of _ uminus] simp: bij_betw_def)
+
+lemma extreal_closed_uminus:
+  fixes S :: "extreal set"
+  assumes "closed S"
+  shows "closed (uminus ` S)"
+using assms unfolding closed_def
+using extreal_open_uminus[of "- S"] extreal_uminus_complement by auto
+
+lemma not_open_extreal_singleton:
+  "\<not> (open {a :: extreal})"
+proof(rule ccontr)
+  assume "\<not> \<not> open {a}" hence a: "open {a}" by auto
+  show False
+  proof (cases a)
+    case MInf
+    then obtain y where "{..<extreal y} <= {a}" using a open_MInfty2[of "{a}"] by auto
+    hence "extreal(y - 1):{a}" apply (subst subsetD[of "{..<extreal y}"]) by auto
+    then show False using `a=(-\<infinity>)` by auto
+  next
+    case PInf
+    then obtain y where "{extreal y<..} <= {a}" using a open_PInfty2[of "{a}"] by auto
+    hence "extreal(y+1):{a}" apply (subst subsetD[of "{extreal y<..}"]) by auto
+    then show False using `a=\<infinity>` by auto
+  next
+    case (real r) then have fin: "\<bar>a\<bar> \<noteq> \<infinity>" by simp
+    from extreal_open_cont_interval[OF a singletonI this] guess e . note e = this
+    then obtain b where b_def: "a<b & b<a+e"
+      using fin extreal_between extreal_dense[of a "a+e"] by auto
+    then have "b: {a-e <..< a+e}" using fin extreal_between[of a e] e by auto
+    then show False using b_def e by auto
+  qed
+qed
+
+lemma extreal_closed_contains_Inf:
+  fixes S :: "extreal set"
+  assumes "closed S" "S ~= {}"
+  shows "Inf S : S"
+proof(rule ccontr)
+  assume "Inf S \<notin> S" hence a: "open (-S)" "Inf S:(- S)" using assms by auto
+  show False
+  proof (cases "Inf S")
+    case MInf hence "(-\<infinity>) : - S" using a by auto
+    then obtain y where "{..<extreal y} <= (-S)" using a open_MInfty2[of "- S"] by auto
+    hence "extreal y <= Inf S" by (metis Compl_anti_mono Compl_lessThan atLeast_iff
+      complete_lattice_class.Inf_greatest double_complement set_rev_mp)
+    then show False using MInf by auto
+  next
+    case PInf then have "S={\<infinity>}" by (metis Inf_eq_PInfty assms(2))
+    then show False by (metis `Inf S ~: S` insert_code mem_def PInf)
+  next
+    case (real r) then have fin: "\<bar>Inf S\<bar> \<noteq> \<infinity>" by simp
+    from extreal_open_cont_interval[OF a this] guess e . note e = this
+    { fix x assume "x:S" hence "x>=Inf S" by (rule complete_lattice_class.Inf_lower)
+      hence *: "x>Inf S-e" using e by (metis fin extreal_between(1) order_less_le_trans)
+      { assume "x<Inf S+e" hence "x:{Inf S-e <..< Inf S+e}" using * by auto
+        hence False using e `x:S` by auto
+      } hence "x>=Inf S+e" by (metis linorder_le_less_linear)
+    } hence "Inf S + e <= Inf S" by (metis le_Inf_iff)
+    then show False using real e by (cases e) auto
+  qed
+qed
+
+lemma extreal_closed_contains_Sup:
+  fixes S :: "extreal set"
+  assumes "closed S" "S ~= {}"
+  shows "Sup S : S"
+proof-
+  have "closed (uminus ` S)" by (metis assms(1) extreal_closed_uminus)
+  hence "Inf (uminus ` S) : uminus ` S" using assms extreal_closed_contains_Inf[of "uminus ` S"] by auto
+  hence "- Sup S : uminus ` S" using extreal_Sup_uminus_image_eq[of "uminus ` S"] by (auto simp: image_image)
+  thus ?thesis by (metis imageI extreal_uminus_uminus extreal_minus_minus_image)
+qed
+
+lemma extreal_open_closed_aux:
+  fixes S :: "extreal set"
+  assumes "open S" "closed S"
+  assumes S: "(-\<infinity>) ~: S"
+  shows "S = {}"
+proof(rule ccontr)
+  assume "S ~= {}"
+  hence *: "(Inf S):S" by (metis assms(2) extreal_closed_contains_Inf)
+  { assume "Inf S=(-\<infinity>)" hence False using * assms(3) by auto }
+  moreover
+  { assume "Inf S=\<infinity>" hence "S={\<infinity>}" by (metis Inf_eq_PInfty `S ~= {}`)
+    hence False by (metis assms(1) not_open_extreal_singleton) }
+  moreover
+  { assume fin: "\<bar>Inf S\<bar> \<noteq> \<infinity>"
+    from extreal_open_cont_interval[OF assms(1) * fin] guess e . note e = this
+    then obtain b where b_def: "Inf S-e<b & b<Inf S"
+      using fin extreal_between[of "Inf S" e] extreal_dense[of "Inf S-e"] by auto
+    hence "b: {Inf S-e <..< Inf S+e}" using e fin extreal_between[of "Inf S" e] by auto
+    hence "b:S" using e by auto
+    hence False using b_def by (metis complete_lattice_class.Inf_lower leD)
+  } ultimately show False by auto
+qed
+
+lemma extreal_open_closed:
+  fixes S :: "extreal set"
+  shows "(open S & closed S) <-> (S = {} | S = UNIV)"
+proof-
+{ assume lhs: "open S & closed S"
+  { assume "(-\<infinity>) ~: S" hence "S={}" using lhs extreal_open_closed_aux by auto }
+  moreover
+  { assume "(-\<infinity>) : S" hence "(- S)={}" using lhs extreal_open_closed_aux[of "-S"] by auto }
+  ultimately have "S = {} | S = UNIV" by auto
+} thus ?thesis by auto
+qed
+
+lemma extreal_open_affinity_pos:
+  assumes "open S" and m: "m \<noteq> \<infinity>" "0 < m" and t: "\<bar>t\<bar> \<noteq> \<infinity>"
+  shows "open ((\<lambda>x. m * x + t) ` S)"
+proof -
+  obtain r where r[simp]: "m = extreal r" using m by (cases m) auto
+  obtain p where p[simp]: "t = extreal p" using t by auto
+  have "r \<noteq> 0" "0 < r" and m': "m \<noteq> \<infinity>" "m \<noteq> -\<infinity>" "m \<noteq> 0" using m by auto
+  from `open S`[THEN extreal_openE] guess l u . note T = this
+  let ?f = "(\<lambda>x. m * x + t)"
+  show ?thesis unfolding open_extreal_def
+  proof (intro conjI impI exI subsetI)
+    have "extreal -` ?f ` S = (\<lambda>x. r * x + p) ` (extreal -` S)"
+    proof safe
+      fix x y assume "extreal y = m * x + t" "x \<in> S"
+      then show "y \<in> (\<lambda>x. r * x + p) ` extreal -` S"
+        using `r \<noteq> 0` by (cases x) (auto intro!: image_eqI[of _ _ "real x"] split: split_if_asm)
+    qed force
+    then show "open (extreal -` ?f ` S)"
+      using open_affinity[OF T(1) `r \<noteq> 0`] by (auto simp: ac_simps)
+  next
+    assume "\<infinity> \<in> ?f`S" with `0 < r` have "\<infinity> \<in> S" by auto
+    fix x assume "x \<in> {extreal (r * l + p)<..}"
+    then have [simp]: "extreal (r * l + p) < x" by auto
+    show "x \<in> ?f`S"
+    proof (rule image_eqI)
+      show "x = m * ((x - t) / m) + t"
+        using m t by (cases rule: extreal3_cases[of m x t]) auto
+      have "extreal l < (x - t)/m"
+        using m t by (simp add: extreal_less_divide_pos extreal_less_minus)
+      then show "(x - t)/m \<in> S" using T(2)[OF `\<infinity> \<in> S`] by auto
+    qed
+  next
+    assume "-\<infinity> \<in> ?f`S" with `0 < r` have "-\<infinity> \<in> S" by auto
+    fix x assume "x \<in> {..<extreal (r * u + p)}"
+    then have [simp]: "x < extreal (r * u + p)" by auto
+    show "x \<in> ?f`S"
+    proof (rule image_eqI)
+      show "x = m * ((x - t) / m) + t"
+        using m t by (cases rule: extreal3_cases[of m x t]) auto
+      have "(x - t)/m < extreal u"
+        using m t by (simp add: extreal_divide_less_pos extreal_minus_less)
+      then show "(x - t)/m \<in> S" using T(3)[OF `-\<infinity> \<in> S`] by auto
+    qed
+  qed
+qed
+
+lemma extreal_open_affinity:
+  assumes "open S" and m: "\<bar>m\<bar> \<noteq> \<infinity>" "m \<noteq> 0" and t: "\<bar>t\<bar> \<noteq> \<infinity>"
+  shows "open ((\<lambda>x. m * x + t) ` S)"
+proof cases
+  assume "0 < m" then show ?thesis
+    using extreal_open_affinity_pos[OF `open S` _ _ t, of m] m by auto
+next
+  assume "\<not> 0 < m" then
+  have "0 < -m" using `m \<noteq> 0` by (cases m) auto
+  then have m: "-m \<noteq> \<infinity>" "0 < -m" using `\<bar>m\<bar> \<noteq> \<infinity>`
+    by (auto simp: extreal_uminus_eq_reorder)
+  from extreal_open_affinity_pos[OF extreal_open_uminus[OF `open S`] m t]
+  show ?thesis unfolding image_image by simp
+qed
+
+lemma extreal_lim_mult:
+  fixes X :: "'a \<Rightarrow> extreal"
+  assumes lim: "(X ---> L) net" and a: "\<bar>a\<bar> \<noteq> \<infinity>"
+  shows "((\<lambda>i. a * X i) ---> a * L) net"
+proof cases
+  assume "a \<noteq> 0"
+  show ?thesis
+  proof (rule topological_tendstoI)
+    fix S assume "open S" "a * L \<in> S"
+    have "a * L / a = L"
+      using `a \<noteq> 0` a by (cases rule: extreal2_cases[of a L]) auto
+    then have L: "L \<in> ((\<lambda>x. x / a) ` S)"
+      using `a * L \<in> S` by (force simp: image_iff)
+    moreover have "open ((\<lambda>x. x / a) ` S)"
+      using extreal_open_affinity[OF `open S`, of "inverse a" 0] `a \<noteq> 0` a
+      by (auto simp: extreal_divide_eq extreal_inverse_eq_0 divide_extreal_def ac_simps)
+    note * = lim[THEN topological_tendstoD, OF this L]
+    { fix x from a `a \<noteq> 0` have "a * (x / a) = x"
+        by (cases rule: extreal2_cases[of a x]) auto }
+    note this[simp]
+    show "eventually (\<lambda>x. a * X x \<in> S) net"
+      by (rule eventually_mono[OF _ *]) auto
+  qed
+qed auto
+
+lemma extreal_lim_uminus:
+  fixes X :: "'a \<Rightarrow> extreal" shows "((\<lambda>i. - X i) ---> -L) net \<longleftrightarrow> (X ---> L) net"
+  using extreal_lim_mult[of X L net "extreal (-1)"]
+        extreal_lim_mult[of "(\<lambda>i. - X i)" "-L" net "extreal (-1)"]
+  by (auto simp add: algebra_simps)
+
+lemma Lim_bounded2_extreal:
+  assumes lim:"f ----> (l :: extreal)"
+  and ge: "ALL n>=N. f n >= C"
+  shows "l>=C"
+proof-
+def g == "(%i. -(f i))"
+{ fix n assume "n>=N" hence "g n <= -C" using assms extreal_minus_le_minus g_def by auto }
+hence "ALL n>=N. g n <= -C" by auto
+moreover have limg: "g ----> (-l)" using g_def extreal_lim_uminus lim by auto
+ultimately have "-l <= -C" using Lim_bounded_extreal[of g "-l" _ "-C"] by auto
+from this show ?thesis using extreal_minus_le_minus by auto
+qed
+
+
+lemma extreal_open_atLeast: "open {x..} \<longleftrightarrow> x = -\<infinity>"
+proof
+  assume "x = -\<infinity>" then have "{x..} = UNIV" by auto
+  then show "open {x..}" by auto
+next
+  assume "open {x..}"
+  then have "open {x..} \<and> closed {x..}" by auto
+  then have "{x..} = UNIV" unfolding extreal_open_closed by auto
+  then show "x = -\<infinity>" by (simp add: bot_extreal_def atLeast_eq_UNIV_iff)
+qed
+
+lemma extreal_open_mono_set:
+  fixes S :: "extreal set"
+  defines "a \<equiv> Inf S"
+  shows "(open S \<and> mono S) \<longleftrightarrow> (S = UNIV \<or> S = {a <..})"
+  by (metis Inf_UNIV a_def atLeast_eq_UNIV_iff extreal_open_atLeast
+            extreal_open_closed mono_set_iff open_extreal_greaterThan)
+
+lemma extreal_closed_mono_set:
+  fixes S :: "extreal set"
+  shows "(closed S \<and> mono S) \<longleftrightarrow> (S = {} \<or> S = {Inf S ..})"
+  by (metis Inf_UNIV atLeast_eq_UNIV_iff closed_extreal_atLeast
+            extreal_open_closed mono_empty mono_set_iff open_extreal_greaterThan)
+
+lemma extreal_Liminf_Sup_monoset:
+  fixes f :: "'a => extreal"
+  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}"
+  unfolding Liminf_Sup
+proof (intro arg_cong[where f="\<lambda>P. Sup (Collect P)"] ext iffI allI impI)
+  fix l S assume ev: "\<forall>y<l. eventually (\<lambda>x. y < f x) net" and "open S" "mono S" "l \<in> S"
+  then have "S = UNIV \<or> S = {Inf S <..}"
+    using extreal_open_mono_set[of S] by auto
+  then show "eventually (\<lambda>x. f x \<in> S) net"
+  proof
+    assume S: "S = {Inf S<..}"
+    then have "Inf S < l" using `l \<in> S` by auto
+    then have "eventually (\<lambda>x. Inf S < f x) net" using ev by auto
+    then show "eventually (\<lambda>x. f x \<in> S) net"  by (subst S) auto
+  qed auto
+next
+  fix l y assume S: "\<forall>S. open S \<longrightarrow> mono S \<longrightarrow> l \<in> S \<longrightarrow> eventually  (\<lambda>x. f x \<in> S) net" "y < l"
+  have "eventually  (\<lambda>x. f x \<in> {y <..}) net"
+    using `y < l` by (intro S[rule_format]) auto
+  then show "eventually (\<lambda>x. y < f x) net" by auto
+qed
+
+lemma extreal_Limsup_Inf_monoset:
+  fixes f :: "'a => extreal"
+  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}"
+  unfolding Limsup_Inf
+proof (intro arg_cong[where f="\<lambda>P. Inf (Collect P)"] ext iffI allI impI)
+  fix l S assume ev: "\<forall>y>l. eventually (\<lambda>x. f x < y) net" and "open S" "mono (uminus`S)" "l \<in> S"
+  then have "open (uminus`S) \<and> mono (uminus`S)" by (simp add: extreal_open_uminus)
+  then have "S = UNIV \<or> S = {..< Sup S}"
+    unfolding extreal_open_mono_set extreal_Inf_uminus_image_eq extreal_image_uminus_shift by simp
+  then show "eventually (\<lambda>x. f x \<in> S) net"
+  proof
+    assume S: "S = {..< Sup S}"
+    then have "l < Sup S" using `l \<in> S` by auto
+    then have "eventually (\<lambda>x. f x < Sup S) net" using ev by auto
+    then show "eventually (\<lambda>x. f x \<in> S) net"  by (subst S) auto
+  qed auto
+next
+  fix l y assume S: "\<forall>S. open S \<longrightarrow> mono (uminus`S) \<longrightarrow> l \<in> S \<longrightarrow> eventually  (\<lambda>x. f x \<in> S) net" "l < y"
+  have "eventually  (\<lambda>x. f x \<in> {..< y}) net"
+    using `l < y` by (intro S[rule_format]) auto
+  then show "eventually (\<lambda>x. f x < y) net" by auto
+qed
+
+
+lemma open_uminus_iff: "open (uminus ` S) \<longleftrightarrow> open (S::extreal set)"
+  using extreal_open_uminus[of S] extreal_open_uminus[of "uminus`S"] by auto
+
+lemma extreal_Limsup_uminus:
+  fixes f :: "'a => extreal"
+  shows "Limsup net (\<lambda>x. - (f x)) = -(Liminf net f)"
+proof -
+  { fix P l have "(\<exists>x. (l::extreal) = -x \<and> P x) \<longleftrightarrow> P (-l)" by (auto intro!: exI[of _ "-l"]) }
+  note Ex_cancel = this
+  { fix P :: "extreal set \<Rightarrow> bool" have "(\<forall>S. P S) \<longleftrightarrow> (\<forall>S. P (uminus`S))"
+      apply auto by (erule_tac x="uminus`S" in allE) (auto simp: image_image) }
+  note add_uminus_image = this
+  { fix x S have "(x::extreal) \<in> uminus`S \<longleftrightarrow> -x\<in>S" by (auto intro!: image_eqI[of _ _ "-x"]) }
+  note remove_uminus_image = this
+  show ?thesis
+    unfolding extreal_Limsup_Inf_monoset extreal_Liminf_Sup_monoset
+    unfolding extreal_Inf_uminus_image_eq[symmetric] image_Collect Ex_cancel
+    by (subst add_uminus_image) (simp add: open_uminus_iff remove_uminus_image)
+qed
+
+lemma extreal_Liminf_uminus:
+  fixes f :: "'a => extreal"
+  shows "Liminf net (\<lambda>x. - (f x)) = -(Limsup net f)"
+  using extreal_Limsup_uminus[of _ "(\<lambda>x. - (f x))"] by auto
+
+lemma extreal_Lim_uminus:
+  fixes f :: "'a \<Rightarrow> extreal" shows "(f ---> f0) net \<longleftrightarrow> ((\<lambda>x. - f x) ---> - f0) net"
+  using
+    extreal_lim_mult[of f f0 net "- 1"]
+    extreal_lim_mult[of "\<lambda>x. - (f x)" "-f0" net "- 1"]
+  by (auto simp: extreal_uminus_reorder)
+
+lemma lim_imp_Limsup:
+  fixes f :: "'a => extreal"
+  assumes "\<not> trivial_limit net"
+  assumes lim: "(f ---> f0) net"
+  shows "Limsup net f = f0"
+  using extreal_Lim_uminus[of f f0] lim_imp_Liminf[of net "(%x. -(f x))" "-f0"]
+     extreal_Liminf_uminus[of net f] assms by simp
+
+lemma Liminf_PInfty:
+  fixes f :: "'a \<Rightarrow> extreal"
+  assumes "\<not> trivial_limit net"
+  shows "(f ---> \<infinity>) net \<longleftrightarrow> Liminf net f = \<infinity>"
+proof (intro lim_imp_Liminf iffI assms)
+  assume rhs: "Liminf net f = \<infinity>"
+  { fix S assume "open S & \<infinity> : S"
+    then obtain m where "{extreal m<..} <= S" using open_PInfty2 by auto
+    moreover have "eventually (\<lambda>x. f x \<in> {extreal m<..}) net"
+      using rhs unfolding Liminf_Sup top_extreal_def[symmetric] Sup_eq_top_iff
+      by (auto elim!: allE[where x="extreal m"] simp: top_extreal_def)
+    ultimately have "eventually (%x. f x : S) net" apply (subst eventually_mono) by auto
+  } then show "(f ---> \<infinity>) net" unfolding tendsto_def by auto
+qed
+
+lemma Limsup_MInfty:
+  fixes f :: "'a \<Rightarrow> extreal"
+  assumes "\<not> trivial_limit net"
+  shows "(f ---> -\<infinity>) net \<longleftrightarrow> Limsup net f = -\<infinity>"
+  using assms extreal_Lim_uminus[of f "-\<infinity>"] Liminf_PInfty[of _ "\<lambda>x. - (f x)"]
+        extreal_Liminf_uminus[of _ f] by (auto simp: extreal_uminus_eq_reorder)
+
+lemma extreal_Liminf_eq_Limsup:
+  fixes f :: "'a \<Rightarrow> extreal"
+  assumes ntriv: "\<not> trivial_limit net"
+  assumes lim: "Liminf net f = f0" "Limsup net f = f0"
+  shows "(f ---> f0) net"
+proof (cases f0)
+  case PInf then show ?thesis using Liminf_PInfty[OF ntriv] lim by auto
+next
+  case MInf then show ?thesis using Limsup_MInfty[OF ntriv] lim by auto
+next
+  case (real r)
+  show "(f ---> f0) net"
+  proof (rule topological_tendstoI)
+    fix S assume "open S""f0 \<in> S"
+    then obtain a b where "a < Liminf net f" "Limsup net f < b" "{a<..<b} \<subseteq> S"
+      using extreal_open_cont_interval2[of S f0] real lim by auto
+    then have "eventually (\<lambda>x. f x \<in> {a<..<b}) net"
+      unfolding Liminf_Sup Limsup_Inf less_Sup_iff Inf_less_iff
+      by (auto intro!: eventually_conj simp add: greaterThanLessThan_iff)
+    with `{a<..<b} \<subseteq> S` show "eventually (%x. f x : S) net"
+      by (rule_tac eventually_mono) auto
+  qed
+qed
+
+lemma extreal_Liminf_eq_Limsup_iff:
+  fixes f :: "'a \<Rightarrow> extreal"
+  assumes "\<not> trivial_limit net"
+  shows "(f ---> f0) net \<longleftrightarrow> Liminf net f = f0 \<and> Limsup net f = f0"
+  by (metis assms extreal_Liminf_eq_Limsup lim_imp_Liminf lim_imp_Limsup)
+
+lemma limsup_INFI_SUPR:
+  fixes f :: "nat \<Rightarrow> extreal"
+  shows "limsup f = (INF n. SUP m:{n..}. f m)"
+  using extreal_Limsup_uminus[of sequentially "\<lambda>x. - f x"]
+  by (simp add: liminf_SUPR_INFI extreal_INFI_uminus extreal_SUPR_uminus)
+
+lemma liminf_PInfty:
+  fixes X :: "nat => extreal"
+  shows "X ----> \<infinity> <-> liminf X = \<infinity>"
+by (metis Liminf_PInfty trivial_limit_sequentially)
+
+lemma limsup_MInfty:
+  fixes X :: "nat => extreal"
+  shows "X ----> (-\<infinity>) <-> limsup X = (-\<infinity>)"
+by (metis Limsup_MInfty trivial_limit_sequentially)
+
+lemma extreal_lim_mono:
+  fixes X Y :: "nat => extreal"
+  assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= Y n"
+  assumes "X ----> x" "Y ----> y"
+  shows "x <= y"
+  by (metis extreal_Liminf_eq_Limsup_iff[OF trivial_limit_sequentially] assms liminf_mono)
+
+lemma incseq_le_extreal:
+  fixes X :: "nat \<Rightarrow> extreal"
+  assumes inc: "incseq X" and lim: "X ----> L"
+  shows "X N \<le> L"
+  using inc
+  by (intro extreal_lim_mono[of N, OF _ Lim_const lim]) (simp add: incseq_def)
+
+lemma decseq_ge_extreal: assumes dec: "decseq X"
+  and lim: "X ----> (L::extreal)" shows "X N >= L"
+  using dec
+  by (intro extreal_lim_mono[of N, OF _ lim Lim_const]) (simp add: decseq_def)
+
+lemma liminf_bounded_open:
+  fixes x :: "nat \<Rightarrow> extreal"
+  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))" 
+  (is "_ \<longleftrightarrow> ?P x0")
+proof
+  assume "?P x0" then show "x0 \<le> liminf x"
+    unfolding extreal_Liminf_Sup_monoset eventually_sequentially
+    by (intro complete_lattice_class.Sup_upper) auto
+next
+  assume "x0 \<le> liminf x"
+  { fix S :: "extreal set" assume om: "open S & mono S & x0:S"
+    { assume "S = UNIV" hence "EX N. (ALL n>=N. x n : S)" by auto }
+    moreover
+    { assume "~(S=UNIV)"
+      then obtain B where B_def: "S = {B<..}" using om extreal_open_mono_set by auto
+      hence "B<x0" using om by auto
+      hence "EX N. ALL n>=N. x n : S" unfolding B_def using `x0 \<le> liminf x` liminf_bounded_iff by auto
+    } ultimately have "EX N. (ALL n>=N. x n : S)" by auto
+  } then show "?P x0" by auto
+qed
+
+lemma limsup_subseq_mono:
+  fixes X :: "nat \<Rightarrow> extreal"
+  assumes "subseq r"
+  shows "limsup (X \<circ> r) \<le> limsup X"
+proof-
+  have "(\<lambda>n. - X n) \<circ> r = (\<lambda>n. - (X \<circ> r) n)" by (simp add: fun_eq_iff)
+  then have "- limsup X \<le> - limsup (X \<circ> r)"
+     using liminf_subseq_mono[of r "(%n. - X n)"]
+       extreal_Liminf_uminus[of sequentially X]
+       extreal_Liminf_uminus[of sequentially "X o r"] assms by auto
+  then show ?thesis by auto
+qed
+
+lemma bounded_abs:
+  assumes "(a::real)<=x" "x<=b"
+  shows "abs x <= max (abs a) (abs b)"
+by (metis abs_less_iff assms leI le_max_iff_disj less_eq_real_def less_le_not_le less_minus_iff minus_minus)
+
+lemma bounded_increasing_convergent2: fixes f::"nat => real"
+  assumes "ALL n. f n <= B"  "ALL n m. n>=m --> f n >= f m"
+  shows "EX l. (f ---> l) sequentially"
+proof-
+def N == "max (abs (f 0)) (abs B)"
+{ fix n have "abs (f n) <= N" unfolding N_def apply (subst bounded_abs) using assms by auto }
+hence "bounded {f n| n::nat. True}" unfolding bounded_real by auto
+from this show ?thesis apply(rule Topology_Euclidean_Space.bounded_increasing_convergent)
+   using assms by auto
+qed
+lemma lim_extreal_increasing: assumes "\<And>n m. n >= m \<Longrightarrow> f n >= f m"
+  obtains l where "f ----> (l::extreal)"
+proof(cases "f = (\<lambda>x. - \<infinity>)")
+  case True then show thesis using Lim_const[of "- \<infinity>" sequentially] by (intro that[of "-\<infinity>"]) auto
+next
+  case False
+  from this obtain N where N_def: "f N > (-\<infinity>)" by (auto simp: fun_eq_iff)
+  have "ALL n>=N. f n >= f N" using assms by auto
+  hence minf: "ALL n>=N. f n > (-\<infinity>)" using N_def by auto
+  def Y == "(%n. (if n>=N then f n else f N))"
+  hence incy: "!!n m. n>=m ==> Y n >= Y m" using assms by auto
+  from minf have minfy: "ALL n. Y n ~= (-\<infinity>)" using Y_def by auto
+  show thesis
+  proof(cases "EX B. ALL n. f n < extreal B")
+    case False thus thesis apply- apply(rule that[of \<infinity>]) unfolding Lim_PInfty not_ex not_all
+    apply safe apply(erule_tac x=B in allE,safe) apply(rule_tac x=x in exI,safe)
+    apply(rule order_trans[OF _ assms[rule_format]]) by auto
+  next case True then guess B ..
+    hence "ALL n. Y n < extreal B" using Y_def by auto note B = this[rule_format]
+    { fix n have "Y n < \<infinity>" using B[of n] apply (subst less_le_trans) by auto
+      hence "Y n ~= \<infinity> & Y n ~= (-\<infinity>)" using minfy by auto
+    } hence *: "ALL n. \<bar>Y n\<bar> \<noteq> \<infinity>" by auto
+    { fix n have "real (Y n) < B" proof- case goal1 thus ?case
+        using B[of n] apply-apply(subst(asm) extreal_real'[THEN sym]) defer defer
+        unfolding extreal_less using * by auto
+      qed
+    }
+    hence B': "ALL n. (real (Y n) <= B)" using less_imp_le by auto
+    have "EX l. (%n. real (Y n)) ----> l"
+      apply(rule bounded_increasing_convergent2)
+    proof safe show "!!n. real (Y n) <= B" using B' by auto
+      fix n m::nat assume "n<=m"
+      hence "extreal (real (Y n)) <= extreal (real (Y m))"
+        using incy[rule_format,of n m] apply(subst extreal_real)+
+        using *[rule_format, of n] *[rule_format, of m] by auto
+      thus "real (Y n) <= real (Y m)" by auto
+    qed then guess l .. note l=this
+    have "Y ----> extreal l" using l apply-apply(subst(asm) lim_extreal[THEN sym])
+    unfolding extreal_real using * by auto
+    thus thesis apply-apply(rule that[of "extreal l"])
+       apply (subst tail_same_limit[of Y _ N]) using Y_def by auto
+  qed
+qed
+
+lemma lim_extreal_decreasing: assumes "\<And>n m. n >= m \<Longrightarrow> f n <= f m"
+  obtains l where "f ----> (l::extreal)"
+proof -
+  from lim_extreal_increasing[of "\<lambda>x. - f x"] assms
+  obtain l where "(\<lambda>x. - f x) ----> l" by auto
+  from extreal_lim_mult[OF this, of "- 1"] show thesis
+    by (intro that[of "-l"]) (simp add: extreal_uminus_eq_reorder)
+qed
+
+lemma compact_extreal:
+  fixes X :: "nat \<Rightarrow> extreal"
+  shows "\<exists>l r. subseq r \<and> (X \<circ> r) ----> l"
+proof -
+  obtain r where "subseq r" and mono: "monoseq (X \<circ> r)"
+    using seq_monosub[of X] unfolding comp_def by auto
+  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)"
+    by (auto simp add: monoseq_def)
+  then obtain l where "(X\<circ>r) ----> l"
+     using lim_extreal_increasing[of "X \<circ> r"] lim_extreal_decreasing[of "X \<circ> r"] by auto
+  then show ?thesis using `subseq r` by auto
+qed
+
+lemma extreal_Sup_lim:
+  assumes "\<And>n. b n \<in> s" "b ----> (a::extreal)"
+  shows "a \<le> Sup s"
+by (metis Lim_bounded_extreal assms complete_lattice_class.Sup_upper)
+
+lemma extreal_Inf_lim:
+  assumes "\<And>n. b n \<in> s" "b ----> (a::extreal)"
+  shows "Inf s \<le> a"
+by (metis Lim_bounded2_extreal assms complete_lattice_class.Inf_lower)
+
+lemma SUP_Lim_extreal:
+  fixes X :: "nat \<Rightarrow> extreal" assumes "incseq X" "X ----> l" shows "(SUP n. X n) = l"
+proof (rule extreal_SUPI)
+  fix n from assms show "X n \<le> l"
+    by (intro incseq_le_extreal) (simp add: incseq_def)
+next
+  fix y assume "\<And>n. n \<in> UNIV \<Longrightarrow> X n \<le> y"
+  with extreal_Sup_lim[OF _ `X ----> l`, of "{..y}"]
+  show "l \<le> y" by auto
+qed
+
+lemma LIMSEQ_extreal_SUPR:
+  fixes X :: "nat \<Rightarrow> extreal" assumes "incseq X" shows "X ----> (SUP n. X n)"
+proof (rule lim_extreal_increasing)
+  fix n m :: nat assume "m \<le> n" then show "X m \<le> X n"
+    using `incseq X` by (simp add: incseq_def)
+next
+  fix l assume "X ----> l"
+  with SUP_Lim_extreal[of X, OF assms this] show ?thesis by simp
+qed
+
+lemma INF_Lim_extreal: "decseq X \<Longrightarrow> X ----> l \<Longrightarrow> (INF n. X n) = (l::extreal)"
+  using SUP_Lim_extreal[of "\<lambda>i. - X i" "- l"]
+  by (simp add: extreal_SUPR_uminus extreal_lim_uminus)
+
+lemma LIMSEQ_extreal_INFI: "decseq X \<Longrightarrow> X ----> (INF n. X n :: extreal)"
+  using LIMSEQ_extreal_SUPR[of "\<lambda>i. - X i"]
+  by (simp add: extreal_SUPR_uminus extreal_lim_uminus)
+
+lemma SUP_eq_LIMSEQ:
+  assumes "mono f"
+  shows "(SUP n. extreal (f n)) = extreal x \<longleftrightarrow> f ----> x"
+proof
+  have inc: "incseq (\<lambda>i. extreal (f i))"
+    using `mono f` unfolding mono_def incseq_def by auto
+  { assume "f ----> x"
+   then have "(\<lambda>i. extreal (f i)) ----> extreal x" by auto
+   from SUP_Lim_extreal[OF inc this]
+   show "(SUP n. extreal (f n)) = extreal x" . }
+  { assume "(SUP n. extreal (f n)) = extreal x"
+    with LIMSEQ_extreal_SUPR[OF inc]
+    show "f ----> x" by auto }
+qed
+
+lemma Liminf_within:
+  fixes f :: "'a::metric_space => extreal"
+  shows "Liminf (at x within S) f = (SUP e:{0<..}. INF y:(S Int ball x e - {x}). f y)"
+proof-
+let ?l="(SUP e:{0<..}. INF y:(S Int ball x e - {x}). f y)"
+{ fix T assume T_def: "open T & mono T & ?l:T"
+  have "EX d>0. ALL y:S. 0 < dist y x & dist y x < d --> f y : T"
+  proof-
+  { assume "T=UNIV" hence ?thesis by (simp add: gt_ex) }
+  moreover
+  { assume "~(T=UNIV)"
+    then obtain B where "T={B<..}" using T_def extreal_open_mono_set[of T] by auto
+    hence "B<?l" using T_def by auto
+    then obtain d where d_def: "0<d & B<(INF y:(S Int ball x d - {x}). f y)"
+      unfolding less_SUP_iff by auto
+    { fix y assume "y:S & 0 < dist y x & dist y x < d"
+      hence "y:(S Int ball x d - {x})" unfolding ball_def by (auto simp add: dist_commute)
+      hence "f y:T" using d_def INF_leI[of y "S Int ball x d - {x}" f] `T={B<..}` by auto
+    } hence ?thesis apply(rule_tac x="d" in exI) using d_def by auto
+  } ultimately show ?thesis by auto
+  qed
+}
+moreover
+{ fix z
+  assume a: "ALL T. open T --> mono T --> z : T -->
+     (EX d>0. ALL y:S. 0 < dist y x & dist y x < d --> f y : T)"
+  { fix B assume "B<z"
+    then obtain d where d_def: "d>0 & (ALL y:S. 0 < dist y x & dist y x < d --> B < f y)"
+       using a[rule_format, of "{B<..}"] mono_greaterThan by auto
+    { fix y assume "y:(S Int ball x d - {x})"
+      hence "y:S & 0 < dist y x & dist y x < d" unfolding ball_def apply (simp add: dist_commute)
+         by (metis dist_eq_0_iff real_less_def zero_le_dist)
+      hence "B <= f y" using d_def by auto
+    } hence "B <= INFI (S Int ball x d - {x}) f" apply (subst le_INFI) by auto
+    also have "...<=?l" apply (subst le_SUPI) using d_def by auto
+    finally have "B<=?l" by auto
+  } hence "z <= ?l" using extreal_le_extreal[of z "?l"] by auto
+}
+ultimately show ?thesis unfolding extreal_Liminf_Sup_monoset eventually_within
+   apply (subst extreal_SupI[of _ "(SUP e:{0<..}. INFI (S Int ball x e - {x}) f)"]) by auto
+qed
+
+lemma Limsup_within:
+  fixes f :: "'a::metric_space => extreal"
+  shows "Limsup (at x within S) f = (INF e:{0<..}. SUP y:(S Int ball x e - {x}). f y)"
+proof-
+let ?l="(INF e:{0<..}. SUP y:(S Int ball x e - {x}). f y)"
+{ fix T assume T_def: "open T & mono (uminus ` T) & ?l:T"
+  have "EX d>0. ALL y:S. 0 < dist y x & dist y x < d --> f y : T"
+  proof-
+  { assume "T=UNIV" hence ?thesis by (simp add: gt_ex) }
+  moreover
+  { assume "~(T=UNIV)" hence "~(uminus ` T = UNIV)"
+       by (metis Int_UNIV_right Int_absorb1 image_mono extreal_minus_minus_image subset_UNIV)
+    hence "uminus ` T = {Inf (uminus ` T)<..}" using T_def extreal_open_mono_set[of "uminus ` T"]
+       extreal_open_uminus[of T] by auto
+    then obtain B where "T={..<B}"
+      unfolding extreal_Inf_uminus_image_eq extreal_uminus_lessThan[symmetric]
+      unfolding inj_image_eq_iff[OF extreal_inj_on_uminus] by simp
+    hence "?l<B" using T_def by auto
+    then obtain d where d_def: "0<d & (SUP y:(S Int ball x d - {x}). f y)<B"
+      unfolding INF_less_iff by auto
+    { fix y assume "y:S & 0 < dist y x & dist y x < d"
+      hence "y:(S Int ball x d - {x})" unfolding ball_def by (auto simp add: dist_commute)
+      hence "f y:T" using d_def le_SUPI[of y "S Int ball x d - {x}" f] `T={..<B}` by auto
+    } hence ?thesis apply(rule_tac x="d" in exI) using d_def by auto
+  } ultimately show ?thesis by auto
+  qed
+}
+moreover
+{ fix z
+  assume a: "ALL T. open T --> mono (uminus ` T) --> z : T -->
+     (EX d>0. ALL y:S. 0 < dist y x & dist y x < d --> f y : T)"
+  { fix B assume "z<B"
+    then obtain d where d_def: "d>0 & (ALL y:S. 0 < dist y x & dist y x < d --> f y<B)"
+       using a[rule_format, of "{..<B}"] by auto
+    { fix y assume "y:(S Int ball x d - {x})"
+      hence "y:S & 0 < dist y x & dist y x < d" unfolding ball_def apply (simp add: dist_commute)
+         by (metis dist_eq_0_iff real_less_def zero_le_dist)
+      hence "f y <= B" using d_def by auto
+    } hence "SUPR (S Int ball x d - {x}) f <= B" apply (subst SUP_leI) by auto
+    moreover have "?l<=SUPR (S Int ball x d - {x}) f" apply (subst INF_leI) using d_def by auto
+    ultimately have "?l<=B" by auto
+  } hence "?l <= z" using extreal_ge_extreal[of z "?l"] by auto
+}
+ultimately show ?thesis unfolding extreal_Limsup_Inf_monoset eventually_within
+   apply (subst extreal_InfI) by auto
+qed
+
+
+lemma Liminf_within_UNIV:
+  fixes f :: "'a::metric_space => extreal"
+  shows "Liminf (at x) f = Liminf (at x within UNIV) f"
+by (metis within_UNIV)
+
+
+lemma Liminf_at:
+  fixes f :: "'a::metric_space => extreal"
+  shows "Liminf (at x) f = (SUP e:{0<..}. INF y:(ball x e - {x}). f y)"
+using Liminf_within[of x UNIV f] Liminf_within_UNIV[of x f] by auto
+
+
+lemma Limsup_within_UNIV:
+  fixes f :: "'a::metric_space => extreal"
+  shows "Limsup (at x) f = Limsup (at x within UNIV) f"
+by (metis within_UNIV)
+
+
+lemma Limsup_at:
+  fixes f :: "'a::metric_space => extreal"
+  shows "Limsup (at x) f = (INF e:{0<..}. SUP y:(ball x e - {x}). f y)"
+using Limsup_within[of x UNIV f] Limsup_within_UNIV[of x f] by auto
+
+lemma Lim_within_constant:
+  fixes f :: "'a::metric_space => 'b::topological_space"
+  assumes "ALL y:S. f y = C"
+  shows "(f ---> C) (at x within S)"
+unfolding tendsto_def eventually_within
+by (metis assms(1) linorder_le_less_linear n_not_Suc_n real_of_nat_le_zero_cancel_iff)
+
+lemma Liminf_within_constant:
+  fixes f :: "'a::metric_space => extreal"
+  assumes "ALL y:S. f y = C"
+  assumes "~trivial_limit (at x within S)"
+  shows "Liminf (at x within S) f = C"
+by (metis Lim_within_constant assms lim_imp_Liminf)
+
+lemma Limsup_within_constant:
+  fixes f :: "'a::metric_space => extreal"
+  assumes "ALL y:S. f y = C"
+  assumes "~trivial_limit (at x within S)"
+  shows "Limsup (at x within S) f = C"
+by (metis Lim_within_constant assms lim_imp_Limsup)
+
+lemma islimpt_punctured:
+"x islimpt S = x islimpt (S-{x})"
+unfolding islimpt_def by blast
+
+
+lemma islimpt_in_closure:
+"(x islimpt S) = (x:closure(S-{x}))"
+unfolding closure_def using islimpt_punctured by blast
+
+
+lemma not_trivial_limit_within:
+  "~trivial_limit (at x within S) = (x:closure(S-{x}))"
+using islimpt_in_closure by (metis trivial_limit_within)
+
+
+lemma not_trivial_limit_within_ball:
+  "(~trivial_limit (at x within S)) = (ALL e>0. S Int ball x e - {x} ~= {})"
+  (is "?lhs = ?rhs")
+proof-
+{ assume "?lhs"
+  { fix e :: real assume "e>0"
+    then obtain y where "y:(S-{x}) & dist y x < e"
+       using `?lhs` not_trivial_limit_within[of x S] closure_approachable[of x "S - {x}"] by auto
+    hence "y : (S Int ball x e - {x})" unfolding ball_def by (simp add: dist_commute)
+    hence "S Int ball x e - {x} ~= {}" by blast
+  } hence "?rhs" by auto
+}
+moreover
+{ assume "?rhs"
+  { fix e :: real assume "e>0"
+    then obtain y where "y : (S Int ball x e - {x})" using `?rhs` by blast
+    hence "y:(S-{x}) & dist y x < e" unfolding ball_def by (simp add: dist_commute)
+    hence "EX y:(S-{x}). dist y x < e" by auto
+  } hence "?lhs" using not_trivial_limit_within[of x S] closure_approachable[of x "S - {x}"] by auto
+} ultimately show ?thesis by auto
+qed
+
+subsubsection {* Continuity *}
+
+lemma continuous_imp_tendsto:
+  assumes "continuous (at x0) f"
+  assumes "x ----> x0"
+  shows "(f o x) ----> (f x0)"
+proof-
+{ fix S assume "open S & (f x0):S"
+  from this obtain T where T_def: "open T & x0 : T & (ALL x:T. f x : S)"
+     using assms continuous_at_open by metis
+  hence "(EX N. ALL n>=N. x n : T)" using assms tendsto_explicit T_def by auto
+  hence "(EX N. ALL n>=N. f(x n) : S)" using T_def by auto
+} from this show ?thesis using tendsto_explicit[of "f o x" "f x0"] by auto
+qed
+
+
+lemma continuous_at_sequentially2:
+fixes f :: "'a::metric_space => 'b:: topological_space"
+shows "continuous (at x0) f <-> (ALL x. (x ----> x0) --> (f o x) ----> (f x0))"
+proof-
+{ assume "~(continuous (at x0) f)"
+  from this obtain T where T_def:
+     "open T & f x0 : T & (ALL S. (open S & x0 : S) --> (EX x':S. f x' ~: T))"
+     using continuous_at_open[of x0 f] by metis
+  def X == "{x'. f x' ~: T}" hence "x0 islimpt X" unfolding islimpt_def using T_def by auto
+  from this obtain x where x_def: "(ALL n. x n : X) & x ----> x0"
+     using islimpt_sequential[of x0 X] by auto
+  hence "~(f o x) ----> (f x0)" unfolding tendsto_explicit using X_def T_def by auto
+  hence "EX x. x ----> x0 & (~(f o x) ----> (f x0))" using x_def by auto
+}
+from this show ?thesis using continuous_imp_tendsto by auto
+qed
+
+lemma continuous_at_of_extreal:
+  fixes x0 :: extreal
+  assumes "\<bar>x0\<bar> \<noteq> \<infinity>"
+  shows "continuous (at x0) real"
+proof-
+{ fix T assume T_def: "open T & real x0 : T"
+  def S == "extreal ` T"
+  hence "extreal (real x0) : S" using T_def by auto
+  hence "x0 : S" using assms extreal_real by auto
+  moreover have "open S" using open_extreal S_def T_def by auto
+  moreover have "ALL y:S. real y : T" using S_def T_def by auto
+  ultimately have "EX S. x0 : S & open S & (ALL y:S. real y : T)" by auto
+} from this show ?thesis unfolding continuous_at_open by blast
+qed
+
+
+lemma continuous_at_iff_extreal:
+fixes f :: "'a::t2_space => real"
+shows "continuous (at x0) f <-> continuous (at x0) (extreal o f)"
+proof-
+{ assume "continuous (at x0) f" hence "continuous (at x0) (extreal o f)"
+     using continuous_at_extreal continuous_at_compose[of x0 f extreal] by auto
+}
+moreover
+{ assume "continuous (at x0) (extreal o f)"
+  hence "continuous (at x0) (real o (extreal o f))"
+     using continuous_at_of_extreal by (intro continuous_at_compose[of x0 "extreal o f"]) auto
+  moreover have "real o (extreal o f) = f" using real_extreal_id by (simp add: o_assoc)
+  ultimately have "continuous (at x0) f" by auto
+} ultimately show ?thesis by auto
+qed
+
+
+lemma continuous_on_iff_extreal:
+fixes f :: "'a::t2_space => real"
+fixes A assumes "open A"
+shows "continuous_on A f <-> continuous_on A (extreal o f)"
+   using continuous_at_iff_extreal assms by (auto simp add: continuous_on_eq_continuous_at)
+
+
+lemma continuous_on_real: "continuous_on (UNIV-{\<infinity>,(-\<infinity>)}) real"
+   using continuous_at_of_extreal continuous_on_eq_continuous_at open_image_extreal by auto
+
+
+lemma continuous_on_iff_real:
+  fixes f :: "'a::t2_space => extreal"
+  assumes "\<And>x. x \<in> A \<Longrightarrow> \<bar>f x\<bar> \<noteq> \<infinity>"
+  shows "continuous_on A f \<longleftrightarrow> continuous_on A (real \<circ> f)"
+proof-
+  have "f ` A <= UNIV-{\<infinity>,(-\<infinity>)}" using assms by force
+  hence *: "continuous_on (f ` A) real"
+     using continuous_on_real by (simp add: continuous_on_subset)
+have **: "continuous_on ((real o f) ` A) extreal"
+   using continuous_on_extreal continuous_on_subset[of "UNIV" "extreal" "(real o f) ` A"] by blast
+{ assume "continuous_on A f" hence "continuous_on A (real o f)"
+  apply (subst continuous_on_compose) using * by auto
+}
+moreover
+{ assume "continuous_on A (real o f)"
+  hence "continuous_on A (extreal o (real o f))"
+     apply (subst continuous_on_compose) using ** by auto
+  hence "continuous_on A f"
+     apply (subst continuous_on_eq[of A "extreal o (real o f)" f])
+     using assms extreal_real by auto
+}
+ultimately show ?thesis by auto
+qed
+
+
+lemma continuous_at_const:
+  fixes f :: "'a::t2_space => extreal"
+  assumes "ALL x. (f x = C)"
+  shows "ALL x. continuous (at x) f"
+unfolding continuous_at_open using assms t1_space by auto
+
+
+lemma closure_contains_Inf:
+  fixes S :: "real set"
+  assumes "S ~= {}" "EX B. ALL x:S. B<=x"
+  shows "Inf S : closure S"
+proof-
+have *: "ALL x:S. Inf S <= x" using Inf_lower_EX[of _ S] assms by metis
+{ fix e assume "e>(0 :: real)"
+  from this obtain x where x_def: "x:S & x < Inf S + e" using Inf_close `S ~= {}` by auto
+  moreover hence "x > Inf S - e" using * by auto
+  ultimately have "abs (x - Inf S) < e" by (simp add: abs_diff_less_iff)
+  hence "EX x:S. abs (x - Inf S) < e" using x_def by auto
+} from this show ?thesis apply (subst closure_approachable) unfolding dist_norm by auto
+qed
+
+
+lemma closed_contains_Inf:
+  fixes S :: "real set"
+  assumes "S ~= {}" "EX B. ALL x:S. B<=x"
+  assumes "closed S"
+  shows "Inf S : S"
+by (metis closure_contains_Inf closure_closed assms)
+
+
+lemma mono_closed_real:
+  fixes S :: "real set"
+  assumes mono: "ALL y z. y:S & y<=z --> z:S"
+  assumes "closed S"
+  shows "S = {} | S = UNIV | (EX a. S = {a ..})"
+proof-
+{ assume "S ~= {}"
+  { assume ex: "EX B. ALL x:S. B<=x"
+    hence *: "ALL x:S. Inf S <= x" using Inf_lower_EX[of _ S] ex by metis
+    hence "Inf S : S" apply (subst closed_contains_Inf) using ex `S ~= {}` `closed S` by auto
+    hence "ALL x. (Inf S <= x <-> x:S)" using mono[rule_format, of "Inf S"] * by auto
+    hence "S = {Inf S ..}" by auto
+    hence "EX a. S = {a ..}" by auto
+  }
+  moreover
+  { assume "~(EX B. ALL x:S. B<=x)"
+    hence nex: "ALL B. EX x:S. x<B" by (simp add: not_le)
+    { fix y obtain x where "x:S & x < y" using nex by auto
+      hence "y:S" using mono[rule_format, of x y] by auto
+    } hence "S = UNIV" by auto
+  } ultimately have "S = UNIV | (EX a. S = {a ..})" by blast
+} from this show ?thesis by blast
+qed
+
+
+lemma mono_closed_extreal:
+  fixes S :: "real set"
+  assumes mono: "ALL y z. y:S & y<=z --> z:S"
+  assumes "closed S"
+  shows "EX a. S = {x. a <= extreal x}"
+proof-
+{ assume "S = {}" hence ?thesis apply(rule_tac x=PInfty in exI) by auto }
+moreover
+{ assume "S = UNIV" hence ?thesis apply(rule_tac x="-\<infinity>" in exI) by auto }
+moreover
+{ assume "EX a. S = {a ..}"
+  from this obtain a where "S={a ..}" by auto
+  hence ?thesis apply(rule_tac x="extreal a" in exI) by auto
+} ultimately show ?thesis using mono_closed_real[of S] assms by auto
+qed
+
+subsection {* Sums *}
+
+lemma setsum_extreal[simp]:
+  "(\<Sum>x\<in>A. extreal (f x)) = extreal (\<Sum>x\<in>A. f x)"
+proof cases
+  assume "finite A" then show ?thesis by induct auto
+qed simp
+
+lemma setsum_Pinfty: "(\<Sum>x\<in>P. f x) = \<infinity> \<longleftrightarrow> (finite P \<and> (\<exists>i\<in>P. f i = \<infinity>))"
+proof safe
+  assume *: "setsum f P = \<infinity>"
+  show "finite P"
+  proof (rule ccontr) assume "infinite P" with * show False by auto qed
+  show "\<exists>i\<in>P. f i = \<infinity>"
+  proof (rule ccontr)
+    assume "\<not> ?thesis" then have "\<And>i. i \<in> P \<Longrightarrow> f i \<noteq> \<infinity>" by auto
+    from `finite P` this have "setsum f P \<noteq> \<infinity>"
+      by induct auto
+    with * show False by auto
+  qed
+next
+  fix i assume "finite P" "i \<in> P" "f i = \<infinity>"
+  thus "setsum f P = \<infinity>"
+  proof induct
+    case (insert x A)
+    show ?case using insert by (cases "x = i") auto
+  qed simp
+qed
+
+lemma setsum_Inf:
+  shows "\<bar>setsum f A\<bar> = \<infinity> \<longleftrightarrow> (finite A \<and> (\<exists>i\<in>A. \<bar>f i\<bar> = \<infinity>))"
+proof
+  assume *: "\<bar>setsum f A\<bar> = \<infinity>"
+  have "finite A" by (rule ccontr) (insert *, auto)
+  moreover have "\<exists>i\<in>A. \<bar>f i\<bar> = \<infinity>"
+  proof (rule ccontr)
+    assume "\<not> ?thesis" then have "\<forall>i\<in>A. \<exists>r. f i = extreal r" by auto
+    from bchoice[OF this] guess r ..
+    with * show False by (auto simp: setsum_extreal)
+  qed
+  ultimately show "finite A \<and> (\<exists>i\<in>A. \<bar>f i\<bar> = \<infinity>)" by auto
+next
+  assume "finite A \<and> (\<exists>i\<in>A. \<bar>f i\<bar> = \<infinity>)"
+  then obtain i where "finite A" "i \<in> A" "\<bar>f i\<bar> = \<infinity>" by auto
+  then show "\<bar>setsum f A\<bar> = \<infinity>"
+  proof induct
+    case (insert j A) then show ?case
+      by (cases rule: extreal3_cases[of "f i" "f j" "setsum f A"]) auto
+  qed simp
+qed
+
+lemma setsum_real_of_extreal:
+  assumes "\<And>x. x \<in> S \<Longrightarrow> \<bar>f x\<bar> \<noteq> \<infinity>"
+  shows "(\<Sum>x\<in>S. real (f x)) = real (setsum f S)"
+proof -
+  have "\<forall>x\<in>S. \<exists>r. f x = extreal r"
+  proof
+    fix x assume "x \<in> S"
+    from assms[OF this] show "\<exists>r. f x = extreal r" by (cases "f x") auto
+  qed
+  from bchoice[OF this] guess r ..
+  then show ?thesis by simp
+qed
+
+lemma setsum_extreal_0:
+  fixes f :: "'a \<Rightarrow> extreal" assumes "finite A" "\<And>i. i \<in> A \<Longrightarrow> 0 \<le> f i"
+  shows "(\<Sum>x\<in>A. f x) = 0 \<longleftrightarrow> (\<forall>i\<in>A. f i = 0)"
+proof
+  assume *: "(\<Sum>x\<in>A. f x) = 0"
+  then have "(\<Sum>x\<in>A. f x) \<noteq> \<infinity>" by auto
+  then have "\<forall>i\<in>A. \<bar>f i\<bar> \<noteq> \<infinity>" using assms by (force simp: setsum_Pinfty)
+  then have "\<forall>i\<in>A. \<exists>r. f i = extreal r" by auto
+  from bchoice[OF this] * assms show "\<forall>i\<in>A. f i = 0"
+    using setsum_nonneg_eq_0_iff[of A "\<lambda>i. real (f i)"] by auto
+qed (rule setsum_0')
+
+
+lemma setsum_extreal_right_distrib:
+  fixes f :: "'a \<Rightarrow> extreal" assumes "\<And>i. i \<in> A \<Longrightarrow> 0 \<le> f i"
+  shows "r * setsum f A = (\<Sum>n\<in>A. r * f n)"
+proof cases
+  assume "finite A" then show ?thesis using assms
+    by induct (auto simp: extreal_right_distrib setsum_nonneg)
+qed simp
+
+lemma sums_extreal_positive:
+  fixes f :: "nat \<Rightarrow> extreal" assumes "\<And>i. 0 \<le> f i" shows "f sums (SUP n. \<Sum>i<n. f i)"
+proof -
+  have "incseq (\<lambda>i. \<Sum>j=0..<i. f j)"
+    using extreal_add_mono[OF _ assms] by (auto intro!: incseq_SucI)
+  from LIMSEQ_extreal_SUPR[OF this]
+  show ?thesis unfolding sums_def by (simp add: atLeast0LessThan)
+qed
+
+lemma summable_extreal_pos:
+  fixes f :: "nat \<Rightarrow> extreal" assumes "\<And>i. 0 \<le> f i" shows "summable f"
+  using sums_extreal_positive[of f, OF assms] unfolding summable_def by auto
+
+lemma suminf_extreal_eq_SUPR:
+  fixes f :: "nat \<Rightarrow> extreal" assumes "\<And>i. 0 \<le> f i"
+  shows "(\<Sum>x. f x) = (SUP n. \<Sum>i<n. f i)"
+  using sums_extreal_positive[of f, OF assms, THEN sums_unique] by simp
+
+lemma sums_extreal:
+  "(\<lambda>x. extreal (f x)) sums extreal x \<longleftrightarrow> f sums x"
+  unfolding sums_def by simp
+
+lemma suminf_bound:
+  fixes f :: "nat \<Rightarrow> extreal"
+  assumes "\<forall>N. (\<Sum>n<N. f n) \<le> x" and pos: "\<And>n. 0 \<le> f n"
+  shows "suminf f \<le> x"
+proof (rule Lim_bounded_extreal)
+  have "summable f" using pos[THEN summable_extreal_pos] .
+  then show "(\<lambda>N. \<Sum>n<N. f n) ----> suminf f"
+    by (auto dest!: summable_sums simp: sums_def atLeast0LessThan)
+  show "\<forall>n\<ge>0. setsum f {..<n} \<le> x"
+    using assms by auto
+qed
+
+lemma suminf_bound_add:
+  fixes f :: "nat \<Rightarrow> extreal"
+  assumes "\<forall>N. (\<Sum>n<N. f n) + y \<le> x" and pos: "\<And>n. 0 \<le> f n" and "y \<noteq> -\<infinity>"
+  shows "suminf f + y \<le> x"
+proof (cases y)
+  case (real r) then have "\<forall>N. (\<Sum>n<N. f n) \<le> x - y"
+    using assms by (simp add: extreal_le_minus)
+  then have "(\<Sum> n. f n) \<le> x - y" using pos by (rule suminf_bound)
+  then show "(\<Sum> n. f n) + y \<le> x"
+    using assms real by (simp add: extreal_le_minus)
+qed (insert assms, auto)
+
+lemma sums_finite:
+  assumes "\<forall>N\<ge>n. f N = 0"
+  shows "f sums (\<Sum>N<n. f N)"
+proof -
+  { fix i have "(\<Sum>N<i + n. f N) = (\<Sum>N<n. f N)"
+      by (induct i) (insert assms, auto) }
+  note this[simp]
+  show ?thesis unfolding sums_def
+    by (rule LIMSEQ_offset[of _ n]) (auto simp add: atLeast0LessThan)
+qed
+
+lemma suminf_finite:
+  fixes f :: "nat \<Rightarrow> 'a::{comm_monoid_add,t2_space}" assumes "\<forall>N\<ge>n. f N = 0"
+  shows "suminf f = (\<Sum>N<n. f N)"
+  using sums_finite[OF assms, THEN sums_unique] by simp
+
+lemma suminf_extreal_0[simp]: "(\<Sum>i. 0) = (0::'a::{comm_monoid_add,t2_space})"
+  using suminf_finite[of 0 "\<lambda>x. 0"] by simp
+
+lemma suminf_upper:
+  fixes f :: "nat \<Rightarrow> extreal" assumes "\<And>n. 0 \<le> f n"
+  shows "(\<Sum>n<N. f n) \<le> (\<Sum>n. f n)"
+  unfolding suminf_extreal_eq_SUPR[OF assms] SUPR_def
+  by (auto intro: complete_lattice_class.Sup_upper image_eqI)
+
+lemma suminf_0_le:
+  fixes f :: "nat \<Rightarrow> extreal" assumes "\<And>n. 0 \<le> f n"
+  shows "0 \<le> (\<Sum>n. f n)"
+  using suminf_upper[of f 0, OF assms] by simp
+
+lemma suminf_le_pos:
+  fixes f g :: "nat \<Rightarrow> extreal"
+  assumes "\<And>N. f N \<le> g N" "\<And>N. 0 \<le> f N"
+  shows "suminf f \<le> suminf g"
+proof (safe intro!: suminf_bound)
+  fix n { fix N have "0 \<le> g N" using assms(2,1)[of N] by auto }
+  have "setsum f {..<n} \<le> setsum g {..<n}" using assms by (auto intro: setsum_mono)
+  also have "... \<le> suminf g" using `\<And>N. 0 \<le> g N` by (rule suminf_upper)
+  finally show "setsum f {..<n} \<le> suminf g" .
+qed (rule assms(2))
+
+lemma suminf_half_series_extreal: "(\<Sum>n. (1/2 :: extreal)^Suc n) = 1"
+  using sums_extreal[THEN iffD2, OF power_half_series, THEN sums_unique, symmetric]
+  by (simp add: one_extreal_def)
+
+lemma suminf_add_extreal:
+  fixes f g :: "nat \<Rightarrow> extreal"
+  assumes "\<And>i. 0 \<le> f i" "\<And>i. 0 \<le> g i"
+  shows "(\<Sum>i. f i + g i) = suminf f + suminf g"
+  apply (subst (1 2 3) suminf_extreal_eq_SUPR)
+  unfolding setsum_addf
+  by (intro assms extreal_add_nonneg_nonneg SUPR_extreal_add_pos incseq_setsumI setsum_nonneg ballI)+
+
+lemma suminf_cmult_extreal:
+  fixes f g :: "nat \<Rightarrow> extreal"
+  assumes "\<And>i. 0 \<le> f i" "0 \<le> a"
+  shows "(\<Sum>i. a * f i) = a * suminf f"
+  by (auto simp: setsum_extreal_right_distrib[symmetric] assms
+                 extreal_zero_le_0_iff setsum_nonneg suminf_extreal_eq_SUPR
+           intro!: SUPR_extreal_cmult )
+
+lemma suminf_PInfty:
+  assumes "\<And>i. 0 \<le> f i" "suminf f \<noteq> \<infinity>"
+  shows "f i \<noteq> \<infinity>"
+proof -
+  from suminf_upper[of f "Suc i", OF assms(1)] assms(2)
+  have "(\<Sum>i<Suc i. f i) \<noteq> \<infinity>" by auto
+  then show ?thesis
+    unfolding setsum_Pinfty by simp
+qed
+
+lemma suminf_PInfty_fun:
+  assumes "\<And>i. 0 \<le> f i" "suminf f \<noteq> \<infinity>"
+  shows "\<exists>f'. f = (\<lambda>x. extreal (f' x))"
+proof -
+  have "\<forall>i. \<exists>r. f i = extreal r"
+  proof
+    fix i show "\<exists>r. f i = extreal r"
+      using suminf_PInfty[OF assms] assms(1)[of i] by (cases "f i") auto
+  qed
+  from choice[OF this] show ?thesis by auto
+qed
+
+lemma summable_extreal:
+  assumes "\<And>i. 0 \<le> f i" "(\<Sum>i. extreal (f i)) \<noteq> \<infinity>"
+  shows "summable f"
+proof -
+  have "0 \<le> (\<Sum>i. extreal (f i))"
+    using assms by (intro suminf_0_le) auto
+  with assms obtain r where r: "(\<Sum>i. extreal (f i)) = extreal r"
+    by (cases "\<Sum>i. extreal (f i)") auto
+  from summable_extreal_pos[of "\<lambda>x. extreal (f x)"]
+  have "summable (\<lambda>x. extreal (f x))" using assms by auto
+  from summable_sums[OF this]
+  have "(\<lambda>x. extreal (f x)) sums (\<Sum>x. extreal (f x))" by auto
+  then show "summable f"
+    unfolding r sums_extreal summable_def ..
+qed
+
+lemma suminf_extreal:
+  assumes "\<And>i. 0 \<le> f i" "(\<Sum>i. extreal (f i)) \<noteq> \<infinity>"
+  shows "(\<Sum>i. extreal (f i)) = extreal (suminf f)"
+proof (rule sums_unique[symmetric])
+  from summable_extreal[OF assms]
+  show "(\<lambda>x. extreal (f x)) sums (extreal (suminf f))"
+    unfolding sums_extreal using assms by (intro summable_sums summable_extreal)
+qed
+
+lemma suminf_extreal_minus:
+  fixes f g :: "nat \<Rightarrow> extreal"
+  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>"
+  shows "(\<Sum>i. f i - g i) = suminf f - suminf g"
+proof -
+  { fix i have "0 \<le> f i" using ord[of i] by auto }
+  moreover
+  from suminf_PInfty_fun[OF `\<And>i. 0 \<le> f i` fin(1)] guess f' .. note this[simp]
+  from suminf_PInfty_fun[OF `\<And>i. 0 \<le> g i` fin(2)] guess g' .. note this[simp]
+  { fix i have "0 \<le> f i - g i" using ord[of i] by (auto simp: extreal_le_minus_iff) }
+  moreover
+  have "suminf (\<lambda>i. f i - g i) \<le> suminf f"
+    using assms by (auto intro!: suminf_le_pos simp: field_simps)
+  then have "suminf (\<lambda>i. f i - g i) \<noteq> \<infinity>" using fin by auto
+  ultimately show ?thesis using assms `\<And>i. 0 \<le> f i`
+    apply simp
+    by (subst (1 2 3) suminf_extreal)
+       (auto intro!: suminf_diff[symmetric] summable_extreal)
+qed
+
+lemma suminf_extreal_PInf[simp]:
+  "(\<Sum>x. \<infinity>) = \<infinity>"
+proof -
+  have "(\<Sum>i<Suc 0. \<infinity>) \<le> (\<Sum>x. \<infinity>)" by (rule suminf_upper) auto
+  then show ?thesis by simp
+qed
+
+lemma summable_real_of_extreal:
+  assumes f: "\<And>i. 0 \<le> f i" and fin: "(\<Sum>i. f i) \<noteq> \<infinity>"
+  shows "summable (\<lambda>i. real (f i))"
+proof (rule summable_def[THEN iffD2])
+  have "0 \<le> (\<Sum>i. f i)" using assms by (auto intro: suminf_0_le)
+  with fin obtain r where r: "extreal r = (\<Sum>i. f i)" by (cases "(\<Sum>i. f i)") auto
+  { fix i have "f i \<noteq> \<infinity>" using f by (intro suminf_PInfty[OF _ fin]) auto
+    then have "\<bar>f i\<bar> \<noteq> \<infinity>" using f[of i] by auto }
+  note fin = this
+  have "(\<lambda>i. extreal (real (f i))) sums (\<Sum>i. extreal (real (f i)))"
+    using f by (auto intro!: summable_extreal_pos summable_sums simp: extreal_le_real_iff zero_extreal_def)
+  also have "\<dots> = extreal r" using fin r by (auto simp: extreal_real)
+  finally show "\<exists>r. (\<lambda>i. real (f i)) sums r" by (auto simp: sums_extreal)
+qed
+
+end
\ No newline at end of file
--- a/src/HOL/Multivariate_Analysis/Integration.thy	Mon Mar 14 15:17:10 2011 +0100
+++ b/src/HOL/Multivariate_Analysis/Integration.thy	Mon Mar 14 15:29:10 2011 +0100
@@ -1,4 +1,3 @@
-
 header {* Kurzweil-Henstock Gauge Integration in many dimensions. *}
 (*  Author:                     John Harrison
     Translation from HOL light: Robert Himmelmann, TU Muenchen *)
@@ -3780,7 +3779,7 @@
   shows "f x = y"
 proof- have "{x \<in> s. f x \<in> {y}} = {} \<or> {x \<in> s. f x \<in> {y}} = s"
     apply(rule assms(1)[unfolded connected_clopen,rule_format]) apply rule defer
-    apply(rule continuous_closed_in_preimage[OF assms(4) closed_sing])
+    apply(rule continuous_closed_in_preimage[OF assms(4) closed_singleton])
     apply(rule open_openin_trans[OF assms(2)]) unfolding open_contains_ball
   proof safe fix x assume "x\<in>s" 
     from assms(2)[unfolded open_contains_ball,rule_format,OF this] guess e .. note e=conjunctD2[OF this]
--- a/src/HOL/Multivariate_Analysis/Multivariate_Analysis.thy	Mon Mar 14 15:17:10 2011 +0100
+++ b/src/HOL/Multivariate_Analysis/Multivariate_Analysis.thy	Mon Mar 14 15:29:10 2011 +0100
@@ -1,5 +1,5 @@
 theory Multivariate_Analysis
-imports Fashoda
+imports Fashoda Extended_Real_Limits
 begin
 
 end
--- a/src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy	Mon Mar 14 15:17:10 2011 +0100
+++ b/src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy	Mon Mar 14 15:29:10 2011 +0100
@@ -424,80 +424,6 @@
 lemma connected_empty[simp, intro]: "connected {}"
   by (simp add: connected_def)
 
-subsection{* Hausdorff and other separation properties *}
-
-class t0_space = topological_space +
-  assumes t0_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> \<not> (x \<in> U \<longleftrightarrow> y \<in> U)"
-
-class t1_space = topological_space +
-  assumes t1_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> x \<in> U \<and> y \<notin> U"
-
-instance t1_space \<subseteq> t0_space
-proof qed (fast dest: t1_space)
-
-lemma separation_t1:
-  fixes x y :: "'a::t1_space"
-  shows "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> x \<in> U \<and> y \<notin> U)"
-  using t1_space[of x y] by blast
-
-lemma closed_sing:
-  fixes a :: "'a::t1_space"
-  shows "closed {a}"
-proof -
-  let ?T = "\<Union>{S. open S \<and> a \<notin> S}"
-  have "open ?T" by (simp add: open_Union)
-  also have "?T = - {a}"
-    by (simp add: set_eq_iff separation_t1, auto)
-  finally show "closed {a}" unfolding closed_def .
-qed
-
-lemma closed_insert [simp]:
-  fixes a :: "'a::t1_space"
-  assumes "closed S" shows "closed (insert a S)"
-proof -
-  from closed_sing assms
-  have "closed ({a} \<union> S)" by (rule closed_Un)
-  thus "closed (insert a S)" by simp
-qed
-
-lemma finite_imp_closed:
-  fixes S :: "'a::t1_space set"
-  shows "finite S \<Longrightarrow> closed S"
-by (induct set: finite, simp_all)
-
-text {* T2 spaces are also known as Hausdorff spaces. *}
-
-class t2_space = topological_space +
-  assumes hausdorff: "x \<noteq> y \<Longrightarrow> \<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
-
-instance t2_space \<subseteq> t1_space
-proof qed (fast dest: hausdorff)
-
-instance metric_space \<subseteq> t2_space
-proof
-  fix x y :: "'a::metric_space"
-  assume xy: "x \<noteq> y"
-  let ?U = "ball x (dist x y / 2)"
-  let ?V = "ball y (dist x y / 2)"
-  have th0: "\<And>d x y z. (d x z :: real) <= d x y + d y z \<Longrightarrow> d y z = d z y
-               ==> ~(d x y * 2 < d x z \<and> d z y * 2 < d x z)" by arith
-  have "open ?U \<and> open ?V \<and> x \<in> ?U \<and> y \<in> ?V \<and> ?U \<inter> ?V = {}"
-    using dist_pos_lt[OF xy] th0[of dist,OF dist_triangle dist_commute]
-    by (auto simp add: set_eq_iff)
-  then show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
-    by blast
-qed
-
-lemma separation_t2:
-  fixes x y :: "'a::t2_space"
-  shows "x \<noteq> y \<longleftrightarrow> (\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {})"
-  using hausdorff[of x y] by blast
-
-lemma separation_t0:
-  fixes x y :: "'a::t0_space"
-  shows "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> ~(x\<in>U \<longleftrightarrow> y\<in>U))"
-  using t0_space[of x y] by blast
-
 subsection{* Limit points *}
 
 definition
@@ -994,10 +920,6 @@
 
 text {* Identify Trivial limits, where we can't approach arbitrarily closely. *}
 
-definition
-  trivial_limit :: "'a net \<Rightarrow> bool" where
-  "trivial_limit net \<longleftrightarrow> eventually (\<lambda>x. False) net"
-
 lemma trivial_limit_within:
   shows "trivial_limit (at a within S) \<longleftrightarrow> \<not> a islimpt S"
 proof
@@ -1040,9 +962,6 @@
   apply (simp add: norm_sgn)
   done
 
-lemma trivial_limit_sequentially[intro]: "\<not> trivial_limit sequentially"
-  by (auto simp add: trivial_limit_def eventually_sequentially)
-
 text {* Some property holds "sufficiently close" to the limit point. *}
 
 lemma eventually_at: (* FIXME: this replaces Limits.eventually_at *)
@@ -1074,6 +993,7 @@
 lemma eventually_False: "eventually (\<lambda>x. False) net \<longleftrightarrow> trivial_limit net"
   unfolding trivial_limit_def ..
 
+
 lemma trivial_limit_eq: "trivial_limit net \<longleftrightarrow> (\<forall>P. eventually P net)"
   apply (safe elim!: trivial_limit_eventually)
   apply (simp add: eventually_False [symmetric])
@@ -1417,35 +1337,10 @@
 
 text{* Uniqueness of the limit, when nontrivial. *}
 
-lemma Lim_unique:
-  fixes f :: "'a \<Rightarrow> 'b::t2_space"
-  assumes "\<not> trivial_limit net"  "(f ---> l) net"  "(f ---> l') net"
-  shows "l = l'"
-proof (rule ccontr)
-  assume "l \<noteq> l'"
-  obtain U V where "open U" "open V" "l \<in> U" "l' \<in> V" "U \<inter> V = {}"
-    using hausdorff [OF `l \<noteq> l'`] by fast
-  have "eventually (\<lambda>x. f x \<in> U) net"
-    using `(f ---> l) net` `open U` `l \<in> U` by (rule topological_tendstoD)
-  moreover
-  have "eventually (\<lambda>x. f x \<in> V) net"
-    using `(f ---> l') net` `open V` `l' \<in> V` by (rule topological_tendstoD)
-  ultimately
-  have "eventually (\<lambda>x. False) net"
-  proof (rule eventually_elim2)
-    fix x
-    assume "f x \<in> U" "f x \<in> V"
-    hence "f x \<in> U \<inter> V" by simp
-    with `U \<inter> V = {}` show "False" by simp
-  qed
-  with `\<not> trivial_limit net` show "False"
-    by (simp add: eventually_False)
-qed
-
 lemma tendsto_Lim:
   fixes f :: "'a \<Rightarrow> 'b::t2_space"
   shows "~(trivial_limit net) \<Longrightarrow> (f ---> l) net ==> Lim net f = l"
-  unfolding Lim_def using Lim_unique[of net f] by auto
+  unfolding Lim_def using tendsto_unique[of net f] by auto
 
 text{* Limit under bilinear function *}
 
@@ -1518,7 +1413,7 @@
 apply (rule some_equality)
 apply (rule Lim_at_within)
 apply (rule Lim_ident_at)
-apply (erule Lim_unique [OF assms])
+apply (erule tendsto_unique [OF assms])
 apply (rule Lim_at_within)
 apply (rule Lim_ident_at)
 done
@@ -2558,7 +2453,7 @@
       unfolding islimpt_sequential by auto
     then obtain l where l: "l\<in>s" "(f ---> l) sequentially"
       using `complete s`[unfolded complete_def] using convergent_imp_cauchy[of f x] by auto
-    hence "x \<in> s"  using Lim_unique[of sequentially f l x] trivial_limit_sequentially f(2) by auto
+    hence "x \<in> s"  using tendsto_unique[of sequentially f l x] trivial_limit_sequentially f(2) by auto
   }
   thus "closed s" unfolding closed_limpt by auto
 qed
@@ -3131,7 +3026,7 @@
     using assms(1) unfolding uniformly_convergent_eq_cauchy[THEN sym] by auto
   moreover
   { fix x assume "P x"
-    hence "l x = l' x" using Lim_unique[OF trivial_limit_sequentially, of "\<lambda>n. s n x" "l x" "l' x"]
+    hence "l x = l' x" using tendsto_unique[OF trivial_limit_sequentially, of "\<lambda>n. s n x" "l x" "l' x"]
       using l and assms(2) unfolding Lim_sequentially by blast  }
   ultimately show ?thesis by auto
 qed
@@ -5954,7 +5849,7 @@
   hence "((snd \<circ> h \<circ> r) ---> g a) sequentially" unfolding continuous_on_sequentially
     apply (rule allE[where x="\<lambda>n. (fst \<circ> h \<circ> r) n"]) apply (erule ballE[where x=a])
     using lima unfolding h_def o_def using fs[OF `x\<in>s`] by (auto simp add: y_def)
-  hence "g a = a" using Lim_unique[OF trivial_limit_sequentially limb, of "g a"]
+  hence "g a = a" using tendsto_unique[OF trivial_limit_sequentially limb, of "g a"]
     unfolding `a=b` and o_assoc by auto
   moreover
   { fix x assume "x\<in>s" "g x = x" "x\<noteq>a"
--- a/src/HOL/Probability/Borel_Space.thy	Mon Mar 14 15:17:10 2011 +0100
+++ b/src/HOL/Probability/Borel_Space.thy	Mon Mar 14 15:29:10 2011 +0100
@@ -3,13 +3,9 @@
 header {*Borel spaces*}
 
 theory Borel_Space
-  imports Sigma_Algebra Positive_Extended_Real Multivariate_Analysis
+  imports Sigma_Algebra Multivariate_Analysis
 begin
 
-lemma LIMSEQ_max:
-  "u ----> (x::real) \<Longrightarrow> (\<lambda>i. max (u i) 0) ----> max x 0"
-  by (fastsimp intro!: LIMSEQ_I dest!: LIMSEQ_D)
-
 section "Generic Borel spaces"
 
 definition "borel = sigma \<lparr> space = UNIV::'a::topological_space set, sets = open\<rparr>"
@@ -57,7 +53,7 @@
   shows "f -` {x} \<inter> space M \<in> sets M"
 proof (cases "x \<in> f ` space M")
   case True then obtain y where "x = f y" by auto
-  from closed_sing[of "f y"]
+  from closed_singleton[of "f y"]
   have "{f y} \<in> sets borel" by (rule borel_closed)
   with assms show ?thesis
     unfolding in_borel_measurable_borel `x = f y` by auto
@@ -81,7 +77,7 @@
   shows "A \<in> sets borel \<Longrightarrow> insert x A \<in> sets borel"
   proof (rule borel.insert_in_sets)
     show "{x} \<in> sets borel"
-      using closed_sing[of x] by (rule borel_closed)
+      using closed_singleton[of x] by (rule borel_closed)
   qed simp
 
 lemma (in sigma_algebra) borel_measurable_const[simp, intro]:
@@ -112,26 +108,8 @@
   ultimately show "?I \<in> borel_measurable M" by auto
 qed
 
-lemma borel_measurable_translate:
-  assumes "A \<in> sets borel" and trans: "\<And>B. open B \<Longrightarrow> f -` B \<in> sets borel"
-  shows "f -` A \<in> sets borel"
-proof -
-  have "A \<in> sigma_sets UNIV open" using assms
-    by (simp add: borel_def sigma_def)
-  thus ?thesis
-  proof (induct rule: sigma_sets.induct)
-    case (Basic a) thus ?case using trans[of a] by (simp add: mem_def)
-  next
-    case (Compl a)
-    moreover have "UNIV \<in> sets borel"
-      using borel.top by simp
-    ultimately show ?case
-      by (auto simp: vimage_Diff borel.Diff)
-  qed (auto simp add: vimage_UN)
-qed
-
 lemma (in sigma_algebra) borel_measurable_restricted:
-  fixes f :: "'a \<Rightarrow> 'x\<Colon>{topological_space, semiring_1}" assumes "A \<in> sets M"
+  fixes f :: "'a \<Rightarrow> extreal" assumes "A \<in> sets M"
   shows "f \<in> borel_measurable (restricted_space A) \<longleftrightarrow>
     (\<lambda>x. f x * indicator A x) \<in> borel_measurable M"
     (is "f \<in> borel_measurable ?R \<longleftrightarrow> ?f \<in> borel_measurable M")
@@ -142,7 +120,7 @@
   show ?thesis unfolding *
     unfolding in_borel_measurable_borel
   proof (simp, safe)
-    fix S :: "'x set" assume "S \<in> sets borel"
+    fix S :: "extreal set" assume "S \<in> sets borel"
       "\<forall>S\<in>sets borel. ?f -` S \<inter> A \<in> op \<inter> A ` sets M"
     then have "?f -` S \<inter> A \<in> op \<inter> A ` sets M" by auto
     then have f: "?f -` S \<inter> A \<in> sets M"
@@ -161,7 +139,7 @@
       then show ?thesis using f by auto
     qed
   next
-    fix S :: "'x set" assume "S \<in> sets borel"
+    fix S :: "extreal set" assume "S \<in> sets borel"
       "\<forall>S\<in>sets borel. ?f -` S \<inter> space M \<in> sets M"
     then have f: "?f -` S \<inter> space M \<in> sets M" by auto
     then show "?f -` S \<inter> A \<in> op \<inter> A ` sets M"
@@ -1024,103 +1002,6 @@
   using borel_measurable_euclidean_component
   unfolding nth_conv_component by auto
 
-section "Borel space over the real line with infinity"
-
-lemma borel_Real_measurable:
-  "A \<in> sets borel \<Longrightarrow> Real -` A \<in> sets borel"
-proof (rule borel_measurable_translate)
-  fix B :: "pextreal set" assume "open B"
-  then obtain T x where T: "open T" "Real ` (T \<inter> {0..}) = B - {\<omega>}" and
-    x: "\<omega> \<in> B \<Longrightarrow> 0 \<le> x" "\<omega> \<in> B \<Longrightarrow> {Real x <..} \<subseteq> B"
-    unfolding open_pextreal_def by blast
-  have "Real -` B = Real -` (B - {\<omega>})" by auto
-  also have "\<dots> = Real -` (Real ` (T \<inter> {0..}))" using T by simp
-  also have "\<dots> = (if 0 \<in> T then T \<union> {.. 0} else T \<inter> {0..})"
-    apply (auto simp add: Real_eq_Real image_iff)
-    apply (rule_tac x="max 0 x" in bexI)
-    by (auto simp: max_def)
-  finally show "Real -` B \<in> sets borel"
-    using `open T` by auto
-qed simp
-
-lemma borel_real_measurable:
-  "A \<in> sets borel \<Longrightarrow> (real -` A :: pextreal set) \<in> sets borel"
-proof (rule borel_measurable_translate)
-  fix B :: "real set" assume "open B"
-  { fix x have "0 < real x \<longleftrightarrow> (\<exists>r>0. x = Real r)" by (cases x) auto }
-  note Ex_less_real = this
-  have *: "real -` B = (if 0 \<in> B then real -` (B \<inter> {0 <..}) \<union> {0, \<omega>} else real -` (B \<inter> {0 <..}))"
-    by (force simp: Ex_less_real)
-
-  have "open (real -` (B \<inter> {0 <..}) :: pextreal set)"
-    unfolding open_pextreal_def using `open B`
-    by (auto intro!: open_Int exI[of _ "B \<inter> {0 <..}"] simp: image_iff Ex_less_real)
-  then show "(real -` B :: pextreal set) \<in> sets borel" unfolding * by auto
-qed simp
-
-lemma (in sigma_algebra) borel_measurable_Real[intro, simp]:
-  assumes "f \<in> borel_measurable M"
-  shows "(\<lambda>x. Real (f x)) \<in> borel_measurable M"
-  unfolding in_borel_measurable_borel
-proof safe
-  fix S :: "pextreal set" assume "S \<in> sets borel"
-  from borel_Real_measurable[OF this]
-  have "(Real \<circ> f) -` S \<inter> space M \<in> sets M"
-    using assms
-    unfolding vimage_compose in_borel_measurable_borel
-    by auto
-  thus "(\<lambda>x. Real (f x)) -` S \<inter> space M \<in> sets M" by (simp add: comp_def)
-qed
-
-lemma (in sigma_algebra) borel_measurable_real[intro, simp]:
-  fixes f :: "'a \<Rightarrow> pextreal"
-  assumes "f \<in> borel_measurable M"
-  shows "(\<lambda>x. real (f x)) \<in> borel_measurable M"
-  unfolding in_borel_measurable_borel
-proof safe
-  fix S :: "real set" assume "S \<in> sets borel"
-  from borel_real_measurable[OF this]
-  have "(real \<circ> f) -` S \<inter> space M \<in> sets M"
-    using assms
-    unfolding vimage_compose in_borel_measurable_borel
-    by auto
-  thus "(\<lambda>x. real (f x)) -` S \<inter> space M \<in> sets M" by (simp add: comp_def)
-qed
-
-lemma (in sigma_algebra) borel_measurable_Real_eq:
-  assumes "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
-  shows "(\<lambda>x. Real (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
-proof
-  have [simp]: "(\<lambda>x. Real (f x)) -` {\<omega>} \<inter> space M = {}"
-    by auto
-  assume "(\<lambda>x. Real (f x)) \<in> borel_measurable M"
-  hence "(\<lambda>x. real (Real (f x))) \<in> borel_measurable M"
-    by (rule borel_measurable_real)
-  moreover have "\<And>x. x \<in> space M \<Longrightarrow> real (Real (f x)) = f x"
-    using assms by auto
-  ultimately show "f \<in> borel_measurable M"
-    by (simp cong: measurable_cong)
-qed auto
-
-lemma (in sigma_algebra) borel_measurable_pextreal_eq_real:
-  "f \<in> borel_measurable M \<longleftrightarrow>
-    ((\<lambda>x. real (f x)) \<in> borel_measurable M \<and> f -` {\<omega>} \<inter> space M \<in> sets M)"
-proof safe
-  assume "f \<in> borel_measurable M"
-  then show "(\<lambda>x. real (f x)) \<in> borel_measurable M" "f -` {\<omega>} \<inter> space M \<in> sets M"
-    by (auto intro: borel_measurable_vimage borel_measurable_real)
-next
-  assume *: "(\<lambda>x. real (f x)) \<in> borel_measurable M" "f -` {\<omega>} \<inter> space M \<in> sets M"
-  have "f -` {\<omega>} \<inter> space M = {x\<in>space M. f x = \<omega>}" by auto
-  with * have **: "{x\<in>space M. f x = \<omega>} \<in> sets M" by simp
-  have f: "f = (\<lambda>x. if f x = \<omega> then \<omega> else Real (real (f x)))"
-    by (simp add: fun_eq_iff Real_real)
-  show "f \<in> borel_measurable M"
-    apply (subst f)
-    apply (rule measurable_If)
-    using * ** by auto
-qed
-
 lemma borel_measurable_continuous_on1:
   fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
   assumes "continuous_on UNIV f"
@@ -1187,206 +1068,213 @@
   using measurable_comp[OF f borel_measurable_borel_log[OF `1 < b`]]
   by (simp add: comp_def)
 
+subsection "Borel space on the extended reals"
+
+lemma borel_measurable_extreal_borel:
+  "extreal \<in> borel_measurable borel"
+  unfolding borel_def[where 'a=extreal]
+proof (rule borel.measurable_sigma)
+  fix X :: "extreal set" assume "X \<in> sets \<lparr>space = UNIV, sets = open \<rparr>"
+  then have "open X" by (auto simp: mem_def)
+  then have "open (extreal -` X \<inter> space borel)"
+    by (simp add: open_extreal_vimage)
+  then show "extreal -` X \<inter> space borel \<in> sets borel" by auto
+qed auto
+
+lemma (in sigma_algebra) borel_measurable_extreal[simp, intro]:
+  assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. extreal (f x)) \<in> borel_measurable M"
+  using measurable_comp[OF f borel_measurable_extreal_borel] unfolding comp_def .
+
+lemma borel_measurable_real_of_extreal_borel:
+  "(real :: extreal \<Rightarrow> real) \<in> borel_measurable borel"
+  unfolding borel_def[where 'a=real]
+proof (rule borel.measurable_sigma)
+  fix B :: "real set" assume "B \<in> sets \<lparr>space = UNIV, sets = open \<rparr>"
+  then have "open B" by (auto simp: mem_def)
+  have *: "extreal -` real -` (B - {0}) = B - {0}" by auto
+  have open_real: "open (real -` (B - {0}) :: extreal set)"
+    unfolding open_extreal_def * using `open B` by auto
+  show "(real -` B \<inter> space borel :: extreal set) \<in> sets borel"
+  proof cases
+    assume "0 \<in> B"
+    then have *: "real -` B = real -` (B - {0}) \<union> {-\<infinity>, \<infinity>, 0}"
+      by (auto simp add: real_of_extreal_eq_0)
+    then show "(real -` B :: extreal set) \<inter> space borel \<in> sets borel"
+      using open_real by auto
+  next
+    assume "0 \<notin> B"
+    then have *: "(real -` B :: extreal set) = real -` (B - {0})"
+      by (auto simp add: real_of_extreal_eq_0)
+    then show "(real -` B :: extreal set) \<inter> space borel \<in> sets borel"
+      using open_real by auto
+  qed
+qed auto
+
+lemma (in sigma_algebra) borel_measurable_real_of_extreal[simp, intro]:
+  assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. real (f x :: extreal)) \<in> borel_measurable M"
+  using measurable_comp[OF f borel_measurable_real_of_extreal_borel] unfolding comp_def .
+
+lemma (in sigma_algebra) borel_measurable_extreal_iff:
+  shows "(\<lambda>x. extreal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
+proof
+  assume "(\<lambda>x. extreal (f x)) \<in> borel_measurable M"
+  from borel_measurable_real_of_extreal[OF this]
+  show "f \<in> borel_measurable M" by auto
+qed auto
+
+lemma (in sigma_algebra) borel_measurable_extreal_iff_real:
+  "f \<in> borel_measurable M \<longleftrightarrow>
+    ((\<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)"
+proof safe
+  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"
+  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
+  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
+  let "?f x" = "if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else extreal (real (f x))"
+  have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
+  also have "?f = f" by (auto simp: fun_eq_iff extreal_real)
+  finally show "f \<in> borel_measurable M" .
+qed (auto intro: measurable_sets borel_measurable_real_of_extreal)
 
 lemma (in sigma_algebra) less_eq_ge_measurable:
   fixes f :: "'a \<Rightarrow> 'c::linorder"
-  shows "{x\<in>space M. a < f x} \<in> sets M \<longleftrightarrow> {x\<in>space M. f x \<le> a} \<in> sets M"
+  shows "f -` {a <..} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {..a} \<inter> space M \<in> sets M"
 proof
-  assume "{x\<in>space M. f x \<le> a} \<in> sets M"
-  moreover have "{x\<in>space M. a < f x} = space M - {x\<in>space M. f x \<le> a}" by auto
-  ultimately show "{x\<in>space M. a < f x} \<in> sets M" by auto
+  assume "f -` {a <..} \<inter> space M \<in> sets M"
+  moreover have "f -` {..a} \<inter> space M = space M - f -` {a <..} \<inter> space M" by auto
+  ultimately show "f -` {..a} \<inter> space M \<in> sets M" by auto
 next
-  assume "{x\<in>space M. a < f x} \<in> sets M"
-  moreover have "{x\<in>space M. f x \<le> a} = space M - {x\<in>space M. a < f x}" by auto
-  ultimately show "{x\<in>space M. f x \<le> a} \<in> sets M" by auto
+  assume "f -` {..a} \<inter> space M \<in> sets M"
+  moreover have "f -` {a <..} \<inter> space M = space M - f -` {..a} \<inter> space M" by auto
+  ultimately show "f -` {a <..} \<inter> space M \<in> sets M" by auto
 qed
 
 lemma (in sigma_algebra) greater_eq_le_measurable:
   fixes f :: "'a \<Rightarrow> 'c::linorder"
-  shows "{x\<in>space M. f x < a} \<in> sets M \<longleftrightarrow> {x\<in>space M. a \<le> f x} \<in> sets M"
+  shows "f -` {..< a} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {a ..} \<inter> space M \<in> sets M"
 proof
-  assume "{x\<in>space M. a \<le> f x} \<in> sets M"
-  moreover have "{x\<in>space M. f x < a} = space M - {x\<in>space M. a \<le> f x}" by auto
-  ultimately show "{x\<in>space M. f x < a} \<in> sets M" by auto
+  assume "f -` {a ..} \<inter> space M \<in> sets M"
+  moreover have "f -` {..< a} \<inter> space M = space M - f -` {a ..} \<inter> space M" by auto
+  ultimately show "f -` {..< a} \<inter> space M \<in> sets M" by auto
 next
-  assume "{x\<in>space M. f x < a} \<in> sets M"
-  moreover have "{x\<in>space M. a \<le> f x} = space M - {x\<in>space M. f x < a}" by auto
-  ultimately show "{x\<in>space M. a \<le> f x} \<in> sets M" by auto
+  assume "f -` {..< a} \<inter> space M \<in> sets M"
+  moreover have "f -` {a ..} \<inter> space M = space M - f -` {..< a} \<inter> space M" by auto
+  ultimately show "f -` {a ..} \<inter> space M \<in> sets M" by auto
 qed
 
-lemma (in sigma_algebra) less_eq_le_pextreal_measurable:
-  fixes f :: "'a \<Rightarrow> pextreal"
-  shows "(\<forall>a. {x\<in>space M. a < f x} \<in> sets M) \<longleftrightarrow> (\<forall>a. {x\<in>space M. a \<le> f x} \<in> sets M)"
+lemma (in sigma_algebra) borel_measurable_uminus_borel_extreal:
+  "(uminus :: extreal \<Rightarrow> extreal) \<in> borel_measurable borel"
+proof (subst borel_def, rule borel.measurable_sigma)
+  fix X :: "extreal set" assume "X \<in> sets \<lparr>space = UNIV, sets = open\<rparr>"
+  then have "open X" by (simp add: mem_def)
+  have "uminus -` X = uminus ` X" by (force simp: image_iff)
+  then have "open (uminus -` X)" using `open X` extreal_open_uminus by auto
+  then show "uminus -` X \<inter> space borel \<in> sets borel" by auto
+qed auto
+
+lemma (in sigma_algebra) borel_measurable_uminus_extreal[intro]:
+  assumes "f \<in> borel_measurable M"
+  shows "(\<lambda>x. - f x :: extreal) \<in> borel_measurable M"
+  using measurable_comp[OF assms borel_measurable_uminus_borel_extreal] by (simp add: comp_def)
+
+lemma (in sigma_algebra) borel_measurable_uminus_eq_extreal[simp]:
+  "(\<lambda>x. - f x :: extreal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
 proof
-  assume a: "\<forall>a. {x\<in>space M. a \<le> f x} \<in> sets M"
-  show "\<forall>a. {x \<in> space M. a < f x} \<in> sets M"
-  proof
-    fix a show "{x \<in> space M. a < f x} \<in> sets M"
-    proof (cases a)
-      case (preal r)
-      have "{x\<in>space M. a < f x} = (\<Union>i. {x\<in>space M. a + inverse (of_nat (Suc i)) \<le> f x})"
-      proof safe
-        fix x assume "a < f x" and [simp]: "x \<in> space M"
-        with ex_pextreal_inverse_of_nat_Suc_less[of "f x - a"]
-        obtain n where "a + inverse (of_nat (Suc n)) < f x"
-          by (cases "f x", auto simp: pextreal_minus_order)
-        then have "a + inverse (of_nat (Suc n)) \<le> f x" by simp
-        then show "x \<in> (\<Union>i. {x \<in> space M. a + inverse (of_nat (Suc i)) \<le> f x})"
-          by auto
-      next
-        fix i x assume [simp]: "x \<in> space M"
-        have "a < a + inverse (of_nat (Suc i))" using preal by auto
-        also assume "a + inverse (of_nat (Suc i)) \<le> f x"
-        finally show "a < f x" .
-      qed
-      with a show ?thesis by auto
-    qed simp
+  assume ?l from borel_measurable_uminus_extreal[OF this] show ?r by simp
+qed auto
+
+lemma (in sigma_algebra) borel_measurable_eq_atMost_extreal:
+  "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
+proof (intro iffI allI)
+  assume pos[rule_format]: "\<forall>a. f -` {..a} \<inter> space M \<in> sets M"
+  show "f \<in> borel_measurable M"
+    unfolding borel_measurable_extreal_iff_real borel_measurable_iff_le
+  proof (intro conjI allI)
+    fix a :: real
+    { fix x :: extreal assume *: "\<forall>i::nat. real i < x"
+      have "x = \<infinity>"
+      proof (rule extreal_top)
+        fix B from real_arch_lt[of B] guess n ..
+        then have "extreal B < real n" by auto
+        with * show "B \<le> x" by (metis less_trans less_imp_le)
+      qed }
+    then have "f -` {\<infinity>} \<inter> space M = space M - (\<Union>i::nat. f -` {.. real i} \<inter> space M)"
+      by (auto simp: not_le)
+    then show "f -` {\<infinity>} \<inter> space M \<in> sets M" using pos by (auto simp del: UN_simps intro!: Diff)
+    moreover
+    have "{-\<infinity>} = {..-\<infinity>}" by auto
+    then show "f -` {-\<infinity>} \<inter> space M \<in> sets M" using pos by auto
+    moreover have "{x\<in>space M. f x \<le> extreal a} \<in> sets M"
+      using pos[of "extreal a"] by (simp add: vimage_def Int_def conj_commute)
+    moreover have "{w \<in> space M. real (f w) \<le> a} =
+      (if a < 0 then {w \<in> space M. f w \<le> extreal a} - f -` {-\<infinity>} \<inter> space M
+      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")
+      proof (intro set_eqI) fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases "f x") auto qed
+    ultimately show "{w \<in> space M. real (f w) \<le> a} \<in> sets M" by auto
   qed
-next
-  assume a': "\<forall>a. {x \<in> space M. a < f x} \<in> sets M"
-  then have a: "\<forall>a. {x \<in> space M. f x \<le> a} \<in> sets M" unfolding less_eq_ge_measurable .
-  show "\<forall>a. {x \<in> space M. a \<le> f x} \<in> sets M" unfolding greater_eq_le_measurable[symmetric]
-  proof
-    fix a show "{x \<in> space M. f x < a} \<in> sets M"
-    proof (cases a)
-      case (preal r)
-      show ?thesis
-      proof cases
-        assume "a = 0" then show ?thesis by simp
-      next
-        assume "a \<noteq> 0"
-        have "{x\<in>space M. f x < a} = (\<Union>i. {x\<in>space M. f x \<le> a - inverse (of_nat (Suc i))})"
-        proof safe
-          fix x assume "f x < a" and [simp]: "x \<in> space M"
-          with ex_pextreal_inverse_of_nat_Suc_less[of "a - f x"]
-          obtain n where "inverse (of_nat (Suc n)) < a - f x"
-            using preal by (cases "f x") auto
-          then have "f x \<le> a - inverse (of_nat (Suc n)) "
-            using preal by (cases "f x") (auto split: split_if_asm)
-          then show "x \<in> (\<Union>i. {x \<in> space M. f x \<le> a - inverse (of_nat (Suc i))})"
-            by auto
-        next
-          fix i x assume [simp]: "x \<in> space M"
-          assume "f x \<le> a - inverse (of_nat (Suc i))"
-          also have "\<dots> < a" using `a \<noteq> 0` preal by auto
-          finally show "f x < a" .
-        qed
-        with a show ?thesis by auto
-      qed
-    next
-      case infinite
-      have "f -` {\<omega>} \<inter> space M = (\<Inter>n. {x\<in>space M. of_nat n < f x})"
-      proof (safe, simp_all, safe)
-        fix x assume *: "\<forall>n::nat. Real (real n) < f x"
-        show "f x = \<omega>"    proof (rule ccontr)
-          assume "f x \<noteq> \<omega>"
-          with real_arch_lt[of "real (f x)"] obtain n where "f x < of_nat n"
-            by (auto simp: pextreal_noteq_omega_Ex)
-          with *[THEN spec, of n] show False by auto
-        qed
-      qed
-      with a' have \<omega>: "f -` {\<omega>} \<inter> space M \<in> sets M" by auto
-      moreover have "{x \<in> space M. f x < a} = space M - f -` {\<omega>} \<inter> space M"
-        using infinite by auto
-      ultimately show ?thesis by auto
-    qed
-  qed
-qed
+qed (simp add: measurable_sets)
 
-lemma (in sigma_algebra) borel_measurable_pextreal_iff_greater:
-  "(f::'a \<Rightarrow> pextreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. a < f x} \<in> sets M)"
-proof safe
-  fix a assume f: "f \<in> borel_measurable M"
-  have "{x\<in>space M. a < f x} = f -` {a <..} \<inter> space M" by auto
-  with f show "{x\<in>space M. a < f x} \<in> sets M"
-    by (auto intro!: measurable_sets)
-next
-  assume *: "\<forall>a. {x\<in>space M. a < f x} \<in> sets M"
-  hence **: "\<forall>a. {x\<in>space M. f x < a} \<in> sets M"
-    unfolding less_eq_le_pextreal_measurable
-    unfolding greater_eq_le_measurable .
-  show "f \<in> borel_measurable M" unfolding borel_measurable_pextreal_eq_real borel_measurable_iff_greater
-  proof safe
-    have "f -` {\<omega>} \<inter> space M = space M - {x\<in>space M. f x < \<omega>}" by auto
-    then show \<omega>: "f -` {\<omega>} \<inter> space M \<in> sets M" using ** by auto
-    fix a
-    have "{w \<in> space M. a < real (f w)} =
-      (if 0 \<le> a then {w\<in>space M. Real a < f w} - (f -` {\<omega>} \<inter> space M) else space M)"
-    proof (split split_if, safe del: notI)
-      fix x assume "0 \<le> a"
-      { assume "a < real (f x)" then show "Real a < f x" "x \<notin> f -` {\<omega>} \<inter> space M"
-          using `0 \<le> a` by (cases "f x", auto) }
-      { assume "Real a < f x" "x \<notin> f -` {\<omega>}" then show "a < real (f x)"
-          using `0 \<le> a` by (cases "f x", auto) }
-    next
-      fix x assume "\<not> 0 \<le> a" then show "a < real (f x)" by (cases "f x") auto
-    qed
-    then show "{w \<in> space M. a < real (f w)} \<in> sets M"
-      using \<omega> * by (auto intro!: Diff)
-  qed
-qed
+lemma (in sigma_algebra) borel_measurable_eq_atLeast_extreal:
+  "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
+proof
+  assume pos: "\<forall>a. f -` {a..} \<inter> space M \<in> sets M"
+  moreover have "\<And>a. (\<lambda>x. - f x) -` {..a} = f -` {-a ..}"
+    by (auto simp: extreal_uminus_le_reorder)
+  ultimately have "(\<lambda>x. - f x) \<in> borel_measurable M"
+    unfolding borel_measurable_eq_atMost_extreal by auto
+  then show "f \<in> borel_measurable M" by simp
+qed (simp add: measurable_sets)
 
-lemma (in sigma_algebra) borel_measurable_pextreal_iff_less:
-  "(f::'a \<Rightarrow> pextreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. f x < a} \<in> sets M)"
-  using borel_measurable_pextreal_iff_greater unfolding less_eq_le_pextreal_measurable greater_eq_le_measurable .
+lemma (in sigma_algebra) borel_measurable_extreal_iff_less:
+  "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
+  unfolding borel_measurable_eq_atLeast_extreal greater_eq_le_measurable ..
 
-lemma (in sigma_algebra) borel_measurable_pextreal_iff_le:
-  "(f::'a \<Rightarrow> pextreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. f x \<le> a} \<in> sets M)"
-  using borel_measurable_pextreal_iff_greater unfolding less_eq_ge_measurable .
+lemma (in sigma_algebra) borel_measurable_extreal_iff_ge:
+  "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
+  unfolding borel_measurable_eq_atMost_extreal less_eq_ge_measurable ..
 
-lemma (in sigma_algebra) borel_measurable_pextreal_iff_ge:
-  "(f::'a \<Rightarrow> pextreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. {x\<in>space M. a \<le> f x} \<in> sets M)"
-  using borel_measurable_pextreal_iff_greater unfolding less_eq_le_pextreal_measurable .
-
-lemma (in sigma_algebra) borel_measurable_pextreal_eq_const:
-  fixes f :: "'a \<Rightarrow> pextreal" assumes "f \<in> borel_measurable M"
+lemma (in sigma_algebra) borel_measurable_extreal_eq_const:
+  fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M"
   shows "{x\<in>space M. f x = c} \<in> sets M"
 proof -
   have "{x\<in>space M. f x = c} = (f -` {c} \<inter> space M)" by auto
   then show ?thesis using assms by (auto intro!: measurable_sets)
 qed
 
-lemma (in sigma_algebra) borel_measurable_pextreal_neq_const:
-  fixes f :: "'a \<Rightarrow> pextreal"
-  assumes "f \<in> borel_measurable M"
+lemma (in sigma_algebra) borel_measurable_extreal_neq_const:
+  fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M"
   shows "{x\<in>space M. f x \<noteq> c} \<in> sets M"
 proof -
   have "{x\<in>space M. f x \<noteq> c} = space M - (f -` {c} \<inter> space M)" by auto
   then show ?thesis using assms by (auto intro!: measurable_sets)
 qed
 
-lemma (in sigma_algebra) borel_measurable_pextreal_less[intro,simp]:
-  fixes f g :: "'a \<Rightarrow> pextreal"
+lemma (in sigma_algebra) borel_measurable_extreal_le[intro,simp]:
+  fixes f g :: "'a \<Rightarrow> extreal"
+  assumes f: "f \<in> borel_measurable M"
+  assumes g: "g \<in> borel_measurable M"
+  shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
+proof -
+  have "{x \<in> space M. f x \<le> g x} =
+    {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>
+    f -` {-\<infinity>} \<inter> space M \<union> g -` {\<infinity>} \<inter> space M" (is "?l = ?r")
+  proof (intro set_eqI)
+    fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases rule: extreal2_cases[of "f x" "g x"]) auto
+  qed
+  with f g show ?thesis by (auto intro!: Un simp: measurable_sets)
+qed
+
+lemma (in sigma_algebra) borel_measurable_extreal_less[intro,simp]:
+  fixes f :: "'a \<Rightarrow> extreal"
   assumes f: "f \<in> borel_measurable M"
   assumes g: "g \<in> borel_measurable M"
   shows "{x \<in> space M. f x < g x} \<in> sets M"
 proof -
-  have "(\<lambda>x. real (f x)) \<in> borel_measurable M"
-    "(\<lambda>x. real (g x)) \<in> borel_measurable M"
-    using assms by (auto intro!: borel_measurable_real)
-  from borel_measurable_less[OF this]
-  have "{x \<in> space M. real (f x) < real (g x)} \<in> sets M" .
-  moreover have "{x \<in> space M. f x \<noteq> \<omega>} \<in> sets M" using f by (rule borel_measurable_pextreal_neq_const)
-  moreover have "{x \<in> space M. g x = \<omega>} \<in> sets M" using g by (rule borel_measurable_pextreal_eq_const)
-  moreover have "{x \<in> space M. g x \<noteq> \<omega>} \<in> sets M" using g by (rule borel_measurable_pextreal_neq_const)
-  moreover have "{x \<in> space M. f x < g x} = ({x \<in> space M. g x = \<omega>} \<inter> {x \<in> space M. f x \<noteq> \<omega>}) \<union>
-    ({x \<in> space M. g x \<noteq> \<omega>} \<inter> {x \<in> space M. f x \<noteq> \<omega>} \<inter> {x \<in> space M. real (f x) < real (g x)})"
-    by (auto simp: real_of_pextreal_strict_mono_iff)
-  ultimately show ?thesis by auto
-qed
-
-lemma (in sigma_algebra) borel_measurable_pextreal_le[intro,simp]:
-  fixes f :: "'a \<Rightarrow> pextreal"
-  assumes f: "f \<in> borel_measurable M"
-  assumes g: "g \<in> borel_measurable M"
-  shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
-proof -
-  have "{x \<in> space M. f x \<le> g x} = space M - {x \<in> space M. g x < f x}" by auto
+  have "{x \<in> space M. f x < g x} = space M - {x \<in> space M. g x \<le> f x}" by auto
   then show ?thesis using g f by auto
 qed
 
-lemma (in sigma_algebra) borel_measurable_pextreal_eq[intro,simp]:
-  fixes f :: "'a \<Rightarrow> pextreal"
+lemma (in sigma_algebra) borel_measurable_extreal_eq[intro,simp]:
+  fixes f :: "'a \<Rightarrow> extreal"
   assumes f: "f \<in> borel_measurable M"
   assumes g: "g \<in> borel_measurable M"
   shows "{w \<in> space M. f w = g w} \<in> sets M"
@@ -1395,8 +1283,8 @@
   then show ?thesis using g f by auto
 qed
 
-lemma (in sigma_algebra) borel_measurable_pextreal_neq[intro,simp]:
-  fixes f :: "'a \<Rightarrow> pextreal"
+lemma (in sigma_algebra) borel_measurable_extreal_neq[intro,simp]:
+  fixes f :: "'a \<Rightarrow> extreal"
   assumes f: "f \<in> borel_measurable M"
   assumes g: "g \<in> borel_measurable M"
   shows "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
@@ -1405,20 +1293,28 @@
   thus ?thesis using f g by auto
 qed
 
-lemma (in sigma_algebra) borel_measurable_pextreal_add[intro, simp]:
-  fixes f :: "'a \<Rightarrow> pextreal"
+lemma (in sigma_algebra) split_sets:
+  "{x\<in>space M. P x \<or> Q x} = {x\<in>space M. P x} \<union> {x\<in>space M. Q x}"
+  "{x\<in>space M. P x \<and> Q x} = {x\<in>space M. P x} \<inter> {x\<in>space M. Q x}"
+  by auto
+
+lemma (in sigma_algebra) borel_measurable_extreal_add[intro, simp]:
+  fixes f :: "'a \<Rightarrow> extreal"
   assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
 proof -
-  have *: "(\<lambda>x. f x + g x) =
-     (\<lambda>x. if f x = \<omega> then \<omega> else if g x = \<omega> then \<omega> else Real (real (f x) + real (g x)))"
-     by (auto simp: fun_eq_iff pextreal_noteq_omega_Ex)
-  show ?thesis using assms unfolding *
-    by (auto intro!: measurable_If)
+  { fix x assume "x \<in> space M" then have "f x + g x =
+      (if f x = \<infinity> \<or> g x = \<infinity> then \<infinity>
+        else if f x = -\<infinity> \<or> g x = -\<infinity> then -\<infinity>
+        else extreal (real (f x) + real (g x)))"
+      by (cases rule: extreal2_cases[of "f x" "g x"]) auto }
+  with assms show ?thesis
+    by (auto cong: measurable_cong simp: split_sets
+             intro!: Un measurable_If measurable_sets)
 qed
 
-lemma (in sigma_algebra) borel_measurable_pextreal_setsum[simp, intro]:
-  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> pextreal"
+lemma (in sigma_algebra) borel_measurable_extreal_setsum[simp, intro]:
+  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> extreal"
   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
 proof cases
@@ -1427,20 +1323,49 @@
     by induct auto
 qed (simp add: borel_measurable_const)
 
-lemma (in sigma_algebra) borel_measurable_pextreal_times[intro, simp]:
-  fixes f :: "'a \<Rightarrow> pextreal" assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
+lemma abs_extreal_ge0[simp]: "0 \<le> x \<Longrightarrow> \<bar>x :: extreal\<bar> = x"
+  by (cases x) auto
+
+lemma abs_extreal_less0[simp]: "x < 0 \<Longrightarrow> \<bar>x :: extreal\<bar> = -x"
+  by (cases x) auto
+
+lemma abs_extreal_pos[simp]: "0 \<le> \<bar>x :: extreal\<bar>"
+  by (cases x) auto
+
+lemma (in sigma_algebra) borel_measurable_extreal_abs[intro, simp]:
+  fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M"
+  shows "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
+proof -
+  { fix x have "\<bar>f x\<bar> = (if 0 \<le> f x then f x else - f x)" by auto }
+  then show ?thesis using assms by (auto intro!: measurable_If)
+qed
+
+lemma (in sigma_algebra) borel_measurable_extreal_times[intro, simp]:
+  fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
 proof -
+  { fix f g :: "'a \<Rightarrow> extreal"
+    assume b: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
+      and pos: "\<And>x. 0 \<le> f x" "\<And>x. 0 \<le> g x"
+    { fix x have *: "f x * g x = (if f x = 0 \<or> g x = 0 then 0
+        else if f x = \<infinity> \<or> g x = \<infinity> then \<infinity>
+        else extreal (real (f x) * real (g x)))"
+      apply (cases rule: extreal2_cases[of "f x" "g x"])
+      using pos[of x] by auto }
+    with b have "(\<lambda>x. f x * g x) \<in> borel_measurable M"
+      by (auto cong: measurable_cong simp: split_sets
+               intro!: Un measurable_If measurable_sets) }
+  note pos_times = this
   have *: "(\<lambda>x. f x * g x) =
-     (\<lambda>x. if f x = 0 then 0 else if g x = 0 then 0 else if f x = \<omega> then \<omega> else if g x = \<omega> then \<omega> else
-      Real (real (f x) * real (g x)))"
-     by (auto simp: fun_eq_iff pextreal_noteq_omega_Ex)
+    (\<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>))"
+    by (auto simp: fun_eq_iff)
   show ?thesis using assms unfolding *
-    by (auto intro!: measurable_If)
+    by (intro measurable_If pos_times borel_measurable_uminus_extreal)
+       (auto simp: split_sets intro!: Int)
 qed
 
-lemma (in sigma_algebra) borel_measurable_pextreal_setprod[simp, intro]:
-  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> pextreal"
+lemma (in sigma_algebra) borel_measurable_extreal_setprod[simp, intro]:
+  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> extreal"
   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
 proof cases
@@ -1448,64 +1373,73 @@
   thus ?thesis using assms by induct auto
 qed simp
 
-lemma (in sigma_algebra) borel_measurable_pextreal_min[simp, intro]:
-  fixes f g :: "'a \<Rightarrow> pextreal"
+lemma (in sigma_algebra) borel_measurable_extreal_min[simp, intro]:
+  fixes f g :: "'a \<Rightarrow> extreal"
   assumes "f \<in> borel_measurable M"
   assumes "g \<in> borel_measurable M"
   shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
   using assms unfolding min_def by (auto intro!: measurable_If)
 
-lemma (in sigma_algebra) borel_measurable_pextreal_max[simp, intro]:
-  fixes f g :: "'a \<Rightarrow> pextreal"
+lemma (in sigma_algebra) borel_measurable_extreal_max[simp, intro]:
+  fixes f g :: "'a \<Rightarrow> extreal"
   assumes "f \<in> borel_measurable M"
   and "g \<in> borel_measurable M"
   shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
   using assms unfolding max_def by (auto intro!: measurable_If)
 
 lemma (in sigma_algebra) borel_measurable_SUP[simp, intro]:
-  fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> pextreal"
+  fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> extreal"
   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
   shows "(\<lambda>x. SUP i : A. f i x) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
-  unfolding borel_measurable_pextreal_iff_greater
-proof safe
+  unfolding borel_measurable_extreal_iff_ge
+proof
   fix a
-  have "{x\<in>space M. a < ?sup x} = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
+  have "?sup -` {a<..} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
     by (auto simp: less_SUP_iff SUPR_apply)
-  then show "{x\<in>space M. a < ?sup x} \<in> sets M"
+  then show "?sup -` {a<..} \<inter> space M \<in> sets M"
     using assms by auto
 qed
 
 lemma (in sigma_algebra) borel_measurable_INF[simp, intro]:
-  fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> pextreal"
+  fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> extreal"
   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
   shows "(\<lambda>x. INF i : A. f i x) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
-  unfolding borel_measurable_pextreal_iff_less
-proof safe
+  unfolding borel_measurable_extreal_iff_less
+proof
   fix a
-  have "{x\<in>space M. ?inf x < a} = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
+  have "?inf -` {..<a} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
     by (auto simp: INF_less_iff INFI_apply)
-  then show "{x\<in>space M. ?inf x < a} \<in> sets M"
+  then show "?inf -` {..<a} \<inter> space M \<in> sets M"
     using assms by auto
 qed
 
-lemma (in sigma_algebra) borel_measurable_pextreal_diff[simp, intro]:
-  fixes f g :: "'a \<Rightarrow> pextreal"
+lemma (in sigma_algebra) borel_measurable_liminf[simp, intro]:
+  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
+  assumes "\<And>i. f i \<in> borel_measurable M"
+  shows "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
+  unfolding liminf_SUPR_INFI using assms by auto
+
+lemma (in sigma_algebra) borel_measurable_limsup[simp, intro]:
+  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
+  assumes "\<And>i. f i \<in> borel_measurable M"
+  shows "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
+  unfolding limsup_INFI_SUPR using assms by auto
+
+lemma (in sigma_algebra) borel_measurable_extreal_diff[simp, intro]:
+  fixes f g :: "'a \<Rightarrow> extreal"
   assumes "f \<in> borel_measurable M"
   assumes "g \<in> borel_measurable M"
   shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
-  unfolding borel_measurable_pextreal_iff_greater
-proof safe
-  fix a
-  have "{x \<in> space M. a < f x - g x} = {x \<in> space M. g x + a < f x}"
-    by (simp add: pextreal_less_minus_iff)
-  then show "{x \<in> space M. a < f x - g x} \<in> sets M"
-    using assms by auto
-qed
+  unfolding minus_extreal_def using assms by auto
 
 lemma (in sigma_algebra) borel_measurable_psuminf[simp, intro]:
-  assumes "\<And>i. f i \<in> borel_measurable M"
-  shows "(\<lambda>x. (\<Sum>\<^isub>\<infinity> i. f i x)) \<in> borel_measurable M"
-  using assms unfolding psuminf_def by auto
+  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
+  assumes "\<And>i. f i \<in> borel_measurable M" and pos: "\<And>i x. x \<in> space M \<Longrightarrow> 0 \<le> f i x"
+  shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
+  apply (subst measurable_cong)
+  apply (subst suminf_extreal_eq_SUPR)
+  apply (rule pos)
+  using assms by auto
 
 section "LIMSEQ is borel measurable"
 
@@ -1515,28 +1449,11 @@
   and u: "\<And>i. u i \<in> borel_measurable M"
   shows "u' \<in> borel_measurable M"
 proof -
-  let "?pu x i" = "max (u i x) 0"
-  let "?nu x i" = "max (- u i x) 0"
-  { fix x assume x: "x \<in> space M"
-    have "(?pu x) ----> max (u' x) 0"
-      "(?nu x) ----> max (- u' x) 0"
-      using u'[OF x] by (auto intro!: LIMSEQ_max LIMSEQ_minus)
-    from LIMSEQ_imp_lim_INF[OF _ this(1)] LIMSEQ_imp_lim_INF[OF _ this(2)]
-    have "(SUP n. INF m. Real (u (n + m) x)) = Real (u' x)"
-      "(SUP n. INF m. Real (- u (n + m) x)) = Real (- u' x)"
-      by (simp_all add: Real_max'[symmetric]) }
-  note eq = this
-  have *: "\<And>x. real (Real (u' x)) - real (Real (- u' x)) = u' x"
+  have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. extreal (u n x)) = extreal (u' x)"
+    using u' by (simp add: lim_imp_Liminf trivial_limit_sequentially lim_extreal)
+  moreover from u have "(\<lambda>x. liminf (\<lambda>n. extreal (u n x))) \<in> borel_measurable M"
     by auto
-  have "(\<lambda>x. SUP n. INF m. Real (u (n + m) x)) \<in> borel_measurable M"
-       "(\<lambda>x. SUP n. INF m. Real (- u (n + m) x)) \<in> borel_measurable M"
-    using u by auto
-  with eq[THEN measurable_cong, of M "\<lambda>x. x" borel]
-  have "(\<lambda>x. Real (u' x)) \<in> borel_measurable M"
-       "(\<lambda>x. Real (- u' x)) \<in> borel_measurable M" by auto
-  note this[THEN borel_measurable_real]
-  from borel_measurable_diff[OF this]
-  show ?thesis unfolding * .
+  ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_extreal_iff)
 qed
 
 end
--- a/src/HOL/Probability/Caratheodory.thy	Mon Mar 14 15:17:10 2011 +0100
+++ b/src/HOL/Probability/Caratheodory.thy	Mon Mar 14 15:29:10 2011 +0100
@@ -1,36 +1,66 @@
 header {*Caratheodory Extension Theorem*}
 
 theory Caratheodory
-  imports Sigma_Algebra Positive_Extended_Real
+  imports Sigma_Algebra Extended_Real_Limits
 begin
 
+lemma suminf_extreal_2dimen:
+  fixes f:: "nat \<times> nat \<Rightarrow> extreal"
+  assumes pos: "\<And>p. 0 \<le> f p"
+  assumes "\<And>m. g m = (\<Sum>n. f (m,n))"
+  shows "(\<Sum>i. f (prod_decode i)) = suminf g"
+proof -
+  have g_def: "g = (\<lambda>m. (\<Sum>n. f (m,n)))"
+    using assms by (simp add: fun_eq_iff)
+  have reindex: "\<And>B. (\<Sum>x\<in>B. f (prod_decode x)) = setsum f (prod_decode ` B)"
+    by (simp add: setsum_reindex[OF inj_prod_decode] comp_def)
+  { fix n
+    let ?M = "\<lambda>f. Suc (Max (f ` prod_decode ` {..<n}))"
+    { fix a b x assume "x < n" and [symmetric]: "(a, b) = prod_decode x"
+      then have "a < ?M fst" "b < ?M snd"
+        by (auto intro!: Max_ge le_imp_less_Suc image_eqI) }
+    then have "setsum f (prod_decode ` {..<n}) \<le> setsum f ({..<?M fst} \<times> {..<?M snd})"
+      by (auto intro!: setsum_mono3 simp: pos)
+    then have "\<exists>a b. setsum f (prod_decode ` {..<n}) \<le> setsum f ({..<a} \<times> {..<b})" by auto }
+  moreover
+  { fix a b
+    let ?M = "prod_decode ` {..<Suc (Max (prod_encode ` ({..<a} \<times> {..<b})))}"
+    { fix a' b' assume "a' < a" "b' < b" then have "(a', b') \<in> ?M"
+        by (auto intro!: Max_ge le_imp_less_Suc image_eqI[where x="prod_encode (a', b')"]) }
+    then have "setsum f ({..<a} \<times> {..<b}) \<le> setsum f ?M"
+      by (auto intro!: setsum_mono3 simp: pos) }
+  ultimately
+  show ?thesis unfolding g_def using pos
+    by (auto intro!: SUPR_eq  simp: setsum_cartesian_product reindex le_SUPI2
+                     setsum_nonneg suminf_extreal_eq_SUPR SUPR_pair
+                     SUPR_extreal_setsum[symmetric] incseq_setsumI setsum_nonneg)
+qed
+
 text{*From the Hurd/Coble measure theory development, translated by Lawrence Paulson.*}
 
 subsection {* Measure Spaces *}
 
 record 'a measure_space = "'a algebra" +
-  measure :: "'a set \<Rightarrow> pextreal"
+  measure :: "'a set \<Rightarrow> extreal"
 
-definition positive where "positive M f \<longleftrightarrow> f {} = (0::pextreal)"
-  -- "Positive is enforced by the type"
+definition positive where "positive M f \<longleftrightarrow> f {} = (0::extreal) \<and> (\<forall>A\<in>sets M. 0 \<le> f A)"
 
 definition additive where "additive M f \<longleftrightarrow>
   (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<inter> y = {} \<longrightarrow> f (x \<union> y) = f x + f y)"
 
-definition countably_additive where "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>
-    (\<Sum>\<^isub>\<infinity> n. f (A n)) = f (\<Union>i. A i))"
+definition countably_additive :: "('a, 'b) algebra_scheme \<Rightarrow> ('a set \<Rightarrow> extreal) \<Rightarrow> bool" where
+  "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>
+    (\<Sum>i. f (A i)) = f (\<Union>i. A i))"
 
 definition increasing where "increasing M f \<longleftrightarrow>
   (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<subseteq> y \<longrightarrow> f x \<le> f y)"
 
 definition subadditive where "subadditive M f \<longleftrightarrow>
-  (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<inter> y = {} \<longrightarrow>
-    f (x \<union> y) \<le> f x + f y)"
+  (\<forall>x \<in> sets M. \<forall>y \<in> sets M. x \<inter> y = {} \<longrightarrow> f (x \<union> y) \<le> f x + f y)"
 
 definition countably_subadditive where "countably_subadditive M f \<longleftrightarrow>
   (\<forall>A. range A \<subseteq> sets M \<longrightarrow> disjoint_family A \<longrightarrow> (\<Union>i. A i) \<in> sets M \<longrightarrow>
-    f (\<Union>i. A i) \<le> (\<Sum>\<^isub>\<infinity> n. f (A n)))"
+    (f (\<Union>i. A i) \<le> (\<Sum>i. f (A i))))"
 
 definition lambda_system where "lambda_system M f = {l \<in> sets M.
   \<forall>x \<in> sets M. f (l \<inter> x) + f ((space M - l) \<inter> x) = f x}"
@@ -39,14 +69,19 @@
   positive M f \<and> increasing M f \<and> countably_subadditive M f"
 
 definition measure_set where "measure_set M f X = {r.
-  \<exists>A. range A \<subseteq> sets M \<and> disjoint_family A \<and> X \<subseteq> (\<Union>i. A i) \<and> (\<Sum>\<^isub>\<infinity> i. f (A i)) = r}"
+  \<exists>A. range A \<subseteq> sets M \<and> disjoint_family A \<and> X \<subseteq> (\<Union>i. A i) \<and> (\<Sum>i. f (A i)) = r}"
 
 locale measure_space = sigma_algebra M for M :: "('a, 'b) measure_space_scheme" +
-  assumes empty_measure [simp]: "measure M {} = 0"
+  assumes measure_positive: "positive M (measure M)"
       and ca: "countably_additive M (measure M)"
 
 abbreviation (in measure_space) "\<mu> \<equiv> measure M"
 
+lemma (in measure_space)
+  shows empty_measure[simp, intro]: "\<mu> {} = 0"
+  and positive_measure[simp, intro!]: "\<And>A. A \<in> sets M \<Longrightarrow> 0 \<le> \<mu> A"
+  using measure_positive unfolding positive_def by auto
+
 lemma increasingD:
   "increasing M f \<Longrightarrow> x \<subseteq> y \<Longrightarrow> x\<in>sets M \<Longrightarrow> y\<in>sets M \<Longrightarrow> f x \<le> f y"
   by (auto simp add: increasing_def)
@@ -61,39 +96,30 @@
     \<Longrightarrow> f (x \<union> y) = f x + f y"
   by (auto simp add: additive_def)
 
-lemma countably_additiveD:
-  "countably_additive M f \<Longrightarrow> range A \<subseteq> sets M \<Longrightarrow> disjoint_family A
-    \<Longrightarrow> (\<Union>i. A i) \<in> sets M \<Longrightarrow> (\<Sum>\<^isub>\<infinity> n. f (A n)) = f (\<Union>i. A i)"
-  by (simp add: countably_additive_def)
-
-lemma countably_subadditiveD:
-  "countably_subadditive M f \<Longrightarrow> range A \<subseteq> sets M \<Longrightarrow> disjoint_family A \<Longrightarrow>
-   (\<Union>i. A i) \<in> sets M \<Longrightarrow> f (\<Union>i. A i) \<le> psuminf (f o A)"
-  by (auto simp add: countably_subadditive_def o_def)
-
 lemma countably_additiveI:
-  "(\<And>A. range A \<subseteq> sets M \<Longrightarrow> disjoint_family A \<Longrightarrow> (\<Union>i. A i) \<in> sets M
-    \<Longrightarrow> (\<Sum>\<^isub>\<infinity> n. f (A n)) = f (\<Union>i. A i)) \<Longrightarrow> countably_additive M f"
-  by (simp add: countably_additive_def)
+  assumes "\<And>A x. range A \<subseteq> sets M \<Longrightarrow> disjoint_family A \<Longrightarrow> (\<Union>i. A i) \<in> sets M
+    \<Longrightarrow> (\<Sum>i. f (A i)) = f (\<Union>i. A i)"
+  shows "countably_additive M f"
+  using assms by (simp add: countably_additive_def)
 
 section "Extend binary sets"
 
 lemma LIMSEQ_binaryset:
   assumes f: "f {} = 0"
-  shows  "(\<lambda>n. \<Sum>i = 0..<n. f (binaryset A B i)) ----> f A + f B"
+  shows  "(\<lambda>n. \<Sum>i<n. f (binaryset A B i)) ----> f A + f B"
 proof -
-  have "(\<lambda>n. \<Sum>i = 0..< Suc (Suc n). f (binaryset A B i)) = (\<lambda>n. f A + f B)"
+  have "(\<lambda>n. \<Sum>i < Suc (Suc n). f (binaryset A B i)) = (\<lambda>n. f A + f B)"
     proof
       fix n
-      show "(\<Sum>i\<Colon>nat = 0\<Colon>nat..<Suc (Suc n). f (binaryset A B i)) = f A + f B"
+      show "(\<Sum>i < Suc (Suc n). f (binaryset A B i)) = f A + f B"
         by (induct n)  (auto simp add: binaryset_def f)
     qed
   moreover
   have "... ----> f A + f B" by (rule LIMSEQ_const)
   ultimately
-  have "(\<lambda>n. \<Sum>i = 0..< Suc (Suc n). f (binaryset A B i)) ----> f A + f B"
+  have "(\<lambda>n. \<Sum>i< Suc (Suc n). f (binaryset A B i)) ----> f A + f B"
     by metis
-  hence "(\<lambda>n. \<Sum>i = 0..< n+2. f (binaryset A B i)) ----> f A + f B"
+  hence "(\<lambda>n. \<Sum>i< n+2. f (binaryset A B i)) ----> f A + f B"
     by simp
   thus ?thesis by (rule LIMSEQ_offset [where k=2])
 qed
@@ -101,28 +127,13 @@
 lemma binaryset_sums:
   assumes f: "f {} = 0"
   shows  "(\<lambda>n. f (binaryset A B n)) sums (f A + f B)"
-    by (simp add: sums_def LIMSEQ_binaryset [where f=f, OF f])
+    by (simp add: sums_def LIMSEQ_binaryset [where f=f, OF f] atLeast0LessThan)
 
 lemma suminf_binaryset_eq:
-  fixes f :: "'a set \<Rightarrow> real"
+  fixes f :: "'a set \<Rightarrow> 'b::{comm_monoid_add, t2_space}"
   shows "f {} = 0 \<Longrightarrow> (\<Sum>n. f (binaryset A B n)) = f A + f B"
   by (metis binaryset_sums sums_unique)
 
-lemma binaryset_psuminf:
-  assumes "f {} = 0"
-  shows "(\<Sum>\<^isub>\<infinity> n. f (binaryset A B n)) = f A + f B" (is "?suminf = ?sum")
-proof -
-  have *: "{..<2} = {0, 1::nat}" by auto
-  have "\<forall>n\<ge>2. f (binaryset A B n) = 0"
-    unfolding binaryset_def
-    using assms by auto
-  hence "?suminf = (\<Sum>N<2. f (binaryset A B N))"
-    by (rule psuminf_finite)
-  also have "... = ?sum" unfolding * binaryset_def
-    by simp
-  finally show ?thesis .
-qed
-
 subsection {* Lambda Systems *}
 
 lemma (in algebra) lambda_system_eq:
@@ -144,7 +155,7 @@
   by (simp add: lambda_system_def)
 
 lemma (in algebra) lambda_system_Compl:
-  fixes f:: "'a set \<Rightarrow> pextreal"
+  fixes f:: "'a set \<Rightarrow> extreal"
   assumes x: "x \<in> lambda_system M f"
   shows "space M - x \<in> lambda_system M f"
 proof -
@@ -157,7 +168,7 @@
 qed
 
 lemma (in algebra) lambda_system_Int:
-  fixes f:: "'a set \<Rightarrow> pextreal"
+  fixes f:: "'a set \<Rightarrow> extreal"
   assumes xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
   shows "x \<inter> y \<in> lambda_system M f"
 proof -
@@ -191,7 +202,7 @@
 qed
 
 lemma (in algebra) lambda_system_Un:
-  fixes f:: "'a set \<Rightarrow> pextreal"
+  fixes f:: "'a set \<Rightarrow> extreal"
   assumes xl: "x \<in> lambda_system M f" and yl: "y \<in> lambda_system M f"
   shows "x \<union> y \<in> lambda_system M f"
 proof -
@@ -250,53 +261,54 @@
     by (auto simp add: disjoint_family_on_def binaryset_def)
   hence "range (binaryset x y) \<subseteq> sets M \<longrightarrow>
          (\<Union>i. binaryset x y i) \<in> sets M \<longrightarrow>
-         f (\<Union>i. binaryset x y i) \<le> (\<Sum>\<^isub>\<infinity> n. f (binaryset x y n))"
-    using cs by (simp add: countably_subadditive_def)
+         f (\<Union>i. binaryset x y i) \<le> (\<Sum> n. f (binaryset x y n))"
+    using cs by (auto simp add: countably_subadditive_def)
   hence "{x,y,{}} \<subseteq> sets M \<longrightarrow> x \<union> y \<in> sets M \<longrightarrow>
-         f (x \<union> y) \<le> (\<Sum>\<^isub>\<infinity> n. f (binaryset x y n))"
+         f (x \<union> y) \<le> (\<Sum> n. f (binaryset x y n))"
     by (simp add: range_binaryset_eq UN_binaryset_eq)
   thus "f (x \<union> y) \<le>  f x + f y" using f x y
-    by (auto simp add: Un o_def binaryset_psuminf positive_def)
+    by (auto simp add: Un o_def suminf_binaryset_eq positive_def)
 qed
 
 lemma (in algebra) additive_sum:
   fixes A:: "nat \<Rightarrow> 'a set"
-  assumes f: "positive M f" and ad: "additive M f"
+  assumes f: "positive M f" and ad: "additive M f" and "finite S"
       and A: "range A \<subseteq> sets M"
-      and disj: "disjoint_family A"
-  shows  "setsum (f \<circ> A) {0..<n} = f (\<Union>i\<in>{0..<n}. A i)"
-proof (induct n)
-  case 0 show ?case using f by (simp add: positive_def)
+      and disj: "disjoint_family_on A S"
+  shows  "(\<Sum>i\<in>S. f (A i)) = f (\<Union>i\<in>S. A i)"
+using `finite S` disj proof induct
+  case empty show ?case using f by (simp add: positive_def)
 next
-  case (Suc n)
-  have "A n \<inter> (\<Union>i\<in>{0..<n}. A i) = {}" using disj
-    by (auto simp add: disjoint_family_on_def neq_iff) blast
+  case (insert s S)
+  then have "A s \<inter> (\<Union>i\<in>S. A i) = {}"
+    by (auto simp add: disjoint_family_on_def neq_iff)
   moreover
-  have "A n \<in> sets M" using A by blast
-  moreover have "(\<Union>i\<in>{0..<n}. A i) \<in> sets M"
-    by (metis A UNION_in_sets atLeast0LessThan)
+  have "A s \<in> sets M" using A by blast
+  moreover have "(\<Union>i\<in>S. A i) \<in> sets M"
+    using A `finite S` by auto
   moreover
-  ultimately have "f (A n \<union> (\<Union>i\<in>{0..<n}. A i)) = f (A n) + f(\<Union>i\<in>{0..<n}. A i)"
+  ultimately have "f (A s \<union> (\<Union>i\<in>S. A i)) = f (A s) + f(\<Union>i\<in>S. A i)"
     using ad UNION_in_sets A by (auto simp add: additive_def)
-  with Suc.hyps show ?case using ad
-    by (auto simp add: atLeastLessThanSuc additive_def)
+  with insert show ?case using ad disjoint_family_on_mono[of S "insert s S" A]
+    by (auto simp add: additive_def subset_insertI)
 qed
 
 lemma (in algebra) increasing_additive_bound:
-  fixes A:: "nat \<Rightarrow> 'a set" and  f :: "'a set \<Rightarrow> pextreal"
+  fixes A:: "nat \<Rightarrow> 'a set" and  f :: "'a set \<Rightarrow> extreal"
   assumes f: "positive M f" and ad: "additive M f"
       and inc: "increasing M f"
       and A: "range A \<subseteq> sets M"
       and disj: "disjoint_family A"
-  shows  "psuminf (f \<circ> A) \<le> f (space M)"
-proof (safe intro!: psuminf_bound)
+  shows  "(\<Sum>i. f (A i)) \<le> f (space M)"
+proof (safe intro!: suminf_bound)
   fix N
-  have "setsum (f \<circ> A) {0..<N} = f (\<Union>i\<in>{0..<N}. A i)"
-    by (rule additive_sum [OF f ad A disj])
+  note disj_N = disjoint_family_on_mono[OF _ disj, of "{..<N}"]
+  have "(\<Sum>i<N. f (A i)) = f (\<Union>i\<in>{..<N}. A i)"
+    by (rule additive_sum [OF f ad _ A]) (auto simp: disj_N)
   also have "... \<le> f (space M)" using space_closed A
-    by (blast intro: increasingD [OF inc] UNION_in_sets top)
-  finally show "setsum (f \<circ> A) {..<N} \<le> f (space M)" by (simp add: atLeast0LessThan)
-qed
+    by (intro increasingD[OF inc] finite_UN) auto
+  finally show "(\<Sum>i<N. f (A i)) \<le> f (space M)" by simp
+qed (insert f A, auto simp: positive_def)
 
 lemma lambda_system_increasing:
  "increasing M f \<Longrightarrow> increasing (M (|sets := lambda_system M f|)) f"
@@ -307,7 +319,7 @@
   by (simp add: positive_def lambda_system_def)
 
 lemma (in algebra) lambda_system_strong_sum:
-  fixes A:: "nat \<Rightarrow> 'a set" and f :: "'a set \<Rightarrow> pextreal"
+  fixes A:: "nat \<Rightarrow> 'a set" and f :: "'a set \<Rightarrow> extreal"
   assumes f: "positive M f" and a: "a \<in> sets M"
       and A: "range A \<subseteq> lambda_system M f"
       and disj: "disjoint_family A"
@@ -331,7 +343,7 @@
   assumes oms: "outer_measure_space M f"
       and A: "range A \<subseteq> lambda_system M f"
       and disj: "disjoint_family A"
-  shows  "(\<Union>i. A i) \<in> lambda_system M f \<and> psuminf (f \<circ> A) = f (\<Union>i. A i)"
+  shows  "(\<Union>i. A i) \<in> lambda_system M f \<and> (\<Sum>i. f (A i)) = f (\<Union>i. A i)"
 proof -
   have pos: "positive M f" and inc: "increasing M f"
    and csa: "countably_subadditive M f"
@@ -347,15 +359,16 @@
 
   have U_in: "(\<Union>i. A i) \<in> sets M"
     by (metis A'' countable_UN)
-  have U_eq: "f (\<Union>i. A i) = psuminf (f o A)"
+  have U_eq: "f (\<Union>i. A i) = (\<Sum>i. f (A i))"
   proof (rule antisym)
-    show "f (\<Union>i. A i) \<le> psuminf (f \<circ> A)"
-      by (rule countably_subadditiveD [OF csa A'' disj U_in])
-    show "psuminf (f \<circ> A) \<le> f (\<Union>i. A i)"
-      by (rule psuminf_bound, unfold atLeast0LessThan[symmetric])
-         (metis algebra.additive_sum [OF alg_ls] pos disj UN_Un Un_UNIV_right
-                lambda_system_positive lambda_system_additive
-                subset_Un_eq increasingD [OF inc] A' A'' UNION_in_sets U_in)
+    show "f (\<Union>i. A i) \<le> (\<Sum>i. f (A i))"
+      using csa[unfolded countably_subadditive_def] A'' disj U_in by auto
+    have *: "\<And>i. 0 \<le> f (A i)" using pos A'' unfolding positive_def by auto
+    have dis: "\<And>N. disjoint_family_on A {..<N}" by (intro disjoint_family_on_mono[OF _ disj]) auto
+    show "(\<Sum>i. f (A i)) \<le> f (\<Union>i. A i)"
+      using algebra.additive_sum [OF alg_ls lambda_system_positive[OF pos] lambda_system_additive _ A' dis]
+      using A''
+      by (intro suminf_bound[OF _ *]) (auto intro!: increasingD[OF inc] allI countable_UN)
   qed
   {
     fix a
@@ -373,15 +386,15 @@
         have "a \<inter> (\<Union>i. A i) \<in> sets M"
           by (metis Int U_in a)
         ultimately
-        have "f (a \<inter> (\<Union>i. A i)) \<le> psuminf (f \<circ> (\<lambda>i. a \<inter> i) \<circ> A)"
-          using countably_subadditiveD [OF csa, of "(\<lambda>i. a \<inter> A i)"]
+        have "f (a \<inter> (\<Union>i. A i)) \<le> (\<Sum>i. f (a \<inter> A i))"
+          using csa[unfolded countably_subadditive_def, rule_format, of "(\<lambda>i. a \<inter> A i)"]
           by (simp add: o_def)
         hence "f (a \<inter> (\<Union>i. A i)) + f (a - (\<Union>i. A i)) \<le>
-            psuminf (f \<circ> (\<lambda>i. a \<inter> i) \<circ> A) + f (a - (\<Union>i. A i))"
+            (\<Sum>i. f (a \<inter> A i)) + f (a - (\<Union>i. A i))"
           by (rule add_right_mono)
         moreover
-        have "psuminf (f \<circ> (\<lambda>i. a \<inter> i) \<circ> A) + f (a - (\<Union>i. A i)) \<le> f a"
-          proof (safe intro!: psuminf_bound_add)
+        have "(\<Sum>i. f (a \<inter> A i)) + f (a - (\<Union>i. A i)) \<le> f a"
+          proof (intro suminf_bound_add allI)
             fix n
             have UNION_in: "(\<Union>i\<in>{0..<n}. A i) \<in> sets M"
               by (metis A'' UNION_in_sets)
@@ -395,8 +408,14 @@
             have "f (a - (\<Union>i. A i)) \<le> f (a - (\<Union>i\<in>{0..<n}. A i))"
               by (blast intro: increasingD [OF inc] UNION_eq_Union_image
                                UNION_in U_in)
-            thus "setsum (f \<circ> (\<lambda>i. a \<inter> i) \<circ> A) {..<n} + f (a - (\<Union>i. A i)) \<le> f a"
+            thus "(\<Sum>i<n. f (a \<inter> A i)) + f (a - (\<Union>i. A i)) \<le> f a"
               by (simp add: lambda_system_strong_sum pos A disj eq_fa add_left_mono atLeast0LessThan[symmetric])
+          next
+            have "\<And>i. a \<inter> A i \<in> sets M" using A'' by auto
+            then show "\<And>i. 0 \<le> f (a \<inter> A i)" using pos[unfolded positive_def] by auto
+            have "\<And>i. a - (\<Union>i. A i) \<in> sets M" using A'' by auto
+            then have "\<And>i. 0 \<le> f (a - (\<Union>i. A i))" using pos[unfolded positive_def] by auto
+            then show "f (a - (\<Union>i. A i)) \<noteq> -\<infinity>" by auto
           qed
         ultimately show "f (a \<inter> (\<Union>i. A i)) + f (a - (\<Union>i. A i)) \<le> f a"
           by (rule order_trans)
@@ -443,12 +462,14 @@
 proof (auto simp add: increasing_def)
   fix x y
   assume xy: "x \<in> sets M" "y \<in> sets M" "x \<subseteq> y"
-  have "f x \<le> f x + f (y-x)" ..
+  then have "y - x \<in> sets M" by auto
+  then have "0 \<le> f (y-x)" using posf[unfolded positive_def] by auto
+  then have "f x + 0 \<le> f x + f (y-x)" by (intro add_left_mono) auto
   also have "... = f (x \<union> (y-x))" using addf
     by (auto simp add: additive_def) (metis Diff_disjoint Un_Diff_cancel Diff xy(1,2))
   also have "... = f y"
     by (metis Un_Diff_cancel Un_absorb1 xy(3))
-  finally show "f x \<le> f y" .
+  finally show "f x \<le> f y" by simp
 qed
 
 lemma (in algebra) countably_additive_additive:
@@ -461,27 +482,27 @@
     by (auto simp add: disjoint_family_on_def binaryset_def)
   hence "range (binaryset x y) \<subseteq> sets M \<longrightarrow>
          (\<Union>i. binaryset x y i) \<in> sets M \<longrightarrow>
-         f (\<Union>i. binaryset x y i) = (\<Sum>\<^isub>\<infinity> n. f (binaryset x y n))"
+         f (\<Union>i. binaryset x y i) = (\<Sum> n. f (binaryset x y n))"
     using ca
     by (simp add: countably_additive_def)
   hence "{x,y,{}} \<subseteq> sets M \<longrightarrow> x \<union> y \<in> sets M \<longrightarrow>
-         f (x \<union> y) = (\<Sum>\<^isub>\<infinity> n. f (binaryset x y n))"
+         f (x \<union> y) = (\<Sum>n. f (binaryset x y n))"
     by (simp add: range_binaryset_eq UN_binaryset_eq)
   thus "f (x \<union> y) = f x + f y" using posf x y
-    by (auto simp add: Un binaryset_psuminf positive_def)
+    by (auto simp add: Un suminf_binaryset_eq positive_def)
 qed
 
 lemma inf_measure_nonempty:
   assumes f: "positive M f" and b: "b \<in> sets M" and a: "a \<subseteq> b" "{} \<in> sets M"
   shows "f b \<in> measure_set M f a"
 proof -
-  have "psuminf (f \<circ> (\<lambda>i. {})(0 := b)) = setsum (f \<circ> (\<lambda>i. {})(0 := b)) {..<1::nat}"
-    by (rule psuminf_finite) (simp add: f[unfolded positive_def])
+  let ?A = "\<lambda>i::nat. (if i = 0 then b else {})"
+  have "(\<Sum>i. f (?A i)) = (\<Sum>i<1::nat. f (?A i))"
+    by (rule suminf_finite) (simp add: f[unfolded positive_def])
   also have "... = f b"
     by simp
-  finally have "psuminf (f \<circ> (\<lambda>i. {})(0 := b)) = f b" .
-  thus ?thesis using assms
-    by (auto intro!: exI [of _ "(\<lambda>i. {})(0 := b)"]
+  finally show ?thesis using assms
+    by (auto intro!: exI [of _ ?A]
              simp: measure_set_def disjoint_family_on_def split_if_mem2 comp_def)
 qed
 
@@ -489,19 +510,19 @@
   assumes posf: "positive M f" and ca: "countably_additive M f"
       and s: "s \<in> sets M"
   shows "Inf (measure_set M f s) = f s"
-  unfolding Inf_pextreal_def
+  unfolding Inf_extreal_def
 proof (safe intro!: Greatest_equality)
   fix z
   assume z: "z \<in> measure_set M f s"
   from this obtain A where
     A: "range A \<subseteq> sets M" and disj: "disjoint_family A"
-    and "s \<subseteq> (\<Union>x. A x)" and si: "psuminf (f \<circ> A) = z"
+    and "s \<subseteq> (\<Union>x. A x)" and si: "(\<Sum>i. f (A i)) = z"
     by (auto simp add: measure_set_def comp_def)
   hence seq: "s = (\<Union>i. A i \<inter> s)" by blast
   have inc: "increasing M f"
     by (metis additive_increasing ca countably_additive_additive posf)
-  have sums: "psuminf (\<lambda>i. f (A i \<inter> s)) = f (\<Union>i. A i \<inter> s)"
-    proof (rule countably_additiveD [OF ca])
+  have sums: "(\<Sum>i. f (A i \<inter> s)) = f (\<Union>i. A i \<inter> s)"
+    proof (rule ca[unfolded countably_additive_def, rule_format])
       show "range (\<lambda>n. A n \<inter> s) \<subseteq> sets M" using A s
         by blast
       show "disjoint_family (\<lambda>n. A n \<inter> s)" using disj
@@ -509,12 +530,14 @@
       show "(\<Union>i. A i \<inter> s) \<in> sets M" using A s
         by (metis UN_extend_simps(4) s seq)
     qed
-  hence "f s = psuminf (\<lambda>i. f (A i \<inter> s))"
+  hence "f s = (\<Sum>i. f (A i \<inter> s))"
     using seq [symmetric] by (simp add: sums_iff)
-  also have "... \<le> psuminf (f \<circ> A)"
-    proof (rule psuminf_le)
-      fix n show "f (A n \<inter> s) \<le> (f \<circ> A) n" using A s
+  also have "... \<le> (\<Sum>i. f (A i))"
+    proof (rule suminf_le_pos)
+      fix n show "f (A n \<inter> s) \<le> f (A n)" using A s
         by (force intro: increasingD [OF inc])
+      fix N have "A N \<inter> s \<in> sets M"  using A s by auto
+      then show "0 \<le> f (A N \<inter> s)" using posf unfolding positive_def by auto
     qed
   also have "... = z" by (rule si)
   finally show "f s \<le> z" .
@@ -525,18 +548,40 @@
     by (blast intro: inf_measure_nonempty [of _ f, OF posf s subset_refl])
 qed
 
+lemma measure_set_pos:
+  assumes posf: "positive M f" "r \<in> measure_set M f X"
+  shows "0 \<le> r"
+proof -
+  obtain A where "range A \<subseteq> sets M" and r: "r = (\<Sum>i. f (A i))"
+    using `r \<in> measure_set M f X` unfolding measure_set_def by auto
+  then show "0 \<le> r" using posf unfolding r positive_def
+    by (intro suminf_0_le) auto
+qed
+
+lemma inf_measure_pos:
+  assumes posf: "positive M f"
+  shows "0 \<le> Inf (measure_set M f X)"
+proof (rule complete_lattice_class.Inf_greatest)
+  fix r assume "r \<in> measure_set M f X" with posf show "0 \<le> r"
+    by (rule measure_set_pos)
+qed
+
 lemma inf_measure_empty:
-  assumes posf: "positive M f" "{} \<in> sets M"
+  assumes posf: "positive M f" and "{} \<in> sets M"
   shows "Inf (measure_set M f {}) = 0"
 proof (rule antisym)
   show "Inf (measure_set M f {}) \<le> 0"
     by (metis complete_lattice_class.Inf_lower `{} \<in> sets M`
               inf_measure_nonempty[OF posf] subset_refl posf[unfolded positive_def])
-qed simp
+qed (rule inf_measure_pos[OF posf])
 
 lemma (in algebra) inf_measure_positive:
-  "positive M f \<Longrightarrow> positive M (\<lambda>x. Inf (measure_set M f x))"
-  by (simp add: positive_def inf_measure_empty)
+  assumes p: "positive M f" and "{} \<in> sets M"
+  shows "positive M (\<lambda>x. Inf (measure_set M f x))"
+proof (unfold positive_def, intro conjI ballI)
+  show "Inf (measure_set M f {}) = 0" using inf_measure_empty[OF assms] by auto
+  fix A assume "A \<in> sets M"
+qed (rule inf_measure_pos[OF p])
 
 lemma (in algebra) inf_measure_increasing:
   assumes posf: "positive M f"
@@ -548,25 +593,25 @@
 apply (clarsimp simp add: measure_set_def, rule_tac x=A in exI, blast)
 done
 
-
 lemma (in algebra) inf_measure_le:
   assumes posf: "positive M f" and inc: "increasing M f"
-      and x: "x \<in> {r . \<exists>A. range A \<subseteq> sets M \<and> s \<subseteq> (\<Union>i. A i) \<and> psuminf (f \<circ> A) = r}"
+      and x: "x \<in> {r . \<exists>A. range A \<subseteq> sets M \<and> s \<subseteq> (\<Union>i. A i) \<and> (\<Sum>i. f (A i)) = r}"
   shows "Inf (measure_set M f s) \<le> x"
 proof -
-  from x
   obtain A where A: "range A \<subseteq> sets M" and ss: "s \<subseteq> (\<Union>i. A i)"
-             and xeq: "psuminf (f \<circ> A) = x"
-    by auto
+             and xeq: "(\<Sum>i. f (A i)) = x"
+    using x by auto
   have dA: "range (disjointed A) \<subseteq> sets M"
     by (metis A range_disjointed_sets)
-  have "\<forall>n.(f o disjointed A) n \<le> (f \<circ> A) n" unfolding comp_def
+  have "\<forall>n. f (disjointed A n) \<le> f (A n)"
     by (metis increasingD [OF inc] UNIV_I dA image_subset_iff disjointed_subset A comp_def)
-  hence sda: "psuminf (f o disjointed A) \<le> psuminf (f \<circ> A)"
-    by (blast intro: psuminf_le)
-  hence ley: "psuminf (f o disjointed A) \<le> x"
+  moreover have "\<forall>i. 0 \<le> f (disjointed A i)"
+    using posf dA unfolding positive_def by auto
+  ultimately have sda: "(\<Sum>i. f (disjointed A i)) \<le> (\<Sum>i. f (A i))"
+    by (blast intro!: suminf_le_pos)
+  hence ley: "(\<Sum>i. f (disjointed A i)) \<le> x"
     by (metis xeq)
-  hence y: "psuminf (f o disjointed A) \<in> measure_set M f s"
+  hence y: "(\<Sum>i. f (disjointed A i)) \<in> measure_set M f s"
     apply (auto simp add: measure_set_def)
     apply (rule_tac x="disjointed A" in exI)
     apply (simp add: disjoint_family_disjointed UN_disjointed_eq ss dA comp_def)
@@ -576,13 +621,16 @@
 qed
 
 lemma (in algebra) inf_measure_close:
+  fixes e :: extreal
   assumes posf: "positive M f" and e: "0 < e" and ss: "s \<subseteq> (space M)"
   shows "\<exists>A. range A \<subseteq> sets M \<and> disjoint_family A \<and> s \<subseteq> (\<Union>i. A i) \<and>
-               psuminf (f \<circ> A) \<le> Inf (measure_set M f s) + e"
-proof (cases "Inf (measure_set M f s) = \<omega>")
+               (\<Sum>i. f (A i)) \<le> Inf (measure_set M f s) + e"
+proof (cases "Inf (measure_set M f s) = \<infinity>")
   case False
+  then have fin: "\<bar>Inf (measure_set M f s)\<bar> \<noteq> \<infinity>"
+    using inf_measure_pos[OF posf, of s] by auto
   obtain l where "l \<in> measure_set M f s" "l \<le> Inf (measure_set M f s) + e"
-    using Inf_close[OF False e] by auto
+    using Inf_extreal_close[OF fin e] by auto
   thus ?thesis
     by (auto intro!: exI[of _ l] simp: measure_set_def comp_def)
 next
@@ -600,9 +648,8 @@
   shows "countably_subadditive (| space = space M, sets = Pow (space M) |)
                   (\<lambda>x. Inf (measure_set M f x))"
   unfolding countably_subadditive_def o_def
-proof (safe, simp, rule pextreal_le_epsilon)
-  fix A :: "nat \<Rightarrow> 'a set" and e :: pextreal
-
+proof (safe, simp, rule extreal_le_epsilon, safe)
+  fix A :: "nat \<Rightarrow> 'a set" and e :: extreal
   let "?outer n" = "Inf (measure_set M f (A n))"
   assume A: "range A \<subseteq> Pow (space M)"
      and disj: "disjoint_family A"
@@ -610,21 +657,27 @@
      and e: "0 < e"
   hence "\<exists>BB. \<forall>n. range (BB n) \<subseteq> sets M \<and> disjoint_family (BB n) \<and>
                    A n \<subseteq> (\<Union>i. BB n i) \<and>
-                   psuminf (f o BB n) \<le> ?outer n + e * (1/2)^(Suc n)"
-    apply (safe intro!: choice inf_measure_close [of f, OF posf _])
-    using e sb by (cases e, auto simp add: not_le mult_pos_pos)
+                   (\<Sum>i. f (BB n i)) \<le> ?outer n + e * (1/2)^(Suc n)"
+    apply (safe intro!: choice inf_measure_close [of f, OF posf])
+    using e sb by (cases e) (auto simp add: not_le mult_pos_pos one_extreal_def)
   then obtain BB
     where BB: "\<And>n. (range (BB n) \<subseteq> sets M)"
       and disjBB: "\<And>n. disjoint_family (BB n)"
       and sbBB: "\<And>n. A n \<subseteq> (\<Union>i. BB n i)"
-      and BBle: "\<And>n. psuminf (f o BB n) \<le> ?outer n + e * (1/2)^(Suc n)"
+      and BBle: "\<And>n. (\<Sum>i. f (BB n i)) \<le> ?outer n + e * (1/2)^(Suc n)"
     by auto blast
-  have sll: "(\<Sum>\<^isub>\<infinity> n. psuminf (f o BB n)) \<le> psuminf ?outer + e"
+  have sll: "(\<Sum>n. \<Sum>i. (f (BB n i))) \<le> suminf ?outer + e"
     proof -
-      have "(\<Sum>\<^isub>\<infinity> n. psuminf (f o BB n)) \<le> (\<Sum>\<^isub>\<infinity> n. ?outer n + e*(1/2) ^ Suc n)"
-        by (rule psuminf_le[OF BBle])
-      also have "... = psuminf ?outer + e"
-        using psuminf_half_series by simp
+      have sum_eq_1: "(\<Sum>n. e*(1/2) ^ Suc n) = e"
+        using suminf_half_series_extreal e
+        by (simp add: extreal_zero_le_0_iff zero_le_divide_extreal suminf_cmult_extreal)
+      have "\<And>n i. 0 \<le> f (BB n i)" using posf[unfolded positive_def] BB by auto
+      then have "\<And>n. 0 \<le> (\<Sum>i. f (BB n i))" by (rule suminf_0_le)
+      then have "(\<Sum>n. \<Sum>i. (f (BB n i))) \<le> (\<Sum>n. ?outer n + e*(1/2) ^ Suc n)"
+        by (rule suminf_le_pos[OF BBle])
+      also have "... = suminf ?outer + e"
+        using sum_eq_1 inf_measure_pos[OF posf] e
+        by (subst suminf_add_extreal) (auto simp add: extreal_zero_le_0_iff)
       finally show ?thesis .
     qed
   def C \<equiv> "(split BB) o prod_decode"
@@ -644,23 +697,25 @@
     qed
   have "(f \<circ> C) = (f \<circ> (\<lambda>(x, y). BB x y)) \<circ> prod_decode"
     by (rule ext)  (auto simp add: C_def)
-  moreover have "psuminf ... = (\<Sum>\<^isub>\<infinity> n. psuminf (f o BB n))" using BBle
-    by (force intro!: psuminf_2dimen simp: o_def)
-  ultimately have Csums: "psuminf (f \<circ> C) = (\<Sum>\<^isub>\<infinity> n. psuminf (f o BB n))" by simp
-  have "Inf (measure_set M f (\<Union>i. A i)) \<le> (\<Sum>\<^isub>\<infinity> n. psuminf (f o BB n))"
+  moreover have "suminf ... = (\<Sum>n. \<Sum>i. f (BB n i))" using BBle
+    using BB posf[unfolded positive_def]
+    by (force intro!: suminf_extreal_2dimen simp: o_def)
+  ultimately have Csums: "(\<Sum>i. f (C i)) = (\<Sum>n. \<Sum>i. f (BB n i))" by (simp add: o_def)
+  have "Inf (measure_set M f (\<Union>i. A i)) \<le> (\<Sum>n. \<Sum>i. f (BB n i))"
     apply (rule inf_measure_le [OF posf(1) inc], auto)
     apply (rule_tac x="C" in exI)
     apply (auto simp add: C sbC Csums)
     done
-  also have "... \<le> (\<Sum>\<^isub>\<infinity>n. Inf (measure_set M f (A n))) + e" using sll
+  also have "... \<le> (\<Sum>n. Inf (measure_set M f (A n))) + e" using sll
     by blast
-  finally show "Inf (measure_set M f (\<Union>i. A i)) \<le> psuminf ?outer + e" .
+  finally show "Inf (measure_set M f (\<Union>i. A i)) \<le> suminf ?outer + e" .
 qed
 
 lemma (in algebra) inf_measure_outer:
   "\<lbrakk> positive M f ; increasing M f \<rbrakk>
    \<Longrightarrow> outer_measure_space \<lparr> space = space M, sets = Pow (space M) \<rparr>
                           (\<lambda>x. Inf (measure_set M f x))"
+  using inf_measure_pos[of M f]
   by (simp add: outer_measure_space_def inf_measure_empty
                 inf_measure_increasing inf_measure_countably_subadditive positive_def)
 
@@ -680,13 +735,13 @@
     by blast
   have "Inf (measure_set M f (s\<inter>x)) + Inf (measure_set M f (s-x))
         \<le> Inf (measure_set M f s)"
-    proof (rule pextreal_le_epsilon)
-      fix e :: pextreal
+    proof (rule extreal_le_epsilon, intro allI impI)
+      fix e :: extreal
       assume e: "0 < e"
       from inf_measure_close [of f, OF posf e s]
       obtain A where A: "range A \<subseteq> sets M" and disj: "disjoint_family A"
                  and sUN: "s \<subseteq> (\<Union>i. A i)"
-                 and l: "psuminf (f \<circ> A) \<le> Inf (measure_set M f s) + e"
+                 and l: "(\<Sum>i. f (A i)) \<le> Inf (measure_set M f s) + e"
         by auto
       have [simp]: "!!x. x \<in> sets M \<Longrightarrow>
                       (f o (\<lambda>z. z \<inter> (space M - x)) o A) = (f o (\<lambda>z. z - x) o A)"
@@ -698,9 +753,9 @@
         assume u: "u \<in> sets M"
         have [simp]: "\<And>n. f (A n \<inter> u) \<le> f (A n)"
           by (simp add: increasingD [OF inc] u Int range_subsetD [OF A])
-        have 2: "Inf (measure_set M f (s \<inter> u)) \<le> psuminf (f \<circ> (\<lambda>z. z \<inter> u) \<circ> A)"
+        have 2: "Inf (measure_set M f (s \<inter> u)) \<le> (\<Sum>i. f (A i \<inter> u))"
           proof (rule complete_lattice_class.Inf_lower)
-            show "psuminf (f \<circ> (\<lambda>z. z \<inter> u) \<circ> A) \<in> measure_set M f (s \<inter> u)"
+            show "(\<Sum>i. f (A i \<inter> u)) \<in> measure_set M f (s \<inter> u)"
               apply (simp add: measure_set_def)
               apply (rule_tac x="(\<lambda>z. z \<inter> u) o A" in exI)
               apply (auto simp add: disjoint_family_subset [OF disj] o_def)
@@ -709,15 +764,16 @@
               done
           qed
       } note lesum = this
-      have inf1: "Inf (measure_set M f (s\<inter>x)) \<le> psuminf (f o (\<lambda>z. z\<inter>x) o A)"
+      have [simp]: "\<And>i. A i \<inter> (space M - x) = A i - x" using A sets_into_space by auto
+      have inf1: "Inf (measure_set M f (s\<inter>x)) \<le> (\<Sum>i. f (A i \<inter> x))"
         and inf2: "Inf (measure_set M f (s \<inter> (space M - x)))
-                   \<le> psuminf (f o (\<lambda>z. z \<inter> (space M - x)) o A)"
+                   \<le> (\<Sum>i. f (A i \<inter> (space M - x)))"
         by (metis Diff lesum top x)+
       hence "Inf (measure_set M f (s\<inter>x)) + Inf (measure_set M f (s-x))
-           \<le> psuminf (f o (\<lambda>s. s\<inter>x) o A) + psuminf (f o (\<lambda>s. s-x) o A)"
-        by (simp add: x add_mono)
-      also have "... \<le> psuminf (f o A)"
-        by (simp add: x psuminf_add[symmetric] o_def)
+           \<le> (\<Sum>i. f (A i \<inter> x)) + (\<Sum>i. f (A i - x))"
+        by (simp add: add_mono x)
+      also have "... \<le> (\<Sum>i. f (A i))" using posf[unfolded positive_def] A x
+        by (subst suminf_add_extreal[symmetric]) auto
       also have "... \<le> Inf (measure_set M f s) + e"
         by (rule l)
       finally show "Inf (measure_set M f (s\<inter>x)) + Inf (measure_set M f (s-x))
@@ -732,7 +788,7 @@
     also have "... \<le> Inf (measure_set M f (s\<inter>x)) + Inf (measure_set M f (s-x))"
       apply (rule subadditiveD)
       apply (rule algebra.countably_subadditive_subadditive[OF algebra_Pow])
-      apply (simp add: positive_def inf_measure_empty[OF posf])
+      apply (simp add: positive_def inf_measure_empty[OF posf] inf_measure_pos[OF posf])
       apply (rule inf_measure_countably_subadditive)
       using s by (auto intro!: posf inc)
     finally show ?thesis .
@@ -751,7 +807,7 @@
 
 theorem (in algebra) caratheodory:
   assumes posf: "positive M f" and ca: "countably_additive M f"
-  shows "\<exists>\<mu> :: 'a set \<Rightarrow> pextreal. (\<forall>s \<in> sets M. \<mu> s = f s) \<and>
+  shows "\<exists>\<mu> :: 'a set \<Rightarrow> extreal. (\<forall>s \<in> sets M. \<mu> s = f s) \<and>
             measure_space \<lparr> space = space M, sets = sets (sigma M), measure = \<mu> \<rparr>"
 proof -
   have inc: "increasing M f"
--- a/src/HOL/Probability/Complete_Measure.thy	Mon Mar 14 15:17:10 2011 +0100
+++ b/src/HOL/Probability/Complete_Measure.thy	Mon Mar 14 15:29:10 2011 +0100
@@ -1,7 +1,6 @@
-(*  Title:      HOL/Probability/Complete_Measure.thy
+(*  Title:      Complete_Measure.thy
     Author:     Robert Himmelmann, Johannes Hoelzl, TU Muenchen
 *)
-
 theory Complete_Measure
 imports Product_Measure
 begin
@@ -177,7 +176,8 @@
 proof -
   show "measure_space completion"
   proof
-    show "measure completion {} = 0" by (auto simp: completion_def)
+    show "positive completion (measure completion)"
+      by (auto simp: completion_def positive_def)
   next
     show "countably_additive completion (measure completion)"
     proof (intro countably_additiveI)
@@ -189,9 +189,9 @@
           using A by (subst (1 2) main_part_null_part_Un) auto
         then show "main_part (A n) \<inter> main_part (A m) = {}" by auto
       qed
-      then have "(\<Sum>\<^isub>\<infinity>n. measure completion (A n)) = \<mu> (\<Union>i. main_part (A i))"
+      then have "(\<Sum>n. measure completion (A n)) = \<mu> (\<Union>i. main_part (A i))"
         unfolding completion_def using A by (auto intro!: measure_countably_additive)
-      then show "(\<Sum>\<^isub>\<infinity>n. measure completion (A n)) = measure completion (UNION UNIV A)"
+      then show "(\<Sum>n. measure completion (A n)) = measure completion (UNION UNIV A)"
         by (simp add: completion_def \<mu>_main_part_UN[OF A(1)])
     qed
   qed
@@ -251,30 +251,52 @@
   qed
 qed
 
-lemma (in completeable_measure_space) completion_ex_borel_measurable:
-  fixes g :: "'a \<Rightarrow> pextreal"
-  assumes g: "g \<in> borel_measurable completion"
+lemma (in completeable_measure_space) completion_ex_borel_measurable_pos:
+  fixes g :: "'a \<Rightarrow> extreal"
+  assumes g: "g \<in> borel_measurable completion" and "\<And>x. 0 \<le> g x"
   shows "\<exists>g'\<in>borel_measurable M. (AE x. g x = g' x)"
 proof -
-  from g[THEN completion.borel_measurable_implies_simple_function_sequence]
-  obtain f where "\<And>i. simple_function completion (f i)" "f \<up> g" by auto
-  then have "\<forall>i. \<exists>f'. simple_function M f' \<and> (AE x. f i x = f' x)"
-    using completion_ex_simple_function by auto
+  from g[THEN completion.borel_measurable_implies_simple_function_sequence'] guess f . note f = this
+  from this(1)[THEN completion_ex_simple_function]
+  have "\<forall>i. \<exists>f'. simple_function M f' \<and> (AE x. f i x = f' x)" ..
   from this[THEN choice] obtain f' where
     sf: "\<And>i. simple_function M (f' i)" and
     AE: "\<forall>i. AE x. f i x = f' i x" by auto
   show ?thesis
   proof (intro bexI)
-    from AE[unfolded all_AE_countable]
+    from AE[unfolded AE_all_countable[symmetric]]
     show "AE x. g x = (SUP i. f' i x)" (is "AE x. g x = ?f x")
     proof (elim AE_mp, safe intro!: AE_I2)
       fix x assume eq: "\<forall>i. f i x = f' i x"
-      moreover have "g = SUPR UNIV f" using `f \<up> g` unfolding isoton_def by simp
-      ultimately show "g x = ?f x" by (simp add: SUPR_apply)
+      moreover have "g x = (SUP i. f i x)"
+        unfolding f using `0 \<le> g x` by (auto split: split_max)
+      ultimately show "g x = ?f x" by auto
     qed
     show "?f \<in> borel_measurable M"
       using sf by (auto intro: borel_measurable_simple_function)
   qed
 qed
 
+lemma (in completeable_measure_space) completion_ex_borel_measurable:
+  fixes g :: "'a \<Rightarrow> extreal"
+  assumes g: "g \<in> borel_measurable completion"
+  shows "\<exists>g'\<in>borel_measurable M. (AE x. g x = g' x)"
+proof -
+  have "(\<lambda>x. max 0 (g x)) \<in> borel_measurable completion" "\<And>x. 0 \<le> max 0 (g x)" using g by auto
+  from completion_ex_borel_measurable_pos[OF this] guess g_pos ..
+  moreover
+  have "(\<lambda>x. max 0 (- g x)) \<in> borel_measurable completion" "\<And>x. 0 \<le> max 0 (- g x)" using g by auto
+  from completion_ex_borel_measurable_pos[OF this] guess g_neg ..
+  ultimately
+  show ?thesis
+  proof (safe intro!: bexI[of _ "\<lambda>x. g_pos x - g_neg x"])
+    show "AE x. max 0 (- g x) = g_neg x \<longrightarrow> max 0 (g x) = g_pos x \<longrightarrow> g x = g_pos x - g_neg x"
+    proof (intro AE_I2 impI)
+      fix x assume g: "max 0 (- g x) = g_neg x" "max 0 (g x) = g_pos x"
+      show "g x = g_pos x - g_neg x" unfolding g[symmetric]
+        by (cases "g x") (auto split: split_max)
+    qed
+  qed auto
+qed
+
 end
--- a/src/HOL/Probability/Information.thy	Mon Mar 14 15:17:10 2011 +0100
+++ b/src/HOL/Probability/Information.thy	Mon Mar 14 15:29:10 2011 +0100
@@ -2,9 +2,12 @@
 imports
   Probability_Space
   "~~/src/HOL/Library/Convex"
-  Lebesgue_Measure
 begin
 
+lemma (in prob_space) not_zero_less_distribution[simp]:
+  "(\<not> 0 < distribution X A) \<longleftrightarrow> distribution X A = 0"
+  using distribution_positive[of X A] by arith
+
 lemma log_le: "1 < a \<Longrightarrow> 0 < x \<Longrightarrow> x \<le> y \<Longrightarrow> log a x \<le> log a y"
   by (subst log_le_cancel_iff) auto
 
@@ -238,7 +241,7 @@
   have ms: "measure_space (M\<lparr>measure := \<nu>\<rparr>)" by default
   show "(\<Sum>x \<in> space M. log b (real (RN_deriv M \<nu> x)) * real (\<nu> {x})) = ?sum"
     using RN_deriv_finite_measure[OF ms ac]
-    by (auto intro!: setsum_cong simp: field_simps real_of_pextreal_mult[symmetric])
+    by (auto intro!: setsum_cong simp: field_simps)
 qed
 
 lemma (in finite_prob_space) KL_divergence_positive_finite:
@@ -254,7 +257,8 @@
   proof (subst KL_divergence_eq_finite[OF ms ac], safe intro!: log_setsum_divide not_empty)
     show "finite (space M)" using finite_space by simp
     show "1 < b" by fact
-    show "(\<Sum>x\<in>space M. real (\<nu> {x})) = 1" using v.finite_sum_over_space_eq_1 by simp
+    show "(\<Sum>x\<in>space M. real (\<nu> {x})) = 1"
+      using v.finite_sum_over_space_eq_1 by (simp add: v.\<mu>'_def)
 
     fix x assume "x \<in> space M"
     then have x: "{x} \<in> sets M" unfolding sets_eq_Pow by auto
@@ -262,17 +266,19 @@
       then have "\<nu> {x} \<noteq> 0" by auto
       then have "\<mu> {x} \<noteq> 0"
         using ac[unfolded absolutely_continuous_def, THEN bspec, of "{x}"] x by auto
-      thus "0 < prob {x}" using finite_measure[of "{x}"] x by auto }
-  qed auto
-  thus "0 \<le> KL_divergence b M \<nu>" using finite_sum_over_space_eq_1 by simp
+      thus "0 < real (\<mu> {x})" using real_measure[OF x] by auto }
+    show "0 \<le> real (\<mu> {x})" "0 \<le> real (\<nu> {x})"
+      using real_measure[OF x] v.real_measure[of "{x}"] x by auto
+  qed
+  thus "0 \<le> KL_divergence b M \<nu>" using finite_sum_over_space_eq_1 by (simp add: \<mu>'_def)
 qed
 
 subsection {* Mutual Information *}
 
 definition (in prob_space)
   "mutual_information b S T X Y =
-    KL_divergence b (S\<lparr>measure := distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := distribution Y\<rparr>)
-      (joint_distribution X Y)"
+    KL_divergence b (S\<lparr>measure := extreal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := extreal\<circ>distribution Y\<rparr>)
+      (extreal\<circ>joint_distribution X Y)"
 
 definition (in prob_space)
   "entropy b s X = mutual_information b s s X X"
@@ -280,38 +286,33 @@
 abbreviation (in information_space)
   mutual_information_Pow ("\<I>'(_ ; _')") where
   "\<I>(X ; Y) \<equiv> mutual_information b
-    \<lparr> space = X`space M, sets = Pow (X`space M), measure = distribution X \<rparr>
-    \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = distribution Y \<rparr> X Y"
+    \<lparr> space = X`space M, sets = Pow (X`space M), measure = extreal\<circ>distribution X \<rparr>
+    \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = extreal\<circ>distribution Y \<rparr> X Y"
 
 lemma (in prob_space) finite_variables_absolutely_continuous:
   assumes X: "finite_random_variable S X" and Y: "finite_random_variable T Y"
   shows "measure_space.absolutely_continuous
-    (S\<lparr>measure := distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := distribution Y\<rparr>)
-    (joint_distribution X Y)"
+    (S\<lparr>measure := extreal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := extreal\<circ>distribution Y\<rparr>)
+    (extreal\<circ>joint_distribution X Y)"
 proof -
-  interpret X: finite_prob_space "S\<lparr>measure := distribution X\<rparr>"
+  interpret X: finite_prob_space "S\<lparr>measure := extreal\<circ>distribution X\<rparr>"
     using X by (rule distribution_finite_prob_space)
-  interpret Y: finite_prob_space "T\<lparr>measure := distribution Y\<rparr>"
+  interpret Y: finite_prob_space "T\<lparr>measure := extreal\<circ>distribution Y\<rparr>"
     using Y by (rule distribution_finite_prob_space)
   interpret XY: pair_finite_prob_space
-    "S\<lparr>measure := distribution X\<rparr>" "T\<lparr> measure := distribution Y\<rparr>" by default
-  interpret P: finite_prob_space "XY.P\<lparr> measure := joint_distribution X Y\<rparr>"
+    "S\<lparr>measure := extreal\<circ>distribution X\<rparr>" "T\<lparr> measure := extreal\<circ>distribution Y\<rparr>" by default
+  interpret P: finite_prob_space "XY.P\<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>"
     using assms by (auto intro!: joint_distribution_finite_prob_space)
   note rv = assms[THEN finite_random_variableD]
-  show "XY.absolutely_continuous (joint_distribution X Y)"
+  show "XY.absolutely_continuous (extreal\<circ>joint_distribution X Y)"
   proof (rule XY.absolutely_continuousI)
-    show "finite_measure_space (XY.P\<lparr> measure := joint_distribution X Y\<rparr>)" by default
+    show "finite_measure_space (XY.P\<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>)" by default
     fix x assume "x \<in> space XY.P" and "XY.\<mu> {x} = 0"
-    then obtain a b where "(a, b) = x" and "a \<in> space S" "b \<in> space T"
-      and distr: "distribution X {a} * distribution Y {b} = 0"
+    then obtain a b where "x = (a, b)"
+      and "distribution X {a} = 0 \<or> distribution Y {b} = 0"
       by (cases x) (auto simp: space_pair_measure)
-    with X.sets_eq_Pow Y.sets_eq_Pow
-      joint_distribution_Times_le_fst[OF rv, of "{a}" "{b}"]
-      joint_distribution_Times_le_snd[OF rv, of "{a}" "{b}"]
-    have "joint_distribution X Y {x} \<le> distribution Y {b}"
-         "joint_distribution X Y {x} \<le> distribution X {a}"
-      by (auto simp del: X.sets_eq_Pow Y.sets_eq_Pow)
-    with distr show "joint_distribution X Y {x} = 0" by auto
+    with finite_distribution_order(5,6)[OF X Y]
+    show "(extreal \<circ> joint_distribution X Y) {x} = 0" by auto
   qed
 qed
 
@@ -320,28 +321,28 @@
   assumes MY: "finite_random_variable MY Y"
   shows mutual_information_generic_eq:
     "mutual_information b MX MY X Y = (\<Sum> (x,y) \<in> space MX \<times> space MY.
-      real (joint_distribution X Y {(x,y)}) *
-      log b (real (joint_distribution X Y {(x,y)}) /
-      (real (distribution X {x}) * real (distribution Y {y}))))"
+      joint_distribution X Y {(x,y)} *
+      log b (joint_distribution X Y {(x,y)} /
+      (distribution X {x} * distribution Y {y})))"
     (is ?sum)
   and mutual_information_positive_generic:
      "0 \<le> mutual_information b MX MY X Y" (is ?positive)
 proof -
-  interpret X: finite_prob_space "MX\<lparr>measure := distribution X\<rparr>"
+  interpret X: finite_prob_space "MX\<lparr>measure := extreal\<circ>distribution X\<rparr>"
     using MX by (rule distribution_finite_prob_space)
-  interpret Y: finite_prob_space "MY\<lparr>measure := distribution Y\<rparr>"
+  interpret Y: finite_prob_space "MY\<lparr>measure := extreal\<circ>distribution Y\<rparr>"
     using MY by (rule distribution_finite_prob_space)
-  interpret XY: pair_finite_prob_space "MX\<lparr>measure := distribution X\<rparr>" "MY\<lparr>measure := distribution Y\<rparr>" by default
-  interpret P: finite_prob_space "XY.P\<lparr>measure := joint_distribution X Y\<rparr>"
+  interpret XY: pair_finite_prob_space "MX\<lparr>measure := extreal\<circ>distribution X\<rparr>" "MY\<lparr>measure := extreal\<circ>distribution Y\<rparr>" by default
+  interpret P: finite_prob_space "XY.P\<lparr>measure := extreal\<circ>joint_distribution X Y\<rparr>"
     using assms by (auto intro!: joint_distribution_finite_prob_space)
 
-  have P_ms: "finite_measure_space (XY.P\<lparr>measure :=joint_distribution X Y\<rparr>)" by default
-  have P_ps: "finite_prob_space (XY.P\<lparr>measure := joint_distribution X Y\<rparr>)" by default
+  have P_ms: "finite_measure_space (XY.P\<lparr>measure := extreal\<circ>joint_distribution X Y\<rparr>)" by default
+  have P_ps: "finite_prob_space (XY.P\<lparr>measure := extreal\<circ>joint_distribution X Y\<rparr>)" by default
 
   show ?sum
     unfolding Let_def mutual_information_def
     by (subst XY.KL_divergence_eq_finite[OF P_ms finite_variables_absolutely_continuous[OF MX MY]])
-       (auto simp add: space_pair_measure setsum_cartesian_product' real_of_pextreal_mult[symmetric])
+       (auto simp add: space_pair_measure setsum_cartesian_product')
 
   show ?positive
     using XY.KL_divergence_positive_finite[OF P_ps finite_variables_absolutely_continuous[OF MX MY] b_gt_1]
@@ -351,10 +352,10 @@
 lemma (in information_space) mutual_information_commute_generic:
   assumes X: "random_variable S X" and Y: "random_variable T Y"
   assumes ac: "measure_space.absolutely_continuous
-    (S\<lparr>measure := distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := distribution Y\<rparr>) (joint_distribution X Y)"
+    (S\<lparr>measure := extreal\<circ>distribution X\<rparr> \<Otimes>\<^isub>M T\<lparr>measure := extreal\<circ>distribution Y\<rparr>) (extreal\<circ>joint_distribution X Y)"
   shows "mutual_information b S T X Y = mutual_information b T S Y X"
 proof -
-  let ?S = "S\<lparr>measure := distribution X\<rparr>" and ?T = "T\<lparr>measure := distribution Y\<rparr>"
+  let ?S = "S\<lparr>measure := extreal\<circ>distribution X\<rparr>" and ?T = "T\<lparr>measure := extreal\<circ>distribution Y\<rparr>"
   interpret S: prob_space ?S using X by (rule distribution_prob_space)
   interpret T: prob_space ?T using Y by (rule distribution_prob_space)
   interpret P: pair_prob_space ?S ?T ..
@@ -363,13 +364,13 @@
     unfolding mutual_information_def
   proof (intro Q.KL_divergence_vimage[OF Q.measure_preserving_swap _ _ _ _ ac b_gt_1])
     show "(\<lambda>(x,y). (y,x)) \<in> measure_preserving
-      (P.P \<lparr> measure := joint_distribution X Y\<rparr>) (Q.P \<lparr> measure := joint_distribution Y X\<rparr>)"
+      (P.P \<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>) (Q.P \<lparr> measure := extreal\<circ>joint_distribution Y X\<rparr>)"
       using X Y unfolding measurable_def
       unfolding measure_preserving_def using P.pair_sigma_algebra_swap_measurable
-      by (auto simp add: space_pair_measure distribution_def intro!: arg_cong[where f=\<mu>])
-    have "prob_space (P.P\<lparr> measure := joint_distribution X Y\<rparr>)"
+      by (auto simp add: space_pair_measure distribution_def intro!: arg_cong[where f=\<mu>'])
+    have "prob_space (P.P\<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>)"
       using X Y by (auto intro!: distribution_prob_space random_variable_pairI)
-    then show "measure_space (P.P\<lparr> measure := joint_distribution X Y\<rparr>)"
+    then show "measure_space (P.P\<lparr> measure := extreal\<circ>joint_distribution X Y\<rparr>)"
       unfolding prob_space_def by simp
   qed auto
 qed
@@ -389,8 +390,8 @@
 lemma (in information_space) mutual_information_eq:
   assumes "simple_function M X" "simple_function M Y"
   shows "\<I>(X;Y) = (\<Sum> (x,y) \<in> X ` space M \<times> Y ` space M.
-    real (distribution (\<lambda>x. (X x, Y x)) {(x,y)}) * log b (real (distribution (\<lambda>x. (X x, Y x)) {(x,y)}) /
-                                                   (real (distribution X {x}) * real (distribution Y {y}))))"
+    distribution (\<lambda>x. (X x, Y x)) {(x,y)} * log b (distribution (\<lambda>x. (X x, Y x)) {(x,y)} /
+                                                   (distribution X {x} * distribution Y {y})))"
   using assms by (simp add: mutual_information_generic_eq)
 
 lemma (in information_space) mutual_information_generic_cong:
@@ -416,22 +417,27 @@
 
 abbreviation (in information_space)
   entropy_Pow ("\<H>'(_')") where
-  "\<H>(X) \<equiv> entropy b \<lparr> space = X`space M, sets = Pow (X`space M), measure = distribution X \<rparr> X"
+  "\<H>(X) \<equiv> entropy b \<lparr> space = X`space M, sets = Pow (X`space M), measure = extreal\<circ>distribution X \<rparr> X"
 
 lemma (in information_space) entropy_generic_eq:
+  fixes X :: "'a \<Rightarrow> 'c"
   assumes MX: "finite_random_variable MX X"
-  shows "entropy b MX X = -(\<Sum> x \<in> space MX. real (distribution X {x}) * log b (real (distribution X {x})))"
+  shows "entropy b MX X = -(\<Sum> x \<in> space MX. distribution X {x} * log b (distribution X {x}))"
 proof -
-  interpret MX: finite_prob_space "MX\<lparr>measure := distribution X\<rparr>"
+  interpret MX: finite_prob_space "MX\<lparr>measure := extreal\<circ>distribution X\<rparr>"
     using MX by (rule distribution_finite_prob_space)
-  let "?X x" = "real (distribution X {x})"
-  let "?XX x y" = "real (joint_distribution X X {(x, y)})"
-  { fix x y
-    have "(\<lambda>x. (X x, X x)) -` {(x, y)} = (if x = y then X -` {x} else {})" by auto
+  let "?X x" = "distribution X {x}"
+  let "?XX x y" = "joint_distribution X X {(x, y)}"
+
+  { fix x y :: 'c
+    { assume "x \<noteq> y"
+      then have "(\<lambda>x. (X x, X x)) -` {(x,y)} \<inter> space M = {}" by auto
+      then have "joint_distribution X X {(x, y)} = 0" by (simp add: distribution_def) }
     then have "?XX x y * log b (?XX x y / (?X x * ?X y)) =
         (if x = y then - ?X y * log b (?X y) else 0)"
-      unfolding distribution_def by (auto simp: log_simps zero_less_mult_iff) }
+      by (auto simp: log_simps zero_less_mult_iff) }
   note remove_XX = this
+
   show ?thesis
     unfolding entropy_def mutual_information_generic_eq[OF MX MX]
     unfolding setsum_cartesian_product[symmetric] setsum_negf[symmetric] remove_XX
@@ -440,7 +446,7 @@
 
 lemma (in information_space) entropy_eq:
   assumes "simple_function M X"
-  shows "\<H>(X) = -(\<Sum> x \<in> X ` space M. real (distribution X {x}) * log b (real (distribution X {x})))"
+  shows "\<H>(X) = -(\<Sum> x \<in> X ` space M. distribution X {x} * log b (distribution X {x}))"
   using assms by (simp add: entropy_generic_eq)
 
 lemma (in information_space) entropy_positive:
@@ -448,63 +454,77 @@
   unfolding entropy_def by (simp add: mutual_information_positive)
 
 lemma (in information_space) entropy_certainty_eq_0:
-  assumes "simple_function M X" and "x \<in> X ` space M" and "distribution X {x} = 1"
+  assumes X: "simple_function M X" and "x \<in> X ` space M" and "distribution X {x} = 1"
   shows "\<H>(X) = 0"
 proof -
-  let ?X = "\<lparr> space = X ` space M, sets = Pow (X ` space M), measure = distribution X\<rparr>"
+  let ?X = "\<lparr> space = X ` space M, sets = Pow (X ` space M), measure = extreal\<circ>distribution X\<rparr>"
   note simple_function_imp_finite_random_variable[OF `simple_function M X`]
-  from distribution_finite_prob_space[OF this, of "\<lparr> measure = distribution X \<rparr>"]
+  from distribution_finite_prob_space[OF this, of "\<lparr> measure = extreal\<circ>distribution X \<rparr>"]
   interpret X: finite_prob_space ?X by simp
   have "distribution X (X ` space M - {x}) = distribution X (X ` space M) - distribution X {x}"
     using X.measure_compl[of "{x}"] assms by auto
   also have "\<dots> = 0" using X.prob_space assms by auto
   finally have X0: "distribution X (X ` space M - {x}) = 0" by auto
-  { fix y assume asm: "y \<noteq> x" "y \<in> X ` space M"
-    hence "{y} \<subseteq> X ` space M - {x}" by auto
-    from X.measure_mono[OF this] X0 asm
-    have "distribution X {y} = 0" by auto }
-  hence fi: "\<And> y. y \<in> X ` space M \<Longrightarrow> real (distribution X {y}) = (if x = y then 1 else 0)"
-    using assms by auto
+  { fix y assume *: "y \<in> X ` space M"
+    { assume asm: "y \<noteq> x"
+      with * have "{y} \<subseteq> X ` space M - {x}" by auto
+      from X.measure_mono[OF this] X0 asm *
+      have "distribution X {y} = 0"  by (auto intro: antisym) }
+    then have "distribution X {y} = (if x = y then 1 else 0)"
+      using assms by auto }
+  note fi = this
   have y: "\<And>y. (if x = y then 1 else 0) * log b (if x = y then 1 else 0) = 0" by simp
   show ?thesis unfolding entropy_eq[OF `simple_function M X`] by (auto simp: y fi)
 qed
 
 lemma (in information_space) entropy_le_card_not_0:
-  assumes "simple_function M X"
-  shows "\<H>(X) \<le> log b (real (card (X ` space M \<inter> {x . distribution X {x} \<noteq> 0})))"
+  assumes X: "simple_function M X"
+  shows "\<H>(X) \<le> log b (card (X ` space M \<inter> {x. distribution X {x} \<noteq> 0}))"
 proof -
-  let "?d x" = "distribution X {x}"
-  let "?p x" = "real (?d x)"
+  let "?p x" = "distribution X {x}"
   have "\<H>(X) = (\<Sum>x\<in>X`space M. ?p x * log b (1 / ?p x))"
-    by (auto intro!: setsum_cong simp: entropy_eq[OF `simple_function M X`] setsum_negf[symmetric] log_simps not_less)
+    unfolding entropy_eq[OF X] setsum_negf[symmetric]
+    by (auto intro!: setsum_cong simp: log_simps)
   also have "\<dots> \<le> log b (\<Sum>x\<in>X`space M. ?p x * (1 / ?p x))"
-    apply (rule log_setsum')
-    using not_empty b_gt_1 `simple_function M X` sum_over_space_real_distribution
-    by (auto simp: simple_function_def)
-  also have "\<dots> = log b (\<Sum>x\<in>X`space M. if ?d x \<noteq> 0 then 1 else 0)"
-    using distribution_finite[OF `simple_function M X`[THEN simple_function_imp_random_variable], simplified]
-    by (intro arg_cong[where f="\<lambda>X. log b X"] setsum_cong) (auto simp: real_of_pextreal_eq_0)
+    using not_empty b_gt_1 `simple_function M X` sum_over_space_real_distribution[OF X]
+    by (intro log_setsum') (auto simp: simple_function_def)
+  also have "\<dots> = log b (\<Sum>x\<in>X`space M. if ?p x \<noteq> 0 then 1 else 0)"
+    by (intro arg_cong[where f="\<lambda>X. log b X"] setsum_cong) auto
   finally show ?thesis
     using `simple_function M X` by (auto simp: setsum_cases real_eq_of_nat simple_function_def)
 qed
 
+lemma (in prob_space) measure'_translate:
+  assumes X: "random_variable S X" and A: "A \<in> sets S"
+  shows "finite_measure.\<mu>' (S\<lparr> measure := extreal\<circ>distribution X \<rparr>) A = distribution X A"
+proof -
+  interpret S: prob_space "S\<lparr> measure := extreal\<circ>distribution X \<rparr>"
+    using distribution_prob_space[OF X] .
+  from A show "S.\<mu>' A = distribution X A"
+    unfolding S.\<mu>'_def by (simp add: distribution_def_raw \<mu>'_def)
+qed
+
 lemma (in information_space) entropy_uniform_max:
-  assumes "simple_function M X"
+  assumes X: "simple_function M X"
   assumes "\<And>x y. \<lbrakk> x \<in> X ` space M ; y \<in> X ` space M \<rbrakk> \<Longrightarrow> distribution X {x} = distribution X {y}"
   shows "\<H>(X) = log b (real (card (X ` space M)))"
 proof -
-  let ?X = "\<lparr> space = X ` space M, sets = Pow (X ` space M), measure = distribution X\<rparr>"
-  note simple_function_imp_finite_random_variable[OF `simple_function M X`]
-  from distribution_finite_prob_space[OF this, of "\<lparr> measure = distribution X \<rparr>"]
+  let ?X = "\<lparr> space = X ` space M, sets = Pow (X ` space M), measure = undefined\<rparr>\<lparr> measure := extreal\<circ>distribution X\<rparr>"
+  note frv = simple_function_imp_finite_random_variable[OF X]
+  from distribution_finite_prob_space[OF this, of "\<lparr> measure = extreal\<circ>distribution X \<rparr>"]
   interpret X: finite_prob_space ?X by simp
+  note rv = finite_random_variableD[OF frv]
   have card_gt0: "0 < card (X ` space M)" unfolding card_gt_0_iff
     using `simple_function M X` not_empty by (auto simp: simple_function_def)
-  { fix x assume "x \<in> X ` space M"
-    hence "real (distribution X {x}) = 1 / real (card (X ` space M))"
-    proof (rule X.uniform_prob[simplified])
-      fix x y assume "x \<in> X`space M" "y \<in> X`space M"
-      from assms(2)[OF this] show "real (distribution X {x}) = real (distribution X {y})" by simp
-    qed }
+  { fix x assume "x \<in> space ?X"
+    moreover then have "X.\<mu>' {x} = 1 / card (space ?X)"
+    proof (rule X.uniform_prob)
+      fix x y assume "x \<in> space ?X" "y \<in> space ?X"
+      with assms(2)[of x y] show "X.\<mu>' {x} = X.\<mu>' {y}"
+        by (subst (1 2) measure'_translate[OF rv]) auto
+    qed
+    ultimately have "distribution X {x} = 1 / card (space ?X)"
+      by (subst (asm) measure'_translate[OF rv]) auto }
   thus ?thesis
     using not_empty X.finite_space b_gt_1 card_gt0
     by (simp add: entropy_eq[OF `simple_function M X`] real_eq_of_nat[symmetric] log_simps)
@@ -552,8 +572,7 @@
 lemma (in information_space) entropy_eq_cartesian_product:
   assumes "simple_function M X" "simple_function M Y"
   shows "\<H>(\<lambda>x. (X x, Y x)) = -(\<Sum>x\<in>X`space M. \<Sum>y\<in>Y`space M.
-    real (joint_distribution X Y {(x,y)}) *
-    log b (real (joint_distribution X Y {(x,y)})))"
+    joint_distribution X Y {(x,y)} * log b (joint_distribution X Y {(x,y)}))"
 proof -
   have sf: "simple_function M (\<lambda>x. (X x, Y x))"
     using assms by (auto intro: simple_function_Pair)
@@ -576,9 +595,9 @@
 abbreviation (in information_space)
   conditional_mutual_information_Pow ("\<I>'( _ ; _ | _ ')") where
   "\<I>(X ; Y | Z) \<equiv> conditional_mutual_information b
-    \<lparr> space = X`space M, sets = Pow (X`space M), measure = distribution X \<rparr>
-    \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = distribution Y \<rparr>
-    \<lparr> space = Z`space M, sets = Pow (Z`space M), measure = distribution Z \<rparr>
+    \<lparr> space = X`space M, sets = Pow (X`space M), measure = extreal\<circ>distribution X \<rparr>
+    \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = extreal\<circ>distribution Y \<rparr>
+    \<lparr> space = Z`space M, sets = Pow (Z`space M), measure = extreal\<circ>distribution Z \<rparr>
     X Y Z"
 
 lemma (in information_space) conditional_mutual_information_generic_eq:
@@ -586,58 +605,44 @@
     and MY: "finite_random_variable MY Y"
     and MZ: "finite_random_variable MZ Z"
   shows "conditional_mutual_information b MX MY MZ X Y Z = (\<Sum>(x, y, z) \<in> space MX \<times> space MY \<times> space MZ.
-             real (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)}) *
-             log b (real (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)}) /
-    (real (joint_distribution X Z {(x, z)}) * real (joint_distribution Y Z {(y,z)} / distribution Z {z}))))"
-  (is "_ = (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?XZ x z * ?YZdZ y z)))")
+             distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} *
+             log b (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} /
+    (joint_distribution X Z {(x, z)} * (joint_distribution Y Z {(y,z)} / distribution Z {z}))))"
+  (is "_ = (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?XZ x z * (?YZ y z / ?Z z))))")
 proof -
-  let ?YZ = "\<lambda>y z. real (joint_distribution Y Z {(y, z)})"
-  let ?X = "\<lambda>x. real (distribution X {x})"
-  let ?Z = "\<lambda>z. real (distribution Z {z})"
-
-  txt {* This proof is actually quiet easy, however we need to show that the
-    distributions are finite and the joint distributions are zero when one of
-    the variables distribution is also zero. *}
-
+  let ?X = "\<lambda>x. distribution X {x}"
   note finite_var = MX MY MZ
-  note random_var = finite_var[THEN finite_random_variableD]
-
-  note space_simps = space_pair_measure space_sigma algebra.simps
-
   note YZ = finite_random_variable_pairI[OF finite_var(2,3)]
+  note XYZ = finite_random_variable_pairI[OF MX YZ]
   note XZ = finite_random_variable_pairI[OF finite_var(1,3)]
   note ZX = finite_random_variable_pairI[OF finite_var(3,1)]
   note YZX = finite_random_variable_pairI[OF finite_var(2) ZX]
   note order1 =
-    finite_distribution_order(5,6)[OF finite_var(1) YZ, simplified space_simps]
-    finite_distribution_order(5,6)[OF finite_var(1,3), simplified space_simps]
+    finite_distribution_order(5,6)[OF finite_var(1) YZ]
+    finite_distribution_order(5,6)[OF finite_var(1,3)]
 
+  note random_var = finite_var[THEN finite_random_variableD]
   note finite = finite_var(1) YZ finite_var(3) XZ YZX
-  note finite[THEN finite_distribution_finite, simplified space_simps, simp]
 
   have order2: "\<And>x y z. \<lbrakk>x \<in> space MX; y \<in> space MY; z \<in> space MZ; joint_distribution X Z {(x, z)} = 0\<rbrakk>
           \<Longrightarrow> joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)} = 0"
     unfolding joint_distribution_commute_singleton[of X]
     unfolding joint_distribution_assoc_singleton[symmetric]
     using finite_distribution_order(6)[OF finite_var(2) ZX]
-    by (auto simp: space_simps)
+    by auto
 
-  have "(\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?XZ x z * ?YZdZ y z))) =
+  have "(\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?XZ x z * (?YZ y z / ?Z z)))) =
     (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * (log b (?XYZ x y z / (?X x * ?YZ y z)) - log b (?XZ x z / (?X x * ?Z z))))"
     (is "(\<Sum>(x, y, z)\<in>?S. ?L x y z) = (\<Sum>(x, y, z)\<in>?S. ?R x y z)")
   proof (safe intro!: setsum_cong)
     fix x y z assume space: "x \<in> space MX" "y \<in> space MY" "z \<in> space MZ"
-    then have *: "?XYZ x y z / (?XZ x z * ?YZdZ y z) =
-      (?XYZ x y z / (?X x * ?YZ y z)) / (?XZ x z / (?X x * ?Z z))"
-      using order1(3)
-      by (auto simp: real_of_pextreal_mult[symmetric] real_of_pextreal_eq_0)
     show "?L x y z = ?R x y z"
     proof cases
       assume "?XYZ x y z \<noteq> 0"
-      with space b_gt_1 order1 order2 show ?thesis unfolding *
-        by (subst log_divide)
-           (auto simp: zero_less_divide_iff zero_less_real_of_pextreal
-                       real_of_pextreal_eq_0 zero_less_mult_iff)
+      with space have "0 < ?X x" "0 < ?Z z" "0 < ?XZ x z" "0 < ?YZ y z" "0 < ?XYZ x y z"
+        using order1 order2 by (auto simp: less_le)
+      with b_gt_1 show ?thesis
+        by (simp add: log_mult log_divide zero_less_mult_iff zero_less_divide_iff)
     qed simp
   qed
   also have "\<dots> = (\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?X x * ?YZ y z))) -
@@ -649,8 +654,8 @@
               setsum_left_distrib[symmetric]
     unfolding joint_distribution_commute_singleton[of X]
     unfolding joint_distribution_assoc_singleton[symmetric]
-    using setsum_real_joint_distribution_singleton[OF finite_var(2) ZX, unfolded space_simps]
-    by (intro setsum_cong refl) simp
+    using setsum_joint_distribution_singleton[OF finite_var(2) ZX]
+    by (intro setsum_cong refl) (simp add: space_pair_measure)
   also have "(\<Sum>(x, y, z)\<in>?S. ?XYZ x y z * log b (?XYZ x y z / (?X x * ?YZ y z))) -
              (\<Sum>(x, z)\<in>space MX \<times> space MZ. ?XZ x z * log b (?XZ x z / (?X x * ?Z z))) =
              conditional_mutual_information b MX MY MZ X Y Z"
@@ -664,11 +669,11 @@
 lemma (in information_space) conditional_mutual_information_eq:
   assumes "simple_function M X" "simple_function M Y" "simple_function M Z"
   shows "\<I>(X;Y|Z) = (\<Sum>(x, y, z) \<in> X`space M \<times> Y`space M \<times> Z`space M.
-             real (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)}) *
-             log b (real (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)}) /
-    (real (joint_distribution X Z {(x, z)}) * real (joint_distribution Y Z {(y,z)} / distribution Z {z}))))"
-  using conditional_mutual_information_generic_eq[OF assms[THEN simple_function_imp_finite_random_variable]]
-  by simp
+             distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} *
+             log b (distribution (\<lambda>x. (X x, Y x, Z x)) {(x, y, z)} /
+    (joint_distribution X Z {(x, z)} * joint_distribution Y Z {(y,z)} / distribution Z {z})))"
+  by (subst conditional_mutual_information_generic_eq[OF assms[THEN simple_function_imp_finite_random_variable]])
+     simp
 
 lemma (in information_space) conditional_mutual_information_eq_mutual_information:
   assumes X: "simple_function M X" and Y: "simple_function M Y"
@@ -683,10 +688,10 @@
 qed
 
 lemma (in prob_space) distribution_unit[simp]: "distribution (\<lambda>x. ()) {()} = 1"
-  unfolding distribution_def using measure_space_1 by auto
+  unfolding distribution_def using prob_space by auto
 
 lemma (in prob_space) joint_distribution_unit[simp]: "distribution (\<lambda>x. (X x, ())) {(a, ())} = distribution X {a}"
-  unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>])
+  unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>'])
 
 lemma (in prob_space) setsum_distribution:
   assumes X: "finite_random_variable MX X" shows "(\<Sum>a\<in>space MX. distribution X {a}) = 1"
@@ -695,12 +700,13 @@
 
 lemma (in prob_space) setsum_real_distribution:
   fixes MX :: "('c, 'd) measure_space_scheme"
-  assumes X: "finite_random_variable MX X" shows "(\<Sum>a\<in>space MX. real (distribution X {a})) = 1"
-  using setsum_real_joint_distribution[OF assms, of "\<lparr> space = UNIV, sets = Pow UNIV, measure = undefined \<rparr>" "\<lambda>x. ()" "{()}"]
-  using sigma_algebra_Pow[of "UNIV::unit set" "\<lparr> measure = undefined \<rparr>"] by simp
+  assumes X: "finite_random_variable MX X" shows "(\<Sum>a\<in>space MX. distribution X {a}) = 1"
+  using setsum_joint_distribution[OF assms, of "\<lparr> space = UNIV, sets = Pow UNIV, measure = undefined \<rparr>" "\<lambda>x. ()" "{()}"]
+  using sigma_algebra_Pow[of "UNIV::unit set" "\<lparr> measure = undefined \<rparr>"]
+  by auto
 
 lemma (in information_space) conditional_mutual_information_generic_positive:
-  assumes "finite_random_variable MX X" and "finite_random_variable MY Y" and "finite_random_variable MZ Z"
+  assumes X: "finite_random_variable MX X" and Y: "finite_random_variable MY Y" and Z: "finite_random_variable MZ Z"
   shows "0 \<le> conditional_mutual_information b MX MY MZ X Y Z"
 proof (cases "space MX \<times> space MY \<times> space MZ = {}")
   case True show ?thesis
@@ -708,43 +714,35 @@
     by simp
 next
   case False
-  let "?dXYZ A" = "real (distribution (\<lambda>x. (X x, Y x, Z x)) A)"
-  let "?dXZ A" = "real (joint_distribution X Z A)"
-  let "?dYZ A" = "real (joint_distribution Y Z A)"
-  let "?dX A" = "real (distribution X A)"
-  let "?dZ A" = "real (distribution Z A)"
+  let ?dXYZ = "distribution (\<lambda>x. (X x, Y x, Z x))"
+  let ?dXZ = "joint_distribution X Z"
+  let ?dYZ = "joint_distribution Y Z"
+  let ?dX = "distribution X"
+  let ?dZ = "distribution Z"
   let ?M = "space MX \<times> space MY \<times> space MZ"
 
-  have split_beta: "\<And>f. split f = (\<lambda>x. f (fst x) (snd x))" by (simp add: fun_eq_iff)
-
-  note space_simps = space_pair_measure space_sigma algebra.simps
-
-  note finite_var = assms
-  note YZ = finite_random_variable_pairI[OF finite_var(2,3)]
-  note XZ = finite_random_variable_pairI[OF finite_var(1,3)]
-  note ZX = finite_random_variable_pairI[OF finite_var(3,1)]
-  note YZ = finite_random_variable_pairI[OF finite_var(2,3)]
-  note XYZ = finite_random_variable_pairI[OF finite_var(1) YZ]
-  note finite = finite_var(3) YZ XZ XYZ
-  note finite = finite[THEN finite_distribution_finite, simplified space_simps]
-
+  note YZ = finite_random_variable_pairI[OF Y Z]
+  note XZ = finite_random_variable_pairI[OF X Z]
+  note ZX = finite_random_variable_pairI[OF Z X]
+  note YZ = finite_random_variable_pairI[OF Y Z]
+  note XYZ = finite_random_variable_pairI[OF X YZ]
+  note finite = Z YZ XZ XYZ
   have order: "\<And>x y z. \<lbrakk>x \<in> space MX; y \<in> space MY; z \<in> space MZ; joint_distribution X Z {(x, z)} = 0\<rbrakk>
           \<Longrightarrow> joint_distribution X (\<lambda>x. (Y x, Z x)) {(x, y, z)} = 0"
     unfolding joint_distribution_commute_singleton[of X]
     unfolding joint_distribution_assoc_singleton[symmetric]
-    using finite_distribution_order(6)[OF finite_var(2) ZX]
-    by (auto simp: space_simps)
+    using finite_distribution_order(6)[OF Y ZX]
+    by auto
 
   note order = order
-    finite_distribution_order(5,6)[OF finite_var(1) YZ, simplified space_simps]
-    finite_distribution_order(5,6)[OF finite_var(2,3), simplified space_simps]
+    finite_distribution_order(5,6)[OF X YZ]
+    finite_distribution_order(5,6)[OF Y Z]
 
   have "- conditional_mutual_information b MX MY MZ X Y Z = - (\<Sum>(x, y, z) \<in> ?M. ?dXYZ {(x, y, z)} *
     log b (?dXYZ {(x, y, z)} / (?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z})))"
-    unfolding conditional_mutual_information_generic_eq[OF assms] neg_equal_iff_equal
-    by (intro setsum_cong) (auto intro!: arg_cong[where f="log b"] simp: real_of_pextreal_mult[symmetric])
+    unfolding conditional_mutual_information_generic_eq[OF assms] neg_equal_iff_equal by auto
   also have "\<dots> \<le> log b (\<Sum>(x, y, z) \<in> ?M. ?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z})"
-    unfolding split_beta
+    unfolding split_beta'
   proof (rule log_setsum_divide)
     show "?M \<noteq> {}" using False by simp
     show "1 < b" using b_gt_1 .
@@ -757,33 +755,31 @@
       unfolding setsum_commute[of _ "space MY"]
       unfolding setsum_commute[of _ "space MZ"]
       by (simp_all add: space_pair_measure
-        setsum_real_joint_distribution_singleton[OF `finite_random_variable MX X` YZ]
-        setsum_real_joint_distribution_singleton[OF `finite_random_variable MY Y` finite_var(3)]
-        setsum_real_distribution[OF `finite_random_variable MZ Z`])
+                        setsum_joint_distribution_singleton[OF X YZ]
+                        setsum_joint_distribution_singleton[OF Y Z]
+                        setsum_distribution[OF Z])
 
     fix x assume "x \<in> ?M"
     let ?x = "(fst x, fst (snd x), snd (snd x))"
 
-    show "0 \<le> ?dXYZ {?x}" using real_pextreal_nonneg .
-    show "0 \<le> ?dXZ {(fst x, snd (snd x))} * ?dYZ {(fst (snd x), snd (snd x))} / ?dZ {snd (snd x)}"
-     by (simp add: real_pextreal_nonneg mult_nonneg_nonneg divide_nonneg_nonneg)
+    show "0 \<le> ?dXYZ {?x}"
+      "0 \<le> ?dXZ {(fst x, snd (snd x))} * ?dYZ {(fst (snd x), snd (snd x))} / ?dZ {snd (snd x)}"
+     by (simp_all add: mult_nonneg_nonneg divide_nonneg_nonneg)
 
     assume *: "0 < ?dXYZ {?x}"
-    with `x \<in> ?M` show "0 < ?dXZ {(fst x, snd (snd x))} * ?dYZ {(fst (snd x), snd (snd x))} / ?dZ {snd (snd x)}"
-      using finite order
-      by (cases x)
-         (auto simp add: zero_less_real_of_pextreal zero_less_mult_iff zero_less_divide_iff)
+    with `x \<in> ?M` finite order show "0 < ?dXZ {(fst x, snd (snd x))} * ?dYZ {(fst (snd x), snd (snd x))} / ?dZ {snd (snd x)}"
+      by (cases x) (auto simp add: zero_le_mult_iff zero_le_divide_iff less_le)
   qed
   also have "(\<Sum>(x, y, z) \<in> ?M. ?dXZ {(x, z)} * ?dYZ {(y,z)} / ?dZ {z}) = (\<Sum>z\<in>space MZ. ?dZ {z})"
     apply (simp add: setsum_cartesian_product')
     apply (subst setsum_commute)
     apply (subst (2) setsum_commute)
     by (auto simp: setsum_divide_distrib[symmetric] setsum_product[symmetric]
-                   setsum_real_joint_distribution_singleton[OF finite_var(1,3)]
-                   setsum_real_joint_distribution_singleton[OF finite_var(2,3)]
+                   setsum_joint_distribution_singleton[OF X Z]
+                   setsum_joint_distribution_singleton[OF Y Z]
           intro!: setsum_cong)
   also have "log b (\<Sum>z\<in>space MZ. ?dZ {z}) = 0"
-    unfolding setsum_real_distribution[OF finite_var(3)] by simp
+    unfolding setsum_real_distribution[OF Z] by simp
   finally show ?thesis by simp
 qed
 
@@ -800,57 +796,52 @@
 abbreviation (in information_space)
   conditional_entropy_Pow ("\<H>'(_ | _')") where
   "\<H>(X | Y) \<equiv> conditional_entropy b
-    \<lparr> space = X`space M, sets = Pow (X`space M), measure = distribution X \<rparr>
-    \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = distribution Y \<rparr> X Y"
+    \<lparr> space = X`space M, sets = Pow (X`space M), measure = extreal\<circ>distribution X \<rparr>
+    \<lparr> space = Y`space M, sets = Pow (Y`space M), measure = extreal\<circ>distribution Y \<rparr> X Y"
 
 lemma (in information_space) conditional_entropy_positive:
   "simple_function M X \<Longrightarrow> simple_function M Y \<Longrightarrow> 0 \<le> \<H>(X | Y)"
   unfolding conditional_entropy_def by (auto intro!: conditional_mutual_information_positive)
 
-lemma (in measure_space) empty_measureI: "A = {} \<Longrightarrow> \<mu> A = 0" by simp
-
 lemma (in information_space) conditional_entropy_generic_eq:
   fixes MX :: "('c, 'd) measure_space_scheme" and MY :: "('e, 'f) measure_space_scheme"
   assumes MX: "finite_random_variable MX X"
   assumes MZ: "finite_random_variable MZ Z"
   shows "conditional_entropy b MX MZ X Z =
      - (\<Sum>(x, z)\<in>space MX \<times> space MZ.
-         real (joint_distribution X Z {(x, z)}) *
-         log b (real (joint_distribution X Z {(x, z)}) / real (distribution Z {z})))"
+         joint_distribution X Z {(x, z)} * log b (joint_distribution X Z {(x, z)} / distribution Z {z}))"
 proof -
   interpret MX: finite_sigma_algebra MX using MX by simp
   interpret MZ: finite_sigma_algebra MZ using MZ by simp
   let "?XXZ x y z" = "joint_distribution X (\<lambda>x. (X x, Z x)) {(x, y, z)}"
   let "?XZ x z" = "joint_distribution X Z {(x, z)}"
   let "?Z z" = "distribution Z {z}"
-  let "?f x y z" = "log b (real (?XXZ x y z) / (real (?XZ x z) * real (?XZ y z / ?Z z)))"
+  let "?f x y z" = "log b (?XXZ x y z * ?Z z / (?XZ x z * ?XZ y z))"
   { fix x z have "?XXZ x x z = ?XZ x z"
-      unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>]) }
+      unfolding distribution_def by (auto intro!: arg_cong[where f=\<mu>']) }
   note this[simp]
   { fix x x' :: 'c and z assume "x' \<noteq> x"
     then have "?XXZ x x' z = 0"
-      by (auto simp: distribution_def intro!: arg_cong[where f=\<mu>] empty_measureI) }
+      by (auto simp: distribution_def empty_measure'[symmetric]
+               simp del: empty_measure' intro!: arg_cong[where f=\<mu>']) }
   note this[simp]
   { fix x x' z assume *: "x \<in> space MX" "z \<in> space MZ"
-    then have "(\<Sum>x'\<in>space MX. real (?XXZ x x' z) * ?f x x' z)
-      = (\<Sum>x'\<in>space MX. if x = x' then real (?XZ x z) * ?f x x z else 0)"
+    then have "(\<Sum>x'\<in>space MX. ?XXZ x x' z * ?f x x' z)
+      = (\<Sum>x'\<in>space MX. if x = x' then ?XZ x z * ?f x x z else 0)"
       by (auto intro!: setsum_cong)
-    also have "\<dots> = real (?XZ x z) * ?f x x z"
+    also have "\<dots> = ?XZ x z * ?f x x z"
       using `x \<in> space MX` by (simp add: setsum_cases[OF MX.finite_space])
-    also have "\<dots> = real (?XZ x z) * log b (real (?Z z) / real (?XZ x z))"
-      by (auto simp: real_of_pextreal_mult[symmetric])
-    also have "\<dots> = - real (?XZ x z) * log b (real (?XZ x z) / real (?Z z))"
-      using assms[THEN finite_distribution_finite]
+    also have "\<dots> = ?XZ x z * log b (?Z z / ?XZ x z)" by auto
+    also have "\<dots> = - ?XZ x z * log b (?XZ x z / ?Z z)"
       using finite_distribution_order(6)[OF MX MZ]
-      by (auto simp: log_simps field_simps zero_less_mult_iff zero_less_real_of_pextreal real_of_pextreal_eq_0)
-    finally have "(\<Sum>x'\<in>space MX. real (?XXZ x x' z) * ?f x x' z) =
-      - real (?XZ x z) * log b (real (?XZ x z) / real (?Z z))" . }
+      by (auto simp: log_simps field_simps zero_less_mult_iff)
+    finally have "(\<Sum>x'\<in>space MX. ?XXZ x x' z * ?f x x' z) = - ?XZ x z * log b (?XZ x z / ?Z z)" . }
   note * = this
   show ?thesis
     unfolding conditional_entropy_def
     unfolding conditional_mutual_information_generic_eq[OF MX MX MZ]
     by (auto simp: setsum_cartesian_product' setsum_negf[symmetric]
-                   setsum_commute[of _ "space MZ"] *   simp del: divide_pextreal_def
+                   setsum_commute[of _ "space MZ"] *
              intro!: setsum_cong)
 qed
 
@@ -858,29 +849,27 @@
   assumes "simple_function M X" "simple_function M Z"
   shows "\<H>(X | Z) =
      - (\<Sum>(x, z)\<in>X ` space M \<times> Z ` space M.
-         real (joint_distribution X Z {(x, z)}) *
-         log b (real (joint_distribution X Z {(x, z)}) / real (distribution Z {z})))"
-  using conditional_entropy_generic_eq[OF assms[THEN simple_function_imp_finite_random_variable]]
-  by simp
+         joint_distribution X Z {(x, z)} *
+         log b (joint_distribution X Z {(x, z)} / distribution Z {z}))"
+  by (subst conditional_entropy_generic_eq[OF assms[THEN simple_function_imp_finite_random_variable]])
+     simp
 
 lemma (in information_space) conditional_entropy_eq_ce_with_hypothesis:
   assumes X: "simple_function M X" and Y: "simple_function M Y"
   shows "\<H>(X | Y) =
-    -(\<Sum>y\<in>Y`space M. real (distribution Y {y}) *
-      (\<Sum>x\<in>X`space M. real (joint_distribution X Y {(x,y)}) / real (distribution Y {(y)}) *
-              log b (real (joint_distribution X Y {(x,y)}) / real (distribution Y {(y)}))))"
+    -(\<Sum>y\<in>Y`space M. distribution Y {y} *
+      (\<Sum>x\<in>X`space M. joint_distribution X Y {(x,y)} / distribution Y {(y)} *
+              log b (joint_distribution X Y {(x,y)} / distribution Y {(y)})))"
   unfolding conditional_entropy_eq[OF assms]
-  using finite_distribution_finite[OF finite_random_variable_pairI[OF assms[THEN simple_function_imp_finite_random_variable]]]
   using finite_distribution_order(5,6)[OF assms[THEN simple_function_imp_finite_random_variable]]
-  using finite_distribution_finite[OF Y[THEN simple_function_imp_finite_random_variable]]
-  by (auto simp: setsum_cartesian_product'  setsum_commute[of _ "Y`space M"] setsum_right_distrib real_of_pextreal_eq_0
+  by (auto simp: setsum_cartesian_product'  setsum_commute[of _ "Y`space M"] setsum_right_distrib
            intro!: setsum_cong)
 
 lemma (in information_space) conditional_entropy_eq_cartesian_product:
   assumes "simple_function M X" "simple_function M Y"
   shows "\<H>(X | Y) = -(\<Sum>x\<in>X`space M. \<Sum>y\<in>Y`space M.
-    real (joint_distribution X Y {(x,y)}) *
-    log b (real (joint_distribution X Y {(x,y)}) / real (distribution Y {y})))"
+    joint_distribution X Y {(x,y)} *
+    log b (joint_distribution X Y {(x,y)} / distribution Y {y}))"
   unfolding conditional_entropy_eq[OF assms]
   by (auto intro!: setsum_cong simp: setsum_cartesian_product')
 
@@ -890,24 +879,22 @@
   assumes X: "simple_function M X" and Z: "simple_function M Z"
   shows  "\<I>(X ; Z) = \<H>(X) - \<H>(X | Z)"
 proof -
-  let "?XZ x z" = "real (joint_distribution X Z {(x, z)})"
-  let "?Z z" = "real (distribution Z {z})"
-  let "?X x" = "real (distribution X {x})"
+  let "?XZ x z" = "joint_distribution X Z {(x, z)}"
+  let "?Z z" = "distribution Z {z}"
+  let "?X x" = "distribution X {x}"
   note fX = X[THEN simple_function_imp_finite_random_variable]
   note fZ = Z[THEN simple_function_imp_finite_random_variable]
-  note fX[THEN finite_distribution_finite, simp] and fZ[THEN finite_distribution_finite, simp]
   note finite_distribution_order[OF fX fZ, simp]
   { fix x z assume "x \<in> X`space M" "z \<in> Z`space M"
     have "?XZ x z * log b (?XZ x z / (?X x * ?Z z)) =
           ?XZ x z * log b (?XZ x z / ?Z z) - ?XZ x z * log b (?X x)"
-      by (auto simp: log_simps real_of_pextreal_mult[symmetric] zero_less_mult_iff
-                     zero_less_real_of_pextreal field_simps real_of_pextreal_eq_0 abs_mult) }
+      by (auto simp: log_simps zero_le_mult_iff field_simps less_le) }
   note * = this
   show ?thesis
     unfolding entropy_eq[OF X] conditional_entropy_eq[OF X Z] mutual_information_eq[OF X Z]
-    using setsum_real_joint_distribution_singleton[OF fZ fX, unfolded joint_distribution_commute_singleton[of Z X]]
+    using setsum_joint_distribution_singleton[OF fZ fX, unfolded joint_distribution_commute_singleton[of Z X]]
     by (simp add: * setsum_cartesian_product' setsum_subtractf setsum_left_distrib[symmetric]
-                     setsum_real_distribution)
+                     setsum_distribution)
 qed
 
 lemma (in information_space) conditional_entropy_less_eq_entropy:
@@ -923,21 +910,19 @@
   assumes X: "simple_function M X" and Y: "simple_function M Y"
   shows  "\<H>(\<lambda>x. (X x, Y x)) = \<H>(X) + \<H>(Y|X)"
 proof -
-  let "?XY x y" = "real (joint_distribution X Y {(x, y)})"
-  let "?Y y" = "real (distribution Y {y})"
-  let "?X x" = "real (distribution X {x})"
+  let "?XY x y" = "joint_distribution X Y {(x, y)}"
+  let "?Y y" = "distribution Y {y}"
+  let "?X x" = "distribution X {x}"
   note fX = X[THEN simple_function_imp_finite_random_variable]
   note fY = Y[THEN simple_function_imp_finite_random_variable]
-  note fX[THEN finite_distribution_finite, simp] and fY[THEN finite_distribution_finite, simp]
   note finite_distribution_order[OF fX fY, simp]
   { fix x y assume "x \<in> X`space M" "y \<in> Y`space M"
     have "?XY x y * log b (?XY x y / ?X x) =
           ?XY x y * log b (?XY x y) - ?XY x y * log b (?X x)"
-      by (auto simp: log_simps real_of_pextreal_mult[symmetric] zero_less_mult_iff
-                     zero_less_real_of_pextreal field_simps real_of_pextreal_eq_0 abs_mult) }
+      by (auto simp: log_simps zero_le_mult_iff field_simps less_le) }
   note * = this
   show ?thesis
-    using setsum_real_joint_distribution_singleton[OF fY fX]
+    using setsum_joint_distribution_singleton[OF fY fX]
     unfolding entropy_eq[OF X] conditional_entropy_eq_cartesian_product[OF Y X] entropy_eq_cartesian_product[OF X Y]
     unfolding joint_distribution_commute_singleton[of Y X] setsum_commute[of _ "X`space M"]
     by (simp add: * setsum_subtractf setsum_left_distrib[symmetric])
@@ -1063,23 +1048,21 @@
   assumes svi: "subvimage (space M) X P"
   shows "\<H>(X) = \<H>(P) + \<H>(X|P)"
 proof -
-  let "?XP x p" = "real (joint_distribution X P {(x, p)})"
-  let "?X x" = "real (distribution X {x})"
-  let "?P p" = "real (distribution P {p})"
+  let "?XP x p" = "joint_distribution X P {(x, p)}"
+  let "?X x" = "distribution X {x}"
+  let "?P p" = "distribution P {p}"
   note fX = sf(1)[THEN simple_function_imp_finite_random_variable]
   note fP = sf(2)[THEN simple_function_imp_finite_random_variable]
-  note fX[THEN finite_distribution_finite, simp] and fP[THEN finite_distribution_finite, simp]
   note finite_distribution_order[OF fX fP, simp]
-  have "(\<Sum>x\<in>X ` space M. real (distribution X {x}) * log b (real (distribution X {x}))) =
-    (\<Sum>y\<in>P `space M. \<Sum>x\<in>X ` space M.
-    real (joint_distribution X P {(x, y)}) * log b (real (joint_distribution X P {(x, y)})))"
+  have "(\<Sum>x\<in>X ` space M. ?X x * log b (?X x)) =
+    (\<Sum>y\<in>P `space M. \<Sum>x\<in>X ` space M. ?XP x y * log b (?XP x y))"
   proof (subst setsum_image_split[OF svi],
       safe intro!: setsum_mono_zero_cong_left imageI)
     show "finite (X ` space M)" "finite (X ` space M)" "finite (P ` space M)"
       using sf unfolding simple_function_def by auto
   next
     fix p x assume in_space: "p \<in> space M" "x \<in> space M"
-    assume "real (joint_distribution X P {(X x, P p)}) * log b (real (joint_distribution X P {(X x, P p)})) \<noteq> 0"
+    assume "?XP (X x) (P p) * log b (?XP (X x) (P p)) \<noteq> 0"
     hence "(\<lambda>x. (X x, P x)) -` {(X x, P p)} \<inter> space M \<noteq> {}" by (auto simp: distribution_def)
     with svi[unfolded subvimage_def, rule_format, OF `x \<in> space M`]
     show "x \<in> P -` {P p}" by auto
@@ -1091,20 +1074,16 @@
       by auto
     hence "(\<lambda>x. (X x, P x)) -` {(X x, P p)} \<inter> space M = X -` {X x} \<inter> space M"
       by auto
-    thus "real (distribution X {X x}) * log b (real (distribution X {X x})) =
-          real (joint_distribution X P {(X x, P p)}) *
-          log b (real (joint_distribution X P {(X x, P p)}))"
+    thus "?X (X x) * log b (?X (X x)) = ?XP (X x) (P p) * log b (?XP (X x) (P p))"
       by (auto simp: distribution_def)
   qed
-  moreover have "\<And>x y. real (joint_distribution X P {(x, y)}) *
-      log b (real (joint_distribution X P {(x, y)}) / real (distribution P {y})) =
-      real (joint_distribution X P {(x, y)}) * log b (real (joint_distribution X P {(x, y)})) -
-      real (joint_distribution X P {(x, y)}) * log b (real (distribution P {y}))"
+  moreover have "\<And>x y. ?XP x y * log b (?XP x y / ?P y) =
+      ?XP x y * log b (?XP x y) - ?XP x y * log b (?P y)"
     by (auto simp add: log_simps zero_less_mult_iff field_simps)
   ultimately show ?thesis
     unfolding sf[THEN entropy_eq] conditional_entropy_eq[OF sf]
-    using setsum_real_joint_distribution_singleton[OF fX fP]
-    by (simp add: setsum_cartesian_product' setsum_subtractf setsum_real_distribution
+    using setsum_joint_distribution_singleton[OF fX fP]
+    by (simp add: setsum_cartesian_product' setsum_subtractf setsum_distribution
       setsum_left_distrib[symmetric] setsum_commute[where B="P`space M"])
 qed
 
--- a/src/HOL/Probability/Lebesgue_Integration.thy	Mon Mar 14 15:17:10 2011 +0100
+++ b/src/HOL/Probability/Lebesgue_Integration.thy	Mon Mar 14 15:29:10 2011 +0100
@@ -6,6 +6,88 @@
 imports Measure Borel_Space
 begin
 
+lemma extreal_indicator_pos[simp,intro]: "0 \<le> (indicator A x ::extreal)"
+  unfolding indicator_def by auto
+
+lemma tendsto_real_max:
+  fixes x y :: real
+  assumes "(X ---> x) net"
+  assumes "(Y ---> y) net"
+  shows "((\<lambda>x. max (X x) (Y x)) ---> max x y) net"
+proof -
+  have *: "\<And>x y :: real. max x y = y + ((x - y) + norm (x - y)) / 2"
+    by (auto split: split_max simp: field_simps)
+  show ?thesis
+    unfolding *
+    by (intro tendsto_add assms tendsto_divide tendsto_norm tendsto_diff) auto
+qed
+
+lemma (in measure_space) measure_Union:
+  assumes "finite S" "S \<subseteq> sets M" "\<And>A B. A \<in> S \<Longrightarrow> B \<in> S \<Longrightarrow> A \<noteq> B \<Longrightarrow> A \<inter> B = {}"
+  shows "setsum \<mu> S = \<mu> (\<Union>S)"
+proof -
+  have "setsum \<mu> S = \<mu> (\<Union>i\<in>S. i)"
+    using assms by (intro measure_setsum[OF `finite S`]) (auto simp: disjoint_family_on_def)
+  also have "\<dots> = \<mu> (\<Union>S)" by (auto intro!: arg_cong[where f=\<mu>])
+  finally show ?thesis .
+qed
+
+lemma (in sigma_algebra) measurable_sets2[intro]:
+  assumes "f \<in> measurable M M'" "g \<in> measurable M M''"
+  and "A \<in> sets M'" "B \<in> sets M''"
+  shows "f -` A \<inter> g -` B \<inter> space M \<in> sets M"
+proof -
+  have "f -` A \<inter> g -` B \<inter> space M = (f -` A \<inter> space M) \<inter> (g -` B \<inter> space M)"
+    by auto
+  then show ?thesis using assms by (auto intro: measurable_sets)
+qed
+
+lemma incseq_extreal: "incseq f \<Longrightarrow> incseq (\<lambda>x. extreal (f x))"
+  unfolding incseq_def by auto
+
+lemma incseq_Suc_iff: "incseq f \<longleftrightarrow> (\<forall>n. f n \<le> f (Suc n))"
+proof
+  assume "\<forall>n. f n \<le> f (Suc n)" then show "incseq f" by (auto intro!: incseq_SucI)
+qed (auto simp: incseq_def)
+
+lemma borel_measurable_real_floor:
+  "(\<lambda>x::real. real \<lfloor>x\<rfloor>) \<in> borel_measurable borel"
+  unfolding borel.borel_measurable_iff_ge
+proof (intro allI)
+  fix a :: real
+  { fix x have "a \<le> real \<lfloor>x\<rfloor> \<longleftrightarrow> real \<lceil>a\<rceil> \<le> x"
+      using le_floor_eq[of "\<lceil>a\<rceil>" x] ceiling_le_iff[of a "\<lfloor>x\<rfloor>"]
+      unfolding real_eq_of_int by simp }
+  then have "{w::real \<in> space borel. a \<le> real \<lfloor>w\<rfloor>} = {real \<lceil>a\<rceil>..}" by auto
+  then show "{w::real \<in> space borel. a \<le> real \<lfloor>w\<rfloor>} \<in> sets borel" by auto
+qed
+
+lemma measure_preservingD2:
+  "f \<in> measure_preserving A B \<Longrightarrow> f \<in> measurable A B"
+  unfolding measure_preserving_def by auto
+
+lemma measure_preservingD3:
+  "f \<in> measure_preserving A B \<Longrightarrow> f \<in> space A \<rightarrow> space B"
+  unfolding measure_preserving_def measurable_def by auto
+
+lemma measure_preservingD:
+  "T \<in> measure_preserving A B \<Longrightarrow> X \<in> sets B \<Longrightarrow> measure A (T -` X \<inter> space A) = measure B X"
+  unfolding measure_preserving_def by auto
+
+lemma (in sigma_algebra) borel_measurable_real_natfloor[intro, simp]:
+  assumes "f \<in> borel_measurable M"
+  shows "(\<lambda>x. real (natfloor (f x))) \<in> borel_measurable M"
+proof -
+  have "\<And>x. real (natfloor (f x)) = max 0 (real \<lfloor>f x\<rfloor>)"
+    by (auto simp: max_def natfloor_def)
+  with borel_measurable_max[OF measurable_comp[OF assms borel_measurable_real_floor] borel_measurable_const]
+  show ?thesis by (simp add: comp_def)
+qed
+
+lemma (in measure_space) AE_not_in:
+  assumes N: "N \<in> null_sets" shows "AE x. x \<notin> N"
+  using N by (rule AE_I') auto
+
 lemma sums_If_finite:
   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
   assumes finite: "finite {r. P r}"
@@ -55,8 +137,17 @@
     by (auto intro!: finite_UN simp del: UN_simps simp: simple_function_def)
 qed
 
+lemma (in sigma_algebra) simple_function_measurable2[intro]:
+  assumes "simple_function M f" "simple_function M g"
+  shows "f -` A \<inter> g -` B \<inter> space M \<in> sets M"
+proof -
+  have "f -` A \<inter> g -` B \<inter> space M = (f -` A \<inter> space M) \<inter> (g -` B \<inter> space M)"
+    by auto
+  then show ?thesis using assms[THEN simple_functionD(2)] by auto
+qed
+
 lemma (in sigma_algebra) simple_function_indicator_representation:
-  fixes f ::"'a \<Rightarrow> pextreal"
+  fixes f ::"'a \<Rightarrow> extreal"
   assumes f: "simple_function M f" and x: "x \<in> space M"
   shows "f x = (\<Sum>y \<in> f ` space M. y * indicator (f -` {y} \<inter> space M) x)"
   (is "?l = ?r")
@@ -71,7 +162,7 @@
 qed
 
 lemma (in measure_space) simple_function_notspace:
-  "simple_function M (\<lambda>x. h x * indicator (- space M) x::pextreal)" (is "simple_function M ?h")
+  "simple_function M (\<lambda>x. h x * indicator (- space M) x::extreal)" (is "simple_function M ?h")
 proof -
   have "?h ` space M \<subseteq> {0}" unfolding indicator_def by auto
   hence [simp, intro]: "finite (?h ` space M)" by (auto intro: finite_subset)
@@ -111,16 +202,22 @@
 qed
 
 lemma (in sigma_algebra) simple_function_borel_measurable:
-  fixes f :: "'a \<Rightarrow> 'x::t2_space"
+  fixes f :: "'a \<Rightarrow> 'x::{t2_space}"
   assumes "f \<in> borel_measurable M" and "finite (f ` space M)"
   shows "simple_function M f"
   using assms unfolding simple_function_def
   by (auto intro: borel_measurable_vimage)
 
+lemma (in sigma_algebra) simple_function_eq_borel_measurable:
+  fixes f :: "'a \<Rightarrow> extreal"
+  shows "simple_function M f \<longleftrightarrow> finite (f`space M) \<and> f \<in> borel_measurable M"
+  using simple_function_borel_measurable[of f]
+    borel_measurable_simple_function[of f]
+  by (fastsimp simp: simple_function_def)
+
 lemma (in sigma_algebra) simple_function_const[intro, simp]:
   "simple_function M (\<lambda>x. c)"
   by (auto intro: finite_subset simp: simple_function_def)
-
 lemma (in sigma_algebra) simple_function_compose[intro, simp]:
   assumes "simple_function M f"
   shows "simple_function M (g \<circ> f)"
@@ -189,6 +286,7 @@
   and simple_function_mult[intro, simp] = simple_function_compose2[where h="op *"]
   and simple_function_div[intro, simp] = simple_function_compose2[where h="op /"]
   and simple_function_inverse[intro, simp] = simple_function_compose[where g="inverse"]
+  and simple_function_max[intro, simp] = simple_function_compose2[where h=max]
 
 lemma (in sigma_algebra) simple_function_setsum[intro, simp]:
   assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function M (f i)"
@@ -197,247 +295,168 @@
   assume "finite P" from this assms show ?thesis by induct auto
 qed auto
 
-lemma (in sigma_algebra) simple_function_le_measurable:
-  assumes "simple_function M f" "simple_function M g"
-  shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
-proof -
-  have *: "{x \<in> space M. f x \<le> g x} =
-    (\<Union>(F, G)\<in>f`space M \<times> g`space M.
-      if F \<le> G then (f -` {F} \<inter> space M) \<inter> (g -` {G} \<inter> space M) else {})"
-    apply (auto split: split_if_asm)
-    apply (rule_tac x=x in bexI)
-    apply (rule_tac x=x in bexI)
-    by simp_all
-  have **: "\<And>x y. x \<in> space M \<Longrightarrow> y \<in> space M \<Longrightarrow>
-    (f -` {f x} \<inter> space M) \<inter> (g -` {g y} \<inter> space M) \<in> sets M"
-    using assms unfolding simple_function_def by auto
-  have "finite (f`space M \<times> g`space M)"
-    using assms unfolding simple_function_def by auto
-  thus ?thesis unfolding *
-    apply (rule finite_UN)
-    using assms unfolding simple_function_def
-    by (auto intro!: **)
-qed
+lemma (in sigma_algebra)
+  fixes f g :: "'a \<Rightarrow> real" assumes sf: "simple_function M f"
+  shows simple_function_extreal[intro, simp]: "simple_function M (\<lambda>x. extreal (f x))"
+  by (auto intro!: simple_function_compose1[OF sf])
+
+lemma (in sigma_algebra)
+  fixes f g :: "'a \<Rightarrow> nat" assumes sf: "simple_function M f"
+  shows simple_function_real_of_nat[intro, simp]: "simple_function M (\<lambda>x. real (f x))"
+  by (auto intro!: simple_function_compose1[OF sf])
 
 lemma (in sigma_algebra) borel_measurable_implies_simple_function_sequence:
-  fixes u :: "'a \<Rightarrow> pextreal"
+  fixes u :: "'a \<Rightarrow> extreal"
   assumes u: "u \<in> borel_measurable M"
-  shows "\<exists>f. (\<forall>i. simple_function M (f i) \<and> (\<forall>x\<in>space M. f i x \<noteq> \<omega>)) \<and> f \<up> u"
+  shows "\<exists>f. incseq f \<and> (\<forall>i. \<infinity> \<notin> range (f i) \<and> simple_function M (f i)) \<and>
+             (\<forall>x. (SUP i. f i x) = max 0 (u x)) \<and> (\<forall>i x. 0 \<le> f i x)"
 proof -
-  have "\<exists>f. \<forall>x j. (of_nat j \<le> u x \<longrightarrow> f x j = j*2^j) \<and>
-    (u x < of_nat j \<longrightarrow> of_nat (f x j) \<le> u x * 2^j \<and> u x * 2^j < of_nat (Suc (f x j)))"
-    (is "\<exists>f. \<forall>x j. ?P x j (f x j)")
-  proof(rule choice, rule, rule choice, rule)
-    fix x j show "\<exists>n. ?P x j n"
-    proof cases
-      assume *: "u x < of_nat j"
-      then obtain r where r: "u x = Real r" "0 \<le> r" by (cases "u x") auto
-      from reals_Archimedean6a[of "r * 2^j"]
-      obtain n :: nat where "real n \<le> r * 2 ^ j" "r * 2 ^ j < real (Suc n)"
-        using `0 \<le> r` by (auto simp: zero_le_power zero_le_mult_iff)
-      thus ?thesis using r * by (auto intro!: exI[of _ n])
-    qed auto
-  qed
-  then obtain f where top: "\<And>j x. of_nat j \<le> u x \<Longrightarrow> f x j = j*2^j" and
-    upper: "\<And>j x. u x < of_nat j \<Longrightarrow> of_nat (f x j) \<le> u x * 2^j" and
-    lower: "\<And>j x. u x < of_nat j \<Longrightarrow> u x * 2^j < of_nat (Suc (f x j))" by blast
-
-  { fix j x P
-    assume 1: "of_nat j \<le> u x \<Longrightarrow> P (j * 2^j)"
-    assume 2: "\<And>k. \<lbrakk> u x < of_nat j ; of_nat k \<le> u x * 2^j ; u x * 2^j < of_nat (Suc k) \<rbrakk> \<Longrightarrow> P k"
-    have "P (f x j)"
-    proof cases
-      assume "of_nat j \<le> u x" thus "P (f x j)"
-        using top[of j x] 1 by auto
-    next
-      assume "\<not> of_nat j \<le> u x"
-      hence "u x < of_nat j" "of_nat (f x j) \<le> u x * 2^j" "u x * 2^j < of_nat (Suc (f x j))"
-        using upper lower by auto
-      from 2[OF this] show "P (f x j)" .
-    qed }
-  note fI = this
-
-  { fix j x
-    have "f x j = j * 2 ^ j \<longleftrightarrow> of_nat j \<le> u x"
-      by (rule fI, simp, cases "u x") (auto split: split_if_asm) }
-  note f_eq = this
-
-  { fix j x
-    have "f x j \<le> j * 2 ^ j"
-    proof (rule fI)
-      fix k assume *: "u x < of_nat j"
-      assume "of_nat k \<le> u x * 2 ^ j"
-      also have "\<dots> \<le> of_nat (j * 2^j)"
-        using * by (cases "u x") (auto simp: zero_le_mult_iff)
-      finally show "k \<le> j*2^j" by (auto simp del: real_of_nat_mult)
-    qed simp }
+  def f \<equiv> "\<lambda>x i. if real i \<le> u x then i * 2 ^ i else natfloor (real (u x) * 2 ^ i)"
+  { fix x j have "f x j \<le> j * 2 ^ j" unfolding f_def
+    proof (split split_if, intro conjI impI)
+      assume "\<not> real j \<le> u x"
+      then have "natfloor (real (u x) * 2 ^ j) \<le> natfloor (j * 2 ^ j)"
+         by (cases "u x") (auto intro!: natfloor_mono simp: mult_nonneg_nonneg)
+      moreover have "real (natfloor (j * 2 ^ j)) \<le> j * 2^j"
+        by (intro real_natfloor_le) (auto simp: mult_nonneg_nonneg)
+      ultimately show "natfloor (real (u x) * 2 ^ j) \<le> j * 2 ^ j"
+        unfolding real_of_nat_le_iff by auto
+    qed auto }
   note f_upper = this
 
-  let "?g j x" = "of_nat (f x j) / 2^j :: pextreal"
-  show ?thesis unfolding simple_function_def isoton_fun_expand unfolding isoton_def
-  proof (safe intro!: exI[of _ ?g])
-    fix j
-    have *: "?g j ` space M \<subseteq> (\<lambda>x. of_nat x / 2^j) ` {..j * 2^j}"
-      using f_upper by auto
-    thus "finite (?g j ` space M)" by (rule finite_subset) auto
-  next
-    fix j t assume "t \<in> space M"
-    have **: "?g j -` {?g j t} \<inter> space M = {x \<in> space M. f t j = f x j}"
-      by (auto simp: power_le_zero_eq Real_eq_Real mult_le_0_iff)
+  have real_f:
+    "\<And>i x. real (f x i) = (if real i \<le> u x then i * 2 ^ i else real (natfloor (real (u x) * 2 ^ i)))"
+    unfolding f_def by auto
 
-    show "?g j -` {?g j t} \<inter> space M \<in> sets M"
-    proof cases
-      assume "of_nat j \<le> u t"
-      hence "?g j -` {?g j t} \<inter> space M = {x\<in>space M. of_nat j \<le> u x}"
-        unfolding ** f_eq[symmetric] by auto
-      thus "?g j -` {?g j t} \<inter> space M \<in> sets M"
-        using u by auto
-    next
-      assume not_t: "\<not> of_nat j \<le> u t"
-      hence less: "f t j < j*2^j" using f_eq[symmetric] `f t j \<le> j*2^j` by auto
-      have split_vimage: "?g j -` {?g j t} \<inter> space M =
-          {x\<in>space M. of_nat (f t j)/2^j \<le> u x} \<inter> {x\<in>space M. u x < of_nat (Suc (f t j))/2^j}"
-        unfolding **
-      proof safe
-        fix x assume [simp]: "f t j = f x j"
-        have *: "\<not> of_nat j \<le> u x" using not_t f_eq[symmetric] by simp
-        hence "of_nat (f t j) \<le> u x * 2^j \<and> u x * 2^j < of_nat (Suc (f t j))"
-          using upper lower by auto
-        hence "of_nat (f t j) / 2^j \<le> u x \<and> u x < of_nat (Suc (f t j))/2^j" using *
-          by (cases "u x") (auto simp: zero_le_mult_iff power_le_zero_eq divide_real_def[symmetric] field_simps)
-        thus "of_nat (f t j) / 2^j \<le> u x" "u x < of_nat (Suc (f t j))/2^j" by auto
+  let "?g j x" = "real (f x j) / 2^j :: extreal"
+  show ?thesis
+  proof (intro exI[of _ ?g] conjI allI ballI)
+    fix i
+    have "simple_function M (\<lambda>x. real (f x i))"
+    proof (intro simple_function_borel_measurable)
+      show "(\<lambda>x. real (f x i)) \<in> borel_measurable M"
+        using u by (auto intro!: measurable_If simp: real_f)
+      have "(\<lambda>x. real (f x i))`space M \<subseteq> real`{..i*2^i}"
+        using f_upper[of _ i] by auto
+      then show "finite ((\<lambda>x. real (f x i))`space M)"
+        by (rule finite_subset) auto
+    qed
+    then show "simple_function M (?g i)"
+      by (auto intro: simple_function_extreal simple_function_div)
+  next
+    show "incseq ?g"
+    proof (intro incseq_extreal incseq_SucI le_funI)
+      fix x and i :: nat
+      have "f x i * 2 \<le> f x (Suc i)" unfolding f_def
+      proof ((split split_if)+, intro conjI impI)
+        assume "extreal (real i) \<le> u x" "\<not> extreal (real (Suc i)) \<le> u x"
+        then show "i * 2 ^ i * 2 \<le> natfloor (real (u x) * 2 ^ Suc i)"
+          by (cases "u x") (auto intro!: le_natfloor)
       next
-        fix x
-        assume "of_nat (f t j) / 2^j \<le> u x" "u x < of_nat (Suc (f t j))/2^j"
-        hence "of_nat (f t j) \<le> u x * 2 ^ j \<and> u x * 2 ^ j < of_nat (Suc (f t j))"
-          by (cases "u x") (auto simp: zero_le_mult_iff power_le_zero_eq divide_real_def[symmetric] field_simps)
-        hence 1: "of_nat (f t j) \<le> u x * 2 ^ j" and 2: "u x * 2 ^ j < of_nat (Suc (f t j))" by auto
-        note 2
-        also have "\<dots> \<le> of_nat (j*2^j)"
-          using less by (auto simp: zero_le_mult_iff simp del: real_of_nat_mult)
-        finally have bound_ux: "u x < of_nat j"
-          by (cases "u x") (auto simp: zero_le_mult_iff power_le_zero_eq)
-        show "f t j = f x j"
-        proof (rule antisym)
-          from 1 lower[OF bound_ux]
-          show "f t j \<le> f x j" by (cases "u x") (auto split: split_if_asm)
-          from upper[OF bound_ux] 2
-          show "f x j \<le> f t j" by (cases "u x") (auto split: split_if_asm)
+        assume "\<not> extreal (real i) \<le> u x" "extreal (real (Suc i)) \<le> u x"
+        then show "natfloor (real (u x) * 2 ^ i) * 2 \<le> Suc i * 2 ^ Suc i"
+          by (cases "u x") auto
+      next
+        assume "\<not> extreal (real i) \<le> u x" "\<not> extreal (real (Suc i)) \<le> u x"
+        have "natfloor (real (u x) * 2 ^ i) * 2 = natfloor (real (u x) * 2 ^ i) * natfloor 2"
+          by simp
+        also have "\<dots> \<le> natfloor (real (u x) * 2 ^ i * 2)"
+        proof cases
+          assume "0 \<le> u x" then show ?thesis
+            by (intro le_mult_natfloor) (cases "u x", auto intro!: mult_nonneg_nonneg)
+        next
+          assume "\<not> 0 \<le> u x" then show ?thesis
+            by (cases "u x") (auto simp: natfloor_neg mult_nonpos_nonneg)
         qed
-      qed
-      show ?thesis unfolding split_vimage using u by auto
+        also have "\<dots> = natfloor (real (u x) * 2 ^ Suc i)"
+          by (simp add: ac_simps)
+        finally show "natfloor (real (u x) * 2 ^ i) * 2 \<le> natfloor (real (u x) * 2 ^ Suc i)" .
+      qed simp
+      then show "?g i x \<le> ?g (Suc i) x"
+        by (auto simp: field_simps)
     qed
   next
-    fix j t assume "?g j t = \<omega>" thus False by (auto simp: power_le_zero_eq)
-  next
-    fix t
-    { fix i
-      have "f t i * 2 \<le> f t (Suc i)"
-      proof (rule fI)
-        assume "of_nat (Suc i) \<le> u t"
-        hence "of_nat i \<le> u t" by (cases "u t") auto
-        thus "f t i * 2 \<le> Suc i * 2 ^ Suc i" unfolding f_eq[symmetric] by simp
-      next
-        fix k
-        assume *: "u t * 2 ^ Suc i < of_nat (Suc k)"
-        show "f t i * 2 \<le> k"
-        proof (rule fI)
-          assume "of_nat i \<le> u t"
-          hence "of_nat (i * 2^Suc i) \<le> u t * 2^Suc i"
-            by (cases "u t") (auto simp: zero_le_mult_iff power_le_zero_eq)
-          also have "\<dots> < of_nat (Suc k)" using * by auto
-          finally show "i * 2 ^ i * 2 \<le> k"
-            by (auto simp del: real_of_nat_mult)
-        next
-          fix j assume "of_nat j \<le> u t * 2 ^ i"
-          with * show "j * 2 \<le> k" by (cases "u t") (auto simp: zero_le_mult_iff power_le_zero_eq)
+    fix x show "(SUP i. ?g i x) = max 0 (u x)"
+    proof (rule extreal_SUPI)
+      fix i show "?g i x \<le> max 0 (u x)" unfolding max_def f_def
+        by (cases "u x") (auto simp: field_simps real_natfloor_le natfloor_neg
+                                     mult_nonpos_nonneg mult_nonneg_nonneg)
+    next
+      fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> ?g i x \<le> y"
+      have "\<And>i. 0 \<le> ?g i x" by (auto simp: divide_nonneg_pos)
+      from order_trans[OF this *] have "0 \<le> y" by simp
+      show "max 0 (u x) \<le> y"
+      proof (cases y)
+        case (real r)
+        with * have *: "\<And>i. f x i \<le> r * 2^i" by (auto simp: divide_le_eq)
+        from real_arch_lt[of r] * have "u x \<noteq> \<infinity>" by (auto simp: f_def) (metis less_le_not_le)
+        then have "\<exists>p. max 0 (u x) = extreal p \<and> 0 \<le> p" by (cases "u x") (auto simp: max_def)
+        then guess p .. note ux = this
+        obtain m :: nat where m: "p < real m" using real_arch_lt ..
+        have "p \<le> r"
+        proof (rule ccontr)
+          assume "\<not> p \<le> r"
+          with LIMSEQ_inverse_realpow_zero[unfolded LIMSEQ_iff, rule_format, of 2 "p - r"]
+          obtain N where "\<forall>n\<ge>N. r * 2^n < p * 2^n - 1" by (auto simp: inverse_eq_divide field_simps)
+          then have "r * 2^max N m < p * 2^max N m - 1" by simp
+          moreover
+          have "real (natfloor (p * 2 ^ max N m)) \<le> r * 2 ^ max N m"
+            using *[of "max N m"] m unfolding real_f using ux
+            by (cases "0 \<le> u x") (simp_all add: max_def mult_nonneg_nonneg split: split_if_asm)
+          then have "p * 2 ^ max N m - 1 < r * 2 ^ max N m"
+            by (metis real_natfloor_gt_diff_one less_le_trans)
+          ultimately show False by auto
         qed
-      qed
-      thus "?g i t \<le> ?g (Suc i) t"
-        by (auto simp: zero_le_mult_iff power_le_zero_eq divide_real_def[symmetric] field_simps) }
-    hence mono: "mono (\<lambda>i. ?g i t)" unfolding mono_iff_le_Suc by auto
+        then show "max 0 (u x) \<le> y" using real ux by simp
+      qed (insert `0 \<le> y`, auto)
+    qed
+  qed (auto simp: divide_nonneg_pos)
+qed
 
-    show "(SUP j. of_nat (f t j) / 2 ^ j) = u t"
-    proof (rule pextreal_SUPI)
-      fix j show "of_nat (f t j) / 2 ^ j \<le> u t"
-      proof (rule fI)
-        assume "of_nat j \<le> u t" thus "of_nat (j * 2 ^ j) / 2 ^ j \<le> u t"
-          by (cases "u t") (auto simp: power_le_zero_eq divide_real_def[symmetric] field_simps)
-      next
-        fix k assume "of_nat k \<le> u t * 2 ^ j"
-        thus "of_nat k / 2 ^ j \<le> u t"
-          by (cases "u t")
-             (auto simp: power_le_zero_eq divide_real_def[symmetric] field_simps zero_le_mult_iff)
-      qed
-    next
-      fix y :: pextreal assume *: "\<And>j. j \<in> UNIV \<Longrightarrow> of_nat (f t j) / 2 ^ j \<le> y"
-      show "u t \<le> y"
-      proof (cases "u t")
-        case (preal r)
-        show ?thesis
-        proof (rule ccontr)
-          assume "\<not> u t \<le> y"
-          then obtain p where p: "y = Real p" "0 \<le> p" "p < r" using preal by (cases y) auto
-          with LIMSEQ_inverse_realpow_zero[of 2, unfolded LIMSEQ_iff, rule_format, of "r - p"]
-          obtain n where n: "\<And>N. n \<le> N \<Longrightarrow> inverse (2^N) < r - p" by auto
-          let ?N = "max n (natfloor r + 1)"
-          have "u t < of_nat ?N" "n \<le> ?N"
-            using ge_natfloor_plus_one_imp_gt[of r n] preal
-            using real_natfloor_add_one_gt
-            by (auto simp: max_def real_of_nat_Suc)
-          from lower[OF this(1)]
-          have "r < (real (f t ?N) + 1) / 2 ^ ?N" unfolding less_divide_eq
-            using preal by (auto simp add: power_le_zero_eq zero_le_mult_iff real_of_nat_Suc split: split_if_asm)
-          hence "u t < of_nat (f t ?N) / 2 ^ ?N + 1 / 2 ^ ?N"
-            using preal by (auto simp: field_simps divide_real_def[symmetric])
-          with n[OF `n \<le> ?N`] p preal *[of ?N]
-          show False
-            by (cases "f t ?N") (auto simp: power_le_zero_eq split: split_if_asm)
-        qed
-      next
-        case infinite
-        { fix j have "f t j = j*2^j" using top[of j t] infinite by simp
-          hence "of_nat j \<le> y" using *[of j]
-            by (cases y) (auto simp: power_le_zero_eq zero_le_mult_iff field_simps) }
-        note all_less_y = this
-        show ?thesis unfolding infinite
-        proof (rule ccontr)
-          assume "\<not> \<omega> \<le> y" then obtain r where r: "y = Real r" "0 \<le> r" by (cases y) auto
-          moreover obtain n ::nat where "r < real n" using ex_less_of_nat by (auto simp: real_eq_of_nat)
-          with all_less_y[of n] r show False by auto
-        qed
-      qed
-    qed
+lemma (in sigma_algebra) borel_measurable_implies_simple_function_sequence':
+  fixes u :: "'a \<Rightarrow> extreal"
+  assumes u: "u \<in> borel_measurable M"
+  obtains f where "\<And>i. simple_function M (f i)" "incseq f" "\<And>i. \<infinity> \<notin> range (f i)"
+    "\<And>x. (SUP i. f i x) = max 0 (u x)" "\<And>i x. 0 \<le> f i x"
+  using borel_measurable_implies_simple_function_sequence[OF u] by auto
+
+lemma (in sigma_algebra) simple_function_If_set:
+  assumes sf: "simple_function M f" "simple_function M g" and A: "A \<inter> space M \<in> sets M"
+  shows "simple_function M (\<lambda>x. if x \<in> A then f x else g x)" (is "simple_function M ?IF")
+proof -
+  def F \<equiv> "\<lambda>x. f -` {x} \<inter> space M" and G \<equiv> "\<lambda>x. g -` {x} \<inter> space M"
+  show ?thesis unfolding simple_function_def
+  proof safe
+    have "?IF ` space M \<subseteq> f ` space M \<union> g ` space M" by auto
+    from finite_subset[OF this] assms
+    show "finite (?IF ` space M)" unfolding simple_function_def by auto
+  next
+    fix x assume "x \<in> space M"
+    then have *: "?IF -` {?IF x} \<inter> space M = (if x \<in> A
+      then ((F (f x) \<inter> (A \<inter> space M)) \<union> (G (f x) - (G (f x) \<inter> (A \<inter> space M))))
+      else ((F (g x) \<inter> (A \<inter> space M)) \<union> (G (g x) - (G (g x) \<inter> (A \<inter> space M)))))"
+      using sets_into_space[OF A] by (auto split: split_if_asm simp: G_def F_def)
+    have [intro]: "\<And>x. F x \<in> sets M" "\<And>x. G x \<in> sets M"
+      unfolding F_def G_def using sf[THEN simple_functionD(2)] by auto
+    show "?IF -` {?IF x} \<inter> space M \<in> sets M" unfolding * using A by auto
   qed
 qed
 
-lemma (in sigma_algebra) borel_measurable_implies_simple_function_sequence':
-  fixes u :: "'a \<Rightarrow> pextreal"
-  assumes "u \<in> borel_measurable M"
-  obtains (x) f where "f \<up> u" "\<And>i. simple_function M (f i)" "\<And>i. \<omega>\<notin>f i`space M"
+lemma (in sigma_algebra) simple_function_If:
+  assumes sf: "simple_function M f" "simple_function M g" and P: "{x\<in>space M. P x} \<in> sets M"
+  shows "simple_function M (\<lambda>x. if P x then f x else g x)"
 proof -
-  from borel_measurable_implies_simple_function_sequence[OF assms]
-  obtain f where x: "\<And>i. simple_function M (f i)" "f \<up> u"
-    and fin: "\<And>i. \<And>x. x\<in>space M \<Longrightarrow> f i x \<noteq> \<omega>" by auto
-  { fix i from fin[of _ i] have "\<omega> \<notin> f i`space M" by fastsimp }
-  with x show thesis by (auto intro!: that[of f])
+  have "{x\<in>space M. P x} = {x. P x} \<inter> space M" by auto
+  with simple_function_If_set[OF sf, of "{x. P x}"] P show ?thesis by simp
 qed
 
-lemma (in sigma_algebra) simple_function_eq_borel_measurable:
-  fixes f :: "'a \<Rightarrow> pextreal"
-  shows "simple_function M f \<longleftrightarrow>
-    finite (f`space M) \<and> f \<in> borel_measurable M"
-  using simple_function_borel_measurable[of f]
-    borel_measurable_simple_function[of f]
-  by (fastsimp simp: simple_function_def)
-
 lemma (in measure_space) simple_function_restricted:
-  fixes f :: "'a \<Rightarrow> pextreal" assumes "A \<in> sets M"
+  fixes f :: "'a \<Rightarrow> extreal" assumes "A \<in> sets M"
   shows "simple_function (restricted_space A) f \<longleftrightarrow> simple_function M (\<lambda>x. f x * indicator A x)"
     (is "simple_function ?R f \<longleftrightarrow> simple_function M ?f")
 proof -
   interpret R: sigma_algebra ?R by (rule restricted_sigma_algebra[OF `A \<in> sets M`])
-  have "finite (f`A) \<longleftrightarrow> finite (?f`space M)"
+  have f: "finite (f`A) \<longleftrightarrow> finite (?f`space M)"
   proof cases
     assume "A = space M"
     then have "f`A = ?f`space M" by (fastsimp simp: image_iff)
@@ -456,7 +475,7 @@
         using `A \<in> sets M` sets_into_space by (auto intro!: bexI[of _ x])
     next
       fix x
-      assume "indicator A x \<noteq> (0::pextreal)"
+      assume "indicator A x \<noteq> (0::extreal)"
       then have "x \<in> A" by (auto simp: indicator_def split: split_if_asm)
       moreover assume "x \<in> space M" "\<forall>y\<in>A. ?f x \<noteq> f y"
       ultimately show "f x = 0" by auto
@@ -467,7 +486,8 @@
     unfolding simple_function_eq_borel_measurable
       R.simple_function_eq_borel_measurable
     unfolding borel_measurable_restricted[OF `A \<in> sets M`]
-    by auto
+    using assms(1)[THEN sets_into_space]
+    by (auto simp: indicator_def)
 qed
 
 lemma (in sigma_algebra) simple_function_subalgebra:
@@ -504,7 +524,7 @@
   "integral\<^isup>S M f = (\<Sum>x \<in> f ` space M. x * measure M (f -` {x} \<inter> space M))"
 
 syntax
-  "_simple_integral" :: "'a \<Rightarrow> ('a \<Rightarrow> pextreal) \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> pextreal" ("\<integral>\<^isup>S _. _ \<partial>_" [60,61] 110)
+  "_simple_integral" :: "'a \<Rightarrow> ('a \<Rightarrow> extreal) \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> extreal" ("\<integral>\<^isup>S _. _ \<partial>_" [60,61] 110)
 
 translations
   "\<integral>\<^isup>S x. f \<partial>M" == "CONST integral\<^isup>S M (%x. f)"
@@ -540,7 +560,7 @@
 qed
 
 lemma (in measure_space) simple_function_partition:
-  assumes "simple_function M f" and "simple_function M g"
+  assumes f: "simple_function M f" and g: "simple_function M g"
   shows "integral\<^isup>S M f = (\<Sum>A\<in>(\<lambda>x. f -` {f x} \<inter> g -` {g x} \<inter> space M) ` space M. the_elem (f`A) * \<mu> A)"
     (is "_ = setsum _ (?p ` space M)")
 proof-
@@ -559,23 +579,16 @@
     hence "finite (?p ` (A \<inter> space M))"
       by (rule finite_subset) auto }
   note this[intro, simp]
+  note sets = simple_function_measurable2[OF f g]
 
   { fix x assume "x \<in> space M"
     have "\<Union>(?sub (f x)) = (f -` {f x} \<inter> space M)" by auto
-    moreover {
-      fix x y
-      have *: "f -` {f x} \<inter> g -` {g x} \<inter> space M
-          = (f -` {f x} \<inter> space M) \<inter> (g -` {g x} \<inter> space M)" by auto
-      assume "x \<in> space M" "y \<in> space M"
-      hence "f -` {f x} \<inter> g -` {g x} \<inter> space M \<in> sets M"
-        using assms unfolding simple_function_def * by auto }
-    ultimately
-    have "\<mu> (f -` {f x} \<inter> space M) = setsum (\<mu>) (?sub (f x))"
-      by (subst measure_finitely_additive) auto }
+    with sets have "\<mu> (f -` {f x} \<inter> space M) = setsum \<mu> (?sub (f x))"
+      by (subst measure_Union) auto }
   hence "integral\<^isup>S M f = (\<Sum>(x,A)\<in>?SIGMA. x * \<mu> A)"
-    unfolding simple_integral_def
-    by (subst setsum_Sigma[symmetric],
-       auto intro!: setsum_cong simp: setsum_right_distrib[symmetric])
+    unfolding simple_integral_def using f sets
+    by (subst setsum_Sigma[symmetric])
+       (auto intro!: setsum_cong setsum_extreal_right_distrib)
   also have "\<dots> = (\<Sum>A\<in>?p ` space M. the_elem (f`A) * \<mu> A)"
   proof -
     have [simp]: "\<And>x. x \<in> space M \<Longrightarrow> f ` ?p x = {f x}" by (auto intro!: imageI)
@@ -595,7 +608,7 @@
 qed
 
 lemma (in measure_space) simple_integral_add[simp]:
-  assumes "simple_function M f" and "simple_function M g"
+  assumes f: "simple_function M f" and "\<And>x. 0 \<le> f x" and g: "simple_function M g" and "\<And>x. 0 \<le> g x"
   shows "(\<integral>\<^isup>Sx. f x + g x \<partial>M) = integral\<^isup>S M f + integral\<^isup>S M g"
 proof -
   { fix x let ?S = "g -` {g x} \<inter> f -` {f x} \<inter> space M"
@@ -603,63 +616,43 @@
     hence "(\<lambda>a. f a + g a) ` ?S = {f x + g x}" "f ` ?S = {f x}" "g ` ?S = {g x}"
         "(\<lambda>x. (f x, g x)) -` {(f x, g x)} \<inter> space M = ?S"
       by auto }
-  thus ?thesis
+  with assms show ?thesis
     unfolding
-      simple_function_partition[OF simple_function_add[OF assms] simple_function_Pair[OF assms]]
-      simple_function_partition[OF `simple_function M f` `simple_function M g`]
-      simple_function_partition[OF `simple_function M g` `simple_function M f`]
-    apply (subst (3) Int_commute)
-    by (auto simp add: field_simps setsum_addf[symmetric] intro!: setsum_cong)
+      simple_function_partition[OF simple_function_add[OF f g] simple_function_Pair[OF f g]]
+      simple_function_partition[OF f g]
+      simple_function_partition[OF g f]
+    by (subst (3) Int_commute)
+       (auto simp add: extreal_left_distrib setsum_addf[symmetric] intro!: setsum_cong)
 qed
 
 lemma (in measure_space) simple_integral_setsum[simp]:
+  assumes "\<And>i x. i \<in> P \<Longrightarrow> 0 \<le> f i x"
   assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function M (f i)"
   shows "(\<integral>\<^isup>Sx. (\<Sum>i\<in>P. f i x) \<partial>M) = (\<Sum>i\<in>P. integral\<^isup>S M (f i))"
 proof cases
   assume "finite P"
   from this assms show ?thesis
-    by induct (auto simp: simple_function_setsum simple_integral_add)
+    by induct (auto simp: simple_function_setsum simple_integral_add setsum_nonneg)
 qed auto
 
 lemma (in measure_space) simple_integral_mult[simp]:
-  assumes "simple_function M f"
+  assumes f: "simple_function M f" "\<And>x. 0 \<le> f x"
   shows "(\<integral>\<^isup>Sx. c * f x \<partial>M) = c * integral\<^isup>S M f"
 proof -
-  note mult = simple_function_mult[OF simple_function_const[of c] assms]
+  note mult = simple_function_mult[OF simple_function_const[of c] f(1)]
   { fix x let ?S = "f -` {f x} \<inter> (\<lambda>x. c * f x) -` {c * f x} \<inter> space M"
     assume "x \<in> space M"
     hence "(\<lambda>x. c * f x) ` ?S = {c * f x}" "f ` ?S = {f x}"
       by auto }
-  thus ?thesis
-    unfolding simple_function_partition[OF mult assms]
-      simple_function_partition[OF assms mult]
-    by (auto simp: setsum_right_distrib field_simps intro!: setsum_cong)
-qed
-
-lemma (in sigma_algebra) simple_function_If:
-  assumes sf: "simple_function M f" "simple_function M g" and A: "A \<in> sets M"
-  shows "simple_function M (\<lambda>x. if x \<in> A then f x else g x)" (is "simple_function M ?IF")
-proof -
-  def F \<equiv> "\<lambda>x. f -` {x} \<inter> space M" and G \<equiv> "\<lambda>x. g -` {x} \<inter> space M"
-  show ?thesis unfolding simple_function_def
-  proof safe
-    have "?IF ` space M \<subseteq> f ` space M \<union> g ` space M" by auto
-    from finite_subset[OF this] assms
-    show "finite (?IF ` space M)" unfolding simple_function_def by auto
-  next
-    fix x assume "x \<in> space M"
-    then have *: "?IF -` {?IF x} \<inter> space M = (if x \<in> A
-      then ((F (f x) \<inter> A) \<union> (G (f x) - (G (f x) \<inter> A)))
-      else ((F (g x) \<inter> A) \<union> (G (g x) - (G (g x) \<inter> A))))"
-      using sets_into_space[OF A] by (auto split: split_if_asm simp: G_def F_def)
-    have [intro]: "\<And>x. F x \<in> sets M" "\<And>x. G x \<in> sets M"
-      unfolding F_def G_def using sf[THEN simple_functionD(2)] by auto
-    show "?IF -` {?IF x} \<inter> space M \<in> sets M" unfolding * using A by auto
-  qed
+  with assms show ?thesis
+    unfolding simple_function_partition[OF mult f(1)]
+              simple_function_partition[OF f(1) mult]
+    by (subst setsum_extreal_right_distrib)
+       (auto intro!: extreal_0_le_mult setsum_cong simp: mult_assoc)
 qed
 
 lemma (in measure_space) simple_integral_mono_AE:
-  assumes "simple_function M f" and "simple_function M g"
+  assumes f: "simple_function M f" and g: "simple_function M g"
   and mono: "AE x. f x \<le> g x"
   shows "integral\<^isup>S M f \<le> integral\<^isup>S M g"
 proof -
@@ -668,14 +661,16 @@
     "\<And>x. f -` {f x} \<inter> g -` {g x} \<inter> space M = ?S x" by auto
   show ?thesis
     unfolding *
-      simple_function_partition[OF `simple_function M f` `simple_function M g`]
-      simple_function_partition[OF `simple_function M g` `simple_function M f`]
+      simple_function_partition[OF f g]
+      simple_function_partition[OF g f]
   proof (safe intro!: setsum_mono)
     fix x assume "x \<in> space M"
     then have *: "f ` ?S x = {f x}" "g ` ?S x = {g x}" by auto
     show "the_elem (f`?S x) * \<mu> (?S x) \<le> the_elem (g`?S x) * \<mu> (?S x)"
     proof (cases "f x \<le> g x")
-      case True then show ?thesis using * by (auto intro!: mult_right_mono)
+      case True then show ?thesis
+        using * assms(1,2)[THEN simple_functionD(2)]
+        by (auto intro!: extreal_mult_right_mono)
     next
       case False
       obtain N where N: "{x\<in>space M. \<not> f x \<le> g x} \<subseteq> N" "N \<in> sets M" "\<mu> N = 0"
@@ -685,7 +680,10 @@
         by (rule_tac Int) (auto intro!: simple_functionD)
       ultimately have "\<mu> (?S x) \<le> \<mu> N"
         using `N \<in> sets M` by (auto intro!: measure_mono)
-      then show ?thesis using `\<mu> N = 0` by auto
+      moreover have "0 \<le> \<mu> (?S x)"
+        using assms(1,2)[THEN simple_functionD(2)] by auto
+      ultimately have "\<mu> (?S x) = 0" using `\<mu> N = 0` by auto
+      then show ?thesis by simp
     qed
   qed
 qed
@@ -697,7 +695,8 @@
   using assms by (intro simple_integral_mono_AE) auto
 
 lemma (in measure_space) simple_integral_cong_AE:
-  assumes "simple_function M f" "simple_function M g" and "AE x. f x = g x"
+  assumes "simple_function M f" and "simple_function M g"
+  and "AE x. f x = g x"
   shows "integral\<^isup>S M f = integral\<^isup>S M g"
   using assms by (auto simp: eq_iff intro!: simple_integral_mono_AE)
 
@@ -765,7 +764,7 @@
   assume "space M = {}" hence "A = {}" using sets_into_space[OF assms] by auto
   thus ?thesis unfolding simple_integral_def using `space M = {}` by auto
 next
-  assume "space M \<noteq> {}" hence "(\<lambda>x. 1) ` space M = {1::pextreal}" by auto
+  assume "space M \<noteq> {}" hence "(\<lambda>x. 1) ` space M = {1::extreal}" by auto
   thus ?thesis
     using simple_integral_indicator[OF assms simple_function_const[of 1]]
     using sets_into_space[OF assms]
@@ -773,13 +772,13 @@
 qed
 
 lemma (in measure_space) simple_integral_null_set:
-  assumes "simple_function M u" "N \<in> null_sets"
+  assumes "simple_function M u" "\<And>x. 0 \<le> u x" and "N \<in> null_sets"
   shows "(\<integral>\<^isup>Sx. u x * indicator N x \<partial>M) = 0"
 proof -
-  have "AE x. indicator N x = (0 :: pextreal)"
+  have "AE x. indicator N x = (0 :: extreal)"
     using `N \<in> null_sets` by (auto simp: indicator_def intro!: AE_I[of _ N])
   then have "(\<integral>\<^isup>Sx. u x * indicator N x \<partial>M) = (\<integral>\<^isup>Sx. 0 \<partial>M)"
-    using assms by (intro simple_integral_cong_AE) auto
+    using assms apply (intro simple_integral_cong_AE) by auto
   then show ?thesis by simp
 qed
 
@@ -813,7 +812,7 @@
     by (auto simp: indicator_def split: split_if_asm)
   then show "f x * \<mu> (f -` {f x} \<inter> A) =
     f x * \<mu> (?f -` {f x} \<inter> space M)"
-    unfolding pextreal_mult_cancel_left by auto
+    unfolding extreal_mult_cancel_left by auto
 qed
 
 lemma (in measure_space) simple_integral_subalgebra:
@@ -821,10 +820,6 @@
   shows "integral\<^isup>S N = integral\<^isup>S M"
   unfolding simple_integral_def_raw by simp
 
-lemma measure_preservingD:
-  "T \<in> measure_preserving A B \<Longrightarrow> X \<in> sets B \<Longrightarrow> measure A (T -` X \<inter> space A) = measure B X"
-  unfolding measure_preserving_def by auto
-
 lemma (in measure_space) simple_integral_vimage:
   assumes T: "sigma_algebra M'" "T \<in> measure_preserving M M'"
     and f: "simple_function M' f"
@@ -853,196 +848,164 @@
   qed
 qed
 
+lemma (in measure_space) simple_integral_cmult_indicator:
+  assumes A: "A \<in> sets M"
+  shows "(\<integral>\<^isup>Sx. c * indicator A x \<partial>M) = c * \<mu> A"
+  using simple_integral_mult[OF simple_function_indicator[OF A]]
+  unfolding simple_integral_indicator_only[OF A] by simp
+
+lemma (in measure_space) simple_integral_positive:
+  assumes f: "simple_function M f" and ae: "AE x. 0 \<le> f x"
+  shows "0 \<le> integral\<^isup>S M f"
+proof -
+  have "integral\<^isup>S M (\<lambda>x. 0) \<le> integral\<^isup>S M f"
+    using simple_integral_mono_AE[OF _ f ae] by auto
+  then show ?thesis by simp
+qed
+
 section "Continuous positive integration"
 
 definition positive_integral_def:
-  "integral\<^isup>P M f = (SUP g : {g. simple_function M g \<and> g \<le> f}. integral\<^isup>S M g)"
+  "integral\<^isup>P M f = (SUP g : {g. simple_function M g \<and> g \<le> max 0 \<circ> f}. integral\<^isup>S M g)"
 
 syntax
-  "_positive_integral" :: "'a \<Rightarrow> ('a \<Rightarrow> pextreal) \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> pextreal" ("\<integral>\<^isup>+ _. _ \<partial>_" [60,61] 110)
+  "_positive_integral" :: "'a \<Rightarrow> ('a \<Rightarrow> extreal) \<Rightarrow> ('a, 'b) measure_space_scheme \<Rightarrow> extreal" ("\<integral>\<^isup>+ _. _ \<partial>_" [60,61] 110)
 
 translations
   "\<integral>\<^isup>+ x. f \<partial>M" == "CONST integral\<^isup>P M (%x. f)"
 
-lemma (in measure_space) positive_integral_alt: "integral\<^isup>P M f =
-    (SUP g : {g. simple_function M g \<and> g \<le> f \<and> \<omega> \<notin> g`space M}. integral\<^isup>S M g)"
-  (is "_ = ?alt")
-proof (rule antisym SUP_leI)
-  show "integral\<^isup>P M f \<le> ?alt" unfolding positive_integral_def
-  proof (safe intro!: SUP_leI)
-    fix g assume g: "simple_function M g" "g \<le> f"
-    let ?G = "g -` {\<omega>} \<inter> space M"
-    show "integral\<^isup>S M g \<le>
-      (SUP h : {i. simple_function M i \<and> i \<le> f \<and> \<omega> \<notin> i ` space M}. integral\<^isup>S M h)"
-      (is "integral\<^isup>S M g \<le> SUPR ?A _")
-    proof cases
-      let ?g = "\<lambda>x. indicator (space M - ?G) x * g x"
-      have g': "simple_function M ?g"
-        using g by (auto intro: simple_functionD)
-      moreover
-      assume "\<mu> ?G = 0"
-      then have "AE x. g x = ?g x" using g
-        by (intro AE_I[where N="?G"])
-           (auto intro: simple_functionD simp: indicator_def)
-      with g(1) g' have "integral\<^isup>S M g = integral\<^isup>S M ?g"
-        by (rule simple_integral_cong_AE)
-      moreover have "?g \<le> g" by (auto simp: le_fun_def indicator_def)
-      from this `g \<le> f` have "?g \<le> f" by (rule order_trans)
-      moreover have "\<omega> \<notin> ?g ` space M"
-        by (auto simp: indicator_def split: split_if_asm)
-      ultimately show ?thesis by (auto intro!: le_SUPI)
-    next
-      assume "\<mu> ?G \<noteq> 0"
-      then have "?G \<noteq> {}" by auto
-      then have "\<omega> \<in> g`space M" by force
-      then have "space M \<noteq> {}" by auto
-      have "SUPR ?A (integral\<^isup>S M) = \<omega>"
-      proof (intro SUP_\<omega>[THEN iffD2] allI impI)
-        fix x assume "x < \<omega>"
-        then guess n unfolding less_\<omega>_Ex_of_nat .. note n = this
-        then have "0 < n" by (intro neq0_conv[THEN iffD1] notI) simp
-        let ?g = "\<lambda>x. (of_nat n / (if \<mu> ?G = \<omega> then 1 else \<mu> ?G)) * indicator ?G x"
-        show "\<exists>i\<in>?A. x < integral\<^isup>S M i"
-        proof (intro bexI impI CollectI conjI)
-          show "simple_function M ?g" using g
-            by (auto intro!: simple_functionD simple_function_add)
-          have "?g \<le> g" by (auto simp: le_fun_def indicator_def)
-          from this g(2) show "?g \<le> f" by (rule order_trans)
-          show "\<omega> \<notin> ?g ` space M"
-            using `\<mu> ?G \<noteq> 0` by (auto simp: indicator_def split: split_if_asm)
-          have "x < (of_nat n / (if \<mu> ?G = \<omega> then 1 else \<mu> ?G)) * \<mu> ?G"
-            using n `\<mu> ?G \<noteq> 0` `0 < n`
-            by (auto simp: pextreal_noteq_omega_Ex field_simps)
-          also have "\<dots> = integral\<^isup>S M ?g" using g `space M \<noteq> {}`
-            by (subst simple_integral_indicator)
-               (auto simp: image_constant ac_simps dest: simple_functionD)
-          finally show "x < integral\<^isup>S M ?g" .
-        qed
-      qed
-      then show ?thesis by simp
-    qed
-  qed
-qed (auto intro!: SUP_subset simp: positive_integral_def)
-
 lemma (in measure_space) positive_integral_cong_measure:
   assumes "\<And>A. A \<in> sets M \<Longrightarrow> measure N A = \<mu> A" "sets N = sets M" "space N = space M"
   shows "integral\<^isup>P N f = integral\<^isup>P M f"
-proof -
-  interpret v: measure_space N
-    by (rule measure_space_cong) fact+
-  with assms show ?thesis
-    unfolding positive_integral_def SUPR_def
-    by (auto intro!: arg_cong[where f=Sup] image_cong
-             simp: simple_integral_cong_measure[OF assms]
-                   simple_function_cong_algebra[OF assms(2,3)])
-qed
+  unfolding positive_integral_def
+  unfolding simple_function_cong_algebra[OF assms(2,3), symmetric]
+  using AE_cong_measure[OF assms]
+  using simple_integral_cong_measure[OF assms]
+  by (auto intro!: SUP_cong)
+
+lemma (in measure_space) positive_integral_positive:
+  "0 \<le> integral\<^isup>P M f"
+  by (auto intro!: le_SUPI2[of "\<lambda>x. 0"] simp: positive_integral_def le_fun_def)
 
-lemma (in measure_space) positive_integral_alt1:
-  "integral\<^isup>P M f =
-    (SUP g : {g. simple_function M g \<and> (\<forall>x\<in>space M. g x \<le> f x \<and> g x \<noteq> \<omega>)}. integral\<^isup>S M g)"
-  unfolding positive_integral_alt SUPR_def
-proof (safe intro!: arg_cong[where f=Sup])
-  fix g let ?g = "\<lambda>x. if x \<in> space M then g x else f x"
-  assume "simple_function M g" "\<forall>x\<in>space M. g x \<le> f x \<and> g x \<noteq> \<omega>"
-  hence "?g \<le> f" "simple_function M ?g" "integral\<^isup>S M g = integral\<^isup>S M ?g"
-    "\<omega> \<notin> g`space M"
-    unfolding le_fun_def by (auto cong: simple_function_cong simple_integral_cong)
-  thus "integral\<^isup>S M g \<in> integral\<^isup>S M ` {g. simple_function M g \<and> g \<le> f \<and> \<omega> \<notin> g`space M}"
-    by auto
-next
-  fix g assume "simple_function M g" "g \<le> f" "\<omega> \<notin> g`space M"
-  hence "simple_function M g" "\<forall>x\<in>space M. g x \<le> f x \<and> g x \<noteq> \<omega>"
-    by (auto simp add: le_fun_def image_iff)
-  thus "integral\<^isup>S M g \<in> integral\<^isup>S M ` {g. simple_function M g \<and> (\<forall>x\<in>space M. g x \<le> f x \<and> g x \<noteq> \<omega>)}"
-    by auto
-qed
+lemma (in measure_space) positive_integral_def_finite:
+  "integral\<^isup>P M f = (SUP g : {g. simple_function M g \<and> g \<le> max 0 \<circ> f \<and> range g \<subseteq> {0 ..< \<infinity>}}. integral\<^isup>S M g)"
+    (is "_ = SUPR ?A ?f")
+  unfolding positive_integral_def
+proof (safe intro!: antisym SUP_leI)
+  fix g assume g: "simple_function M g" "g \<le> max 0 \<circ> f"
+  let ?G = "{x \<in> space M. \<not> g x \<noteq> \<infinity>}"
+  note gM = g(1)[THEN borel_measurable_simple_function]
+  have \<mu>G_pos: "0 \<le> \<mu> ?G" using gM by auto
+  let "?g y x" = "if g x = \<infinity> then y else max 0 (g x)"
+  from g gM have g_in_A: "\<And>y. 0 \<le> y \<Longrightarrow> y \<noteq> \<infinity> \<Longrightarrow> ?g y \<in> ?A"
+    apply (safe intro!: simple_function_max simple_function_If)
+    apply (force simp: max_def le_fun_def split: split_if_asm)+
+    done
+  show "integral\<^isup>S M g \<le> SUPR ?A ?f"
+  proof cases
+    have g0: "?g 0 \<in> ?A" by (intro g_in_A) auto
+    assume "\<mu> ?G = 0"
+    with gM have "AE x. x \<notin> ?G" by (simp add: AE_iff_null_set)
+    with gM g show ?thesis
+      by (intro le_SUPI2[OF g0] simple_integral_mono_AE)
+         (auto simp: max_def intro!: simple_function_If)
+  next
+    assume \<mu>G: "\<mu> ?G \<noteq> 0"
+    have "SUPR ?A (integral\<^isup>S M) = \<infinity>"
+    proof (intro SUP_PInfty)
+      fix n :: nat
+      let ?y = "extreal (real n) / (if \<mu> ?G = \<infinity> then 1 else \<mu> ?G)"
+      have "0 \<le> ?y" "?y \<noteq> \<infinity>" using \<mu>G \<mu>G_pos by (auto simp: extreal_divide_eq)
+      then have "?g ?y \<in> ?A" by (rule g_in_A)
+      have "real n \<le> ?y * \<mu> ?G"
+        using \<mu>G \<mu>G_pos by (cases "\<mu> ?G") (auto simp: field_simps)
+      also have "\<dots> = (\<integral>\<^isup>Sx. ?y * indicator ?G x \<partial>M)"
+        using `0 \<le> ?y` `?g ?y \<in> ?A` gM
+        by (subst simple_integral_cmult_indicator) auto
+      also have "\<dots> \<le> integral\<^isup>S M (?g ?y)" using `?g ?y \<in> ?A` gM
+        by (intro simple_integral_mono) auto
+      finally show "\<exists>i\<in>?A. real n \<le> integral\<^isup>S M i"
+        using `?g ?y \<in> ?A` by blast
+    qed
+    then show ?thesis by simp
+  qed
+qed (auto intro: le_SUPI)
 
-lemma (in measure_space) positive_integral_cong:
-  assumes "\<And>x. x \<in> space M \<Longrightarrow> f x = g x"
-  shows "integral\<^isup>P M f = integral\<^isup>P M g"
-proof -
-  have "\<And>h. (\<forall>x\<in>space M. h x \<le> f x \<and> h x \<noteq> \<omega>) = (\<forall>x\<in>space M. h x \<le> g x \<and> h x \<noteq> \<omega>)"
-    using assms by auto
-  thus ?thesis unfolding positive_integral_alt1 by auto
+lemma (in measure_space) positive_integral_mono_AE:
+  assumes ae: "AE x. u x \<le> v x" shows "integral\<^isup>P M u \<le> integral\<^isup>P M v"
+  unfolding positive_integral_def
+proof (safe intro!: SUP_mono)
+  fix n assume n: "simple_function M n" "n \<le> max 0 \<circ> u"
+  from ae[THEN AE_E] guess N . note N = this
+  then have ae_N: "AE x. x \<notin> N" by (auto intro: AE_not_in)
+  let "?n x" = "n x * indicator (space M - N) x"
+  have "AE x. n x \<le> ?n x" "simple_function M ?n"
+    using n N ae_N by auto
+  moreover
+  { fix x have "?n x \<le> max 0 (v x)"
+    proof cases
+      assume x: "x \<in> space M - N"
+      with N have "u x \<le> v x" by auto
+      with n(2)[THEN le_funD, of x] x show ?thesis
+        by (auto simp: max_def split: split_if_asm)
+    qed simp }
+  then have "?n \<le> max 0 \<circ> v" by (auto simp: le_funI)
+  moreover have "integral\<^isup>S M n \<le> integral\<^isup>S M ?n"
+    using ae_N N n by (auto intro!: simple_integral_mono_AE)
+  ultimately show "\<exists>m\<in>{g. simple_function M g \<and> g \<le> max 0 \<circ> v}. integral\<^isup>S M n \<le> integral\<^isup>S M m"
+    by force
 qed
 
-lemma (in measure_space) positive_integral_eq_simple_integral:
-  assumes "simple_function M f"
-  shows "integral\<^isup>P M f = integral\<^isup>S M f"
-  unfolding positive_integral_def
-proof (safe intro!: pextreal_SUPI)
-  fix g assume "simple_function M g" "g \<le> f"
-  with assms show "integral\<^isup>S M g \<le> integral\<^isup>S M f"
-    by (auto intro!: simple_integral_mono simp: le_fun_def)
-next
-  fix y assume "\<forall>x. x\<in>{g. simple_function M g \<and> g \<le> f} \<longrightarrow> integral\<^isup>S M x \<le> y"
-  with assms show "integral\<^isup>S M f \<le> y" by auto
-qed
-
-lemma (in measure_space) positive_integral_mono_AE:
-  assumes ae: "AE x. u x \<le> v x"
-  shows "integral\<^isup>P M u \<le> integral\<^isup>P M v"
-  unfolding positive_integral_alt1
-proof (safe intro!: SUPR_mono)
-  fix a assume a: "simple_function M a" and mono: "\<forall>x\<in>space M. a x \<le> u x \<and> a x \<noteq> \<omega>"
-  from ae obtain N where N: "{x\<in>space M. \<not> u x \<le> v x} \<subseteq> N" "N \<in> sets M" "\<mu> N = 0"
-    by (auto elim!: AE_E)
-  have "simple_function M (\<lambda>x. a x * indicator (space M - N) x)"
-    using `N \<in> sets M` a by auto
-  with a show "\<exists>b\<in>{g. simple_function M g \<and> (\<forall>x\<in>space M. g x \<le> v x \<and> g x \<noteq> \<omega>)}.
-    integral\<^isup>S M a \<le> integral\<^isup>S M b"
-  proof (safe intro!: bexI[of _ "\<lambda>x. a x * indicator (space M - N) x"]
-                      simple_integral_mono_AE)
-    show "AE x. a x \<le> a x * indicator (space M - N) x"
-    proof (rule AE_I, rule subset_refl)
-      have *: "{x \<in> space M. \<not> a x \<le> a x * indicator (space M - N) x} =
-        N \<inter> {x \<in> space M. a x \<noteq> 0}" (is "?N = _")
-        using `N \<in> sets M`[THEN sets_into_space] by (auto simp: indicator_def)
-      then show "?N \<in> sets M"
-        using `N \<in> sets M` `simple_function M a`[THEN borel_measurable_simple_function]
-        by (auto intro!: measure_mono Int)
-      then have "\<mu> ?N \<le> \<mu> N"
-        unfolding * using `N \<in> sets M` by (auto intro!: measure_mono)
-      then show "\<mu> ?N = 0" using `\<mu> N = 0` by auto
-    qed
-  next
-    fix x assume "x \<in> space M"
-    show "a x * indicator (space M - N) x \<le> v x"
-    proof (cases "x \<in> N")
-      case True then show ?thesis by simp
-    next
-      case False
-      with N mono have "a x \<le> u x" "u x \<le> v x" using `x \<in> space M` by auto
-      with False `x \<in> space M` show "a x * indicator (space M - N) x \<le> v x" by auto
-    qed
-    assume "a x * indicator (space M - N) x = \<omega>"
-    with mono `x \<in> space M` show False
-      by (simp split: split_if_asm add: indicator_def)
-  qed
-qed
+lemma (in measure_space) positive_integral_mono:
+  "(\<And>x. x \<in> space M \<Longrightarrow> u x \<le> v x) \<Longrightarrow> integral\<^isup>P M u \<le> integral\<^isup>P M v"
+  by (auto intro: positive_integral_mono_AE)
 
 lemma (in measure_space) positive_integral_cong_AE:
   "AE x. u x = v x \<Longrightarrow> integral\<^isup>P M u = integral\<^isup>P M v"
   by (auto simp: eq_iff intro!: positive_integral_mono_AE)
 
-lemma (in measure_space) positive_integral_mono:
-  "(\<And>x. x \<in> space M \<Longrightarrow> u x \<le> v x) \<Longrightarrow> integral\<^isup>P M u \<le> integral\<^isup>P M v"
-  by (auto intro: positive_integral_mono_AE)
+lemma (in measure_space) positive_integral_cong:
+  "(\<And>x. x \<in> space M \<Longrightarrow> u x = v x) \<Longrightarrow> integral\<^isup>P M u = integral\<^isup>P M v"
+  by (auto intro: positive_integral_cong_AE)
 
-lemma image_set_cong:
-  assumes A: "\<And>x. x \<in> A \<Longrightarrow> \<exists>y\<in>B. f x = g y"
-  assumes B: "\<And>y. y \<in> B \<Longrightarrow> \<exists>x\<in>A. g y = f x"
-  shows "f ` A = g ` B"
-  using assms by blast
+lemma (in measure_space) positive_integral_eq_simple_integral:
+  assumes f: "simple_function M f" "\<And>x. 0 \<le> f x" shows "integral\<^isup>P M f = integral\<^isup>S M f"
+proof -
+  let "?f x" = "f x * indicator (space M) x"
+  have f': "simple_function M ?f" using f by auto
+  with f(2) have [simp]: "max 0 \<circ> ?f = ?f"
+    by (auto simp: fun_eq_iff max_def split: split_indicator)
+  have "integral\<^isup>P M ?f \<le> integral\<^isup>S M ?f" using f'
+    by (force intro!: SUP_leI simple_integral_mono simp: le_fun_def positive_integral_def)
+  moreover have "integral\<^isup>S M ?f \<le> integral\<^isup>P M ?f"
+    unfolding positive_integral_def
+    using f' by (auto intro!: le_SUPI)
+  ultimately show ?thesis
+    by (simp cong: positive_integral_cong simple_integral_cong)
+qed
+
+lemma (in measure_space) positive_integral_eq_simple_integral_AE:
+  assumes f: "simple_function M f" "AE x. 0 \<le> f x" shows "integral\<^isup>P M f = integral\<^isup>S M f"
+proof -
+  have "AE x. f x = max 0 (f x)" using f by (auto split: split_max)
+  with f have "integral\<^isup>P M f = integral\<^isup>S M (\<lambda>x. max 0 (f x))"
+    by (simp cong: positive_integral_cong_AE simple_integral_cong_AE
+             add: positive_integral_eq_simple_integral)
+  with assms show ?thesis
+    by (auto intro!: simple_integral_cong_AE split: split_max)
+qed
 
 lemma (in measure_space) positive_integral_SUP_approx:
-  assumes "f \<up> s"
-  and f: "\<And>i. f i \<in> borel_measurable M"
-  and "simple_function M u"
-  and le: "u \<le> s" and real: "\<omega> \<notin> u`space M"
+  assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
+  and u: "simple_function M u" "u \<le> (SUP i. f i)" "u`space M \<subseteq> {0..<\<infinity>}"
   shows "integral\<^isup>S M u \<le> (SUP i. integral\<^isup>P M (f i))" (is "_ \<le> ?S")
-proof (rule pextreal_le_mult_one_interval)
-  fix a :: pextreal assume "0 < a" "a < 1"
+proof (rule extreal_le_mult_one_interval)
+  have "0 \<le> (SUP i. integral\<^isup>P M (f i))"
+    using f(3) by (auto intro!: le_SUPI2 positive_integral_positive)
+  then show "(SUP i. integral\<^isup>P M (f i)) \<noteq> -\<infinity>" by auto
+  have u_range: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> u x \<and> u x \<noteq> \<infinity>"
+    using u(3) by auto
+  fix a :: extreal assume "0 < a" "a < 1"
   hence "a \<noteq> 0" by auto
   let "?B i" = "{x \<in> space M. a * u x \<le> f i x}"
   have B: "\<And>i. ?B i \<in> sets M"
@@ -1054,203 +1017,269 @@
     proof safe
       fix i x assume "a * u x \<le> f i x"
       also have "\<dots> \<le> f (Suc i) x"
-        using `f \<up> s` unfolding isoton_def le_fun_def by auto
+        using `incseq f`[THEN incseq_SucD] unfolding le_fun_def by auto
       finally show "a * u x \<le> f (Suc i) x" .
     qed }
   note B_mono = this
 
-  have u: "\<And>x. x \<in> space M \<Longrightarrow> u -` {u x} \<inter> space M \<in> sets M"
-    using `simple_function M u` by (auto simp add: simple_function_def)
+  note B_u = Int[OF u(1)[THEN simple_functionD(2)] B]
 
-  have "\<And>i. (\<lambda>n. (u -` {i} \<inter> space M) \<inter> ?B n) \<up> (u -` {i} \<inter> space M)" using B_mono unfolding isoton_def
-  proof safe
-    fix x i assume "x \<in> space M"
-    show "x \<in> (\<Union>i. (u -` {u x} \<inter> space M) \<inter> ?B i)"
-    proof cases
-      assume "u x = 0" thus ?thesis using `x \<in> space M` by simp
-    next
-      assume "u x \<noteq> 0"
-      with `a < 1` real `x \<in> space M`
-      have "a * u x < 1 * u x" by (rule_tac pextreal_mult_strict_right_mono) (auto simp: image_iff)
-      also have "\<dots> \<le> (SUP i. f i x)" using le `f \<up> s`
-        unfolding isoton_fun_expand by (auto simp: isoton_def le_fun_def)
-      finally obtain i where "a * u x < f i x" unfolding SUPR_def
-        by (auto simp add: less_Sup_iff)
-      hence "a * u x \<le> f i x" by auto
-      thus ?thesis using `x \<in> space M` by auto
+  let "?B' i n" = "(u -` {i} \<inter> space M) \<inter> ?B n"
+  have measure_conv: "\<And>i. \<mu> (u -` {i} \<inter> space M) = (SUP n. \<mu> (?B' i n))"
+  proof -
+    fix i
+    have 1: "range (?B' i) \<subseteq> sets M" using B_u by auto
+    have 2: "incseq (?B' i)" using B_mono by (auto intro!: incseq_SucI)
+    have "(\<Union>n. ?B' i n) = u -` {i} \<inter> space M"
+    proof safe
+      fix x i assume x: "x \<in> space M"
+      show "x \<in> (\<Union>i. ?B' (u x) i)"
+      proof cases
+        assume "u x = 0" thus ?thesis using `x \<in> space M` f(3) by simp
+      next
+        assume "u x \<noteq> 0"
+        with `a < 1` u_range[OF `x \<in> space M`]
+        have "a * u x < 1 * u x"
+          by (intro extreal_mult_strict_right_mono) (auto simp: image_iff)
+        also have "\<dots> \<le> (SUP i. f i x)" using u(2) by (auto simp: le_fun_def SUPR_apply)
+        finally obtain i where "a * u x < f i x" unfolding SUPR_def
+          by (auto simp add: less_Sup_iff)
+        hence "a * u x \<le> f i x" by auto
+        thus ?thesis using `x \<in> space M` by auto
+      qed
     qed
-  qed auto
-  note measure_conv = measure_up[OF Int[OF u B] this]
+    then show "?thesis i" using continuity_from_below[OF 1 2] by simp
+  qed
 
   have "integral\<^isup>S M u = (SUP i. integral\<^isup>S M (?uB i))"
     unfolding simple_integral_indicator[OF B `simple_function M u`]
-  proof (subst SUPR_pextreal_setsum, safe)
+  proof (subst SUPR_extreal_setsum, safe)
     fix x n assume "x \<in> space M"
-    have "\<mu> (u -` {u x} \<inter> space M \<inter> {x \<in> space M. a * u x \<le> f n x})
-      \<le> \<mu> (u -` {u x} \<inter> space M \<inter> {x \<in> space M. a * u x \<le> f (Suc n) x})"
-      using B_mono Int[OF u[OF `x \<in> space M`] B] by (auto intro!: measure_mono)
-    thus "u x * \<mu> (u -` {u x} \<inter> space M \<inter> ?B n)
-            \<le> u x * \<mu> (u -` {u x} \<inter> space M \<inter> ?B (Suc n))"
-      by (auto intro: mult_left_mono)
+    with u_range show "incseq (\<lambda>i. u x * \<mu> (?B' (u x) i))" "\<And>i. 0 \<le> u x * \<mu> (?B' (u x) i)"
+      using B_mono B_u by (auto intro!: measure_mono extreal_mult_left_mono incseq_SucI simp: extreal_zero_le_0_iff)
   next
-    show "integral\<^isup>S M u =
-      (\<Sum>i\<in>u ` space M. SUP n. i * \<mu> (u -` {i} \<inter> space M \<inter> ?B n))"
-      using measure_conv unfolding simple_integral_def isoton_def
-      by (auto intro!: setsum_cong simp: pextreal_SUP_cmult)
+    show "integral\<^isup>S M u = (\<Sum>i\<in>u ` space M. SUP n. i * \<mu> (?B' i n))"
+      using measure_conv u_range B_u unfolding simple_integral_def
+      by (auto intro!: setsum_cong SUPR_extreal_cmult[symmetric])
   qed
   moreover
   have "a * (SUP i. integral\<^isup>S M (?uB i)) \<le> ?S"
-    unfolding pextreal_SUP_cmult[symmetric]
+    apply (subst SUPR_extreal_cmult[symmetric])
   proof (safe intro!: SUP_mono bexI)
     fix i
     have "a * integral\<^isup>S M (?uB i) = (\<integral>\<^isup>Sx. a * ?uB i x \<partial>M)"
-      using B `simple_function M u`
-      by (subst simple_integral_mult[symmetric]) (auto simp: field_simps)
+      using B `simple_function M u` u_range
+      by (subst simple_integral_mult) (auto split: split_indicator)
     also have "\<dots> \<le> integral\<^isup>P M (f i)"
     proof -
-      have "\<And>x. a * ?uB i x \<le> f i x" unfolding indicator_def by auto
-      hence *: "simple_function M (\<lambda>x. a * ?uB i x)" using B assms(3)
-        by (auto intro!: simple_integral_mono)
-      show ?thesis unfolding positive_integral_eq_simple_integral[OF *, symmetric]
-        by (auto intro!: positive_integral_mono simp: indicator_def)
+      have *: "simple_function M (\<lambda>x. a * ?uB i x)" using B `0 < a` u(1) by auto
+      show ?thesis using f(3) * u_range `0 < a`
+        by (subst positive_integral_eq_simple_integral[symmetric])
+           (auto intro!: positive_integral_mono split: split_indicator)
     qed
     finally show "a * integral\<^isup>S M (?uB i) \<le> integral\<^isup>P M (f i)"
       by auto
-  qed simp
+  next
+    fix i show "0 \<le> \<integral>\<^isup>S x. ?uB i x \<partial>M" using B `0 < a` u(1) u_range
+      by (intro simple_integral_positive) (auto split: split_indicator)
+  qed (insert `0 < a`, auto)
   ultimately show "a * integral\<^isup>S M u \<le> ?S" by simp
 qed
 
+lemma (in measure_space) incseq_positive_integral:
+  assumes "incseq f" shows "incseq (\<lambda>i. integral\<^isup>P M (f i))"
+proof -
+  have "\<And>i x. f i x \<le> f (Suc i) x"
+    using assms by (auto dest!: incseq_SucD simp: le_fun_def)
+  then show ?thesis
+    by (auto intro!: incseq_SucI positive_integral_mono)
+qed
+
 text {* Beppo-Levi monotone convergence theorem *}
-lemma (in measure_space) positive_integral_isoton:
-  assumes "f \<up> u" "\<And>i. f i \<in> borel_measurable M"
-  shows "(\<lambda>i. integral\<^isup>P M (f i)) \<up> integral\<^isup>P M u"
-  unfolding isoton_def
-proof safe
-  fix i show "integral\<^isup>P M (f i) \<le> integral\<^isup>P M (f (Suc i))"
-    apply (rule positive_integral_mono)
-    using `f \<up> u` unfolding isoton_def le_fun_def by auto
+lemma (in measure_space) positive_integral_monotone_convergence_SUP:
+  assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
+  shows "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^isup>P M (f i))"
+proof (rule antisym)
+  show "(SUP j. integral\<^isup>P M (f j)) \<le> (\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M)"
+    by (auto intro!: SUP_leI le_SUPI positive_integral_mono)
 next
-  have u: "u = (SUP i. f i)" using `f \<up> u` unfolding isoton_def by auto
-  show "(SUP i. integral\<^isup>P M (f i)) = integral\<^isup>P M u"
-  proof (rule antisym)
-    from `f \<up> u`[THEN isoton_Sup, unfolded le_fun_def]
-    show "(SUP j. integral\<^isup>P M (f j)) \<le> integral\<^isup>P M u"
-      by (auto intro!: SUP_leI positive_integral_mono)
-  next
-    show "integral\<^isup>P M u \<le> (SUP i. integral\<^isup>P M (f i))"
-      unfolding positive_integral_alt[of u]
-      by (auto intro!: SUP_leI positive_integral_SUP_approx[OF assms])
+  show "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) \<le> (SUP j. integral\<^isup>P M (f j))"
+    unfolding positive_integral_def_finite[of "\<lambda>x. SUP i. f i x"]
+  proof (safe intro!: SUP_leI)
+    fix g assume g: "simple_function M g"
+      and "g \<le> max 0 \<circ> (\<lambda>x. SUP i. f i x)" "range g \<subseteq> {0..<\<infinity>}"
+    moreover then have "\<And>x. 0 \<le> (SUP i. f i x)" and g': "g`space M \<subseteq> {0..<\<infinity>}"
+      using f by (auto intro!: le_SUPI2)
+    ultimately show "integral\<^isup>S M g \<le> (SUP j. integral\<^isup>P M (f j))"
+      by (intro  positive_integral_SUP_approx[OF f g _ g'])
+         (auto simp: le_fun_def max_def SUPR_apply)
   qed
 qed
 
-lemma (in measure_space) positive_integral_monotone_convergence_SUP:
-  assumes "\<And>i x. x \<in> space M \<Longrightarrow> f i x \<le> f (Suc i) x"
-  assumes "\<And>i. f i \<in> borel_measurable M"
-  shows "(SUP i. integral\<^isup>P M (f i)) = (\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M)"
-    (is "_ = integral\<^isup>P M ?u")
+lemma (in measure_space) positive_integral_monotone_convergence_SUP_AE:
+  assumes f: "\<And>i. AE x. f i x \<le> f (Suc i) x \<and> 0 \<le> f i x" "\<And>i. f i \<in> borel_measurable M"
+  shows "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^isup>P M (f i))"
 proof -
-  show ?thesis
-  proof (rule antisym)
-    show "(SUP j. integral\<^isup>P M (f j)) \<le> integral\<^isup>P M ?u"
-      by (auto intro!: SUP_leI positive_integral_mono le_SUPI)
-  next
-    def rf \<equiv> "\<lambda>i. \<lambda>x\<in>space M. f i x" and ru \<equiv> "\<lambda>x\<in>space M. ?u x"
-    have "\<And>i. rf i \<in> borel_measurable M" unfolding rf_def
-      using assms by (simp cong: measurable_cong)
-    moreover have iso: "rf \<up> ru" using assms unfolding rf_def ru_def
-      unfolding isoton_def le_fun_def fun_eq_iff SUPR_apply
-      using SUP_const[OF UNIV_not_empty]
-      by (auto simp: restrict_def le_fun_def fun_eq_iff)
-    ultimately have "integral\<^isup>P M ru \<le> (SUP i. integral\<^isup>P M (rf i))"
-      unfolding positive_integral_alt[of ru]
-      by (auto simp: le_fun_def intro!: SUP_leI positive_integral_SUP_approx)
-    then show "integral\<^isup>P M ?u \<le> (SUP i. integral\<^isup>P M (f i))"
-      unfolding ru_def rf_def by (simp cong: positive_integral_cong)
+  from f have "AE x. \<forall>i. f i x \<le> f (Suc i) x \<and> 0 \<le> f i x"
+    by (simp add: AE_all_countable)
+  from this[THEN AE_E] guess N . note N = this
+  let "?f i x" = "if x \<in> space M - N then f i x else 0"
+  have f_eq: "AE x. \<forall>i. ?f i x = f i x" using N by (auto intro!: AE_I[of _ N])
+  then have "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (\<integral>\<^isup>+ x. (SUP i. ?f i x) \<partial>M)"
+    by (auto intro!: positive_integral_cong_AE)
+  also have "\<dots> = (SUP i. (\<integral>\<^isup>+ x. ?f i x \<partial>M))"
+  proof (rule positive_integral_monotone_convergence_SUP)
+    show "incseq ?f" using N(1) by (force intro!: incseq_SucI le_funI)
+    { fix i show "(\<lambda>x. if x \<in> space M - N then f i x else 0) \<in> borel_measurable M"
+        using f N(3) by (intro measurable_If_set) auto
+      fix x show "0 \<le> ?f i x"
+        using N(1) by auto }
   qed
+  also have "\<dots> = (SUP i. (\<integral>\<^isup>+ x. f i x \<partial>M))"
+    using f_eq by (force intro!: arg_cong[where f="SUPR UNIV"] positive_integral_cong_AE ext)
+  finally show ?thesis .
+qed
+
+lemma (in measure_space) positive_integral_monotone_convergence_SUP_AE_incseq:
+  assumes f: "incseq f" "\<And>i. AE x. 0 \<le> f i x" and borel: "\<And>i. f i \<in> borel_measurable M"
+  shows "(\<integral>\<^isup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^isup>P M (f i))"
+  using f[unfolded incseq_Suc_iff le_fun_def]
+  by (intro positive_integral_monotone_convergence_SUP_AE[OF _ borel])
+     auto
+
+lemma (in measure_space) positive_integral_monotone_convergence_simple:
+  assumes f: "incseq f" "\<And>i x. 0 \<le> f i x" "\<And>i. simple_function M (f i)"
+  shows "(SUP i. integral\<^isup>S M (f i)) = (\<integral>\<^isup>+x. (SUP i. f i x) \<partial>M)"
+  using assms unfolding positive_integral_monotone_convergence_SUP[OF f(1)
+    f(3)[THEN borel_measurable_simple_function] f(2)]
+  by (auto intro!: positive_integral_eq_simple_integral[symmetric] arg_cong[where f="SUPR UNIV"] ext)
+
+lemma positive_integral_max_0:
+  "(\<integral>\<^isup>+x. max 0 (f x) \<partial>M) = integral\<^isup>P M f"
+  by (simp add: le_fun_def positive_integral_def)
+
+lemma (in measure_space) positive_integral_cong_pos:
+  assumes "\<And>x. x \<in> space M \<Longrightarrow> f x \<le> 0 \<and> g x \<le> 0 \<or> f x = g x"
+  shows "integral\<^isup>P M f = integral\<^isup>P M g"
+proof -
+  have "integral\<^isup>P M (\<lambda>x. max 0 (f x)) = integral\<^isup>P M (\<lambda>x. max 0 (g x))"
+  proof (intro positive_integral_cong)
+    fix x assume "x \<in> space M"
+    from assms[OF this] show "max 0 (f x) = max 0 (g x)"
+      by (auto split: split_max)
+  qed
+  then show ?thesis by (simp add: positive_integral_max_0)
 qed
 
 lemma (in measure_space) SUP_simple_integral_sequences:
-  assumes f: "f \<up> u" "\<And>i. simple_function M (f i)"
-  and g: "g \<up> u" "\<And>i. simple_function M (g i)"
+  assumes f: "incseq f" "\<And>i x. 0 \<le> f i x" "\<And>i. simple_function M (f i)"
+  and g: "incseq g" "\<And>i x. 0 \<le> g i x" "\<And>i. simple_function M (g i)"
+  and eq: "AE x. (SUP i. f i x) = (SUP i. g i x)"
   shows "(SUP i. integral\<^isup>S M (f i)) = (SUP i. integral\<^isup>S M (g i))"
     (is "SUPR _ ?F = SUPR _ ?G")
 proof -
-  have "(SUP i. ?F i) = (SUP i. integral\<^isup>P M (f i))"
-    using assms by (simp add: positive_integral_eq_simple_integral)
-  also have "\<dots> = integral\<^isup>P M u"
-    using positive_integral_isoton[OF f(1) borel_measurable_simple_function[OF f(2)]]
-    unfolding isoton_def by simp
-  also have "\<dots> = (SUP i. integral\<^isup>P M (g i))"
-    using positive_integral_isoton[OF g(1) borel_measurable_simple_function[OF g(2)]]
-    unfolding isoton_def by simp
+  have "(SUP i. integral\<^isup>S M (f i)) = (\<integral>\<^isup>+x. (SUP i. f i x) \<partial>M)"
+    using f by (rule positive_integral_monotone_convergence_simple)
+  also have "\<dots> = (\<integral>\<^isup>+x. (SUP i. g i x) \<partial>M)"
+    unfolding eq[THEN positive_integral_cong_AE] ..
   also have "\<dots> = (SUP i. ?G i)"
-    using assms by (simp add: positive_integral_eq_simple_integral)
-  finally show ?thesis .
+    using g by (rule positive_integral_monotone_convergence_simple[symmetric])
+  finally show ?thesis by simp
 qed
 
 lemma (in measure_space) positive_integral_const[simp]:
-  "(\<integral>\<^isup>+ x. c \<partial>M) = c * \<mu> (space M)"
+  "0 \<le> c \<Longrightarrow> (\<integral>\<^isup>+ x. c \<partial>M) = c * \<mu> (space M)"
   by (subst positive_integral_eq_simple_integral) auto
 
-lemma (in measure_space) positive_integral_isoton_simple:
-  assumes "f \<up> u" and e: "\<And>i. simple_function M (f i)"
-  shows "(\<lambda>i. integral\<^isup>S M (f i)) \<up> integral\<^isup>P M u"
-  using positive_integral_isoton[OF `f \<up> u` e(1)[THEN borel_measurable_simple_function]]
-  unfolding positive_integral_eq_simple_integral[OF e] .
-
-lemma measure_preservingD2:
-  "f \<in> measure_preserving A B \<Longrightarrow> f \<in> measurable A B"
-  unfolding measure_preserving_def by auto
-
 lemma (in measure_space) positive_integral_vimage:
-  assumes T: "sigma_algebra M'" "T \<in> measure_preserving M M'" and f: "f \<in> borel_measurable M'"
+  assumes T: "sigma_algebra M'" "T \<in> measure_preserving M M'"
+  and f: "f \<in> borel_measurable M'"
   shows "integral\<^isup>P M' f = (\<integral>\<^isup>+ x. f (T x) \<partial>M)"
 proof -
   interpret T: measure_space M' by (rule measure_space_vimage[OF T])
-  obtain f' where f': "f' \<up> f" "\<And>i. simple_function M' (f' i)"
-    using T.borel_measurable_implies_simple_function_sequence[OF f] by blast
-  then have f: "(\<lambda>i x. f' i (T x)) \<up> (\<lambda>x. f (T x))" "\<And>i. simple_function M (\<lambda>x. f' i (T x))"
-    using simple_function_vimage[OF T(1) measure_preservingD2[OF T(2)]] unfolding isoton_fun_expand by auto
+  from T.borel_measurable_implies_simple_function_sequence'[OF f]
+  guess f' . note f' = this
+  let "?f i x" = "f' i (T x)"
+  have inc: "incseq ?f" using f' by (force simp: le_fun_def incseq_def)
+  have sup: "\<And>x. (SUP i. ?f i x) = max 0 (f (T x))"
+    using f'(4) .
+  have sf: "\<And>i. simple_function M (\<lambda>x. f' i (T x))"
+    using simple_function_vimage[OF T(1) measure_preservingD2[OF T(2)] f'(1)] .
   show "integral\<^isup>P M' f = (\<integral>\<^isup>+ x. f (T x) \<partial>M)"
-    using positive_integral_isoton_simple[OF f]
-    using T.positive_integral_isoton_simple[OF f']
-    by (simp add: simple_integral_vimage[OF T f'(2)] isoton_def)
+    using
+      T.positive_integral_monotone_convergence_simple[OF f'(2,5,1)]
+      positive_integral_monotone_convergence_simple[OF inc f'(5) sf]
+    by (simp add: positive_integral_max_0 simple_integral_vimage[OF T f'(1)] f')
 qed
 
 lemma (in measure_space) positive_integral_linear:
-  assumes f: "f \<in> borel_measurable M"
-  and g: "g \<in> borel_measurable M"
+  assumes f: "f \<in> borel_measurable M" "\<And>x. 0 \<le> f x" and "0 \<le> a"
+  and g: "g \<in> borel_measurable M" "\<And>x. 0 \<le> g x"
   shows "(\<integral>\<^isup>+ x. a * f x + g x \<partial>M) = a * integral\<^isup>P M f + integral\<^isup>P M g"
     (is "integral\<^isup>P M ?L = _")
 proof -
-  from borel_measurable_implies_simple_function_sequence'[OF f] guess u .
-  note u = this positive_integral_isoton_simple[OF this(1-2)]
-  from borel_measurable_implies_simple_function_sequence'[OF g] guess v .
-  note v = this positive_integral_isoton_simple[OF this(1-2)]
+  from borel_measurable_implies_simple_function_sequence'[OF f(1)] guess u .
+  note u = positive_integral_monotone_convergence_simple[OF this(2,5,1)] this
+  from borel_measurable_implies_simple_function_sequence'[OF g(1)] guess v .
+  note v = positive_integral_monotone_convergence_simple[OF this(2,5,1)] this
   let "?L' i x" = "a * u i x + v i x"
 
-  have "?L \<in> borel_measurable M"
-    using assms by simp
+  have "?L \<in> borel_measurable M" using assms by auto
   from borel_measurable_implies_simple_function_sequence'[OF this] guess l .
-  note positive_integral_isoton_simple[OF this(1-2)] and l = this
-  moreover have "(SUP i. integral\<^isup>S M (l i)) = (SUP i. integral\<^isup>S M (?L' i))"
-  proof (rule SUP_simple_integral_sequences[OF l(1-2)])
-    show "?L' \<up> ?L" "\<And>i. simple_function M (?L' i)"
-      using u v by (auto simp: isoton_fun_expand isoton_add isoton_cmult_right)
+  note l = positive_integral_monotone_convergence_simple[OF this(2,5,1)] this
+
+  have inc: "incseq (\<lambda>i. a * integral\<^isup>S M (u i))" "incseq (\<lambda>i. integral\<^isup>S M (v i))"
+    using u v `0 \<le> a`
+    by (auto simp: incseq_Suc_iff le_fun_def
+             intro!: add_mono extreal_mult_left_mono simple_integral_mono)
+  have pos: "\<And>i. 0 \<le> integral\<^isup>S M (u i)" "\<And>i. 0 \<le> integral\<^isup>S M (v i)" "\<And>i. 0 \<le> a * integral\<^isup>S M (u i)"
+    using u v `0 \<le> a` by (auto simp: simple_integral_positive)
+  { fix i from pos[of i] have "a * integral\<^isup>S M (u i) \<noteq> -\<infinity>" "integral\<^isup>S M (v i) \<noteq> -\<infinity>"
+      by (auto split: split_if_asm) }
+  note not_MInf = this
+
+  have l': "(SUP i. integral\<^isup>S M (l i)) = (SUP i. integral\<^isup>S M (?L' i))"
+  proof (rule SUP_simple_integral_sequences[OF l(3,6,2)])
+    show "incseq ?L'" "\<And>i x. 0 \<le> ?L' i x" "\<And>i. simple_function M (?L' i)"
+      using u v  `0 \<le> a` unfolding incseq_Suc_iff le_fun_def
+      by (auto intro!: add_mono extreal_mult_left_mono extreal_add_nonneg_nonneg)
+    { fix x
+      { fix i have "a * u i x \<noteq> -\<infinity>" "v i x \<noteq> -\<infinity>" "u i x \<noteq> -\<infinity>" using `0 \<le> a` u(6)[of i x] v(6)[of i x]
+          by auto }
+      then have "(SUP i. a * u i x + v i x) = a * (SUP i. u i x) + (SUP i. v i x)"
+        using `0 \<le> a` u(3) v(3) u(6)[of _ x] v(6)[of _ x]
+        by (subst SUPR_extreal_cmult[symmetric, OF u(6) `0 \<le> a`])
+           (auto intro!: SUPR_extreal_add
+                 simp: incseq_Suc_iff le_fun_def add_mono extreal_mult_left_mono extreal_add_nonneg_nonneg) }
+    then show "AE x. (SUP i. l i x) = (SUP i. ?L' i x)"
+      unfolding l(5) using `0 \<le> a` u(5) v(5) l(5) f(2) g(2)
+      by (intro AE_I2) (auto split: split_max simp add: extreal_add_nonneg_nonneg)
   qed
-  moreover from u v have L'_isoton:
-      "(\<lambda>i. integral\<^isup>S M (?L' i)) \<up> a * integral\<^isup>P M f + integral\<^isup>P M g"
-    by (simp add: isoton_add isoton_cmult_right)
-  ultimately show ?thesis by (simp add: isoton_def)
+  also have "\<dots> = (SUP i. a * integral\<^isup>S M (u i) + integral\<^isup>S M (v i))"
+    using u(2, 6) v(2, 6) `0 \<le> a` by (auto intro!: arg_cong[where f="SUPR UNIV"] ext)
+  finally have "(\<integral>\<^isup>+ x. max 0 (a * f x + g x) \<partial>M) = a * (\<integral>\<^isup>+x. max 0 (f x) \<partial>M) + (\<integral>\<^isup>+x. max 0 (g x) \<partial>M)"
+    unfolding l(5)[symmetric] u(5)[symmetric] v(5)[symmetric]
+    unfolding l(1)[symmetric] u(1)[symmetric] v(1)[symmetric]
+    apply (subst SUPR_extreal_cmult[symmetric, OF pos(1) `0 \<le> a`])
+    apply (subst SUPR_extreal_add[symmetric, OF inc not_MInf]) .
+  then show ?thesis by (simp add: positive_integral_max_0)
 qed
 
 lemma (in measure_space) positive_integral_cmult:
-  assumes "f \<in> borel_measurable M"
+  assumes f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x" "0 \<le> c"
   shows "(\<integral>\<^isup>+ x. c * f x \<partial>M) = c * integral\<^isup>P M f"
-  using positive_integral_linear[OF assms, of "\<lambda>x. 0" c] by auto
+proof -
+  have [simp]: "\<And>x. c * max 0 (f x) = max 0 (c * f x)" using `0 \<le> c`
+    by (auto split: split_max simp: extreal_zero_le_0_iff)
+  have "(\<integral>\<^isup>+ x. c * f x \<partial>M) = (\<integral>\<^isup>+ x. c * max 0 (f x) \<partial>M)"
+    by (simp add: positive_integral_max_0)
+  then show ?thesis
+    using positive_integral_linear[OF _ _ `0 \<le> c`, of "\<lambda>x. max 0 (f x)" "\<lambda>x. 0"] f
+    by (auto simp: positive_integral_max_0)
+qed
 
 lemma (in measure_space) positive_integral_multc:
-  assumes "f \<in> borel_measurable M"
+  assumes "f \<in> borel_measurable M" "AE x. 0 \<le> f x" "0 \<le> c"
   shows "(\<integral>\<^isup>+ x. f x * c \<partial>M) = integral\<^isup>P M f * c"
   unfolding mult_commute[of _ c] positive_integral_cmult[OF assms] by simp
 
@@ -1260,143 +1289,172 @@
      (auto simp: simple_function_indicator simple_integral_indicator)
 
 lemma (in measure_space) positive_integral_cmult_indicator:
-  "A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+ x. c * indicator A x \<partial>M) = c * \<mu> A"
+  "0 \<le> c \<Longrightarrow> A \<in> sets M \<Longrightarrow> (\<integral>\<^isup>+ x. c * indicator A x \<partial>M) = c * \<mu> A"
   by (subst positive_integral_eq_simple_integral)
      (auto simp: simple_function_indicator simple_integral_indicator)
 
 lemma (in measure_space) positive_integral_add:
-  assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
+  assumes f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x"
+  and g: "g \<in> borel_measurable M" "AE x. 0 \<le> g x"
   shows "(\<integral>\<^isup>+ x. f x + g x \<partial>M) = integral\<^isup>P M f + integral\<^isup>P M g"
-  using positive_integral_linear[OF assms, of 1] by simp
+proof -
+  have ae: "AE x. max 0 (f x) + max 0 (g x) = max 0 (f x + g x)"
+    using assms by (auto split: split_max simp: extreal_add_nonneg_nonneg)
+  have "(\<integral>\<^isup>+ x. f x + g x \<partial>M) = (\<integral>\<^isup>+ x. max 0 (f x + g x) \<partial>M)"
+    by (simp add: positive_integral_max_0)
+  also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) + max 0 (g x) \<partial>M)"
+    unfolding ae[THEN positive_integral_cong_AE] ..
+  also have "\<dots> = (\<integral>\<^isup>+ x. max 0 (f x) \<partial>M) + (\<integral>\<^isup>+ x. max 0 (g x) \<partial>M)"
+    using positive_integral_linear[of "\<lambda>x. max 0 (f x)" 1 "\<lambda>x. max 0 (g x)"] f g
+    by auto
+  finally show ?thesis
+    by (simp add: positive_integral_max_0)
+qed
 
 lemma (in measure_space) positive_integral_setsum:
-  assumes "\<And>i. i\<in>P \<Longrightarrow> f i \<in> borel_measurable M"
+  assumes "\<And>i. i\<in>P \<Longrightarrow> f i \<in> borel_measurable M" "\<And>i. i\<in>P \<Longrightarrow> AE x. 0 \<le> f i x"
   shows "(\<integral>\<^isup>+ x. (\<Sum>i\<in>P. f i x) \<partial>M) = (\<Sum>i\<in>P. integral\<^isup>P M (f i))"
 proof cases
-  assume "finite P"
-  from this assms show ?thesis
+  assume f: "finite P"
+  from assms have "AE x. \<forall>i\<in>P. 0 \<le> f i x" unfolding AE_finite_all[OF f] by auto
+  from f this assms(1) show ?thesis
   proof induct
     case (insert i P)
-    have "f i \<in> borel_measurable M"
-      "(\<lambda>x. \<Sum>i\<in>P. f i x) \<in> borel_measurable M"
-      using insert by (auto intro!: borel_measurable_pextreal_setsum)
+    then have "f i \<in> borel_measurable M" "AE x. 0 \<le> f i x"
+      "(\<lambda>x. \<Sum>i\<in>P. f i x) \<in> borel_measurable M" "AE x. 0 \<le> (\<Sum>i\<in>P. f i x)"
+      by (auto intro!: borel_measurable_extreal_setsum setsum_nonneg)
     from positive_integral_add[OF this]
     show ?case using insert by auto
   qed simp
 qed simp
 
+lemma (in measure_space) positive_integral_Markov_inequality:
+  assumes u: "u \<in> borel_measurable M" "AE x. 0 \<le> u x" and "A \<in> sets M" and c: "0 \<le> c" "c \<noteq> \<infinity>"
+  shows "\<mu> ({x\<in>space M. 1 \<le> c * u x} \<inter> A) \<le> c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
+    (is "\<mu> ?A \<le> _ * ?PI")
+proof -
+  have "?A \<in> sets M"
+    using `A \<in> sets M` u by auto
+  hence "\<mu> ?A = (\<integral>\<^isup>+ x. indicator ?A x \<partial>M)"
+    using positive_integral_indicator by simp
+  also have "\<dots> \<le> (\<integral>\<^isup>+ x. c * (u x * indicator A x) \<partial>M)" using u c
+    by (auto intro!: positive_integral_mono_AE
+      simp: indicator_def extreal_zero_le_0_iff)
+  also have "\<dots> = c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
+    using assms
+    by (auto intro!: positive_integral_cmult borel_measurable_indicator simp: extreal_zero_le_0_iff)
+  finally show ?thesis .
+qed
+
+lemma (in measure_space) positive_integral_noteq_infinite:
+  assumes g: "g \<in> borel_measurable M" "AE x. 0 \<le> g x"
+  and "integral\<^isup>P M g \<noteq> \<infinity>"
+  shows "AE x. g x \<noteq> \<infinity>"
+proof (rule ccontr)
+  assume c: "\<not> (AE x. g x \<noteq> \<infinity>)"
+  have "\<mu> {x\<in>space M. g x = \<infinity>} \<noteq> 0"
+    using c g by (simp add: AE_iff_null_set)
+  moreover have "0 \<le> \<mu> {x\<in>space M. g x = \<infinity>}" using g by (auto intro: measurable_sets)
+  ultimately have "0 < \<mu> {x\<in>space M. g x = \<infinity>}" by auto
+  then have "\<infinity> = \<infinity> * \<mu> {x\<in>space M. g x = \<infinity>}" by auto
+  also have "\<dots> \<le> (\<integral>\<^isup>+x. \<infinity> * indicator {x\<in>space M. g x = \<infinity>} x \<partial>M)"
+    using g by (subst positive_integral_cmult_indicator) auto
+  also have "\<dots> \<le> integral\<^isup>P M g"
+    using assms by (auto intro!: positive_integral_mono_AE simp: indicator_def)
+  finally show False using `integral\<^isup>P M g \<noteq> \<infinity>` by auto
+qed
+
 lemma (in measure_space) positive_integral_diff:
-  assumes f: "f \<in> borel_measurable M" and g: "g \<in> borel_measurable M"
-  and fin: "integral\<^isup>P M g \<noteq> \<omega>"
-  and mono: "\<And>x. x \<in> space M \<Longrightarrow> g x \<le> f x"
+  assumes f: "f \<in> borel_measurable M"
+  and g: "g \<in> borel_measurable M" "AE x. 0 \<le> g x"
+  and fin: "integral\<^isup>P M g \<noteq> \<infinity>"
+  and mono: "AE x. g x \<le> f x"
   shows "(\<integral>\<^isup>+ x. f x - g x \<partial>M) = integral\<^isup>P M f - integral\<^isup>P M g"
 proof -
-  have borel: "(\<lambda>x. f x - g x) \<in> borel_measurable M"
-    using f g by (rule borel_measurable_pextreal_diff)
-  have "(\<integral>\<^isup>+x. f x - g x \<partial>M) + integral\<^isup>P M g = integral\<^isup>P M f"
-    unfolding positive_integral_add[OF borel g, symmetric]
-  proof (rule positive_integral_cong)
-    fix x assume "x \<in> space M"
-    from mono[OF this] show "f x - g x + g x = f x"
-      by (cases "f x", cases "g x", simp, simp, cases "g x", auto)
-  qed
-  with mono show ?thesis
-    by (subst minus_pextreal_eq2[OF _ fin]) (auto intro!: positive_integral_mono)
+  have diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M" "AE x. 0 \<le> f x - g x"
+    using assms by (auto intro: extreal_diff_positive)
+  have pos_f: "AE x. 0 \<le> f x" using mono g by auto
+  { fix a b :: extreal assume "0 \<le> a" "a \<noteq> \<infinity>" "0 \<le> b" "a \<le> b" then have "b - a + a = b"
+      by (cases rule: extreal2_cases[of a b]) auto }
+  note * = this
+  then have "AE x. f x = f x - g x + g x"
+    using mono positive_integral_noteq_infinite[OF g fin] assms by auto
+  then have **: "integral\<^isup>P M f = (\<integral>\<^isup>+x. f x - g x \<partial>M) + integral\<^isup>P M g"
+    unfolding positive_integral_add[OF diff g, symmetric]
+    by (rule positive_integral_cong_AE)
+  show ?thesis unfolding **
+    using fin positive_integral_positive[of g]
+    by (cases rule: extreal2_cases[of "\<integral>\<^isup>+ x. f x - g x \<partial>M" "integral\<^isup>P M g"]) auto
 qed
 
-lemma (in measure_space) positive_integral_psuminf:
-  assumes "\<And>i. f i \<in> borel_measurable M"
-  shows "(\<integral>\<^isup>+ x. (\<Sum>\<^isub>\<infinity> i. f i x) \<partial>M) = (\<Sum>\<^isub>\<infinity> i. integral\<^isup>P M (f i))"
+lemma (in measure_space) positive_integral_suminf:
+  assumes f: "\<And>i. f i \<in> borel_measurable M" "\<And>i. AE x. 0 \<le> f i x"
+  shows "(\<integral>\<^isup>+ x. (\<Sum>i. f i x) \<partial>M) = (\<Sum>i. integral\<^isup>P M (f i))"
 proof -
-  have "(\<lambda>i. (\<integral>\<^isup>+x. (\<Sum>i<i. f i x) \<partial>M)) \<up> (\<integral>\<^isup>+x. (\<Sum>\<^isub>\<infinity>i. f i x) \<partial>M)"
-    by (rule positive_integral_isoton)
-       (auto intro!: borel_measurable_pextreal_setsum assms positive_integral_mono
-                     arg_cong[where f=Sup]
-             simp: isoton_def le_fun_def psuminf_def fun_eq_iff SUPR_def Sup_fun_def)
-  thus ?thesis
-    by (auto simp: isoton_def psuminf_def positive_integral_setsum[OF assms])
+  have all_pos: "AE x. \<forall>i. 0 \<le> f i x"
+    using assms by (auto simp: AE_all_countable)
+  have "(\<Sum>i. integral\<^isup>P M (f i)) = (SUP n. \<Sum>i<n. integral\<^isup>P M (f i))"
+    using positive_integral_positive by (rule suminf_extreal_eq_SUPR)
+  also have "\<dots> = (SUP n. \<integral>\<^isup>+x. (\<Sum>i<n. f i x) \<partial>M)"
+    unfolding positive_integral_setsum[OF f] ..
+  also have "\<dots> = \<integral>\<^isup>+x. (SUP n. \<Sum>i<n. f i x) \<partial>M" using f all_pos
+    by (intro positive_integral_monotone_convergence_SUP_AE[symmetric])
+       (elim AE_mp, auto simp: setsum_nonneg simp del: setsum_lessThan_Suc intro!: AE_I2 setsum_mono3)
+  also have "\<dots> = \<integral>\<^isup>+x. (\<Sum>i. f i x) \<partial>M" using all_pos
+    by (intro positive_integral_cong_AE) (auto simp: suminf_extreal_eq_SUPR)
+  finally show ?thesis by simp
 qed
 
 text {* Fatou's lemma: convergence theorem on limes inferior *}
 lemma (in measure_space) positive_integral_lim_INF:
-  fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> pextreal"
-  assumes "\<And>i. u i \<in> borel_measurable M"
-  shows "(\<integral>\<^isup>+ x. (SUP n. INF m. u (m + n) x) \<partial>M) \<le>
-    (SUP n. INF m. integral\<^isup>P M (u (m + n)))"
+  fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
+  assumes u: "\<And>i. u i \<in> borel_measurable M" "\<And>i. AE x. 0 \<le> u i x"
+  shows "(\<integral>\<^isup>+ x. liminf (\<lambda>n. u n x) \<partial>M) \<le> liminf (\<lambda>n. integral\<^isup>P M (u n))"
 proof -
-  have "(\<integral>\<^isup>+x. (SUP n. INF m. u (m + n) x) \<partial>M)
-      = (SUP n. (\<integral>\<^isup>+x. (INF m. u (m + n) x) \<partial>M))"
-    using assms
-    by (intro positive_integral_monotone_convergence_SUP[symmetric] INF_mono)
-       (auto simp del: add_Suc simp add: add_Suc[symmetric])
-  also have "\<dots> \<le> (SUP n. INF m. integral\<^isup>P M (u (m + n)))"
-    by (auto intro!: SUP_mono bexI le_INFI positive_integral_mono INF_leI)
+  have pos: "AE x. \<forall>i. 0 \<le> u i x" using u by (auto simp: AE_all_countable)
+  have "(\<integral>\<^isup>+ x. liminf (\<lambda>n. u n x) \<partial>M) =
+    (SUP n. \<integral>\<^isup>+ x. (INF i:{n..}. u i x) \<partial>M)"
+    unfolding liminf_SUPR_INFI using pos u
+    by (intro positive_integral_monotone_convergence_SUP_AE)
+       (elim AE_mp, auto intro!: AE_I2 intro: le_INFI INF_subset)
+  also have "\<dots> \<le> liminf (\<lambda>n. integral\<^isup>P M (u n))"
+    unfolding liminf_SUPR_INFI
+    by (auto intro!: SUP_mono exI le_INFI positive_integral_mono INF_leI)
   finally show ?thesis .
 qed
 
 lemma (in measure_space) measure_space_density:
-  assumes borel: "u \<in> borel_measurable M"
+  assumes u: "u \<in> borel_measurable M" "AE x. 0 \<le> u x"
     and M'[simp]: "M' = (M\<lparr>measure := \<lambda>A. (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)\<rparr>)"
   shows "measure_space M'"
 proof -
   interpret M': sigma_algebra M' by (intro sigma_algebra_cong) auto
   show ?thesis
   proof
-    show "measure M' {} = 0" unfolding M' by simp
+    have pos: "\<And>A. AE x. 0 \<le> u x * indicator A x"
+      using u by (auto simp: extreal_zero_le_0_iff)
+    then show "positive M' (measure M')" unfolding M'
+      using u(1) by (auto simp: positive_def intro!: positive_integral_positive)
     show "countably_additive M' (measure M')"
     proof (intro countably_additiveI)
       fix A :: "nat \<Rightarrow> 'a set" assume "range A \<subseteq> sets M'"
-      then have "\<And>i. (\<lambda>x. u x * indicator (A i) x) \<in> borel_measurable M"
-        using borel by (auto intro: borel_measurable_indicator)
-      moreover assume "disjoint_family A"
-      note psuminf_indicator[OF this]
-      ultimately show "(\<Sum>\<^isub>\<infinity>n. measure M' (A n)) = measure M' (\<Union>x. A x)"
-        by (simp add: positive_integral_psuminf[symmetric])
+      then have *: "\<And>i. (\<lambda>x. u x * indicator (A i) x) \<in> borel_measurable M"
+        using u by (auto intro: borel_measurable_indicator)
+      assume disj: "disjoint_family A"
+      have "(\<Sum>n. measure M' (A n)) = (\<integral>\<^isup>+ x. (\<Sum>n. u x * indicator (A n) x) \<partial>M)"
+        unfolding M' using u(1) *
+        by (simp add: positive_integral_suminf[OF _ pos, symmetric])
+      also have "\<dots> = (\<integral>\<^isup>+ x. u x * (\<Sum>n. indicator (A n) x) \<partial>M)" using u
+        by (intro positive_integral_cong_AE)
+           (elim AE_mp, auto intro!: AE_I2 suminf_cmult_extreal)
+      also have "\<dots> = (\<integral>\<^isup>+ x. u x * indicator (\<Union>n. A n) x \<partial>M)"
+        unfolding suminf_indicator[OF disj] ..
+      finally show "(\<Sum>n. measure M' (A n)) = measure M' (\<Union>x. A x)"
+        unfolding M' by simp
     qed
   qed
 qed
 
-lemma (in measure_space) positive_integral_translated_density:
-  assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
-    and M': "M' = (M\<lparr> measure := \<lambda>A. (\<integral>\<^isup>+ x. f x * indicator A x \<partial>M)\<rparr>)"
-  shows "integral\<^isup>P M' g = (\<integral>\<^isup>+ x. f x * g x \<partial>M)"
-proof -
-  from measure_space_density[OF assms(1) M']
-  interpret T: measure_space M' .
-  have borel[simp]:
-    "borel_measurable M' = borel_measurable M"
-    "simple_function M' = simple_function M"
-    unfolding measurable_def simple_function_def_raw by (auto simp: M')
-  from borel_measurable_implies_simple_function_sequence[OF assms(2)]
-  obtain G where G: "\<And>i. simple_function M (G i)" "G \<up> g" by blast
-  note G_borel = borel_measurable_simple_function[OF this(1)]
-  from T.positive_integral_isoton[unfolded borel, OF `G \<up> g` G_borel]
-  have *: "(\<lambda>i. integral\<^isup>P M' (G i)) \<up> integral\<^isup>P M' g" .
-  { fix i
-    have [simp]: "finite (G i ` space M)"
-      using G(1) unfolding simple_function_def by auto
-    have "integral\<^isup>P M' (G i) = integral\<^isup>S M' (G i)"
-      using G T.positive_integral_eq_simple_integral by simp
-    also have "\<dots> = (\<integral>\<^isup>+x. f x * (\<Sum>y\<in>G i`space M. y * indicator (G i -` {y} \<inter> space M) x) \<partial>M)"
-      apply (simp add: simple_integral_def M')
-      apply (subst positive_integral_cmult[symmetric])
-      using G_borel assms(1) apply (fastsimp intro: borel_measurable_vimage)
-      apply (subst positive_integral_setsum[symmetric])
-      using G_borel assms(1) apply (fastsimp intro: borel_measurable_vimage)
-      by (simp add: setsum_right_distrib field_simps)
-    also have "\<dots> = (\<integral>\<^isup>+x. f x * G i x \<partial>M)"
-      by (auto intro!: positive_integral_cong
-               simp: indicator_def if_distrib setsum_cases)
-    finally have "integral\<^isup>P M' (G i) = (\<integral>\<^isup>+x. f x * G i x \<partial>M)" . }
-  with * have eq_Tg: "(\<lambda>i. (\<integral>\<^isup>+x. f x * G i x \<partial>M)) \<up> integral\<^isup>P M' g" by simp
-  from G(2) have "(\<lambda>i x. f x * G i x) \<up> (\<lambda>x. f x * g x)"
-    unfolding isoton_fun_expand by (auto intro!: isoton_cmult_right)
-  then have "(\<lambda>i. (\<integral>\<^isup>+x. f x * G i x \<partial>M)) \<up> (\<integral>\<^isup>+x. f x * g x \<partial>M)"
-    using assms(1) G_borel by (auto intro!: positive_integral_isoton borel_measurable_pextreal_times)
-  with eq_Tg show "integral\<^isup>P M' g = (\<integral>\<^isup>+x. f x * g x \<partial>M)"
-    unfolding isoton_def by simp
-qed
-
 lemma (in measure_space) positive_integral_null_set:
   assumes "N \<in> null_sets" shows "(\<integral>\<^isup>+ x. u x * indicator N x \<partial>M) = 0"
 proof -
@@ -1410,144 +1468,199 @@
   then show ?thesis by simp
 qed
 
-lemma (in measure_space) positive_integral_Markov_inequality:
-  assumes borel: "u \<in> borel_measurable M" and "A \<in> sets M" and c: "c \<noteq> \<omega>"
-  shows "\<mu> ({x\<in>space M. 1 \<le> c * u x} \<inter> A) \<le> c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
-    (is "\<mu> ?A \<le> _ * ?PI")
+lemma (in measure_space) positive_integral_translated_density:
+  assumes f: "f \<in> borel_measurable M" "AE x. 0 \<le> f x"
+  assumes g: "g \<in> borel_measurable M" "AE x. 0 \<le> g x"
+    and M': "M' = (M\<lparr> measure := \<lambda>A. (\<integral>\<^isup>+ x. f x * indicator A x \<partial>M)\<rparr>)"
+  shows "integral\<^isup>P M' g = (\<integral>\<^isup>+ x. f x * g x \<partial>M)"
 proof -
-  have "?A \<in> sets M"
-    using `A \<in> sets M` borel by auto
-  hence "\<mu> ?A = (\<integral>\<^isup>+ x. indicator ?A x \<partial>M)"
-    using positive_integral_indicator by simp
-  also have "\<dots> \<le> (\<integral>\<^isup>+ x. c * (u x * indicator A x) \<partial>M)"
-  proof (rule positive_integral_mono)
-    fix x assume "x \<in> space M"
-    show "indicator ?A x \<le> c * (u x * indicator A x)"
-      by (cases "x \<in> ?A") auto
-  qed
-  also have "\<dots> = c * (\<integral>\<^isup>+ x. u x * indicator A x \<partial>M)"
-    using assms
-    by (auto intro!: positive_integral_cmult borel_measurable_indicator)
-  finally show ?thesis .
+  from measure_space_density[OF f M']
+  interpret T: measure_space M' .
+  have borel[simp]:
+    "borel_measurable M' = borel_measurable M"
+    "simple_function M' = simple_function M"
+    unfolding measurable_def simple_function_def_raw by (auto simp: M')
+  from borel_measurable_implies_simple_function_sequence'[OF g(1)] guess G . note G = this
+  note G' = borel_measurable_simple_function[OF this(1)] simple_functionD[OF G(1)]
+  note G'(2)[simp]
+  { fix P have "AE x. P x \<Longrightarrow> AE x in M'. P x"
+      using positive_integral_null_set[of _ f]
+      unfolding T.almost_everywhere_def almost_everywhere_def
+      by (auto simp: M') }
+  note ac =