HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
authorhoelzl
Tue Oct 18 12:01:54 2016 +0200 (2016-10-18)
changeset 64284f3b905b2eee2
parent 64283 979cdfdf7a79
child 64285 d7e0123a752b
child 64287 d85d88722745
HOL-Analysis: more theorems from Sébastien Gouëzel's Ergodic_Theory
src/HOL/Analysis/Borel_Space.thy
src/HOL/Analysis/Complete_Measure.thy
src/HOL/Analysis/Set_Integral.thy
src/HOL/Analysis/Topology_Euclidean_Space.thy
src/HOL/Library/Extended_Nonnegative_Real.thy
src/HOL/Library/Permutations.thy
src/HOL/Probability/Levy.thy
src/HOL/Topological_Spaces.thy
     1.1 --- a/src/HOL/Analysis/Borel_Space.thy	Thu Oct 13 18:36:06 2016 +0200
     1.2 +++ b/src/HOL/Analysis/Borel_Space.thy	Tue Oct 18 12:01:54 2016 +0200
     1.3 @@ -1974,4 +1974,189 @@
     1.4  no_notation
     1.5    eucl_less (infix "<e" 50)
     1.6  
     1.7 +lemma borel_measurable_Max2[measurable (raw)]:
     1.8 +  fixes f::"_ \<Rightarrow> _ \<Rightarrow> 'a::{second_countable_topology, dense_linorder, linorder_topology}"
     1.9 +  assumes "finite I"
    1.10 +    and [measurable]: "\<And>i. f i \<in> borel_measurable M"
    1.11 +  shows "(\<lambda>x. Max{f i x |i. i \<in> I}) \<in> borel_measurable M"
    1.12 +by (simp add: borel_measurable_Max[OF assms(1), where ?f=f and ?M=M] Setcompr_eq_image)
    1.13 +
    1.14 +lemma measurable_compose_n [measurable (raw)]:
    1.15 +  assumes "T \<in> measurable M M"
    1.16 +  shows "(T^^n) \<in> measurable M M"
    1.17 +by (induction n, auto simp add: measurable_compose[OF _ assms])
    1.18 +
    1.19 +lemma measurable_real_imp_nat:
    1.20 +  fixes f::"'a \<Rightarrow> nat"
    1.21 +  assumes [measurable]: "(\<lambda>x. real(f x)) \<in> borel_measurable M"
    1.22 +  shows "f \<in> measurable M (count_space UNIV)"
    1.23 +proof -
    1.24 +  let ?g = "(\<lambda>x. real(f x))"
    1.25 +  have "\<And>(n::nat). ?g-`({real n}) \<inter> space M = f-`{n} \<inter> space M" by auto
    1.26 +  moreover have "\<And>(n::nat). ?g-`({real n}) \<inter> space M \<in> sets M" using assms by measurable
    1.27 +  ultimately have "\<And>(n::nat). f-`{n} \<inter> space M \<in> sets M" by simp
    1.28 +  then show ?thesis using measurable_count_space_eq2_countable by blast
    1.29 +qed
    1.30 +
    1.31 +lemma measurable_equality_set [measurable]:
    1.32 +  fixes f g::"_\<Rightarrow> 'a::{second_countable_topology, t2_space}"
    1.33 +  assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
    1.34 +  shows "{x \<in> space M. f x = g x} \<in> sets M"
    1.35 +
    1.36 +proof -
    1.37 +  define A where "A = {x \<in> space M. f x = g x}"
    1.38 +  define B where "B = {y. \<exists>x::'a. y = (x,x)}"
    1.39 +  have "A = (\<lambda>x. (f x, g x))-`B \<inter> space M" unfolding A_def B_def by auto
    1.40 +  moreover have "(\<lambda>x. (f x, g x)) \<in> borel_measurable M" by simp
    1.41 +  moreover have "B \<in> sets borel" unfolding B_def by (simp add: closed_diagonal)
    1.42 +  ultimately have "A \<in> sets M" by simp
    1.43 +  then show ?thesis unfolding A_def by simp
    1.44 +qed
    1.45 +
    1.46 +lemma measurable_inequality_set [measurable]:
    1.47 +  fixes f g::"_ \<Rightarrow> 'a::{second_countable_topology, linorder_topology}"
    1.48 +  assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
    1.49 +  shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
    1.50 +        "{x \<in> space M. f x < g x} \<in> sets M"
    1.51 +        "{x \<in> space M. f x \<ge> g x} \<in> sets M"
    1.52 +        "{x \<in> space M. f x > g x} \<in> sets M"
    1.53 +proof -
    1.54 +  define F where "F = (\<lambda>x. (f x, g x))"
    1.55 +  have * [measurable]: "F \<in> borel_measurable M" unfolding F_def by simp
    1.56 +
    1.57 +  have "{x \<in> space M. f x \<le> g x} = F-`{(x, y) | x y. x \<le> y} \<inter> space M" unfolding F_def by auto
    1.58 +  moreover have "{(x, y) | x y. x \<le> (y::'a)} \<in> sets borel" using closed_subdiagonal borel_closed by blast
    1.59 +  ultimately show "{x \<in> space M. f x \<le> g x} \<in> sets M" using * by (metis (mono_tags, lifting) measurable_sets)
    1.60 +
    1.61 +  have "{x \<in> space M. f x < g x} = F-`{(x, y) | x y. x < y} \<inter> space M" unfolding F_def by auto
    1.62 +  moreover have "{(x, y) | x y. x < (y::'a)} \<in> sets borel" using open_subdiagonal borel_open by blast
    1.63 +  ultimately show "{x \<in> space M. f x < g x} \<in> sets M" using * by (metis (mono_tags, lifting) measurable_sets)
    1.64 +
    1.65 +  have "{x \<in> space M. f x \<ge> g x} = F-`{(x, y) | x y. x \<ge> y} \<inter> space M" unfolding F_def by auto
    1.66 +  moreover have "{(x, y) | x y. x \<ge> (y::'a)} \<in> sets borel" using closed_superdiagonal borel_closed by blast
    1.67 +  ultimately show "{x \<in> space M. f x \<ge> g x} \<in> sets M" using * by (metis (mono_tags, lifting) measurable_sets)
    1.68 +
    1.69 +  have "{x \<in> space M. f x > g x} = F-`{(x, y) | x y. x > y} \<inter> space M" unfolding F_def by auto
    1.70 +  moreover have "{(x, y) | x y. x > (y::'a)} \<in> sets borel" using open_superdiagonal borel_open by blast
    1.71 +  ultimately show "{x \<in> space M. f x > g x} \<in> sets M" using * by (metis (mono_tags, lifting) measurable_sets)
    1.72 +qed
    1.73 +
    1.74 +lemma measurable_limit [measurable]:
    1.75 +  fixes f::"nat \<Rightarrow> 'a \<Rightarrow> 'b::first_countable_topology"
    1.76 +  assumes [measurable]: "\<And>n::nat. f n \<in> borel_measurable M"
    1.77 +  shows "Measurable.pred M (\<lambda>x. (\<lambda>n. f n x) \<longlonglongrightarrow> c)"
    1.78 +proof -
    1.79 +  obtain A :: "nat \<Rightarrow> 'b set" where A:
    1.80 +    "\<And>i. open (A i)"
    1.81 +    "\<And>i. c \<in> A i"
    1.82 +    "\<And>S. open S \<Longrightarrow> c \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
    1.83 +  by (rule countable_basis_at_decseq) blast
    1.84 +
    1.85 +  have [measurable]: "\<And>N i. (f N)-`(A i) \<inter> space M \<in> sets M" using A(1) by auto
    1.86 +  then have mes: "(\<Inter>i. \<Union>n. \<Inter>N\<in>{n..}. (f N)-`(A i) \<inter> space M) \<in> sets M" by blast
    1.87 +
    1.88 +  have "(u \<longlonglongrightarrow> c) \<longleftrightarrow> (\<forall>i. eventually (\<lambda>n. u n \<in> A i) sequentially)" for u::"nat \<Rightarrow> 'b"
    1.89 +  proof
    1.90 +    assume "u \<longlonglongrightarrow> c"
    1.91 +    then have "eventually (\<lambda>n. u n \<in> A i) sequentially" for i using A(1)[of i] A(2)[of i]
    1.92 +      by (simp add: topological_tendstoD)
    1.93 +    then show "(\<forall>i. eventually (\<lambda>n. u n \<in> A i) sequentially)" by auto
    1.94 +  next
    1.95 +    assume H: "(\<forall>i. eventually (\<lambda>n. u n \<in> A i) sequentially)"
    1.96 +    show "(u \<longlonglongrightarrow> c)"
    1.97 +    proof (rule topological_tendstoI)
    1.98 +      fix S assume "open S" "c \<in> S"
    1.99 +      with A(3)[OF this] obtain i where "A i \<subseteq> S"
   1.100 +        using eventually_False_sequentially eventually_mono by blast
   1.101 +      moreover have "eventually (\<lambda>n. u n \<in> A i) sequentially" using H by simp
   1.102 +      ultimately show "\<forall>\<^sub>F n in sequentially. u n \<in> S"
   1.103 +        by (simp add: eventually_mono subset_eq)
   1.104 +    qed
   1.105 +  qed
   1.106 +  then have "{x. (\<lambda>n. f n x) \<longlonglongrightarrow> c} = (\<Inter>i. \<Union>n. \<Inter>N\<in>{n..}. (f N)-`(A i))"
   1.107 +    by (auto simp add: atLeast_def eventually_at_top_linorder)
   1.108 +  then have "{x \<in> space M. (\<lambda>n. f n x) \<longlonglongrightarrow> c} = (\<Inter>i. \<Union>n. \<Inter>N\<in>{n..}. (f N)-`(A i) \<inter> space M)"
   1.109 +    by auto
   1.110 +  then have "{x \<in> space M. (\<lambda>n. f n x) \<longlonglongrightarrow> c} \<in> sets M" using mes by simp
   1.111 +  then show ?thesis by auto
   1.112 +qed
   1.113 +
   1.114 +lemma measurable_limit2 [measurable]:
   1.115 +  fixes u::"nat \<Rightarrow> 'a \<Rightarrow> real"
   1.116 +  assumes [measurable]: "\<And>n. u n \<in> borel_measurable M" "v \<in> borel_measurable M"
   1.117 +  shows "Measurable.pred M (\<lambda>x. (\<lambda>n. u n x) \<longlonglongrightarrow> v x)"
   1.118 +proof -
   1.119 +  define w where "w = (\<lambda>n x. u n x - v x)"
   1.120 +  have [measurable]: "w n \<in> borel_measurable M" for n unfolding w_def by auto
   1.121 +  have "((\<lambda>n. u n x) \<longlonglongrightarrow> v x) \<longleftrightarrow> ((\<lambda>n. w n x) \<longlonglongrightarrow> 0)" for x
   1.122 +    unfolding w_def using Lim_null by auto
   1.123 +  then show ?thesis using measurable_limit by auto
   1.124 +qed
   1.125 +
   1.126 +lemma measurable_P_restriction [measurable (raw)]:
   1.127 +  assumes [measurable]: "Measurable.pred M P" "A \<in> sets M"
   1.128 +  shows "{x \<in> A. P x} \<in> sets M"
   1.129 +proof -
   1.130 +  have "A \<subseteq> space M" using sets.sets_into_space[OF assms(2)].
   1.131 +  then have "{x \<in> A. P x} = A \<inter> {x \<in> space M. P x}" by blast
   1.132 +  then show ?thesis by auto
   1.133 +qed
   1.134 +
   1.135 +lemma measurable_sum_nat [measurable (raw)]:
   1.136 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> nat"
   1.137 +  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> measurable M (count_space UNIV)"
   1.138 +  shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> measurable M (count_space UNIV)"
   1.139 +proof cases
   1.140 +  assume "finite S"
   1.141 +  then show ?thesis using assms by induct auto
   1.142 +qed simp
   1.143 +
   1.144 +
   1.145 +lemma measurable_abs_powr [measurable]:
   1.146 +  fixes p::real
   1.147 +  assumes [measurable]: "f \<in> borel_measurable M"
   1.148 +  shows "(\<lambda>x. \<bar>f x\<bar> powr p) \<in> borel_measurable M"
   1.149 +unfolding powr_def by auto
   1.150 +
   1.151 +text {* The next one is a variation around \verb+measurable_restrict_space+.*}
   1.152 +
   1.153 +lemma measurable_restrict_space3:
   1.154 +  assumes "f \<in> measurable M N" and
   1.155 +          "f \<in> A \<rightarrow> B"
   1.156 +  shows "f \<in> measurable (restrict_space M A) (restrict_space N B)"
   1.157 +proof -
   1.158 +  have "f \<in> measurable (restrict_space M A) N" using assms(1) measurable_restrict_space1 by auto
   1.159 +  then show ?thesis by (metis Int_iff funcsetI funcset_mem
   1.160 +      measurable_restrict_space2[of f, of "restrict_space M A", of B, of N] assms(2) space_restrict_space)
   1.161 +qed
   1.162 +
   1.163 +text {* The next one is a variation around \verb+measurable_piecewise_restrict+.*}
   1.164 +
   1.165 +lemma measurable_piecewise_restrict2:
   1.166 +  assumes [measurable]: "\<And>n. A n \<in> sets M"
   1.167 +      and "space M = (\<Union>(n::nat). A n)"
   1.168 +          "\<And>n. \<exists>h \<in> measurable M N. (\<forall>x \<in> A n. f x = h x)"
   1.169 +  shows "f \<in> measurable M N"
   1.170 +proof (rule measurableI)
   1.171 +  fix B assume [measurable]: "B \<in> sets N"
   1.172 +  {
   1.173 +    fix n::nat
   1.174 +    obtain h where [measurable]: "h \<in> measurable M N" and "\<forall>x \<in> A n. f x = h x" using assms(3) by blast
   1.175 +    then have *: "f-`B \<inter> A n = h-`B \<inter> A n" by auto
   1.176 +    have "h-`B \<inter> A n = h-`B \<inter> space M \<inter> A n" using assms(2) sets.sets_into_space by auto
   1.177 +    then have "h-`B \<inter> A n \<in> sets M" by simp
   1.178 +    then have "f-`B \<inter> A n \<in> sets M" using * by simp
   1.179 +  }
   1.180 +  then have "(\<Union>n. f-`B \<inter> A n) \<in> sets M" by measurable
   1.181 +  moreover have "f-`B \<inter> space M = (\<Union>n. f-`B \<inter> A n)" using assms(2) by blast
   1.182 +  ultimately show "f-`B \<inter> space M \<in> sets M" by simp
   1.183 +next
   1.184 +  fix x assume "x \<in> space M"
   1.185 +  then obtain n where "x \<in> A n" using assms(2) by blast
   1.186 +  obtain h where [measurable]: "h \<in> measurable M N" and "\<forall>x \<in> A n. f x = h x" using assms(3) by blast
   1.187 +  then have "f x = h x" using `x \<in> A n` by blast
   1.188 +  moreover have "h x \<in> space N" by (metis measurable_space `x \<in> space M` `h \<in> measurable M N`)
   1.189 +  ultimately show "f x \<in> space N" by simp
   1.190 +qed
   1.191 +
   1.192  end
     2.1 --- a/src/HOL/Analysis/Complete_Measure.thy	Thu Oct 13 18:36:06 2016 +0200
     2.2 +++ b/src/HOL/Analysis/Complete_Measure.thy	Tue Oct 18 12:01:54 2016 +0200
     2.3 @@ -791,11 +791,11 @@
     2.4      by (auto simp add: emeasure_density measurable_completion nn_integral_completion intro!: nn_integral_cong_AE)
     2.5  qed
     2.6  
     2.7 -lemma null_sets_subset: "A \<subseteq> B \<Longrightarrow> A \<in> sets M \<Longrightarrow> B \<in> null_sets M \<Longrightarrow> A \<in> null_sets M"
     2.8 +lemma null_sets_subset: "B \<in> null_sets M \<Longrightarrow> A \<in> sets M \<Longrightarrow> A \<subseteq> B \<Longrightarrow> A \<in> null_sets M"
     2.9    using emeasure_mono[of A B M] by (simp add: null_sets_def)
    2.10  
    2.11  lemma (in complete_measure) complete2: "A \<subseteq> B \<Longrightarrow> B \<in> null_sets M \<Longrightarrow> A \<in> null_sets M"
    2.12 -  using complete[of A B] null_sets_subset[of A B M] by simp
    2.13 +  using complete[of A B] null_sets_subset[of B M A] by simp
    2.14  
    2.15  lemma (in complete_measure) AE_iff_null_sets: "(AE x in M. P x) \<longleftrightarrow> {x\<in>space M. \<not> P x} \<in> null_sets M"
    2.16    unfolding eventually_ae_filter by (auto intro: complete2)
     3.1 --- a/src/HOL/Analysis/Set_Integral.thy	Thu Oct 13 18:36:06 2016 +0200
     3.2 +++ b/src/HOL/Analysis/Set_Integral.thy	Tue Oct 18 12:01:54 2016 +0200
     3.3 @@ -11,6 +11,1339 @@
     3.4    imports Radon_Nikodym
     3.5  begin
     3.6  
     3.7 +lemma bij_inv_eq_iff: "bij p \<Longrightarrow> x = inv p y \<longleftrightarrow> p x = y" (* COPIED FROM Permutations *)
     3.8 +  using surj_f_inv_f[of p] by (auto simp add: bij_def)
     3.9 +
    3.10 +subsection {*Fun.thy*}
    3.11 +
    3.12 +lemma inj_fn:
    3.13 +  fixes f::"'a \<Rightarrow> 'a"
    3.14 +  assumes "inj f"
    3.15 +  shows "inj (f^^n)"
    3.16 +proof (induction n)
    3.17 +  case (Suc n)
    3.18 +  have "inj (f o (f^^n))"
    3.19 +    using inj_comp[OF assms Suc.IH] by simp
    3.20 +  then show "inj (f^^(Suc n))"
    3.21 +    by auto
    3.22 +qed (auto)
    3.23 +
    3.24 +lemma surj_fn:
    3.25 +  fixes f::"'a \<Rightarrow> 'a"
    3.26 +  assumes "surj f"
    3.27 +  shows "surj (f^^n)"
    3.28 +proof (induction n)
    3.29 +  case (Suc n)
    3.30 +  have "surj (f o (f^^n))"
    3.31 +    using assms Suc.IH by (simp add: comp_surj)
    3.32 +  then show "surj (f^^(Suc n))"
    3.33 +    by auto
    3.34 +qed (auto)
    3.35 +
    3.36 +lemma bij_fn:
    3.37 +  fixes f::"'a \<Rightarrow> 'a"
    3.38 +  assumes "bij f"
    3.39 +  shows "bij (f^^n)"
    3.40 +by (rule bijI[OF inj_fn[OF bij_is_inj[OF assms]] surj_fn[OF bij_is_surj[OF assms]]])
    3.41 +
    3.42 +lemma inv_fn_o_fn_is_id:
    3.43 +  fixes f::"'a \<Rightarrow> 'a"
    3.44 +  assumes "bij f"
    3.45 +  shows "((inv f)^^n) o (f^^n) = (\<lambda>x. x)"
    3.46 +proof -
    3.47 +  have "((inv f)^^n)((f^^n) x) = x" for x n
    3.48 +  proof (induction n)
    3.49 +    case (Suc n)
    3.50 +    have *: "(inv f) (f y) = y" for y
    3.51 +      by (simp add: assms bij_is_inj)
    3.52 +    have "(inv f ^^ Suc n) ((f ^^ Suc n) x) = (inv f^^n) (inv f (f ((f^^n) x)))"
    3.53 +      by (simp add: funpow_swap1)
    3.54 +    also have "... = (inv f^^n) ((f^^n) x)"
    3.55 +      using * by auto
    3.56 +    also have "... = x" using Suc.IH by auto
    3.57 +    finally show ?case by simp
    3.58 +  qed (auto)
    3.59 +  then show ?thesis unfolding o_def by blast
    3.60 +qed
    3.61 +
    3.62 +lemma fn_o_inv_fn_is_id:
    3.63 +  fixes f::"'a \<Rightarrow> 'a"
    3.64 +  assumes "bij f"
    3.65 +  shows "(f^^n) o ((inv f)^^n) = (\<lambda>x. x)"
    3.66 +proof -
    3.67 +  have "(f^^n) (((inv f)^^n) x) = x" for x n
    3.68 +  proof (induction n)
    3.69 +    case (Suc n)
    3.70 +    have *: "f(inv f y) = y" for y
    3.71 +      using assms by (meson bij_inv_eq_iff)
    3.72 +    have "(f ^^ Suc n) ((inv f ^^ Suc n) x) = (f^^n) (f (inv f ((inv f^^n) x)))"
    3.73 +      by (simp add: funpow_swap1)
    3.74 +    also have "... = (f^^n) ((inv f^^n) x)"
    3.75 +      using * by auto
    3.76 +    also have "... = x" using Suc.IH by auto
    3.77 +    finally show ?case by simp
    3.78 +  qed (auto)
    3.79 +  then show ?thesis unfolding o_def by blast
    3.80 +qed
    3.81 +
    3.82 +lemma inv_fn:
    3.83 +  fixes f::"'a \<Rightarrow> 'a"
    3.84 +  assumes "bij f"
    3.85 +  shows "inv (f^^n) = ((inv f)^^n)"
    3.86 +proof -
    3.87 +  have "inv (f^^n) x = ((inv f)^^n) x" for x
    3.88 +  apply (rule inv_into_f_eq, auto simp add: inj_fn[OF bij_is_inj[OF assms]])
    3.89 +  using fn_o_inv_fn_is_id[OF assms, of n] by (metis comp_apply)
    3.90 +  then show ?thesis by auto
    3.91 +qed
    3.92 +
    3.93 +
    3.94 +lemma mono_inv:
    3.95 +  fixes f::"'a::linorder \<Rightarrow> 'b::linorder"
    3.96 +  assumes "mono f" "bij f"
    3.97 +  shows "mono (inv f)"
    3.98 +proof
    3.99 +  fix x y::'b assume "x \<le> y"
   3.100 +  then show "inv f x \<le> inv f y"
   3.101 +    by (metis (no_types, lifting) assms bij_is_surj eq_iff le_cases mono_def surj_f_inv_f)
   3.102 +qed
   3.103 +
   3.104 +lemma mono_bij_Inf:
   3.105 +  fixes f :: "'a::complete_linorder \<Rightarrow> 'b::complete_linorder"
   3.106 +  assumes "mono f" "bij f"
   3.107 +  shows "f (Inf A) = Inf (f`A)"
   3.108 +proof -
   3.109 +  have "(inv f) (Inf (f`A)) \<le> Inf ((inv f)`(f`A))"
   3.110 +    using mono_Inf[OF mono_inv[OF assms], of "f`A"] by simp
   3.111 +  then have "Inf (f`A) \<le> f (Inf ((inv f)`(f`A)))"
   3.112 +    by (metis (no_types, lifting) assms mono_def bij_inv_eq_iff)
   3.113 +  also have "... = f(Inf A)"
   3.114 +    using assms by (simp add: bij_is_inj)
   3.115 +  finally show ?thesis using mono_Inf[OF assms(1), of A] by auto
   3.116 +qed
   3.117 +
   3.118 +
   3.119 +lemma Inf_nat_def1:
   3.120 +  fixes K::"nat set"
   3.121 +  assumes "K \<noteq> {}"
   3.122 +  shows "Inf K \<in> K"
   3.123 +by (auto simp add: Min_def Inf_nat_def) (meson LeastI assms bot.extremum_unique subsetI)
   3.124 +
   3.125 +subsection {*Liminf-Limsup.thy*}
   3.126 +
   3.127 +lemma limsup_shift:
   3.128 +  "limsup (\<lambda>n. u (n+1)) = limsup u"
   3.129 +proof -
   3.130 +  have "(SUP m:{n+1..}. u m) = (SUP m:{n..}. u (m + 1))" for n
   3.131 +    apply (rule SUP_eq) using Suc_le_D by auto
   3.132 +  then have a: "(INF n. SUP m:{n..}. u (m + 1)) = (INF n. (SUP m:{n+1..}. u m))" by auto
   3.133 +  have b: "(INF n. (SUP m:{n+1..}. u m)) = (INF n:{1..}. (SUP m:{n..}. u m))"
   3.134 +    apply (rule INF_eq) using Suc_le_D by auto
   3.135 +  have "(INF n:{1..}. v n) = (INF n. v n)" if "decseq v" for v::"nat \<Rightarrow> 'a"
   3.136 +    apply (rule INF_eq) using `decseq v` decseq_Suc_iff by auto
   3.137 +  moreover have "decseq (\<lambda>n. (SUP m:{n..}. u m))" by (simp add: SUP_subset_mono decseq_def)
   3.138 +  ultimately have c: "(INF n:{1..}. (SUP m:{n..}. u m)) = (INF n. (SUP m:{n..}. u m))" by simp
   3.139 +  have "(INF n. SUPREMUM {n..} u) = (INF n. SUP m:{n..}. u (m + 1))" using a b c by simp
   3.140 +  then show ?thesis by (auto cong: limsup_INF_SUP)
   3.141 +qed
   3.142 +
   3.143 +lemma limsup_shift_k:
   3.144 +  "limsup (\<lambda>n. u (n+k)) = limsup u"
   3.145 +proof (induction k)
   3.146 +  case (Suc k)
   3.147 +  have "limsup (\<lambda>n. u (n+k+1)) = limsup (\<lambda>n. u (n+k))" using limsup_shift[where ?u="\<lambda>n. u(n+k)"] by simp
   3.148 +  then show ?case using Suc.IH by simp
   3.149 +qed (auto)
   3.150 +
   3.151 +lemma liminf_shift:
   3.152 +  "liminf (\<lambda>n. u (n+1)) = liminf u"
   3.153 +proof -
   3.154 +  have "(INF m:{n+1..}. u m) = (INF m:{n..}. u (m + 1))" for n
   3.155 +    apply (rule INF_eq) using Suc_le_D by (auto)
   3.156 +  then have a: "(SUP n. INF m:{n..}. u (m + 1)) = (SUP n. (INF m:{n+1..}. u m))" by auto
   3.157 +  have b: "(SUP n. (INF m:{n+1..}. u m)) = (SUP n:{1..}. (INF m:{n..}. u m))"
   3.158 +    apply (rule SUP_eq) using Suc_le_D by (auto)
   3.159 +  have "(SUP n:{1..}. v n) = (SUP n. v n)" if "incseq v" for v::"nat \<Rightarrow> 'a"
   3.160 +    apply (rule SUP_eq) using `incseq v` incseq_Suc_iff by auto
   3.161 +  moreover have "incseq (\<lambda>n. (INF m:{n..}. u m))" by (simp add: INF_superset_mono mono_def)
   3.162 +  ultimately have c: "(SUP n:{1..}. (INF m:{n..}. u m)) = (SUP n. (INF m:{n..}. u m))" by simp
   3.163 +  have "(SUP n. INFIMUM {n..} u) = (SUP n. INF m:{n..}. u (m + 1))" using a b c by simp
   3.164 +  then show ?thesis by (auto cong: liminf_SUP_INF)
   3.165 +qed
   3.166 +
   3.167 +lemma liminf_shift_k:
   3.168 +  "liminf (\<lambda>n. u (n+k)) = liminf u"
   3.169 +proof (induction k)
   3.170 +  case (Suc k)
   3.171 +  have "liminf (\<lambda>n. u (n+k+1)) = liminf (\<lambda>n. u (n+k))" using liminf_shift[where ?u="\<lambda>n. u(n+k)"] by simp
   3.172 +  then show ?case using Suc.IH by simp
   3.173 +qed (auto)
   3.174 +
   3.175 +lemma Limsup_obtain:
   3.176 +  fixes u::"_ \<Rightarrow> 'a :: complete_linorder"
   3.177 +  assumes "Limsup F u > c"
   3.178 +  shows "\<exists>i. u i > c"
   3.179 +proof -
   3.180 +  have "(INF P:{P. eventually P F}. SUP x:{x. P x}. u x) > c" using assms by (simp add: Limsup_def)
   3.181 +  then show ?thesis by (metis eventually_True mem_Collect_eq less_INF_D less_SUP_iff)
   3.182 +qed
   3.183 +
   3.184 +text {* The next lemma is extremely useful, as it often makes it possible to reduce statements
   3.185 +about limsups to statements about limits.*}
   3.186 +
   3.187 +lemma limsup_subseq_lim:
   3.188 +  fixes u::"nat \<Rightarrow> 'a :: {complete_linorder, linorder_topology}"
   3.189 +  shows "\<exists>r. subseq r \<and> (u o r) \<longlonglongrightarrow> limsup u"
   3.190 +proof (cases)
   3.191 +  assume "\<forall>n. \<exists>p>n. \<forall>m\<ge>p. u m \<le> u p"
   3.192 +  then have "\<exists>r. \<forall>n. (\<forall>m\<ge>r n. u m \<le> u (r n)) \<and> r n < r (Suc n)"
   3.193 +    by (intro dependent_nat_choice) (auto simp: conj_commute)
   3.194 +  then obtain r where "subseq r" and mono: "\<And>n m. r n \<le> m \<Longrightarrow> u m \<le> u (r n)"
   3.195 +    by (auto simp: subseq_Suc_iff)
   3.196 +  define umax where "umax = (\<lambda>n. (SUP m:{n..}. u m))"
   3.197 +  have "decseq umax" unfolding umax_def by (simp add: SUP_subset_mono antimono_def)
   3.198 +  then have "umax \<longlonglongrightarrow> limsup u" unfolding umax_def by (metis LIMSEQ_INF limsup_INF_SUP)
   3.199 +  then have *: "(umax o r) \<longlonglongrightarrow> limsup u" by (simp add: LIMSEQ_subseq_LIMSEQ `subseq r`)
   3.200 +  have "\<And>n. umax(r n) = u(r n)" unfolding umax_def using mono
   3.201 +    by (metis SUP_le_iff antisym atLeast_def mem_Collect_eq order_refl)
   3.202 +  then have "umax o r = u o r" unfolding o_def by simp
   3.203 +  then have "(u o r) \<longlonglongrightarrow> limsup u" using * by simp
   3.204 +  then show ?thesis using `subseq r` by blast
   3.205 +next
   3.206 +  assume "\<not> (\<forall>n. \<exists>p>n. (\<forall>m\<ge>p. u m \<le> u p))"
   3.207 +  then obtain N where N: "\<And>p. p > N \<Longrightarrow> \<exists>m>p. u p < u m" by (force simp: not_le le_less)
   3.208 +  have "\<exists>r. \<forall>n. N < r n \<and> r n < r (Suc n) \<and> (\<forall>i\<in> {N<..r (Suc n)}. u i \<le> u (r (Suc n)))"
   3.209 +  proof (rule dependent_nat_choice)
   3.210 +    fix x assume "N < x"
   3.211 +    then have a: "finite {N<..x}" "{N<..x} \<noteq> {}" by simp_all
   3.212 +    have "Max {u i |i. i \<in> {N<..x}} \<in> {u i |i. i \<in> {N<..x}}" apply (rule Max_in) using a by (auto)
   3.213 +    then obtain p where "p \<in> {N<..x}" and upmax: "u p = Max{u i |i. i \<in> {N<..x}}" by auto
   3.214 +    define U where "U = {m. m > p \<and> u p < u m}"
   3.215 +    have "U \<noteq> {}" unfolding U_def using N[of p] `p \<in> {N<..x}` by auto
   3.216 +    define y where "y = Inf U"
   3.217 +    then have "y \<in> U" using `U \<noteq> {}` by (simp add: Inf_nat_def1)
   3.218 +    have a: "\<And>i. i \<in> {N<..x} \<Longrightarrow> u i \<le> u p"
   3.219 +    proof -
   3.220 +      fix i assume "i \<in> {N<..x}"
   3.221 +      then have "u i \<in> {u i |i. i \<in> {N<..x}}" by blast
   3.222 +      then show "u i \<le> u p" using upmax by simp
   3.223 +    qed
   3.224 +    moreover have "u p < u y" using `y \<in> U` U_def by auto
   3.225 +    ultimately have "y \<notin> {N<..x}" using not_le by blast
   3.226 +    moreover have "y > N" using `y \<in> U` U_def `p \<in> {N<..x}` by auto
   3.227 +    ultimately have "y > x" by auto
   3.228 +
   3.229 +    have "\<And>i. i \<in> {N<..y} \<Longrightarrow> u i \<le> u y"
   3.230 +    proof -
   3.231 +      fix i assume "i \<in> {N<..y}" show "u i \<le> u y"
   3.232 +      proof (cases)
   3.233 +        assume "i = y"
   3.234 +        then show ?thesis by simp
   3.235 +      next
   3.236 +        assume "\<not>(i=y)"
   3.237 +        then have i:"i \<in> {N<..<y}" using `i \<in> {N<..y}` by simp
   3.238 +        have "u i \<le> u p"
   3.239 +        proof (cases)
   3.240 +          assume "i \<le> x"
   3.241 +          then have "i \<in> {N<..x}" using i by simp
   3.242 +          then show ?thesis using a by simp
   3.243 +        next
   3.244 +          assume "\<not>(i \<le> x)"
   3.245 +          then have "i > x" by simp
   3.246 +          then have *: "i > p" using `p \<in> {N<..x}` by simp
   3.247 +          have "i < Inf U" using i y_def by simp
   3.248 +          then have "i \<notin> U" using Inf_nat_def not_less_Least by auto
   3.249 +          then show ?thesis using U_def * by auto
   3.250 +        qed
   3.251 +        then show "u i \<le> u y" using `u p < u y` by auto
   3.252 +      qed
   3.253 +    qed
   3.254 +    then have "N < y \<and> x < y \<and> (\<forall>i\<in>{N<..y}. u i \<le> u y)" using `y > x` `y > N` by auto
   3.255 +    then show "\<exists>y>N. x < y \<and> (\<forall>i\<in>{N<..y}. u i \<le> u y)" by auto
   3.256 +  qed (auto)
   3.257 +  then obtain r where r: "\<forall>n. N < r n \<and> r n < r (Suc n) \<and> (\<forall>i\<in> {N<..r (Suc n)}. u i \<le> u (r (Suc n)))" by auto
   3.258 +  have "subseq r" using r by (auto simp: subseq_Suc_iff)
   3.259 +  have "incseq (u o r)" unfolding o_def using r by (simp add: incseq_SucI order.strict_implies_order)
   3.260 +  then have "(u o r) \<longlonglongrightarrow> (SUP n. (u o r) n)" using LIMSEQ_SUP by blast
   3.261 +  then have "limsup (u o r) = (SUP n. (u o r) n)" by (simp add: lim_imp_Limsup)
   3.262 +  moreover have "limsup (u o r) \<le> limsup u" using `subseq r` by (simp add: limsup_subseq_mono)
   3.263 +  ultimately have "(SUP n. (u o r) n) \<le> limsup u" by simp
   3.264 +
   3.265 +  {
   3.266 +    fix i assume i: "i \<in> {N<..}"
   3.267 +    obtain n where "i < r (Suc n)" using `subseq r` using Suc_le_eq seq_suble by blast
   3.268 +    then have "i \<in> {N<..r(Suc n)}" using i by simp
   3.269 +    then have "u i \<le> u (r(Suc n))" using r by simp
   3.270 +    then have "u i \<le> (SUP n. (u o r) n)" unfolding o_def by (meson SUP_upper2 UNIV_I)
   3.271 +  }
   3.272 +  then have "(SUP i:{N<..}. u i) \<le> (SUP n. (u o r) n)" using SUP_least by blast
   3.273 +  then have "limsup u \<le> (SUP n. (u o r) n)" unfolding Limsup_def
   3.274 +    by (metis (mono_tags, lifting) INF_lower2 atLeast_Suc_greaterThan atLeast_def eventually_ge_at_top mem_Collect_eq)
   3.275 +  then have "limsup u = (SUP n. (u o r) n)" using `(SUP n. (u o r) n) \<le> limsup u` by simp
   3.276 +  then have "(u o r) \<longlonglongrightarrow> limsup u" using `(u o r) \<longlonglongrightarrow> (SUP n. (u o r) n)` by simp
   3.277 +  then show ?thesis using `subseq r` by auto
   3.278 +qed
   3.279 +
   3.280 +lemma liminf_subseq_lim:
   3.281 +  fixes u::"nat \<Rightarrow> 'a :: {complete_linorder, linorder_topology}"
   3.282 +  shows "\<exists>r. subseq r \<and> (u o r) \<longlonglongrightarrow> liminf u"
   3.283 +proof (cases)
   3.284 +  assume "\<forall>n. \<exists>p>n. \<forall>m\<ge>p. u m \<ge> u p"
   3.285 +  then have "\<exists>r. \<forall>n. (\<forall>m\<ge>r n. u m \<ge> u (r n)) \<and> r n < r (Suc n)"
   3.286 +    by (intro dependent_nat_choice) (auto simp: conj_commute)
   3.287 +  then obtain r where "subseq r" and mono: "\<And>n m. r n \<le> m \<Longrightarrow> u m \<ge> u (r n)"
   3.288 +    by (auto simp: subseq_Suc_iff)
   3.289 +  define umin where "umin = (\<lambda>n. (INF m:{n..}. u m))"
   3.290 +  have "incseq umin" unfolding umin_def by (simp add: INF_superset_mono incseq_def)
   3.291 +  then have "umin \<longlonglongrightarrow> liminf u" unfolding umin_def by (metis LIMSEQ_SUP liminf_SUP_INF)
   3.292 +  then have *: "(umin o r) \<longlonglongrightarrow> liminf u" by (simp add: LIMSEQ_subseq_LIMSEQ `subseq r`)
   3.293 +  have "\<And>n. umin(r n) = u(r n)" unfolding umin_def using mono
   3.294 +    by (metis le_INF_iff antisym atLeast_def mem_Collect_eq order_refl)
   3.295 +  then have "umin o r = u o r" unfolding o_def by simp
   3.296 +  then have "(u o r) \<longlonglongrightarrow> liminf u" using * by simp
   3.297 +  then show ?thesis using `subseq r` by blast
   3.298 +next
   3.299 +  assume "\<not> (\<forall>n. \<exists>p>n. (\<forall>m\<ge>p. u m \<ge> u p))"
   3.300 +  then obtain N where N: "\<And>p. p > N \<Longrightarrow> \<exists>m>p. u p > u m" by (force simp: not_le le_less)
   3.301 +  have "\<exists>r. \<forall>n. N < r n \<and> r n < r (Suc n) \<and> (\<forall>i\<in> {N<..r (Suc n)}. u i \<ge> u (r (Suc n)))"
   3.302 +  proof (rule dependent_nat_choice)
   3.303 +    fix x assume "N < x"
   3.304 +    then have a: "finite {N<..x}" "{N<..x} \<noteq> {}" by simp_all
   3.305 +    have "Min {u i |i. i \<in> {N<..x}} \<in> {u i |i. i \<in> {N<..x}}" apply (rule Min_in) using a by (auto)
   3.306 +    then obtain p where "p \<in> {N<..x}" and upmin: "u p = Min{u i |i. i \<in> {N<..x}}" by auto
   3.307 +    define U where "U = {m. m > p \<and> u p > u m}"
   3.308 +    have "U \<noteq> {}" unfolding U_def using N[of p] `p \<in> {N<..x}` by auto
   3.309 +    define y where "y = Inf U"
   3.310 +    then have "y \<in> U" using `U \<noteq> {}` by (simp add: Inf_nat_def1)
   3.311 +    have a: "\<And>i. i \<in> {N<..x} \<Longrightarrow> u i \<ge> u p"
   3.312 +    proof -
   3.313 +      fix i assume "i \<in> {N<..x}"
   3.314 +      then have "u i \<in> {u i |i. i \<in> {N<..x}}" by blast
   3.315 +      then show "u i \<ge> u p" using upmin by simp
   3.316 +    qed
   3.317 +    moreover have "u p > u y" using `y \<in> U` U_def by auto
   3.318 +    ultimately have "y \<notin> {N<..x}" using not_le by blast
   3.319 +    moreover have "y > N" using `y \<in> U` U_def `p \<in> {N<..x}` by auto
   3.320 +    ultimately have "y > x" by auto
   3.321 +
   3.322 +    have "\<And>i. i \<in> {N<..y} \<Longrightarrow> u i \<ge> u y"
   3.323 +    proof -
   3.324 +      fix i assume "i \<in> {N<..y}" show "u i \<ge> u y"
   3.325 +      proof (cases)
   3.326 +        assume "i = y"
   3.327 +        then show ?thesis by simp
   3.328 +      next
   3.329 +        assume "\<not>(i=y)"
   3.330 +        then have i:"i \<in> {N<..<y}" using `i \<in> {N<..y}` by simp
   3.331 +        have "u i \<ge> u p"
   3.332 +        proof (cases)
   3.333 +          assume "i \<le> x"
   3.334 +          then have "i \<in> {N<..x}" using i by simp
   3.335 +          then show ?thesis using a by simp
   3.336 +        next
   3.337 +          assume "\<not>(i \<le> x)"
   3.338 +          then have "i > x" by simp
   3.339 +          then have *: "i > p" using `p \<in> {N<..x}` by simp
   3.340 +          have "i < Inf U" using i y_def by simp
   3.341 +          then have "i \<notin> U" using Inf_nat_def not_less_Least by auto
   3.342 +          then show ?thesis using U_def * by auto
   3.343 +        qed
   3.344 +        then show "u i \<ge> u y" using `u p > u y` by auto
   3.345 +      qed
   3.346 +    qed
   3.347 +    then have "N < y \<and> x < y \<and> (\<forall>i\<in>{N<..y}. u i \<ge> u y)" using `y > x` `y > N` by auto
   3.348 +    then show "\<exists>y>N. x < y \<and> (\<forall>i\<in>{N<..y}. u i \<ge> u y)" by auto
   3.349 +  qed (auto)
   3.350 +  then obtain r where r: "\<forall>n. N < r n \<and> r n < r (Suc n) \<and> (\<forall>i\<in> {N<..r (Suc n)}. u i \<ge> u (r (Suc n)))" by auto
   3.351 +  have "subseq r" using r by (auto simp: subseq_Suc_iff)
   3.352 +  have "decseq (u o r)" unfolding o_def using r by (simp add: decseq_SucI order.strict_implies_order)
   3.353 +  then have "(u o r) \<longlonglongrightarrow> (INF n. (u o r) n)" using LIMSEQ_INF by blast
   3.354 +  then have "liminf (u o r) = (INF n. (u o r) n)" by (simp add: lim_imp_Liminf)
   3.355 +  moreover have "liminf (u o r) \<ge> liminf u" using `subseq r` by (simp add: liminf_subseq_mono)
   3.356 +  ultimately have "(INF n. (u o r) n) \<ge> liminf u" by simp
   3.357 +
   3.358 +  {
   3.359 +    fix i assume i: "i \<in> {N<..}"
   3.360 +    obtain n where "i < r (Suc n)" using `subseq r` using Suc_le_eq seq_suble by blast
   3.361 +    then have "i \<in> {N<..r(Suc n)}" using i by simp
   3.362 +    then have "u i \<ge> u (r(Suc n))" using r by simp
   3.363 +    then have "u i \<ge> (INF n. (u o r) n)" unfolding o_def by (meson INF_lower2 UNIV_I)
   3.364 +  }
   3.365 +  then have "(INF i:{N<..}. u i) \<ge> (INF n. (u o r) n)" using INF_greatest by blast
   3.366 +  then have "liminf u \<ge> (INF n. (u o r) n)" unfolding Liminf_def
   3.367 +    by (metis (mono_tags, lifting) SUP_upper2 atLeast_Suc_greaterThan atLeast_def eventually_ge_at_top mem_Collect_eq)
   3.368 +  then have "liminf u = (INF n. (u o r) n)" using `(INF n. (u o r) n) \<ge> liminf u` by simp
   3.369 +  then have "(u o r) \<longlonglongrightarrow> liminf u" using `(u o r) \<longlonglongrightarrow> (INF n. (u o r) n)` by simp
   3.370 +  then show ?thesis using `subseq r` by auto
   3.371 +qed
   3.372 +
   3.373 +
   3.374 +subsection {*Extended-Real.thy*}
   3.375 +
   3.376 +text{* The proof of this one is copied from \verb+ereal_add_mono+. *}
   3.377 +lemma ereal_add_strict_mono2:
   3.378 +  fixes a b c d :: ereal
   3.379 +  assumes "a < b"
   3.380 +    and "c < d"
   3.381 +  shows "a + c < b + d"
   3.382 +using assms
   3.383 +apply (cases a)
   3.384 +apply (cases rule: ereal3_cases[of b c d], auto)
   3.385 +apply (cases rule: ereal3_cases[of b c d], auto)
   3.386 +done
   3.387 +
   3.388 +text {* The next ones are analogues of \verb+mult_mono+ and \verb+mult_mono'+ in ereal.*}
   3.389 +
   3.390 +lemma ereal_mult_mono:
   3.391 +  fixes a b c d::ereal
   3.392 +  assumes "b \<ge> 0" "c \<ge> 0" "a \<le> b" "c \<le> d"
   3.393 +  shows "a * c \<le> b * d"
   3.394 +by (metis ereal_mult_right_mono mult.commute order_trans assms)
   3.395 +
   3.396 +lemma ereal_mult_mono':
   3.397 +  fixes a b c d::ereal
   3.398 +  assumes "a \<ge> 0" "c \<ge> 0" "a \<le> b" "c \<le> d"
   3.399 +  shows "a * c \<le> b * d"
   3.400 +by (metis ereal_mult_right_mono mult.commute order_trans assms)
   3.401 +
   3.402 +lemma ereal_mult_mono_strict:
   3.403 +  fixes a b c d::ereal
   3.404 +  assumes "b > 0" "c > 0" "a < b" "c < d"
   3.405 +  shows "a * c < b * d"
   3.406 +proof -
   3.407 +  have "c < \<infinity>" using `c < d` by auto
   3.408 +  then have "a * c < b * c" by (metis ereal_mult_strict_left_mono[OF assms(3) assms(2)] mult.commute)
   3.409 +  moreover have "b * c \<le> b * d" using assms(2) assms(4) by (simp add: assms(1) ereal_mult_left_mono less_imp_le)
   3.410 +  ultimately show ?thesis by simp
   3.411 +qed
   3.412 +
   3.413 +lemma ereal_mult_mono_strict':
   3.414 +  fixes a b c d::ereal
   3.415 +  assumes "a > 0" "c > 0" "a < b" "c < d"
   3.416 +  shows "a * c < b * d"
   3.417 +apply (rule ereal_mult_mono_strict, auto simp add: assms) using assms by auto
   3.418 +
   3.419 +lemma ereal_abs_add:
   3.420 +  fixes a b::ereal
   3.421 +  shows "abs(a+b) \<le> abs a + abs b"
   3.422 +by (cases rule: ereal2_cases[of a b]) (auto)
   3.423 +
   3.424 +lemma ereal_abs_diff:
   3.425 +  fixes a b::ereal
   3.426 +  shows "abs(a-b) \<le> abs a + abs b"
   3.427 +by (cases rule: ereal2_cases[of a b]) (auto)
   3.428 +
   3.429 +lemma sum_constant_ereal:
   3.430 +  fixes a::ereal
   3.431 +  shows "(\<Sum>i\<in>I. a) = a * card I"
   3.432 +apply (cases "finite I", induct set: finite, simp_all)
   3.433 +apply (cases a, auto, metis (no_types, hide_lams) add.commute mult.commute semiring_normalization_rules(3))
   3.434 +done
   3.435 +
   3.436 +lemma real_lim_then_eventually_real:
   3.437 +  assumes "(u \<longlongrightarrow> ereal l) F"
   3.438 +  shows "eventually (\<lambda>n. u n = ereal(real_of_ereal(u n))) F"
   3.439 +proof -
   3.440 +  have "ereal l \<in> {-\<infinity><..<(\<infinity>::ereal)}" by simp
   3.441 +  moreover have "open {-\<infinity><..<(\<infinity>::ereal)}" by simp
   3.442 +  ultimately have "eventually (\<lambda>n. u n \<in> {-\<infinity><..<(\<infinity>::ereal)}) F" using assms tendsto_def by blast
   3.443 +  moreover have "\<And>x. x \<in> {-\<infinity><..<(\<infinity>::ereal)} \<Longrightarrow> x = ereal(real_of_ereal x)" using ereal_real by auto
   3.444 +  ultimately show ?thesis by (metis (mono_tags, lifting) eventually_mono)
   3.445 +qed
   3.446 +
   3.447 +lemma ereal_Inf_cmult:
   3.448 +  assumes "c>(0::real)"
   3.449 +  shows "Inf {ereal c * x |x. P x} = ereal c * Inf {x. P x}"
   3.450 +proof -
   3.451 +  have "(\<lambda>x::ereal. c * x) (Inf {x::ereal. P x}) = Inf ((\<lambda>x::ereal. c * x)`{x::ereal. P x})"
   3.452 +    apply (rule mono_bij_Inf)
   3.453 +    apply (simp add: assms ereal_mult_left_mono less_imp_le mono_def)
   3.454 +    apply (rule bij_betw_byWitness[of _ "\<lambda>x. (x::ereal) / c"], auto simp add: assms ereal_mult_divide)
   3.455 +    using assms ereal_divide_eq apply auto
   3.456 +    done
   3.457 +  then show ?thesis by (simp only: setcompr_eq_image[symmetric])
   3.458 +qed
   3.459 +
   3.460 +
   3.461 +subsubsection {*Continuity of addition*}
   3.462 +
   3.463 +text {*The next few lemmas remove an unnecessary assumption in \verb+tendsto_add_ereal+, culminating
   3.464 +in \verb+tendsto_add_ereal_general+ which essentially says that the addition
   3.465 +is continuous on ereal times ereal, except at $(-\infty, \infty)$ and $(\infty, -\infty)$.
   3.466 +It is much more convenient in many situations, see for instance the proof of
   3.467 +\verb+tendsto_sum_ereal+ below. *}
   3.468 +
   3.469 +lemma tendsto_add_ereal_PInf:
   3.470 +  fixes y :: ereal
   3.471 +  assumes y: "y \<noteq> -\<infinity>"
   3.472 +  assumes f: "(f \<longlongrightarrow> \<infinity>) F" and g: "(g \<longlongrightarrow> y) F"
   3.473 +  shows "((\<lambda>x. f x + g x) \<longlongrightarrow> \<infinity>) F"
   3.474 +proof -
   3.475 +  have "\<exists>C. eventually (\<lambda>x. g x > ereal C) F"
   3.476 +  proof (cases y)
   3.477 +    case (real r)
   3.478 +    have "y > y-1" using y real by (simp add: ereal_between(1))
   3.479 +    then have "eventually (\<lambda>x. g x > y - 1) F" using g y order_tendsto_iff by auto
   3.480 +    moreover have "y-1 = ereal(real_of_ereal(y-1))"
   3.481 +      by (metis real ereal_eq_1(1) ereal_minus(1) real_of_ereal.simps(1))
   3.482 +    ultimately have "eventually (\<lambda>x. g x > ereal(real_of_ereal(y - 1))) F" by simp
   3.483 +    then show ?thesis by auto
   3.484 +  next
   3.485 +    case (PInf)
   3.486 +    have "eventually (\<lambda>x. g x > ereal 0) F" using g PInf by (simp add: tendsto_PInfty)
   3.487 +    then show ?thesis by auto
   3.488 +  qed (simp add: y)
   3.489 +  then obtain C::real where ge: "eventually (\<lambda>x. g x > ereal C) F" by auto
   3.490 +
   3.491 +  {
   3.492 +    fix M::real
   3.493 +    have "eventually (\<lambda>x. f x > ereal(M - C)) F" using f by (simp add: tendsto_PInfty)
   3.494 +    then have "eventually (\<lambda>x. (f x > ereal (M-C)) \<and> (g x > ereal C)) F"
   3.495 +      by (auto simp add: ge eventually_conj_iff)
   3.496 +    moreover have "\<And>x. ((f x > ereal (M-C)) \<and> (g x > ereal C)) \<Longrightarrow> (f x + g x > ereal M)"
   3.497 +      using ereal_add_strict_mono2 by fastforce
   3.498 +    ultimately have "eventually (\<lambda>x. f x + g x > ereal M) F" using eventually_mono by force
   3.499 +  }
   3.500 +  then show ?thesis by (simp add: tendsto_PInfty)
   3.501 +qed
   3.502 +
   3.503 +text{* One would like to deduce the next lemma from the previous one, but the fact
   3.504 +that $-(x+y)$ is in general different from $(-x) + (-y)$ in ereal creates difficulties,
   3.505 +so it is more efficient to copy the previous proof.*}
   3.506 +
   3.507 +lemma tendsto_add_ereal_MInf:
   3.508 +  fixes y :: ereal
   3.509 +  assumes y: "y \<noteq> \<infinity>"
   3.510 +  assumes f: "(f \<longlongrightarrow> -\<infinity>) F" and g: "(g \<longlongrightarrow> y) F"
   3.511 +  shows "((\<lambda>x. f x + g x) \<longlongrightarrow> -\<infinity>) F"
   3.512 +proof -
   3.513 +  have "\<exists>C. eventually (\<lambda>x. g x < ereal C) F"
   3.514 +  proof (cases y)
   3.515 +    case (real r)
   3.516 +    have "y < y+1" using y real by (simp add: ereal_between(1))
   3.517 +    then have "eventually (\<lambda>x. g x < y + 1) F" using g y order_tendsto_iff by force
   3.518 +    moreover have "y+1 = ereal(real_of_ereal (y+1))" by (simp add: real)
   3.519 +    ultimately have "eventually (\<lambda>x. g x < ereal(real_of_ereal(y + 1))) F" by simp
   3.520 +    then show ?thesis by auto
   3.521 +  next
   3.522 +    case (MInf)
   3.523 +    have "eventually (\<lambda>x. g x < ereal 0) F" using g MInf by (simp add: tendsto_MInfty)
   3.524 +    then show ?thesis by auto
   3.525 +  qed (simp add: y)
   3.526 +  then obtain C::real where ge: "eventually (\<lambda>x. g x < ereal C) F" by auto
   3.527 +
   3.528 +  {
   3.529 +    fix M::real
   3.530 +    have "eventually (\<lambda>x. f x < ereal(M - C)) F" using f by (simp add: tendsto_MInfty)
   3.531 +    then have "eventually (\<lambda>x. (f x < ereal (M- C)) \<and> (g x < ereal C)) F"
   3.532 +      by (auto simp add: ge eventually_conj_iff)
   3.533 +    moreover have "\<And>x. ((f x < ereal (M-C)) \<and> (g x < ereal C)) \<Longrightarrow> (f x + g x < ereal M)"
   3.534 +      using ereal_add_strict_mono2 by fastforce
   3.535 +    ultimately have "eventually (\<lambda>x. f x + g x < ereal M) F" using eventually_mono by force
   3.536 +  }
   3.537 +  then show ?thesis by (simp add: tendsto_MInfty)
   3.538 +qed
   3.539 +
   3.540 +lemma tendsto_add_ereal_general1:
   3.541 +  fixes x y :: ereal
   3.542 +  assumes y: "\<bar>y\<bar> \<noteq> \<infinity>"
   3.543 +  assumes f: "(f \<longlongrightarrow> x) F" and g: "(g \<longlongrightarrow> y) F"
   3.544 +  shows "((\<lambda>x. f x + g x) \<longlongrightarrow> x + y) F"
   3.545 +proof (cases x)
   3.546 +  case (real r)
   3.547 +  have a: "\<bar>x\<bar> \<noteq> \<infinity>" by (simp add: real)
   3.548 +  show ?thesis by (rule tendsto_add_ereal[OF a, OF y, OF f, OF g])
   3.549 +next
   3.550 +  case PInf
   3.551 +  then show ?thesis using tendsto_add_ereal_PInf assms by force
   3.552 +next
   3.553 +  case MInf
   3.554 +  then show ?thesis using tendsto_add_ereal_MInf assms
   3.555 +    by (metis abs_ereal.simps(3) ereal_MInfty_eq_plus)
   3.556 +qed
   3.557 +
   3.558 +lemma tendsto_add_ereal_general2:
   3.559 +  fixes x y :: ereal
   3.560 +  assumes x: "\<bar>x\<bar> \<noteq> \<infinity>"
   3.561 +      and f: "(f \<longlongrightarrow> x) F" and g: "(g \<longlongrightarrow> y) F"
   3.562 +  shows "((\<lambda>x. f x + g x) \<longlongrightarrow> x + y) F"
   3.563 +proof -
   3.564 +  have "((\<lambda>x. g x + f x) \<longlongrightarrow> x + y) F"
   3.565 +    using tendsto_add_ereal_general1[OF x, OF g, OF f] add.commute[of "y", of "x"] by simp
   3.566 +  moreover have "\<And>x. g x + f x = f x + g x" using add.commute by auto
   3.567 +  ultimately show ?thesis by simp
   3.568 +qed
   3.569 +
   3.570 +text {* The next lemma says that the addition is continuous on ereal, except at
   3.571 +the pairs $(-\infty, \infty)$ and $(\infty, -\infty)$. *}
   3.572 +
   3.573 +lemma tendsto_add_ereal_general [tendsto_intros]:
   3.574 +  fixes x y :: ereal
   3.575 +  assumes "\<not>((x=\<infinity> \<and> y=-\<infinity>) \<or> (x=-\<infinity> \<and> y=\<infinity>))"
   3.576 +      and f: "(f \<longlongrightarrow> x) F" and g: "(g \<longlongrightarrow> y) F"
   3.577 +  shows "((\<lambda>x. f x + g x) \<longlongrightarrow> x + y) F"
   3.578 +proof (cases x)
   3.579 +  case (real r)
   3.580 +  show ?thesis
   3.581 +    apply (rule tendsto_add_ereal_general2) using real assms by auto
   3.582 +next
   3.583 +  case (PInf)
   3.584 +  then have "y \<noteq> -\<infinity>" using assms by simp
   3.585 +  then show ?thesis using tendsto_add_ereal_PInf PInf assms by auto
   3.586 +next
   3.587 +  case (MInf)
   3.588 +  then have "y \<noteq> \<infinity>" using assms by simp
   3.589 +  then show ?thesis using tendsto_add_ereal_MInf MInf f g by (metis ereal_MInfty_eq_plus)
   3.590 +qed
   3.591 +
   3.592 +subsubsection {*Continuity of multiplication*}
   3.593 +
   3.594 +text {* In the same way as for addition, we prove that the multiplication is continuous on
   3.595 +ereal times ereal, except at $(\infty, 0)$ and $(-\infty, 0)$ and $(0, \infty)$ and $(0, -\infty)$,
   3.596 +starting with specific situations.*}
   3.597 +
   3.598 +lemma tendsto_mult_real_ereal:
   3.599 +  assumes "(u \<longlongrightarrow> ereal l) F" "(v \<longlongrightarrow> ereal m) F"
   3.600 +  shows "((\<lambda>n. u n * v n) \<longlongrightarrow> ereal l * ereal m) F"
   3.601 +proof -
   3.602 +  have ureal: "eventually (\<lambda>n. u n = ereal(real_of_ereal(u n))) F" by (rule real_lim_then_eventually_real[OF assms(1)])
   3.603 +  then have "((\<lambda>n. ereal(real_of_ereal(u n))) \<longlongrightarrow> ereal l) F" using assms by auto
   3.604 +  then have limu: "((\<lambda>n. real_of_ereal(u n)) \<longlongrightarrow> l) F" by auto
   3.605 +  have vreal: "eventually (\<lambda>n. v n = ereal(real_of_ereal(v n))) F" by (rule real_lim_then_eventually_real[OF assms(2)])
   3.606 +  then have "((\<lambda>n. ereal(real_of_ereal(v n))) \<longlongrightarrow> ereal m) F" using assms by auto
   3.607 +  then have limv: "((\<lambda>n. real_of_ereal(v n)) \<longlongrightarrow> m) F" by auto
   3.608 +
   3.609 +  {
   3.610 +    fix n assume "u n = ereal(real_of_ereal(u n))" "v n = ereal(real_of_ereal(v n))"
   3.611 +    then have "ereal(real_of_ereal(u n) * real_of_ereal(v n)) = u n * v n" by (metis times_ereal.simps(1))
   3.612 +  }
   3.613 +  then have *: "eventually (\<lambda>n. ereal(real_of_ereal(u n) * real_of_ereal(v n)) = u n * v n) F"
   3.614 +    using eventually_elim2[OF ureal vreal] by auto
   3.615 +
   3.616 +  have "((\<lambda>n. real_of_ereal(u n) * real_of_ereal(v n)) \<longlongrightarrow> l * m) F" using tendsto_mult[OF limu limv] by auto
   3.617 +  then have "((\<lambda>n. ereal(real_of_ereal(u n)) * real_of_ereal(v n)) \<longlongrightarrow> ereal(l * m)) F" by auto
   3.618 +  then show ?thesis using * filterlim_cong by fastforce
   3.619 +qed
   3.620 +
   3.621 +lemma tendsto_mult_ereal_PInf:
   3.622 +  fixes f g::"_ \<Rightarrow> ereal"
   3.623 +  assumes "(f \<longlongrightarrow> l) F" "l>0" "(g \<longlongrightarrow> \<infinity>) F"
   3.624 +  shows "((\<lambda>x. f x * g x) \<longlongrightarrow> \<infinity>) F"
   3.625 +proof -
   3.626 +  obtain a::real where "0 < ereal a" "a < l" using assms(2) using ereal_dense2 by blast
   3.627 +  have *: "eventually (\<lambda>x. f x > a) F" using `a < l` assms(1) by (simp add: order_tendsto_iff)
   3.628 +  {
   3.629 +    fix K::real
   3.630 +    define M where "M = max K 1"
   3.631 +    then have "M > 0" by simp
   3.632 +    then have "ereal(M/a) > 0" using `ereal a > 0` by simp
   3.633 +    then have "\<And>x. ((f x > a) \<and> (g x > M/a)) \<Longrightarrow> (f x * g x > ereal a * ereal(M/a))"
   3.634 +      using ereal_mult_mono_strict'[where ?c = "M/a", OF `0 < ereal a`] by auto
   3.635 +    moreover have "ereal a * ereal(M/a) = M" using `ereal a > 0` by simp
   3.636 +    ultimately have "\<And>x. ((f x > a) \<and> (g x > M/a)) \<Longrightarrow> (f x * g x > M)" by simp
   3.637 +    moreover have "M \<ge> K" unfolding M_def by simp
   3.638 +    ultimately have Imp: "\<And>x. ((f x > a) \<and> (g x > M/a)) \<Longrightarrow> (f x * g x > K)"
   3.639 +      using ereal_less_eq(3) le_less_trans by blast
   3.640 +
   3.641 +    have "eventually (\<lambda>x. g x > M/a) F" using assms(3) by (simp add: tendsto_PInfty)
   3.642 +    then have "eventually (\<lambda>x. (f x > a) \<and> (g x > M/a)) F"
   3.643 +      using * by (auto simp add: eventually_conj_iff)
   3.644 +    then have "eventually (\<lambda>x. f x * g x > K) F" using eventually_mono Imp by force
   3.645 +  }
   3.646 +  then show ?thesis by (auto simp add: tendsto_PInfty)
   3.647 +qed
   3.648 +
   3.649 +lemma tendsto_mult_ereal_pos:
   3.650 +  fixes f g::"_ \<Rightarrow> ereal"
   3.651 +  assumes "(f \<longlongrightarrow> l) F" "(g \<longlongrightarrow> m) F" "l>0" "m>0"
   3.652 +  shows "((\<lambda>x. f x * g x) \<longlongrightarrow> l * m) F"
   3.653 +proof (cases)
   3.654 +  assume *: "l = \<infinity> \<or> m = \<infinity>"
   3.655 +  then show ?thesis
   3.656 +  proof (cases)
   3.657 +    assume "m = \<infinity>"
   3.658 +    then show ?thesis using tendsto_mult_ereal_PInf assms by auto
   3.659 +  next
   3.660 +    assume "\<not>(m = \<infinity>)"
   3.661 +    then have "l = \<infinity>" using * by simp
   3.662 +    then have "((\<lambda>x. g x * f x) \<longlongrightarrow> l * m) F" using tendsto_mult_ereal_PInf assms by auto
   3.663 +    moreover have "\<And>x. g x * f x = f x * g x" using mult.commute by auto
   3.664 +    ultimately show ?thesis by simp
   3.665 +  qed
   3.666 +next
   3.667 +  assume "\<not>(l = \<infinity> \<or> m = \<infinity>)"
   3.668 +  then have "l < \<infinity>" "m < \<infinity>" by auto
   3.669 +  then obtain lr mr where "l = ereal lr" "m = ereal mr"
   3.670 +    using `l>0` `m>0` by (metis ereal_cases ereal_less(6) not_less_iff_gr_or_eq)
   3.671 +  then show ?thesis using tendsto_mult_real_ereal assms by auto
   3.672 +qed
   3.673 +
   3.674 +text {*We reduce the general situation to the positive case by multiplying by suitable signs.
   3.675 +Unfortunately, as ereal is not a ring, all the neat sign lemmas are not available there. We
   3.676 +give the bare minimum we need.*}
   3.677 +
   3.678 +lemma ereal_sgn_abs:
   3.679 +  fixes l::ereal
   3.680 +  shows "sgn(l) * l = abs(l)"
   3.681 +apply (cases l) by (auto simp add: sgn_if ereal_less_uminus_reorder)
   3.682 +
   3.683 +lemma sgn_squared_ereal:
   3.684 +  assumes "l \<noteq> (0::ereal)"
   3.685 +  shows "sgn(l) * sgn(l) = 1"
   3.686 +apply (cases l) using assms by (auto simp add: one_ereal_def sgn_if)
   3.687 +
   3.688 +lemma tendsto_mult_ereal [tendsto_intros]:
   3.689 +  fixes f g::"_ \<Rightarrow> ereal"
   3.690 +  assumes "(f \<longlongrightarrow> l) F" "(g \<longlongrightarrow> m) F" "\<not>((l=0 \<and> abs(m) = \<infinity>) \<or> (m=0 \<and> abs(l) = \<infinity>))"
   3.691 +  shows "((\<lambda>x. f x * g x) \<longlongrightarrow> l * m) F"
   3.692 +proof (cases)
   3.693 +  assume "l=0 \<or> m=0"
   3.694 +  then have "abs(l) \<noteq> \<infinity>" "abs(m) \<noteq> \<infinity>" using assms(3) by auto
   3.695 +  then obtain lr mr where "l = ereal lr" "m = ereal mr" by auto
   3.696 +  then show ?thesis using tendsto_mult_real_ereal assms by auto
   3.697 +next
   3.698 +  have sgn_finite: "\<And>a::ereal. abs(sgn a) \<noteq> \<infinity>"
   3.699 +    by (metis MInfty_neq_ereal(2) PInfty_neq_ereal(2) abs_eq_infinity_cases ereal_times(1) ereal_times(3) ereal_uminus_eq_reorder sgn_ereal.elims)
   3.700 +  then have sgn_finite2: "\<And>a b::ereal. abs(sgn a * sgn b) \<noteq> \<infinity>"
   3.701 +    by (metis abs_eq_infinity_cases abs_ereal.simps(2) abs_ereal.simps(3) ereal_mult_eq_MInfty ereal_mult_eq_PInfty)
   3.702 +  assume "\<not>(l=0 \<or> m=0)"
   3.703 +  then have "l \<noteq> 0" "m \<noteq> 0" by auto
   3.704 +  then have "abs(l) > 0" "abs(m) > 0"
   3.705 +    by (metis abs_ereal_ge0 abs_ereal_less0 abs_ereal_pos ereal_uminus_uminus ereal_uminus_zero less_le not_less)+
   3.706 +  then have "sgn(l) * l > 0" "sgn(m) * m > 0" using ereal_sgn_abs by auto
   3.707 +  moreover have "((\<lambda>x. sgn(l) * f x) \<longlongrightarrow> (sgn(l) * l)) F"
   3.708 +    by (rule tendsto_cmult_ereal, auto simp add: sgn_finite assms(1))
   3.709 +  moreover have "((\<lambda>x. sgn(m) * g x) \<longlongrightarrow> (sgn(m) * m)) F"
   3.710 +    by (rule tendsto_cmult_ereal, auto simp add: sgn_finite assms(2))
   3.711 +  ultimately have *: "((\<lambda>x. (sgn(l) * f x) * (sgn(m) * g x)) \<longlongrightarrow> (sgn(l) * l) * (sgn(m) * m)) F"
   3.712 +    using tendsto_mult_ereal_pos by force
   3.713 +  have "((\<lambda>x. (sgn(l) * sgn(m)) * ((sgn(l) * f x) * (sgn(m) * g x))) \<longlongrightarrow> (sgn(l) * sgn(m)) * ((sgn(l) * l) * (sgn(m) * m))) F"
   3.714 +    by (rule tendsto_cmult_ereal, auto simp add: sgn_finite2 *)
   3.715 +  moreover have "\<And>x. (sgn(l) * sgn(m)) * ((sgn(l) * f x) * (sgn(m) * g x)) = f x * g x"
   3.716 +    by (metis mult.left_neutral sgn_squared_ereal[OF `l \<noteq> 0`] sgn_squared_ereal[OF `m \<noteq> 0`] mult.assoc mult.commute)
   3.717 +  moreover have "(sgn(l) * sgn(m)) * ((sgn(l) * l) * (sgn(m) * m)) = l * m"
   3.718 +    by (metis mult.left_neutral sgn_squared_ereal[OF `l \<noteq> 0`] sgn_squared_ereal[OF `m \<noteq> 0`] mult.assoc mult.commute)
   3.719 +  ultimately show ?thesis by auto
   3.720 +qed
   3.721 +
   3.722 +lemma tendsto_cmult_ereal_general [tendsto_intros]:
   3.723 +  fixes f::"_ \<Rightarrow> ereal" and c::ereal
   3.724 +  assumes "(f \<longlongrightarrow> l) F" "\<not> (l=0 \<and> abs(c) = \<infinity>)"
   3.725 +  shows "((\<lambda>x. c * f x) \<longlongrightarrow> c * l) F"
   3.726 +by (cases "c = 0", auto simp add: assms tendsto_mult_ereal)
   3.727 +
   3.728 +
   3.729 +subsubsection {*Continuity of division*}
   3.730 +
   3.731 +lemma tendsto_inverse_ereal_PInf:
   3.732 +  fixes u::"_ \<Rightarrow> ereal"
   3.733 +  assumes "(u \<longlongrightarrow> \<infinity>) F"
   3.734 +  shows "((\<lambda>x. 1/ u x) \<longlongrightarrow> 0) F"
   3.735 +proof -
   3.736 +  {
   3.737 +    fix e::real assume "e>0"
   3.738 +    have "1/e < \<infinity>" by auto
   3.739 +    then have "eventually (\<lambda>n. u n > 1/e) F" using assms(1) by (simp add: tendsto_PInfty)
   3.740 +    moreover
   3.741 +    {
   3.742 +      fix z::ereal assume "z>1/e"
   3.743 +      then have "z>0" using `e>0` using less_le_trans not_le by fastforce
   3.744 +      then have "1/z \<ge> 0" by auto
   3.745 +      moreover have "1/z < e" using `e>0` `z>1/e`
   3.746 +        apply (cases z) apply auto
   3.747 +        by (metis (mono_tags, hide_lams) less_ereal.simps(2) less_ereal.simps(4) divide_less_eq ereal_divide_less_pos ereal_less(4)
   3.748 +            ereal_less_eq(4) less_le_trans mult_eq_0_iff not_le not_one_less_zero times_ereal.simps(1))
   3.749 +      ultimately have "1/z \<ge> 0" "1/z < e" by auto
   3.750 +    }
   3.751 +    ultimately have "eventually (\<lambda>n. 1/u n<e) F" "eventually (\<lambda>n. 1/u n\<ge>0) F" by (auto simp add: eventually_mono)
   3.752 +  } note * = this
   3.753 +  show ?thesis
   3.754 +  proof (subst order_tendsto_iff, auto)
   3.755 +    fix a::ereal assume "a<0"
   3.756 +    then show "eventually (\<lambda>n. 1/u n > a) F" using *(2) eventually_mono less_le_trans linordered_field_no_ub by fastforce
   3.757 +  next
   3.758 +    fix a::ereal assume "a>0"
   3.759 +    then obtain e::real where "e>0" "a>e" using ereal_dense2 ereal_less(2) by blast
   3.760 +    then have "eventually (\<lambda>n. 1/u n < e) F" using *(1) by auto
   3.761 +    then show "eventually (\<lambda>n. 1/u n < a) F" using `a>e` by (metis (mono_tags, lifting) eventually_mono less_trans)
   3.762 +  qed
   3.763 +qed
   3.764 +
   3.765 +text {* The next lemma deserves to exist by itself, as it is so common and useful. *}
   3.766 +
   3.767 +lemma tendsto_inverse_real [tendsto_intros]:
   3.768 +  fixes u::"_ \<Rightarrow> real"
   3.769 +  shows "(u \<longlongrightarrow> l) F \<Longrightarrow> l \<noteq> 0 \<Longrightarrow> ((\<lambda>x. 1/ u x) \<longlongrightarrow> 1/l) F"
   3.770 +  using tendsto_inverse unfolding inverse_eq_divide .
   3.771 +
   3.772 +lemma tendsto_inverse_ereal [tendsto_intros]:
   3.773 +  fixes u::"_ \<Rightarrow> ereal"
   3.774 +  assumes "(u \<longlongrightarrow> l) F" "l \<noteq> 0"
   3.775 +  shows "((\<lambda>x. 1/ u x) \<longlongrightarrow> 1/l) F"
   3.776 +proof (cases l)
   3.777 +  case (real r)
   3.778 +  then have "r \<noteq> 0" using assms(2) by auto
   3.779 +  then have "1/l = ereal(1/r)" using real by (simp add: one_ereal_def)
   3.780 +  define v where "v = (\<lambda>n. real_of_ereal(u n))"
   3.781 +  have ureal: "eventually (\<lambda>n. u n = ereal(v n)) F" unfolding v_def using real_lim_then_eventually_real assms(1) real by auto
   3.782 +  then have "((\<lambda>n. ereal(v n)) \<longlongrightarrow> ereal r) F" using assms real v_def by auto
   3.783 +  then have *: "((\<lambda>n. v n) \<longlongrightarrow> r) F" by auto
   3.784 +  then have "((\<lambda>n. 1/v n) \<longlongrightarrow> 1/r) F" using \<open>r \<noteq> 0\<close> tendsto_inverse_real by auto
   3.785 +  then have lim: "((\<lambda>n. ereal(1/v n)) \<longlongrightarrow> 1/l) F" using \<open>1/l = ereal(1/r)\<close> by auto
   3.786 +
   3.787 +  have "r \<in> -{0}" "open (-{(0::real)})" using \<open>r \<noteq> 0\<close> by auto
   3.788 +  then have "eventually (\<lambda>n. v n \<in> -{0}) F" using * using topological_tendstoD by blast
   3.789 +  then have "eventually (\<lambda>n. v n \<noteq> 0) F" by auto
   3.790 +  moreover
   3.791 +  {
   3.792 +    fix n assume H: "v n \<noteq> 0" "u n = ereal(v n)"
   3.793 +    then have "ereal(1/v n) = 1/ereal(v n)" by (simp add: one_ereal_def)
   3.794 +    then have "ereal(1/v n) = 1/u n" using H(2) by simp
   3.795 +  }
   3.796 +  ultimately have "eventually (\<lambda>n. ereal(1/v n) = 1/u n) F" using ureal eventually_elim2 by force
   3.797 +  with Lim_transform_eventually[OF this lim] show ?thesis by simp
   3.798 +next
   3.799 +  case (PInf)
   3.800 +  then have "1/l = 0" by auto
   3.801 +  then show ?thesis using tendsto_inverse_ereal_PInf assms PInf by auto
   3.802 +next
   3.803 +  case (MInf)
   3.804 +  then have "1/l = 0" by auto
   3.805 +  have "1/z = -1/ -z" if "z < 0" for z::ereal
   3.806 +    apply (cases z) using divide_ereal_def \<open> z < 0 \<close> by auto
   3.807 +  moreover have "eventually (\<lambda>n. u n < 0) F" by (metis (no_types) MInf assms(1) tendsto_MInfty zero_ereal_def)
   3.808 +  ultimately have *: "eventually (\<lambda>n. -1/-u n = 1/u n) F" by (simp add: eventually_mono)
   3.809 +
   3.810 +  define v where "v = (\<lambda>n. - u n)"
   3.811 +  have "(v \<longlongrightarrow> \<infinity>) F" unfolding v_def using MInf assms(1) tendsto_uminus_ereal by fastforce
   3.812 +  then have "((\<lambda>n. 1/v n) \<longlongrightarrow> 0) F" using tendsto_inverse_ereal_PInf by auto
   3.813 +  then have "((\<lambda>n. -1/v n) \<longlongrightarrow> 0) F" using tendsto_uminus_ereal by fastforce
   3.814 +  then show ?thesis unfolding v_def using Lim_transform_eventually[OF *] \<open> 1/l = 0 \<close> by auto
   3.815 +qed
   3.816 +
   3.817 +lemma tendsto_divide_ereal [tendsto_intros]:
   3.818 +  fixes f g::"_ \<Rightarrow> ereal"
   3.819 +  assumes "(f \<longlongrightarrow> l) F" "(g \<longlongrightarrow> m) F" "m \<noteq> 0" "\<not>(abs(l) = \<infinity> \<and> abs(m) = \<infinity>)"
   3.820 +  shows "((\<lambda>x. f x / g x) \<longlongrightarrow> l / m) F"
   3.821 +proof -
   3.822 +  define h where "h = (\<lambda>x. 1/ g x)"
   3.823 +  have *: "(h \<longlongrightarrow> 1/m) F" unfolding h_def using assms(2) assms(3) tendsto_inverse_ereal by auto
   3.824 +  have "((\<lambda>x. f x * h x) \<longlongrightarrow> l * (1/m)) F"
   3.825 +    apply (rule tendsto_mult_ereal[OF assms(1) *]) using assms(3) assms(4) by (auto simp add: divide_ereal_def)
   3.826 +  moreover have "f x * h x = f x / g x" for x unfolding h_def by (simp add: divide_ereal_def)
   3.827 +  moreover have "l * (1/m) = l/m" by (simp add: divide_ereal_def)
   3.828 +  ultimately show ?thesis unfolding h_def using Lim_transform_eventually by auto
   3.829 +qed
   3.830 +
   3.831 +
   3.832 +subsubsection {*Further limits*}
   3.833 +
   3.834 +lemma id_nat_ereal_tendsto_PInf [tendsto_intros]:
   3.835 +  "(\<lambda> n::nat. real n) \<longlonglongrightarrow> \<infinity>"
   3.836 +by (simp add: filterlim_real_sequentially tendsto_PInfty_eq_at_top)
   3.837 +
   3.838 +lemma tendsto_at_top_pseudo_inverse [tendsto_intros]:
   3.839 +  fixes u::"nat \<Rightarrow> nat"
   3.840 +  assumes "LIM n sequentially. u n :> at_top"
   3.841 +  shows "LIM n sequentially. Inf {N. u N \<ge> n} :> at_top"
   3.842 +proof -
   3.843 +  {
   3.844 +    fix C::nat
   3.845 +    define M where "M = Max {u n| n. n \<le> C}+1"
   3.846 +    {
   3.847 +      fix n assume "n \<ge> M"
   3.848 +      have "eventually (\<lambda>N. u N \<ge> n) sequentially" using assms
   3.849 +        by (simp add: filterlim_at_top)
   3.850 +      then have *: "{N. u N \<ge> n} \<noteq> {}" by force
   3.851 +
   3.852 +      have "N > C" if "u N \<ge> n" for N
   3.853 +      proof (rule ccontr)
   3.854 +        assume "\<not>(N > C)"
   3.855 +        have "u N \<le> Max {u n| n. n \<le> C}"
   3.856 +          apply (rule Max_ge) using `\<not>(N > C)` by auto
   3.857 +        then show False using `u N \<ge> n` `n \<ge> M` unfolding M_def by auto
   3.858 +      qed
   3.859 +      then have **: "{N. u N \<ge> n} \<subseteq> {C..}" by fastforce
   3.860 +      have "Inf {N. u N \<ge> n} \<ge> C"
   3.861 +        by (metis "*" "**" Inf_nat_def1 atLeast_iff subset_eq)
   3.862 +    }
   3.863 +    then have "eventually (\<lambda>n. Inf {N. u N \<ge> n} \<ge> C) sequentially"
   3.864 +      using eventually_sequentially by auto
   3.865 +  }
   3.866 +  then show ?thesis using filterlim_at_top by auto
   3.867 +qed
   3.868 +
   3.869 +lemma pseudo_inverse_finite_set:
   3.870 +  fixes u::"nat \<Rightarrow> nat"
   3.871 +  assumes "LIM n sequentially. u n :> at_top"
   3.872 +  shows "finite {N. u N \<le> n}"
   3.873 +proof -
   3.874 +  fix n
   3.875 +  have "eventually (\<lambda>N. u N \<ge> n+1) sequentially" using assms
   3.876 +    by (simp add: filterlim_at_top)
   3.877 +  then obtain N1 where N1: "\<And>N. N \<ge> N1 \<Longrightarrow> u N \<ge> n + 1"
   3.878 +    using eventually_sequentially by auto
   3.879 +  have "{N. u N \<le> n} \<subseteq> {..<N1}"
   3.880 +    apply auto using N1 by (metis Suc_eq_plus1 not_less not_less_eq_eq)
   3.881 +  then show "finite {N. u N \<le> n}" by (simp add: finite_subset)
   3.882 +qed
   3.883 +
   3.884 +lemma tendsto_at_top_pseudo_inverse2 [tendsto_intros]:
   3.885 +  fixes u::"nat \<Rightarrow> nat"
   3.886 +  assumes "LIM n sequentially. u n :> at_top"
   3.887 +  shows "LIM n sequentially. Max {N. u N \<le> n} :> at_top"
   3.888 +proof -
   3.889 +  {
   3.890 +    fix N0::nat
   3.891 +    have "N0 \<le> Max {N. u N \<le> n}" if "n \<ge> u N0" for n
   3.892 +      apply (rule Max.coboundedI) using pseudo_inverse_finite_set[OF assms] that by auto
   3.893 +    then have "eventually (\<lambda>n. N0 \<le> Max {N. u N \<le> n}) sequentially"
   3.894 +      using eventually_sequentially by blast
   3.895 +  }
   3.896 +  then show ?thesis using filterlim_at_top by auto
   3.897 +qed
   3.898 +
   3.899 +lemma ereal_truncation_top [tendsto_intros]:
   3.900 +  fixes x::ereal
   3.901 +  shows "(\<lambda>n::nat. min x n) \<longlonglongrightarrow> x"
   3.902 +proof (cases x)
   3.903 +  case (real r)
   3.904 +  then obtain K::nat where "K>0" "K > abs(r)" using reals_Archimedean2 gr0I by auto
   3.905 +  then have "min x n = x" if "n \<ge> K" for n apply (subst real, subst real, auto) using that eq_iff by fastforce
   3.906 +  then have "eventually (\<lambda>n. min x n = x) sequentially" using eventually_at_top_linorder by blast
   3.907 +  then show ?thesis by (simp add: Lim_eventually)
   3.908 +next
   3.909 +  case (PInf)
   3.910 +  then have "min x n = n" for n::nat by (auto simp add: min_def)
   3.911 +  then show ?thesis using id_nat_ereal_tendsto_PInf PInf by auto
   3.912 +next
   3.913 +  case (MInf)
   3.914 +  then have "min x n = x" for n::nat by (auto simp add: min_def)
   3.915 +  then show ?thesis by auto
   3.916 +qed
   3.917 +
   3.918 +lemma ereal_truncation_real_top [tendsto_intros]:
   3.919 +  fixes x::ereal
   3.920 +  assumes "x \<noteq> - \<infinity>"
   3.921 +  shows "(\<lambda>n::nat. real_of_ereal(min x n)) \<longlonglongrightarrow> x"
   3.922 +proof (cases x)
   3.923 +  case (real r)
   3.924 +  then obtain K::nat where "K>0" "K > abs(r)" using reals_Archimedean2 gr0I by auto
   3.925 +  then have "min x n = x" if "n \<ge> K" for n apply (subst real, subst real, auto) using that eq_iff by fastforce
   3.926 +  then have "real_of_ereal(min x n) = r" if "n \<ge> K" for n using real that by auto
   3.927 +  then have "eventually (\<lambda>n. real_of_ereal(min x n) = r) sequentially" using eventually_at_top_linorder by blast
   3.928 +  then have "(\<lambda>n. real_of_ereal(min x n)) \<longlonglongrightarrow> r" by (simp add: Lim_eventually)
   3.929 +  then show ?thesis using real by auto
   3.930 +next
   3.931 +  case (PInf)
   3.932 +  then have "real_of_ereal(min x n) = n" for n::nat by (auto simp add: min_def)
   3.933 +  then show ?thesis using id_nat_ereal_tendsto_PInf PInf by auto
   3.934 +qed (simp add: assms)
   3.935 +
   3.936 +lemma ereal_truncation_bottom [tendsto_intros]:
   3.937 +  fixes x::ereal
   3.938 +  shows "(\<lambda>n::nat. max x (- real n)) \<longlonglongrightarrow> x"
   3.939 +proof (cases x)
   3.940 +  case (real r)
   3.941 +  then obtain K::nat where "K>0" "K > abs(r)" using reals_Archimedean2 gr0I by auto
   3.942 +  then have "max x (-real n) = x" if "n \<ge> K" for n apply (subst real, subst real, auto) using that eq_iff by fastforce
   3.943 +  then have "eventually (\<lambda>n. max x (-real n) = x) sequentially" using eventually_at_top_linorder by blast
   3.944 +  then show ?thesis by (simp add: Lim_eventually)
   3.945 +next
   3.946 +  case (MInf)
   3.947 +  then have "max x (-real n) = (-1)* ereal(real n)" for n::nat by (auto simp add: max_def)
   3.948 +  moreover have "(\<lambda>n. (-1)* ereal(real n)) \<longlonglongrightarrow> -\<infinity>"
   3.949 +    using tendsto_cmult_ereal[of "-1", OF _ id_nat_ereal_tendsto_PInf] by (simp add: one_ereal_def)
   3.950 +  ultimately show ?thesis using MInf by auto
   3.951 +next
   3.952 +  case (PInf)
   3.953 +  then have "max x (-real n) = x" for n::nat by (auto simp add: max_def)
   3.954 +  then show ?thesis by auto
   3.955 +qed
   3.956 +
   3.957 +lemma ereal_truncation_real_bottom [tendsto_intros]:
   3.958 +  fixes x::ereal
   3.959 +  assumes "x \<noteq> \<infinity>"
   3.960 +  shows "(\<lambda>n::nat. real_of_ereal(max x (- real n))) \<longlonglongrightarrow> x"
   3.961 +proof (cases x)
   3.962 +  case (real r)
   3.963 +  then obtain K::nat where "K>0" "K > abs(r)" using reals_Archimedean2 gr0I by auto
   3.964 +  then have "max x (-real n) = x" if "n \<ge> K" for n apply (subst real, subst real, auto) using that eq_iff by fastforce
   3.965 +  then have "real_of_ereal(max x (-real n)) = r" if "n \<ge> K" for n using real that by auto
   3.966 +  then have "eventually (\<lambda>n. real_of_ereal(max x (-real n)) = r) sequentially" using eventually_at_top_linorder by blast
   3.967 +  then have "(\<lambda>n. real_of_ereal(max x (-real n))) \<longlonglongrightarrow> r" by (simp add: Lim_eventually)
   3.968 +  then show ?thesis using real by auto
   3.969 +next
   3.970 +  case (MInf)
   3.971 +  then have "real_of_ereal(max x (-real n)) = (-1)* ereal(real n)" for n::nat by (auto simp add: max_def)
   3.972 +  moreover have "(\<lambda>n. (-1)* ereal(real n)) \<longlonglongrightarrow> -\<infinity>"
   3.973 +    using tendsto_cmult_ereal[of "-1", OF _ id_nat_ereal_tendsto_PInf] by (simp add: one_ereal_def)
   3.974 +  ultimately show ?thesis using MInf by auto
   3.975 +qed (simp add: assms)
   3.976 +
   3.977 +text {* the next one is copied from \verb+tendsto_sum+. *}
   3.978 +lemma tendsto_sum_ereal [tendsto_intros]:
   3.979 +  fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> ereal"
   3.980 +  assumes "\<And>i. i \<in> S \<Longrightarrow> (f i \<longlongrightarrow> a i) F"
   3.981 +          "\<And>i. abs(a i) \<noteq> \<infinity>"
   3.982 +  shows "((\<lambda>x. \<Sum>i\<in>S. f i x) \<longlongrightarrow> (\<Sum>i\<in>S. a i)) F"
   3.983 +proof (cases "finite S")
   3.984 +  assume "finite S" then show ?thesis using assms
   3.985 +    by (induct, simp, simp add: tendsto_add_ereal_general2 assms)
   3.986 +qed(simp)
   3.987 +
   3.988 +subsubsection {*Limsups and liminfs*}
   3.989 +
   3.990 +lemma limsup_finite_then_bounded:
   3.991 +  fixes u::"nat \<Rightarrow> real"
   3.992 +  assumes "limsup u < \<infinity>"
   3.993 +  shows "\<exists>C. \<forall>n. u n \<le> C"
   3.994 +proof -
   3.995 +  obtain C where C: "limsup u < C" "C < \<infinity>" using assms ereal_dense2 by blast
   3.996 +  then have "C = ereal(real_of_ereal C)" using ereal_real by force
   3.997 +  have "eventually (\<lambda>n. u n < C) sequentially" using C(1) unfolding Limsup_def
   3.998 +    apply (auto simp add: INF_less_iff)
   3.999 +    using SUP_lessD eventually_mono by fastforce
  3.1000 +  then obtain N where N: "\<And>n. n \<ge> N \<Longrightarrow> u n < C" using eventually_sequentially by auto
  3.1001 +  define D where "D = max (real_of_ereal C) (Max {u n |n. n \<le> N})"
  3.1002 +  have "\<And>n. u n \<le> D"
  3.1003 +  proof -
  3.1004 +    fix n show "u n \<le> D"
  3.1005 +    proof (cases)
  3.1006 +      assume *: "n \<le> N"
  3.1007 +      have "u n \<le> Max {u n |n. n \<le> N}" by (rule Max_ge, auto simp add: *)
  3.1008 +      then show "u n \<le> D" unfolding D_def by linarith
  3.1009 +    next
  3.1010 +      assume "\<not>(n \<le> N)"
  3.1011 +      then have "n \<ge> N" by simp
  3.1012 +      then have "u n < C" using N by auto
  3.1013 +      then have "u n < real_of_ereal C" using `C = ereal(real_of_ereal C)` less_ereal.simps(1) by fastforce
  3.1014 +      then show "u n \<le> D" unfolding D_def by linarith
  3.1015 +    qed
  3.1016 +  qed
  3.1017 +  then show ?thesis by blast
  3.1018 +qed
  3.1019 +
  3.1020 +lemma liminf_finite_then_bounded_below:
  3.1021 +  fixes u::"nat \<Rightarrow> real"
  3.1022 +  assumes "liminf u > -\<infinity>"
  3.1023 +  shows "\<exists>C. \<forall>n. u n \<ge> C"
  3.1024 +proof -
  3.1025 +  obtain C where C: "liminf u > C" "C > -\<infinity>" using assms using ereal_dense2 by blast
  3.1026 +  then have "C = ereal(real_of_ereal C)" using ereal_real by force
  3.1027 +  have "eventually (\<lambda>n. u n > C) sequentially" using C(1) unfolding Liminf_def
  3.1028 +    apply (auto simp add: less_SUP_iff)
  3.1029 +    using eventually_elim2 less_INF_D by fastforce
  3.1030 +  then obtain N where N: "\<And>n. n \<ge> N \<Longrightarrow> u n > C" using eventually_sequentially by auto
  3.1031 +  define D where "D = min (real_of_ereal C) (Min {u n |n. n \<le> N})"
  3.1032 +  have "\<And>n. u n \<ge> D"
  3.1033 +  proof -
  3.1034 +    fix n show "u n \<ge> D"
  3.1035 +    proof (cases)
  3.1036 +      assume *: "n \<le> N"
  3.1037 +      have "u n \<ge> Min {u n |n. n \<le> N}" by (rule Min_le, auto simp add: *)
  3.1038 +      then show "u n \<ge> D" unfolding D_def by linarith
  3.1039 +    next
  3.1040 +      assume "\<not>(n \<le> N)"
  3.1041 +      then have "n \<ge> N" by simp
  3.1042 +      then have "u n > C" using N by auto
  3.1043 +      then have "u n > real_of_ereal C" using `C = ereal(real_of_ereal C)` less_ereal.simps(1) by fastforce
  3.1044 +      then show "u n \<ge> D" unfolding D_def by linarith
  3.1045 +    qed
  3.1046 +  qed
  3.1047 +  then show ?thesis by blast
  3.1048 +qed
  3.1049 +
  3.1050 +lemma liminf_upper_bound:
  3.1051 +  fixes u:: "nat \<Rightarrow> ereal"
  3.1052 +  assumes "liminf u < l"
  3.1053 +  shows "\<exists>N>k. u N < l"
  3.1054 +by (metis assms gt_ex less_le_trans liminf_bounded_iff not_less)
  3.1055 +
  3.1056 +text {* The following statement about limsups is reduced to a statement about limits using
  3.1057 +subsequences thanks to \verb+limsup_subseq_lim+. The statement for limits follows for instance from
  3.1058 +\verb+tendsto_add_ereal_general+.*}
  3.1059 +
  3.1060 +lemma ereal_limsup_add_mono:
  3.1061 +  fixes u v::"nat \<Rightarrow> ereal"
  3.1062 +  shows "limsup (\<lambda>n. u n + v n) \<le> limsup u + limsup v"
  3.1063 +proof (cases)
  3.1064 +  assume "(limsup u = \<infinity>) \<or> (limsup v = \<infinity>)"
  3.1065 +  then have "limsup u + limsup v = \<infinity>" by simp
  3.1066 +  then show ?thesis by auto
  3.1067 +next
  3.1068 +  assume "\<not>((limsup u = \<infinity>) \<or> (limsup v = \<infinity>))"
  3.1069 +  then have "limsup u < \<infinity>" "limsup v < \<infinity>" by auto
  3.1070 +
  3.1071 +  define w where "w = (\<lambda>n. u n + v n)"
  3.1072 +  obtain r where r: "subseq r" "(w o r) \<longlonglongrightarrow> limsup w" using limsup_subseq_lim by auto
  3.1073 +  obtain s where s: "subseq s" "(u o r o s) \<longlonglongrightarrow> limsup (u o r)" using limsup_subseq_lim by auto
  3.1074 +  obtain t where t: "subseq t" "(v o r o s o t) \<longlonglongrightarrow> limsup (v o r o s)" using limsup_subseq_lim by auto
  3.1075 +
  3.1076 +  define a where "a = r o s o t"
  3.1077 +  have "subseq a" using r s t by (simp add: a_def subseq_o)
  3.1078 +  have l:"(w o a) \<longlonglongrightarrow> limsup w"
  3.1079 +         "(u o a) \<longlonglongrightarrow> limsup (u o r)"
  3.1080 +         "(v o a) \<longlonglongrightarrow> limsup (v o r o s)"
  3.1081 +  apply (metis (no_types, lifting) r(2) s(1) t(1) LIMSEQ_subseq_LIMSEQ a_def comp_assoc)
  3.1082 +  apply (metis (no_types, lifting) s(2) t(1) LIMSEQ_subseq_LIMSEQ a_def comp_assoc)
  3.1083 +  apply (metis (no_types, lifting) t(2) a_def comp_assoc)
  3.1084 +  done
  3.1085 +
  3.1086 +  have "limsup (u o r) \<le> limsup u" by (simp add: limsup_subseq_mono r(1))
  3.1087 +  then have a: "limsup (u o r) \<noteq> \<infinity>" using `limsup u < \<infinity>` by auto
  3.1088 +  have "limsup (v o r o s) \<le> limsup v" by (simp add: comp_assoc limsup_subseq_mono r(1) s(1) subseq_o)
  3.1089 +  then have b: "limsup (v o r o s) \<noteq> \<infinity>" using `limsup v < \<infinity>` by auto
  3.1090 +
  3.1091 +  have "(\<lambda>n. (u o a) n + (v o a) n) \<longlonglongrightarrow> limsup (u o r) + limsup (v o r o s)"
  3.1092 +    using l tendsto_add_ereal_general a b by fastforce
  3.1093 +  moreover have "(\<lambda>n. (u o a) n + (v o a) n) = (w o a)" unfolding w_def by auto
  3.1094 +  ultimately have "(w o a) \<longlonglongrightarrow> limsup (u o r) + limsup (v o r o s)" by simp
  3.1095 +  then have "limsup w = limsup (u o r) + limsup (v o r o s)" using l(1) LIMSEQ_unique by blast
  3.1096 +  then have "limsup w \<le> limsup u + limsup v"
  3.1097 +    using `limsup (u o r) \<le> limsup u` `limsup (v o r o s) \<le> limsup v` ereal_add_mono by simp
  3.1098 +  then show ?thesis unfolding w_def by simp
  3.1099 +qed
  3.1100 +
  3.1101 +text {* There is an asymmetry between liminfs and limsups in ereal, as $\infty + (-\infty) = \infty$.
  3.1102 +This explains why there are more assumptions in the next lemma dealing with liminfs that in the
  3.1103 +previous one about limsups.*}
  3.1104 +
  3.1105 +lemma ereal_liminf_add_mono:
  3.1106 +  fixes u v::"nat \<Rightarrow> ereal"
  3.1107 +  assumes "\<not>((liminf u = \<infinity> \<and> liminf v = -\<infinity>) \<or> (liminf u = -\<infinity> \<and> liminf v = \<infinity>))"
  3.1108 +  shows "liminf (\<lambda>n. u n + v n) \<ge> liminf u + liminf v"
  3.1109 +proof (cases)
  3.1110 +  assume "(liminf u = -\<infinity>) \<or> (liminf v = -\<infinity>)"
  3.1111 +  then have *: "liminf u + liminf v = -\<infinity>" using assms by auto
  3.1112 +  show ?thesis by (simp add: *)
  3.1113 +next
  3.1114 +  assume "\<not>((liminf u = -\<infinity>) \<or> (liminf v = -\<infinity>))"
  3.1115 +  then have "liminf u > -\<infinity>" "liminf v > -\<infinity>" by auto
  3.1116 +
  3.1117 +  define w where "w = (\<lambda>n. u n + v n)"
  3.1118 +  obtain r where r: "subseq r" "(w o r) \<longlonglongrightarrow> liminf w" using liminf_subseq_lim by auto
  3.1119 +  obtain s where s: "subseq s" "(u o r o s) \<longlonglongrightarrow> liminf (u o r)" using liminf_subseq_lim by auto
  3.1120 +  obtain t where t: "subseq t" "(v o r o s o t) \<longlonglongrightarrow> liminf (v o r o s)" using liminf_subseq_lim by auto
  3.1121 +
  3.1122 +  define a where "a = r o s o t"
  3.1123 +  have "subseq a" using r s t by (simp add: a_def subseq_o)
  3.1124 +  have l:"(w o a) \<longlonglongrightarrow> liminf w"
  3.1125 +         "(u o a) \<longlonglongrightarrow> liminf (u o r)"
  3.1126 +         "(v o a) \<longlonglongrightarrow> liminf (v o r o s)"
  3.1127 +  apply (metis (no_types, lifting) r(2) s(1) t(1) LIMSEQ_subseq_LIMSEQ a_def comp_assoc)
  3.1128 +  apply (metis (no_types, lifting) s(2) t(1) LIMSEQ_subseq_LIMSEQ a_def comp_assoc)
  3.1129 +  apply (metis (no_types, lifting) t(2) a_def comp_assoc)
  3.1130 +  done
  3.1131 +
  3.1132 +  have "liminf (u o r) \<ge> liminf u" by (simp add: liminf_subseq_mono r(1))
  3.1133 +  then have a: "liminf (u o r) \<noteq> -\<infinity>" using `liminf u > -\<infinity>` by auto
  3.1134 +  have "liminf (v o r o s) \<ge> liminf v" by (simp add: comp_assoc liminf_subseq_mono r(1) s(1) subseq_o)
  3.1135 +  then have b: "liminf (v o r o s) \<noteq> -\<infinity>" using `liminf v > -\<infinity>` by auto
  3.1136 +
  3.1137 +  have "(\<lambda>n. (u o a) n + (v o a) n) \<longlonglongrightarrow> liminf (u o r) + liminf (v o r o s)"
  3.1138 +    using l tendsto_add_ereal_general a b by fastforce
  3.1139 +  moreover have "(\<lambda>n. (u o a) n + (v o a) n) = (w o a)" unfolding w_def by auto
  3.1140 +  ultimately have "(w o a) \<longlonglongrightarrow> liminf (u o r) + liminf (v o r o s)" by simp
  3.1141 +  then have "liminf w = liminf (u o r) + liminf (v o r o s)" using l(1) LIMSEQ_unique by blast
  3.1142 +  then have "liminf w \<ge> liminf u + liminf v"
  3.1143 +    using `liminf (u o r) \<ge> liminf u` `liminf (v o r o s) \<ge> liminf v` ereal_add_mono by simp
  3.1144 +  then show ?thesis unfolding w_def by simp
  3.1145 +qed
  3.1146 +
  3.1147 +lemma ereal_limsup_lim_add:
  3.1148 +  fixes u v::"nat \<Rightarrow> ereal"
  3.1149 +  assumes "u \<longlonglongrightarrow> a" "abs(a) \<noteq> \<infinity>"
  3.1150 +  shows "limsup (\<lambda>n. u n + v n) = a + limsup v"
  3.1151 +proof -
  3.1152 +  have "limsup u = a" using assms(1) using tendsto_iff_Liminf_eq_Limsup trivial_limit_at_top_linorder by blast
  3.1153 +  have "(\<lambda>n. -u n) \<longlonglongrightarrow> -a" using assms(1) by auto
  3.1154 +  then have "limsup (\<lambda>n. -u n) = -a" using tendsto_iff_Liminf_eq_Limsup trivial_limit_at_top_linorder by blast
  3.1155 +
  3.1156 +  have "limsup (\<lambda>n. u n + v n) \<le> limsup u + limsup v"
  3.1157 +    by (rule ereal_limsup_add_mono)
  3.1158 +  then have up: "limsup (\<lambda>n. u n + v n) \<le> a + limsup v" using `limsup u = a` by simp
  3.1159 +
  3.1160 +  have a: "limsup (\<lambda>n. (u n + v n) + (-u n)) \<le> limsup (\<lambda>n. u n + v n) + limsup (\<lambda>n. -u n)"
  3.1161 +    by (rule ereal_limsup_add_mono)
  3.1162 +  have "eventually (\<lambda>n. u n = ereal(real_of_ereal(u n))) sequentially" using assms
  3.1163 +    real_lim_then_eventually_real by auto
  3.1164 +  moreover have "\<And>x. x = ereal(real_of_ereal(x)) \<Longrightarrow> x + (-x) = 0"
  3.1165 +    by (metis plus_ereal.simps(1) right_minus uminus_ereal.simps(1) zero_ereal_def)
  3.1166 +  ultimately have "eventually (\<lambda>n. u n + (-u n) = 0) sequentially"
  3.1167 +    by (metis (mono_tags, lifting) eventually_mono)
  3.1168 +  moreover have "\<And>n. u n + (-u n) = 0 \<Longrightarrow> u n + v n + (-u n) = v n"
  3.1169 +    by (metis add.commute add.left_commute add.left_neutral)
  3.1170 +  ultimately have "eventually (\<lambda>n. u n + v n + (-u n) = v n) sequentially"
  3.1171 +    using eventually_mono by force
  3.1172 +  then have "limsup v = limsup (\<lambda>n. u n + v n + (-u n))" using Limsup_eq by force
  3.1173 +  then have "limsup v \<le> limsup (\<lambda>n. u n + v n) -a" using a `limsup (\<lambda>n. -u n) = -a` by (simp add: minus_ereal_def)
  3.1174 +  then have "limsup (\<lambda>n. u n + v n) \<ge> a + limsup v" using assms(2) by (metis add.commute ereal_le_minus)
  3.1175 +  then show ?thesis using up by simp
  3.1176 +qed
  3.1177 +
  3.1178 +lemma ereal_limsup_lim_mult:
  3.1179 +  fixes u v::"nat \<Rightarrow> ereal"
  3.1180 +  assumes "u \<longlonglongrightarrow> a" "a>0" "a \<noteq> \<infinity>"
  3.1181 +  shows "limsup (\<lambda>n. u n * v n) = a * limsup v"
  3.1182 +proof -
  3.1183 +  define w where "w = (\<lambda>n. u n * v n)"
  3.1184 +  obtain r where r: "subseq r" "(v o r) \<longlonglongrightarrow> limsup v" using limsup_subseq_lim by auto
  3.1185 +  have "(u o r) \<longlonglongrightarrow> a" using assms(1) LIMSEQ_subseq_LIMSEQ r by auto
  3.1186 +  with tendsto_mult_ereal[OF this r(2)] have "(\<lambda>n. (u o r) n * (v o r) n) \<longlonglongrightarrow> a * limsup v" using assms(2) assms(3) by auto
  3.1187 +  moreover have "\<And>n. (w o r) n = (u o r) n * (v o r) n" unfolding w_def by auto
  3.1188 +  ultimately have "(w o r) \<longlonglongrightarrow> a * limsup v" unfolding w_def by presburger
  3.1189 +  then have "limsup (w o r) = a * limsup v" by (simp add: tendsto_iff_Liminf_eq_Limsup)
  3.1190 +  then have I: "limsup w \<ge> a * limsup v" by (metis limsup_subseq_mono r(1))
  3.1191 +
  3.1192 +  obtain s where s: "subseq s" "(w o s) \<longlonglongrightarrow> limsup w" using limsup_subseq_lim by auto
  3.1193 +  have *: "(u o s) \<longlonglongrightarrow> a" using assms(1) LIMSEQ_subseq_LIMSEQ s by auto
  3.1194 +  have "eventually (\<lambda>n. (u o s) n > 0) sequentially" using assms(2) * order_tendsto_iff by blast
  3.1195 +  moreover have "eventually (\<lambda>n. (u o s) n < \<infinity>) sequentially" using assms(3) * order_tendsto_iff by blast
  3.1196 +  moreover have "(w o s) n / (u o s) n = (v o s) n" if "(u o s) n > 0" "(u o s) n < \<infinity>" for n
  3.1197 +    unfolding w_def using that by (auto simp add: ereal_divide_eq)
  3.1198 +  ultimately have "eventually (\<lambda>n. (w o s) n / (u o s) n = (v o s) n) sequentially" using eventually_elim2 by force
  3.1199 +  moreover have "(\<lambda>n. (w o s) n / (u o s) n) \<longlonglongrightarrow> (limsup w) / a"
  3.1200 +    apply (rule tendsto_divide_ereal[OF s(2) *]) using assms(2) assms(3) by auto
  3.1201 +  ultimately have "(v o s) \<longlonglongrightarrow> (limsup w) / a" using Lim_transform_eventually by fastforce
  3.1202 +  then have "limsup (v o s) = (limsup w) / a" by (simp add: tendsto_iff_Liminf_eq_Limsup)
  3.1203 +  then have "limsup v \<ge> (limsup w) / a" by (metis limsup_subseq_mono s(1))
  3.1204 +  then have "a * limsup v \<ge> limsup w" using assms(2) assms(3) by (simp add: ereal_divide_le_pos)
  3.1205 +  then show ?thesis using I unfolding w_def by auto
  3.1206 +qed
  3.1207 +
  3.1208 +lemma ereal_liminf_lim_mult:
  3.1209 +  fixes u v::"nat \<Rightarrow> ereal"
  3.1210 +  assumes "u \<longlonglongrightarrow> a" "a>0" "a \<noteq> \<infinity>"
  3.1211 +  shows "liminf (\<lambda>n. u n * v n) = a * liminf v"
  3.1212 +proof -
  3.1213 +  define w where "w = (\<lambda>n. u n * v n)"
  3.1214 +  obtain r where r: "subseq r" "(v o r) \<longlonglongrightarrow> liminf v" using liminf_subseq_lim by auto
  3.1215 +  have "(u o r) \<longlonglongrightarrow> a" using assms(1) LIMSEQ_subseq_LIMSEQ r by auto
  3.1216 +  with tendsto_mult_ereal[OF this r(2)] have "(\<lambda>n. (u o r) n * (v o r) n) \<longlonglongrightarrow> a * liminf v" using assms(2) assms(3) by auto
  3.1217 +  moreover have "\<And>n. (w o r) n = (u o r) n * (v o r) n" unfolding w_def by auto
  3.1218 +  ultimately have "(w o r) \<longlonglongrightarrow> a * liminf v" unfolding w_def by presburger
  3.1219 +  then have "liminf (w o r) = a * liminf v" by (simp add: tendsto_iff_Liminf_eq_Limsup)
  3.1220 +  then have I: "liminf w \<le> a * liminf v" by (metis liminf_subseq_mono r(1))
  3.1221 +
  3.1222 +  obtain s where s: "subseq s" "(w o s) \<longlonglongrightarrow> liminf w" using liminf_subseq_lim by auto
  3.1223 +  have *: "(u o s) \<longlonglongrightarrow> a" using assms(1) LIMSEQ_subseq_LIMSEQ s by auto
  3.1224 +  have "eventually (\<lambda>n. (u o s) n > 0) sequentially" using assms(2) * order_tendsto_iff by blast
  3.1225 +  moreover have "eventually (\<lambda>n. (u o s) n < \<infinity>) sequentially" using assms(3) * order_tendsto_iff by blast
  3.1226 +  moreover have "(w o s) n / (u o s) n = (v o s) n" if "(u o s) n > 0" "(u o s) n < \<infinity>" for n
  3.1227 +    unfolding w_def using that by (auto simp add: ereal_divide_eq)
  3.1228 +  ultimately have "eventually (\<lambda>n. (w o s) n / (u o s) n = (v o s) n) sequentially" using eventually_elim2 by force
  3.1229 +  moreover have "(\<lambda>n. (w o s) n / (u o s) n) \<longlonglongrightarrow> (liminf w) / a"
  3.1230 +    apply (rule tendsto_divide_ereal[OF s(2) *]) using assms(2) assms(3) by auto
  3.1231 +  ultimately have "(v o s) \<longlonglongrightarrow> (liminf w) / a" using Lim_transform_eventually by fastforce
  3.1232 +  then have "liminf (v o s) = (liminf w) / a" by (simp add: tendsto_iff_Liminf_eq_Limsup)
  3.1233 +  then have "liminf v \<le> (liminf w) / a" by (metis liminf_subseq_mono s(1))
  3.1234 +  then have "a * liminf v \<le> liminf w" using assms(2) assms(3) by (simp add: ereal_le_divide_pos)
  3.1235 +  then show ?thesis using I unfolding w_def by auto
  3.1236 +qed
  3.1237 +
  3.1238 +lemma ereal_liminf_lim_add:
  3.1239 +  fixes u v::"nat \<Rightarrow> ereal"
  3.1240 +  assumes "u \<longlonglongrightarrow> a" "abs(a) \<noteq> \<infinity>"
  3.1241 +  shows "liminf (\<lambda>n. u n + v n) = a + liminf v"
  3.1242 +proof -
  3.1243 +  have "liminf u = a" using assms(1) tendsto_iff_Liminf_eq_Limsup trivial_limit_at_top_linorder by blast
  3.1244 +  then have *: "abs(liminf u) \<noteq> \<infinity>" using assms(2) by auto
  3.1245 +  have "(\<lambda>n. -u n) \<longlonglongrightarrow> -a" using assms(1) by auto
  3.1246 +  then have "liminf (\<lambda>n. -u n) = -a" using tendsto_iff_Liminf_eq_Limsup trivial_limit_at_top_linorder by blast
  3.1247 +  then have **: "abs(liminf (\<lambda>n. -u n)) \<noteq> \<infinity>" using assms(2) by auto
  3.1248 +
  3.1249 +  have "liminf (\<lambda>n. u n + v n) \<ge> liminf u + liminf v"
  3.1250 +    apply (rule ereal_liminf_add_mono) using * by auto
  3.1251 +  then have up: "liminf (\<lambda>n. u n + v n) \<ge> a + liminf v" using `liminf u = a` by simp
  3.1252 +
  3.1253 +  have a: "liminf (\<lambda>n. (u n + v n) + (-u n)) \<ge> liminf (\<lambda>n. u n + v n) + liminf (\<lambda>n. -u n)"
  3.1254 +    apply (rule ereal_liminf_add_mono) using ** by auto
  3.1255 +  have "eventually (\<lambda>n. u n = ereal(real_of_ereal(u n))) sequentially" using assms
  3.1256 +    real_lim_then_eventually_real by auto
  3.1257 +  moreover have "\<And>x. x = ereal(real_of_ereal(x)) \<Longrightarrow> x + (-x) = 0"
  3.1258 +    by (metis plus_ereal.simps(1) right_minus uminus_ereal.simps(1) zero_ereal_def)
  3.1259 +  ultimately have "eventually (\<lambda>n. u n + (-u n) = 0) sequentially"
  3.1260 +    by (metis (mono_tags, lifting) eventually_mono)
  3.1261 +  moreover have "\<And>n. u n + (-u n) = 0 \<Longrightarrow> u n + v n + (-u n) = v n"
  3.1262 +    by (metis add.commute add.left_commute add.left_neutral)
  3.1263 +  ultimately have "eventually (\<lambda>n. u n + v n + (-u n) = v n) sequentially"
  3.1264 +    using eventually_mono by force
  3.1265 +  then have "liminf v = liminf (\<lambda>n. u n + v n + (-u n))" using Liminf_eq by force
  3.1266 +  then have "liminf v \<ge> liminf (\<lambda>n. u n + v n) -a" using a `liminf (\<lambda>n. -u n) = -a` by (simp add: minus_ereal_def)
  3.1267 +  then have "liminf (\<lambda>n. u n + v n) \<le> a + liminf v" using assms(2) by (metis add.commute ereal_minus_le)
  3.1268 +  then show ?thesis using up by simp
  3.1269 +qed
  3.1270 +
  3.1271 +lemma ereal_liminf_limsup_add:
  3.1272 +  fixes u v::"nat \<Rightarrow> ereal"
  3.1273 +  shows "liminf (\<lambda>n. u n + v n) \<le> liminf u + limsup v"
  3.1274 +proof (cases)
  3.1275 +  assume "limsup v = \<infinity> \<or> liminf u = \<infinity>"
  3.1276 +  then show ?thesis by auto
  3.1277 +next
  3.1278 +  assume "\<not>(limsup v = \<infinity> \<or> liminf u = \<infinity>)"
  3.1279 +  then have "limsup v < \<infinity>" "liminf u < \<infinity>" by auto
  3.1280 +
  3.1281 +  define w where "w = (\<lambda>n. u n + v n)"
  3.1282 +  obtain r where r: "subseq r" "(u o r) \<longlonglongrightarrow> liminf u" using liminf_subseq_lim by auto
  3.1283 +  obtain s where s: "subseq s" "(w o r o s) \<longlonglongrightarrow> liminf (w o r)" using liminf_subseq_lim by auto
  3.1284 +  obtain t where t: "subseq t" "(v o r o s o t) \<longlonglongrightarrow> limsup (v o r o s)" using limsup_subseq_lim by auto
  3.1285 +
  3.1286 +  define a where "a = r o s o t"
  3.1287 +  have "subseq a" using r s t by (simp add: a_def subseq_o)
  3.1288 +  have l:"(u o a) \<longlonglongrightarrow> liminf u"
  3.1289 +         "(w o a) \<longlonglongrightarrow> liminf (w o r)"
  3.1290 +         "(v o a) \<longlonglongrightarrow> limsup (v o r o s)"
  3.1291 +  apply (metis (no_types, lifting) r(2) s(1) t(1) LIMSEQ_subseq_LIMSEQ a_def comp_assoc)
  3.1292 +  apply (metis (no_types, lifting) s(2) t(1) LIMSEQ_subseq_LIMSEQ a_def comp_assoc)
  3.1293 +  apply (metis (no_types, lifting) t(2) a_def comp_assoc)
  3.1294 +  done
  3.1295 +
  3.1296 +  have "liminf (w o r) \<ge> liminf w" by (simp add: liminf_subseq_mono r(1))
  3.1297 +  have "limsup (v o r o s) \<le> limsup v" by (simp add: comp_assoc limsup_subseq_mono r(1) s(1) subseq_o)
  3.1298 +  then have b: "limsup (v o r o s) < \<infinity>" using `limsup v < \<infinity>` by auto
  3.1299 +
  3.1300 +  have "(\<lambda>n. (u o a) n + (v o a) n) \<longlonglongrightarrow> liminf u + limsup (v o r o s)"
  3.1301 +    apply (rule tendsto_add_ereal_general) using b `liminf u < \<infinity>` l(1) l(3) by force+
  3.1302 +  moreover have "(\<lambda>n. (u o a) n + (v o a) n) = (w o a)" unfolding w_def by auto
  3.1303 +  ultimately have "(w o a) \<longlonglongrightarrow> liminf u + limsup (v o r o s)" by simp
  3.1304 +  then have "liminf (w o r) = liminf u + limsup (v o r o s)" using l(2) using LIMSEQ_unique by blast
  3.1305 +  then have "liminf w \<le> liminf u + limsup v"
  3.1306 +    using `liminf (w o r) \<ge> liminf w` `limsup (v o r o s) \<le> limsup v`
  3.1307 +    by (metis add_mono_thms_linordered_semiring(2) le_less_trans not_less)
  3.1308 +  then show ?thesis unfolding w_def by simp
  3.1309 +qed
  3.1310 +
  3.1311 +lemma ereal_liminf_limsup_minus:
  3.1312 +  fixes u v::"nat \<Rightarrow> ereal"
  3.1313 +  shows "liminf (\<lambda>n. u n - v n) \<le> limsup u - limsup v"
  3.1314 +  unfolding minus_ereal_def
  3.1315 +  apply (subst add.commute)
  3.1316 +  apply (rule order_trans[OF ereal_liminf_limsup_add])
  3.1317 +  using ereal_Limsup_uminus[of sequentially "\<lambda>n. - v n"]
  3.1318 +  apply (simp add: add.commute)
  3.1319 +  done
  3.1320 +
  3.1321 +
  3.1322 +lemma liminf_minus_ennreal:
  3.1323 +  fixes u v::"nat \<Rightarrow> ennreal"
  3.1324 +  shows "(\<And>n. v n \<le> u n) \<Longrightarrow> liminf (\<lambda>n. u n - v n) \<le> limsup u - limsup v"
  3.1325 +  unfolding liminf_SUP_INF limsup_INF_SUP
  3.1326 +  including ennreal.lifting
  3.1327 +proof (transfer, clarsimp)
  3.1328 +  fix v u :: "nat \<Rightarrow> ereal" assume *: "\<forall>x. 0 \<le> v x" "\<forall>x. 0 \<le> u x" "\<And>n. v n \<le> u n"
  3.1329 +  moreover have "0 \<le> limsup u - limsup v"
  3.1330 +    using * by (intro ereal_diff_positive Limsup_mono always_eventually) simp
  3.1331 +  moreover have "0 \<le> (SUPREMUM {x..} v)" for x
  3.1332 +    using * by (intro SUP_upper2[of x]) auto
  3.1333 +  moreover have "0 \<le> (SUPREMUM {x..} u)" for x
  3.1334 +    using * by (intro SUP_upper2[of x]) auto
  3.1335 +  ultimately show "(SUP n. INF n:{n..}. max 0 (u n - v n))
  3.1336 +            \<le> max 0 ((INF x. max 0 (SUPREMUM {x..} u)) - (INF x. max 0 (SUPREMUM {x..} v)))"
  3.1337 +    by (auto simp: * ereal_diff_positive max.absorb2 liminf_SUP_INF[symmetric] limsup_INF_SUP[symmetric] ereal_liminf_limsup_minus)
  3.1338 +qed
  3.1339 +
  3.1340  (*
  3.1341      Notation
  3.1342  *)
  3.1343 @@ -755,4 +2088,121 @@
  3.1344    then show ?thesis using * by auto
  3.1345  qed
  3.1346  
  3.1347 +text {* The next lemma shows that $L^1$ convergence of a sequence of functions follows from almost
  3.1348 +everywhere convergence and the weaker condition of the convergence of the integrated norms (or even
  3.1349 +just the nontrivial inequality about them). Useful in a lot of contexts! This statement (or its
  3.1350 +variations) are known as Scheffe lemma.
  3.1351 +
  3.1352 +The formalization is more painful as one should jump back and forth between reals and ereals and justify
  3.1353 +all the time positivity or integrability (thankfully, measurability is handled more or less automatically).*}
  3.1354 +
  3.1355 +lemma Scheffe_lemma1:
  3.1356 +  assumes "\<And>n. integrable M (F n)" "integrable M f"
  3.1357 +          "AE x in M. (\<lambda>n. F n x) \<longlonglongrightarrow> f x"
  3.1358 +          "limsup (\<lambda>n. \<integral>\<^sup>+ x. norm(F n x) \<partial>M) \<le> (\<integral>\<^sup>+ x. norm(f x) \<partial>M)"
  3.1359 +  shows "(\<lambda>n. \<integral>\<^sup>+ x. norm(F n x - f x) \<partial>M) \<longlonglongrightarrow> 0"
  3.1360 +proof -
  3.1361 +  have [measurable]: "\<And>n. F n \<in> borel_measurable M" "f \<in> borel_measurable M"
  3.1362 +    using assms(1) assms(2) by simp_all
  3.1363 +  define G where "G = (\<lambda>n x. norm(f x) + norm(F n x) - norm(F n x - f x))"
  3.1364 +  have [measurable]: "\<And>n. G n \<in> borel_measurable M" unfolding G_def by simp
  3.1365 +  have G_pos[simp]: "\<And>n x. G n x \<ge> 0"
  3.1366 +    unfolding G_def by (metis ge_iff_diff_ge_0 norm_minus_commute norm_triangle_ineq4)
  3.1367 +  have finint: "(\<integral>\<^sup>+ x. norm(f x) \<partial>M) \<noteq> \<infinity>"
  3.1368 +    using has_bochner_integral_implies_finite_norm[OF has_bochner_integral_integrable[OF \<open>integrable M f\<close>]]
  3.1369 +    by simp
  3.1370 +  then have fin2: "2 * (\<integral>\<^sup>+ x. norm(f x) \<partial>M) \<noteq> \<infinity>"
  3.1371 +    by (auto simp: ennreal_mult_eq_top_iff)
  3.1372 +
  3.1373 +  {
  3.1374 +    fix x assume *: "(\<lambda>n. F n x) \<longlonglongrightarrow> f x"
  3.1375 +    then have "(\<lambda>n. norm(F n x)) \<longlonglongrightarrow> norm(f x)" using tendsto_norm by blast
  3.1376 +    moreover have "(\<lambda>n. norm(F n x - f x)) \<longlonglongrightarrow> 0" using * Lim_null tendsto_norm_zero_iff by fastforce
  3.1377 +    ultimately have a: "(\<lambda>n. norm(F n x) - norm(F n x - f x)) \<longlonglongrightarrow> norm(f x)" using tendsto_diff by fastforce
  3.1378 +    have "(\<lambda>n. norm(f x) + (norm(F n x) - norm(F n x - f x))) \<longlonglongrightarrow> norm(f x) + norm(f x)"
  3.1379 +      by (rule tendsto_add) (auto simp add: a)
  3.1380 +    moreover have "\<And>n. G n x = norm(f x) + (norm(F n x) - norm(F n x - f x))" unfolding G_def by simp
  3.1381 +    ultimately have "(\<lambda>n. G n x) \<longlonglongrightarrow> 2 * norm(f x)" by simp
  3.1382 +    then have "(\<lambda>n. ennreal(G n x)) \<longlonglongrightarrow> ennreal(2 * norm(f x))" by simp
  3.1383 +    then have "liminf (\<lambda>n. ennreal(G n x)) = ennreal(2 * norm(f x))"
  3.1384 +      using sequentially_bot tendsto_iff_Liminf_eq_Limsup by blast
  3.1385 +  }
  3.1386 +  then have "AE x in M. liminf (\<lambda>n. ennreal(G n x)) = ennreal(2 * norm(f x))" using assms(3) by auto
  3.1387 +  then have "(\<integral>\<^sup>+ x. liminf (\<lambda>n. ennreal (G n x)) \<partial>M) = (\<integral>\<^sup>+ x. 2 * ennreal(norm(f x)) \<partial>M)"
  3.1388 +    by (simp add: nn_integral_cong_AE ennreal_mult)
  3.1389 +  also have "... = 2 * (\<integral>\<^sup>+ x. norm(f x) \<partial>M)" by (rule nn_integral_cmult) auto
  3.1390 +  finally have int_liminf: "(\<integral>\<^sup>+ x. liminf (\<lambda>n. ennreal (G n x)) \<partial>M) = 2 * (\<integral>\<^sup>+ x. norm(f x) \<partial>M)"
  3.1391 +    by simp
  3.1392 +
  3.1393 +  have "(\<integral>\<^sup>+x. ennreal(norm(f x)) + ennreal(norm(F n x)) \<partial>M) = (\<integral>\<^sup>+x. norm(f x) \<partial>M) + (\<integral>\<^sup>+x. norm(F n x) \<partial>M)" for n
  3.1394 +    by (rule nn_integral_add) (auto simp add: assms)
  3.1395 +  then have "limsup (\<lambda>n. (\<integral>\<^sup>+x. ennreal(norm(f x)) + ennreal(norm(F n x)) \<partial>M)) =
  3.1396 +      limsup (\<lambda>n. (\<integral>\<^sup>+x. norm(f x) \<partial>M) + (\<integral>\<^sup>+x. norm(F n x) \<partial>M))"
  3.1397 +    by simp
  3.1398 +  also have "... = (\<integral>\<^sup>+x. norm(f x) \<partial>M) + limsup (\<lambda>n. (\<integral>\<^sup>+x. norm(F n x) \<partial>M))"
  3.1399 +    by (rule Limsup_const_add, auto simp add: finint)
  3.1400 +  also have "... \<le> (\<integral>\<^sup>+x. norm(f x) \<partial>M) + (\<integral>\<^sup>+x. norm(f x) \<partial>M)"
  3.1401 +    using assms(4) by (simp add: add_left_mono)
  3.1402 +  also have "... = 2 * (\<integral>\<^sup>+x. norm(f x) \<partial>M)"
  3.1403 +    unfolding one_add_one[symmetric] distrib_right by simp
  3.1404 +  ultimately have a: "limsup (\<lambda>n. (\<integral>\<^sup>+x. ennreal(norm(f x)) + ennreal(norm(F n x)) \<partial>M)) \<le>
  3.1405 +    2 * (\<integral>\<^sup>+x. norm(f x) \<partial>M)" by simp
  3.1406 +
  3.1407 +  have le: "ennreal (norm (F n x - f x)) \<le> ennreal (norm (f x)) + ennreal (norm (F n x))" for n x
  3.1408 +    by (simp add: norm_minus_commute norm_triangle_ineq4 ennreal_plus[symmetric] ennreal_minus del: ennreal_plus)
  3.1409 +  then have le2: "(\<integral>\<^sup>+ x. ennreal (norm (F n x - f x)) \<partial>M) \<le> (\<integral>\<^sup>+ x. ennreal (norm (f x)) + ennreal (norm (F n x)) \<partial>M)" for n
  3.1410 +    by (rule nn_integral_mono)
  3.1411 +
  3.1412 +  have "2 * (\<integral>\<^sup>+ x. norm(f x) \<partial>M) = (\<integral>\<^sup>+ x. liminf (\<lambda>n. ennreal (G n x)) \<partial>M)"
  3.1413 +    by (simp add: int_liminf)
  3.1414 +  also have "\<dots> \<le> liminf (\<lambda>n. (\<integral>\<^sup>+x. G n x \<partial>M))"
  3.1415 +    by (rule nn_integral_liminf) auto
  3.1416 +  also have "liminf (\<lambda>n. (\<integral>\<^sup>+x. G n x \<partial>M)) =
  3.1417 +    liminf (\<lambda>n. (\<integral>\<^sup>+x. ennreal(norm(f x)) + ennreal(norm(F n x)) \<partial>M) - (\<integral>\<^sup>+x. norm(F n x - f x) \<partial>M))"
  3.1418 +  proof (intro arg_cong[where f=liminf] ext)
  3.1419 +    fix n
  3.1420 +    have "\<And>x. ennreal(G n x) = ennreal(norm(f x)) + ennreal(norm(F n x)) - ennreal(norm(F n x - f x))"
  3.1421 +      unfolding G_def by (simp add: ennreal_plus[symmetric] ennreal_minus del: ennreal_plus)
  3.1422 +    moreover have "(\<integral>\<^sup>+x. ennreal(norm(f x)) + ennreal(norm(F n x)) - ennreal(norm(F n x - f x)) \<partial>M)
  3.1423 +            = (\<integral>\<^sup>+x. ennreal(norm(f x)) + ennreal(norm(F n x)) \<partial>M) - (\<integral>\<^sup>+x. norm(F n x - f x) \<partial>M)"
  3.1424 +    proof (rule nn_integral_diff)
  3.1425 +      from le show "AE x in M. ennreal (norm (F n x - f x)) \<le> ennreal (norm (f x)) + ennreal (norm (F n x))"
  3.1426 +        by simp
  3.1427 +      from le2 have "(\<integral>\<^sup>+x. ennreal (norm (F n x - f x)) \<partial>M) < \<infinity>" using assms(1) assms(2)
  3.1428 +        by (metis has_bochner_integral_implies_finite_norm integrable.simps Bochner_Integration.integrable_diff)
  3.1429 +      then show "(\<integral>\<^sup>+x. ennreal (norm (F n x - f x)) \<partial>M) \<noteq> \<infinity>" by simp
  3.1430 +    qed (auto simp add: assms)
  3.1431 +    ultimately show "(\<integral>\<^sup>+x. G n x \<partial>M) = (\<integral>\<^sup>+x. ennreal(norm(f x)) + ennreal(norm(F n x)) \<partial>M) - (\<integral>\<^sup>+x. norm(F n x - f x) \<partial>M)"
  3.1432 +      by simp
  3.1433 +  qed
  3.1434 +  finally have "2 * (\<integral>\<^sup>+ x. norm(f x) \<partial>M) + limsup (\<lambda>n. (\<integral>\<^sup>+x. norm(F n x - f x) \<partial>M)) \<le>
  3.1435 +    liminf (\<lambda>n. (\<integral>\<^sup>+x. ennreal(norm(f x)) + ennreal(norm(F n x)) \<partial>M) - (\<integral>\<^sup>+x. norm(F n x - f x) \<partial>M)) +
  3.1436 +    limsup (\<lambda>n. (\<integral>\<^sup>+x. norm(F n x - f x) \<partial>M))"
  3.1437 +    by (intro add_mono) auto
  3.1438 +  also have "\<dots> \<le> (limsup (\<lambda>n. \<integral>\<^sup>+x. ennreal(norm(f x)) + ennreal(norm(F n x)) \<partial>M) - limsup (\<lambda>n. \<integral>\<^sup>+x. norm (F n x - f x) \<partial>M)) +
  3.1439 +    limsup (\<lambda>n. (\<integral>\<^sup>+x. norm(F n x - f x) \<partial>M))"
  3.1440 +    by (intro add_mono liminf_minus_ennreal le2) auto
  3.1441 +  also have "\<dots> = limsup (\<lambda>n. (\<integral>\<^sup>+x. ennreal(norm(f x)) + ennreal(norm(F n x)) \<partial>M))"
  3.1442 +    by (intro diff_add_cancel_ennreal Limsup_mono always_eventually allI le2)
  3.1443 +  also have "\<dots> \<le> 2 * (\<integral>\<^sup>+x. norm(f x) \<partial>M)"
  3.1444 +    by fact
  3.1445 +  finally have "limsup (\<lambda>n. (\<integral>\<^sup>+x. norm(F n x - f x) \<partial>M)) = 0"
  3.1446 +    using fin2 by simp
  3.1447 +  then show ?thesis
  3.1448 +    by (rule tendsto_0_if_Limsup_eq_0_ennreal)
  3.1449 +qed
  3.1450 +
  3.1451 +lemma Scheffe_lemma2:
  3.1452 +  fixes F::"nat \<Rightarrow> 'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  3.1453 +  assumes "\<And> n::nat. F n \<in> borel_measurable M" "integrable M f"
  3.1454 +          "AE x in M. (\<lambda>n. F n x) \<longlonglongrightarrow> f x"
  3.1455 +          "\<And>n. (\<integral>\<^sup>+ x. norm(F n x) \<partial>M) \<le> (\<integral>\<^sup>+ x. norm(f x) \<partial>M)"
  3.1456 +  shows "(\<lambda>n. \<integral>\<^sup>+ x. norm(F n x - f x) \<partial>M) \<longlonglongrightarrow> 0"
  3.1457 +proof (rule Scheffe_lemma1)
  3.1458 +  fix n::nat
  3.1459 +  have "(\<integral>\<^sup>+ x. norm(f x) \<partial>M) < \<infinity>" using assms(2) by (metis has_bochner_integral_implies_finite_norm integrable.cases)
  3.1460 +  then have "(\<integral>\<^sup>+ x. norm(F n x) \<partial>M) < \<infinity>" using assms(4)[of n] by auto
  3.1461 +  then show "integrable M (F n)" by (subst integrable_iff_bounded, simp add: assms(1)[of n])
  3.1462 +qed (auto simp add: assms Limsup_bounded)
  3.1463 +
  3.1464  end
     4.1 --- a/src/HOL/Analysis/Topology_Euclidean_Space.thy	Thu Oct 13 18:36:06 2016 +0200
     4.2 +++ b/src/HOL/Analysis/Topology_Euclidean_Space.thy	Tue Oct 18 12:01:54 2016 +0200
     4.3 @@ -459,6 +459,99 @@
     4.4  qed
     4.5  
     4.6  
     4.7 +lemma countable_separating_set_linorder1:
     4.8 +  shows "\<exists>B::('a::{linorder_topology, second_countable_topology} set). countable B \<and> (\<forall>x y. x < y \<longrightarrow> (\<exists>b \<in> B. x < b \<and> b \<le> y))"
     4.9 +proof -
    4.10 +  obtain A::"'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto
    4.11 +  define B1 where "B1 = {(LEAST x. x \<in> U)| U. U \<in> A}"
    4.12 +  then have "countable B1" using `countable A` by (simp add: Setcompr_eq_image)
    4.13 +  define B2 where "B2 = {(SOME x. x \<in> U)| U. U \<in> A}"
    4.14 +  then have "countable B2" using `countable A` by (simp add: Setcompr_eq_image)
    4.15 +  have "\<exists>b \<in> B1 \<union> B2. x < b \<and> b \<le> y" if "x < y" for x y
    4.16 +  proof (cases)
    4.17 +    assume "\<exists>z. x < z \<and> z < y"
    4.18 +    then obtain z where z: "x < z \<and> z < y" by auto
    4.19 +    define U where "U = {x<..<y}"
    4.20 +    then have "open U" by simp
    4.21 +    moreover have "z \<in> U" using z U_def by simp
    4.22 +    ultimately obtain V where "V \<in> A" "z \<in> V" "V \<subseteq> U" using topological_basisE[OF `topological_basis A`] by auto
    4.23 +    define w where "w = (SOME x. x \<in> V)"
    4.24 +    then have "w \<in> V" using `z \<in> V` by (metis someI2)
    4.25 +    then have "x < w \<and> w \<le> y" using `w \<in> V` `V \<subseteq> U` U_def by fastforce
    4.26 +    moreover have "w \<in> B1 \<union> B2" using w_def B2_def `V \<in> A` by auto
    4.27 +    ultimately show ?thesis by auto
    4.28 +  next
    4.29 +    assume "\<not>(\<exists>z. x < z \<and> z < y)"
    4.30 +    then have *: "\<And>z. z > x \<Longrightarrow> z \<ge> y" by auto
    4.31 +    define U where "U = {x<..}"
    4.32 +    then have "open U" by simp
    4.33 +    moreover have "y \<in> U" using `x < y` U_def by simp
    4.34 +    ultimately obtain "V" where "V \<in> A" "y \<in> V" "V \<subseteq> U" using topological_basisE[OF `topological_basis A`] by auto
    4.35 +    have "U = {y..}" unfolding U_def using * `x < y` by auto
    4.36 +    then have "V \<subseteq> {y..}" using `V \<subseteq> U` by simp
    4.37 +    then have "(LEAST w. w \<in> V) = y" using `y \<in> V` by (meson Least_equality atLeast_iff subsetCE)
    4.38 +    then have "y \<in> B1 \<union> B2" using `V \<in> A` B1_def by auto
    4.39 +    moreover have "x < y \<and> y \<le> y" using `x < y` by simp
    4.40 +    ultimately show ?thesis by auto
    4.41 +  qed
    4.42 +  moreover have "countable (B1 \<union> B2)" using `countable B1` `countable B2` by simp
    4.43 +  ultimately show ?thesis by auto
    4.44 +qed
    4.45 +
    4.46 +lemma countable_separating_set_linorder2:
    4.47 +  shows "\<exists>B::('a::{linorder_topology, second_countable_topology} set). countable B \<and> (\<forall>x y. x < y \<longrightarrow> (\<exists>b \<in> B. x \<le> b \<and> b < y))"
    4.48 +proof -
    4.49 +  obtain A::"'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto
    4.50 +  define B1 where "B1 = {(GREATEST x. x \<in> U) | U. U \<in> A}"
    4.51 +  then have "countable B1" using `countable A` by (simp add: Setcompr_eq_image)
    4.52 +  define B2 where "B2 = {(SOME x. x \<in> U)| U. U \<in> A}"
    4.53 +  then have "countable B2" using `countable A` by (simp add: Setcompr_eq_image)
    4.54 +  have "\<exists>b \<in> B1 \<union> B2. x \<le> b \<and> b < y" if "x < y" for x y
    4.55 +  proof (cases)
    4.56 +    assume "\<exists>z. x < z \<and> z < y"
    4.57 +    then obtain z where z: "x < z \<and> z < y" by auto
    4.58 +    define U where "U = {x<..<y}"
    4.59 +    then have "open U" by simp
    4.60 +    moreover have "z \<in> U" using z U_def by simp
    4.61 +    ultimately obtain "V" where "V \<in> A" "z \<in> V" "V \<subseteq> U" using topological_basisE[OF `topological_basis A`] by auto
    4.62 +    define w where "w = (SOME x. x \<in> V)"
    4.63 +    then have "w \<in> V" using `z \<in> V` by (metis someI2)
    4.64 +    then have "x \<le> w \<and> w < y" using `w \<in> V` `V \<subseteq> U` U_def by fastforce
    4.65 +    moreover have "w \<in> B1 \<union> B2" using w_def B2_def `V \<in> A` by auto
    4.66 +    ultimately show ?thesis by auto
    4.67 +  next
    4.68 +    assume "\<not>(\<exists>z. x < z \<and> z < y)"
    4.69 +    then have *: "\<And>z. z < y \<Longrightarrow> z \<le> x" using leI by blast
    4.70 +    define U where "U = {..<y}"
    4.71 +    then have "open U" by simp
    4.72 +    moreover have "x \<in> U" using `x < y` U_def by simp
    4.73 +    ultimately obtain "V" where "V \<in> A" "x \<in> V" "V \<subseteq> U" using topological_basisE[OF `topological_basis A`] by auto
    4.74 +    have "U = {..x}" unfolding U_def using * `x < y` by auto
    4.75 +    then have "V \<subseteq> {..x}" using `V \<subseteq> U` by simp
    4.76 +    then have "(GREATEST x. x \<in> V) = x" using `x \<in> V` by (meson Greatest_equality atMost_iff subsetCE)
    4.77 +    then have "x \<in> B1 \<union> B2" using `V \<in> A` B1_def by auto
    4.78 +    moreover have "x \<le> x \<and> x < y" using `x < y` by simp
    4.79 +    ultimately show ?thesis by auto
    4.80 +  qed
    4.81 +  moreover have "countable (B1 \<union> B2)" using `countable B1` `countable B2` by simp
    4.82 +  ultimately show ?thesis by auto
    4.83 +qed
    4.84 +
    4.85 +lemma countable_separating_set_dense_linorder:
    4.86 +  shows "\<exists>B::('a::{linorder_topology, dense_linorder, second_countable_topology} set). countable B \<and> (\<forall>x y. x < y \<longrightarrow> (\<exists>b \<in> B. x < b \<and> b < y))"
    4.87 +proof -
    4.88 +  obtain B::"'a set" where B: "countable B" "\<And>x y. x < y \<Longrightarrow> (\<exists>b \<in> B. x < b \<and> b \<le> y)"
    4.89 +    using countable_separating_set_linorder1 by auto
    4.90 +  have "\<exists>b \<in> B. x < b \<and> b < y" if "x < y" for x y
    4.91 +  proof -
    4.92 +    obtain z where "x < z" "z < y" using `x < y` dense by blast
    4.93 +    then obtain b where "b \<in> B" "x < b \<and> b \<le> z" using B(2) by auto
    4.94 +    then have "x < b \<and> b < y" using `z < y` by auto
    4.95 +    then show ?thesis using `b \<in> B` by auto
    4.96 +  qed
    4.97 +  then show ?thesis using B(1) by auto
    4.98 +qed
    4.99 +
   4.100  subsection \<open>Polish spaces\<close>
   4.101  
   4.102  text \<open>Textbooks define Polish spaces as completely metrizable.
   4.103 @@ -8688,7 +8781,7 @@
   4.104      unfolding homeomorphic_def homeomorphism_def
   4.105      by (metis equalityI image_subset_iff subsetI)
   4.106   qed
   4.107 - 
   4.108 +
   4.109  lemma homeomorphicI [intro?]:
   4.110     "\<lbrakk>f ` S = T; g ` T = S;
   4.111       continuous_on S f; continuous_on T g;
   4.112 @@ -10037,7 +10130,7 @@
   4.113      apply (rule openin_Union, clarify)
   4.114      apply (metis (full_types) \<open>open U\<close> cont clo openin_trans continuous_openin_preimage_gen)
   4.115      done
   4.116 -qed 
   4.117 +qed
   4.118  
   4.119  lemma pasting_lemma_exists:
   4.120    fixes f :: "'i \<Rightarrow> 'a::topological_space \<Rightarrow> 'b::topological_space"
     5.1 --- a/src/HOL/Library/Extended_Nonnegative_Real.thy	Thu Oct 13 18:36:06 2016 +0200
     5.2 +++ b/src/HOL/Library/Extended_Nonnegative_Real.thy	Tue Oct 18 12:01:54 2016 +0200
     5.3 @@ -949,6 +949,9 @@
     5.4    by (cases "0 \<le> x")
     5.5       (auto simp: ennreal_neg ennreal_1[symmetric] simp del: ennreal_1)
     5.6  
     5.7 +lemma one_less_ennreal[simp]: "1 < ennreal x \<longleftrightarrow> 1 < x"
     5.8 +  by transfer (auto simp: max.absorb2 less_max_iff_disj)
     5.9 +
    5.10  lemma ennreal_plus[simp]:
    5.11    "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> ennreal (a + b) = ennreal a + ennreal b"
    5.12    by (transfer fixing: a b) (auto simp: max_absorb2)
     6.1 --- a/src/HOL/Library/Permutations.thy	Thu Oct 13 18:36:06 2016 +0200
     6.2 +++ b/src/HOL/Library/Permutations.thy	Tue Oct 18 12:01:54 2016 +0200
     6.3 @@ -22,6 +22,23 @@
     6.4    "Fun.swap a b id x = (if x = a then b else if x = b then a else x)"
     6.5    by (simp add: Fun.swap_def)
     6.6  
     6.7 +lemma bij_inv_eq_iff: "bij p \<Longrightarrow> x = inv p y \<longleftrightarrow> p x = y"
     6.8 +  using surj_f_inv_f[of p] by (auto simp add: bij_def)
     6.9 +
    6.10 +lemma bij_swap_comp:
    6.11 +  assumes bp: "bij p"
    6.12 +  shows "Fun.swap a b id \<circ> p = Fun.swap (inv p a) (inv p b) p"
    6.13 +  using surj_f_inv_f[OF bij_is_surj[OF bp]]
    6.14 +  by (simp add: fun_eq_iff Fun.swap_def bij_inv_eq_iff[OF bp])
    6.15 +
    6.16 +lemma bij_swap_ompose_bij: "bij p \<Longrightarrow> bij (Fun.swap a b id \<circ> p)"
    6.17 +proof -
    6.18 +  assume H: "bij p"
    6.19 +  show ?thesis
    6.20 +    unfolding bij_swap_comp[OF H] bij_swap_iff
    6.21 +    using H .
    6.22 +qed
    6.23 +
    6.24  
    6.25  subsection \<open>Basic consequences of the definition\<close>
    6.26  
    6.27 @@ -30,7 +47,7 @@
    6.28  
    6.29  lemma permutes_in_image: "p permutes S \<Longrightarrow> p x \<in> S \<longleftrightarrow> x \<in> S"
    6.30    unfolding permutes_def by metis
    6.31 -  
    6.32 +
    6.33  lemma permutes_not_in:
    6.34    assumes "f permutes S" "x \<notin> S" shows "f x = x"
    6.35    using assms by (auto simp: permutes_def)
    6.36 @@ -107,7 +124,7 @@
    6.37  
    6.38  (* Next three lemmas contributed by Lukas Bulwahn *)
    6.39  lemma permutes_bij_inv_into:
    6.40 -  fixes A :: "'a set" and B :: "'b set" 
    6.41 +  fixes A :: "'a set" and B :: "'b set"
    6.42    assumes "p permutes A"
    6.43    assumes "bij_betw f A B"
    6.44    shows "(\<lambda>x. if x \<in> B then f (p (inv_into A f x)) else x) permutes B"
    6.45 @@ -167,9 +184,9 @@
    6.46    unfolding fun_eq_iff permutes_inv_eq[OF pS] permutes_inv_eq[OF permutes_inv[OF pS]]
    6.47    by blast
    6.48  
    6.49 -lemma permutes_invI: 
    6.50 +lemma permutes_invI:
    6.51    assumes perm: "p permutes S"
    6.52 -      and inv:  "\<And>x. x \<in> S \<Longrightarrow> p' (p x) = x" 
    6.53 +      and inv:  "\<And>x. x \<in> S \<Longrightarrow> p' (p x) = x"
    6.54        and outside: "\<And>x. x \<notin> S \<Longrightarrow> p' x = x"
    6.55    shows   "inv p = p'"
    6.56  proof
    6.57 @@ -177,14 +194,14 @@
    6.58    proof (cases "x \<in> S")
    6.59      assume [simp]: "x \<in> S"
    6.60      from assms have "p' x = p' (p (inv p x))" by (simp add: permutes_inverses)
    6.61 -    also from permutes_inv[OF perm] 
    6.62 +    also from permutes_inv[OF perm]
    6.63        have "\<dots> = inv p x" by (subst inv) (simp_all add: permutes_in_image)
    6.64      finally show "inv p x = p' x" ..
    6.65    qed (insert permutes_inv[OF perm], simp_all add: outside permutes_not_in)
    6.66  qed
    6.67  
    6.68  lemma permutes_vimage: "f permutes A \<Longrightarrow> f -` A = A"
    6.69 -  by (simp add: bij_vimage_eq_inv_image permutes_bij permutes_image[OF permutes_inv])  
    6.70 +  by (simp add: bij_vimage_eq_inv_image permutes_bij permutes_image[OF permutes_inv])
    6.71  
    6.72  
    6.73  subsection \<open>The number of permutations on a finite set\<close>
    6.74 @@ -329,7 +346,7 @@
    6.75  lemma finite_permutations:
    6.76    assumes fS: "finite S"
    6.77    shows "finite {p. p permutes S}"
    6.78 -  using card_permutations[OF refl fS] 
    6.79 +  using card_permutations[OF refl fS]
    6.80    by (auto intro: card_ge_0_finite)
    6.81  
    6.82  
    6.83 @@ -724,23 +741,6 @@
    6.84    qed
    6.85  qed
    6.86  
    6.87 -lemma bij_inv_eq_iff: "bij p \<Longrightarrow> x = inv p y \<longleftrightarrow> p x = y"
    6.88 -  using surj_f_inv_f[of p] by (auto simp add: bij_def)
    6.89 -
    6.90 -lemma bij_swap_comp:
    6.91 -  assumes bp: "bij p"
    6.92 -  shows "Fun.swap a b id \<circ> p = Fun.swap (inv p a) (inv p b) p"
    6.93 -  using surj_f_inv_f[OF bij_is_surj[OF bp]]
    6.94 -  by (simp add: fun_eq_iff Fun.swap_def bij_inv_eq_iff[OF bp])
    6.95 -
    6.96 -lemma bij_swap_ompose_bij: "bij p \<Longrightarrow> bij (Fun.swap a b id \<circ> p)"
    6.97 -proof -
    6.98 -  assume H: "bij p"
    6.99 -  show ?thesis
   6.100 -    unfolding bij_swap_comp[OF H] bij_swap_iff
   6.101 -    using H .
   6.102 -qed
   6.103 -
   6.104  lemma permutation_lemma:
   6.105    assumes fS: "finite S"
   6.106      and p: "bij p"
   6.107 @@ -881,7 +881,7 @@
   6.108  lemma sign_idempotent: "sign p * sign p = 1"
   6.109    by (simp add: sign_def)
   6.110  
   6.111 - 
   6.112 +
   6.113  subsection \<open>Permuting a list\<close>
   6.114  
   6.115  text \<open>This function permutes a list by applying a permutation to the indices.\<close>
   6.116 @@ -889,7 +889,7 @@
   6.117  definition permute_list :: "(nat \<Rightarrow> nat) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
   6.118    "permute_list f xs = map (\<lambda>i. xs ! (f i)) [0..<length xs]"
   6.119  
   6.120 -lemma permute_list_map: 
   6.121 +lemma permute_list_map:
   6.122    assumes "f permutes {..<length xs}"
   6.123    shows   "permute_list f (map g xs) = map g (permute_list f xs)"
   6.124    using permutes_in_image[OF assms] by (auto simp: permute_list_def)
   6.125 @@ -897,7 +897,7 @@
   6.126  lemma permute_list_nth:
   6.127    assumes "f permutes {..<length xs}" "i < length xs"
   6.128    shows   "permute_list f xs ! i = xs ! f i"
   6.129 -  using permutes_in_image[OF assms(1)] assms(2) 
   6.130 +  using permutes_in_image[OF assms(1)] assms(2)
   6.131    by (simp add: permute_list_def)
   6.132  
   6.133  lemma permute_list_Nil [simp]: "permute_list f [] = []"
   6.134 @@ -906,7 +906,7 @@
   6.135  lemma length_permute_list [simp]: "length (permute_list f xs) = length xs"
   6.136    by (simp add: permute_list_def)
   6.137  
   6.138 -lemma permute_list_compose: 
   6.139 +lemma permute_list_compose:
   6.140    assumes "g permutes {..<length xs}"
   6.141    shows   "permute_list (f \<circ> g) xs = permute_list g (permute_list f xs)"
   6.142    using assms[THEN permutes_in_image] by (auto simp add: permute_list_def)
   6.143 @@ -924,7 +924,7 @@
   6.144    fix y :: 'a
   6.145    from assms have [simp]: "f x < length xs \<longleftrightarrow> x < length xs" for x
   6.146      using permutes_in_image[OF assms] by auto
   6.147 -  have "count (mset (permute_list f xs)) y = 
   6.148 +  have "count (mset (permute_list f xs)) y =
   6.149            card ((\<lambda>i. xs ! f i) -` {y} \<inter> {..<length xs})"
   6.150      by (simp add: permute_list_def mset_map count_image_mset atLeast0LessThan)
   6.151    also have "(\<lambda>i. xs ! f i) -` {y} \<inter> {..<length xs} = f -` {i. i < length xs \<and> y = xs ! i}"
   6.152 @@ -935,7 +935,7 @@
   6.153    finally show "count (mset (permute_list f xs)) y = count (mset xs) y" by simp
   6.154  qed
   6.155  
   6.156 -lemma set_permute_list [simp]:                   
   6.157 +lemma set_permute_list [simp]:
   6.158    assumes "f permutes {..<length xs}"
   6.159    shows   "set (permute_list f xs) = set xs"
   6.160    by (rule mset_eq_setD[OF mset_permute_list]) fact
   6.161 @@ -945,7 +945,7 @@
   6.162    shows   "distinct (permute_list f xs) = distinct xs"
   6.163    by (simp add: distinct_count_atmost_1 assms)
   6.164  
   6.165 -lemma permute_list_zip: 
   6.166 +lemma permute_list_zip:
   6.167    assumes "f permutes A" "A = {..<length xs}"
   6.168    assumes [simp]: "length xs = length ys"
   6.169    shows   "permute_list f (zip xs ys) = zip (permute_list f xs) (permute_list f ys)"
   6.170 @@ -961,7 +961,7 @@
   6.171    finally show ?thesis .
   6.172  qed
   6.173  
   6.174 -lemma map_of_permute: 
   6.175 +lemma map_of_permute:
   6.176    assumes "\<sigma> permutes fst ` set xs"
   6.177    shows   "map_of xs \<circ> \<sigma> = map_of (map (\<lambda>(x,y). (inv \<sigma> x, y)) xs)" (is "_ = map_of (map ?f _)")
   6.178  proof
   6.179 @@ -993,7 +993,7 @@
   6.180      from this have "count (image_mset f (mset_set (insert x F))) b = Suc (card {a \<in> F. f a = f x})"
   6.181        using insert.hyps by auto
   6.182      also have "\<dots> = card (insert x {a \<in> F. f a = f x})"
   6.183 -      using insert.hyps(1,2) by simp  
   6.184 +      using insert.hyps(1,2) by simp
   6.185      also have "card (insert x {a \<in> F. f a = f x}) = card {a \<in> insert x F. f a = b}"
   6.186        using \<open>f x = b\<close> by (auto intro: arg_cong[where f="card"])
   6.187      finally show ?thesis using insert by auto
   6.188 @@ -1003,7 +1003,7 @@
   6.189      with insert A show ?thesis by simp
   6.190    qed
   6.191  qed
   6.192 -  
   6.193 +
   6.194  (* Prove image_mset_eq_implies_permutes *)
   6.195  lemma image_mset_eq_implies_permutes:
   6.196    fixes f :: "'a \<Rightarrow> 'b"
   6.197 @@ -1317,7 +1317,7 @@
   6.198  subsection \<open>Constructing permutations from association lists\<close>
   6.199  
   6.200  definition list_permutes where
   6.201 -  "list_permutes xs A \<longleftrightarrow> set (map fst xs) \<subseteq> A \<and> set (map snd xs) = set (map fst xs) \<and> 
   6.202 +  "list_permutes xs A \<longleftrightarrow> set (map fst xs) \<subseteq> A \<and> set (map snd xs) = set (map fst xs) \<and>
   6.203       distinct (map fst xs) \<and> distinct (map snd xs)"
   6.204  
   6.205  lemma list_permutesI [simp]:
   6.206 @@ -1349,8 +1349,8 @@
   6.207  proof (rule inj_onI)
   6.208    fix x y assume xy: "x \<in> set (map fst xs)" "y \<in> set (map fst xs)"
   6.209    assume eq: "map_of xs x = map_of xs y"
   6.210 -  from xy obtain x' y' 
   6.211 -    where x'y': "map_of xs x = Some x'" "map_of xs y = Some y'" 
   6.212 +  from xy obtain x' y'
   6.213 +    where x'y': "map_of xs x = Some x'" "map_of xs y = Some y'"
   6.214      by (cases "map_of xs x"; cases "map_of xs y")
   6.215         (simp_all add: map_of_eq_None_iff)
   6.216    moreover from x'y' have *: "(x,x') \<in> set xs" "(y,y') \<in> set xs"
   6.217 @@ -1398,7 +1398,7 @@
   6.218    also from assms have "?f ` set (map fst xs) = (the \<circ> map_of xs) ` set (map fst xs)"
   6.219      by (intro image_cong refl)
   6.220         (auto simp: permutation_of_list_def map_of_eq_None_iff split: option.splits)
   6.221 -  also from assms have "\<dots> = set (map fst xs)" 
   6.222 +  also from assms have "\<dots> = set (map fst xs)"
   6.223      by (subst image_map_of') (simp_all add: list_permutes_def)
   6.224    finally show "bij_betw ?f (set (map fst xs)) (set (map fst xs))" .
   6.225  qed (force simp: permutation_of_list_def dest!: map_of_SomeD split: option.splits)+
     7.1 --- a/src/HOL/Probability/Levy.thy	Thu Oct 13 18:36:06 2016 +0200
     7.2 +++ b/src/HOL/Probability/Levy.thy	Tue Oct 18 12:01:54 2016 +0200
     7.3 @@ -8,11 +8,6 @@
     7.4    imports Characteristic_Functions Helly_Selection Sinc_Integral
     7.5  begin
     7.6  
     7.7 -lemma LIM_zero_cancel:
     7.8 -  fixes f :: "_ \<Rightarrow> 'b::real_normed_vector"
     7.9 -  shows "((\<lambda>x. f x - l) \<longlongrightarrow> 0) F \<Longrightarrow> (f \<longlongrightarrow> l) F"
    7.10 -unfolding tendsto_iff dist_norm by simp
    7.11 -
    7.12  subsection \<open>The Levy inversion theorem\<close>
    7.13  
    7.14  (* Actually, this is not needed for us -- but it is useful for other purposes. (See Billingsley.) *)
     8.1 --- a/src/HOL/Topological_Spaces.thy	Thu Oct 13 18:36:06 2016 +0200
     8.2 +++ b/src/HOL/Topological_Spaces.thy	Tue Oct 18 12:01:54 2016 +0200
     8.3 @@ -3398,4 +3398,93 @@
     8.4  lemma isCont_swap[continuous_intros]: "isCont prod.swap a"
     8.5    using continuous_on_eq_continuous_within continuous_on_swap by blast
     8.6  
     8.7 +lemma open_diagonal_complement:
     8.8 +  "open {(x,y) | x y. x \<noteq> (y::('a::t2_space))}"
     8.9 +proof (rule topological_space_class.openI)
    8.10 +  fix t assume "t \<in> {(x, y) | x y. x \<noteq> (y::'a)}"
    8.11 +  then obtain x y where "t = (x,y)" "x \<noteq> y" by blast
    8.12 +  then obtain U V where *: "open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
    8.13 +    by (auto simp add: separation_t2)
    8.14 +  define T where "T = U \<times> V"
    8.15 +  have "open T" using * open_Times T_def by auto
    8.16 +  moreover have "t \<in> T" unfolding T_def using `t = (x,y)` * by auto
    8.17 +  moreover have "T \<subseteq> {(x, y) | x y. x \<noteq> y}" unfolding T_def using * by auto
    8.18 +  ultimately show "\<exists>T. open T \<and> t \<in> T \<and> T \<subseteq> {(x, y) | x y. x \<noteq> y}" by auto
    8.19 +qed
    8.20 +
    8.21 +lemma closed_diagonal:
    8.22 +  "closed {y. \<exists> x::('a::t2_space). y = (x,x)}"
    8.23 +proof -
    8.24 +  have "{y. \<exists> x::'a. y = (x,x)} = UNIV - {(x,y) | x y. x \<noteq> y}" by auto
    8.25 +  then show ?thesis using open_diagonal_complement closed_Diff by auto
    8.26 +qed
    8.27 +
    8.28 +lemma open_superdiagonal:
    8.29 +  "open {(x,y) | x y. x > (y::'a::{linorder_topology})}"
    8.30 +proof (rule topological_space_class.openI)
    8.31 +  fix t assume "t \<in> {(x, y) | x y. y < (x::'a)}"
    8.32 +  then obtain x y where "t = (x, y)" "x > y" by blast
    8.33 +  show "\<exists>T. open T \<and> t \<in> T \<and> T \<subseteq> {(x, y) | x y. y < x}"
    8.34 +  proof (cases)
    8.35 +    assume "\<exists>z. y < z \<and> z < x"
    8.36 +    then obtain z where z: "y < z \<and> z < x" by blast
    8.37 +    define T where "T = {z<..} \<times> {..<z}"
    8.38 +    have "open T" unfolding T_def by (simp add: open_Times)
    8.39 +    moreover have "t \<in> T" using T_def z `t = (x,y)` by auto
    8.40 +    moreover have "T \<subseteq> {(x, y) | x y. y < x}" unfolding T_def by auto
    8.41 +    ultimately show ?thesis by auto
    8.42 +  next
    8.43 +    assume "\<not>(\<exists>z. y < z \<and> z < x)"
    8.44 +    then have *: "{x ..} = {y<..}" "{..< x} = {..y}"
    8.45 +      using `x > y` apply auto using leI by blast
    8.46 +    define T where "T = {x ..} \<times> {.. y}"
    8.47 +    then have "T = {y<..} \<times> {..< x}" using * by simp
    8.48 +    then have "open T" unfolding T_def by (simp add: open_Times)
    8.49 +    moreover have "t \<in> T" using T_def `t = (x,y)` by auto
    8.50 +    moreover have "T \<subseteq> {(x, y) | x y. y < x}" unfolding T_def using `x > y` by auto
    8.51 +    ultimately show ?thesis by auto
    8.52 +  qed
    8.53 +qed
    8.54 +
    8.55 +lemma closed_subdiagonal:
    8.56 +  "closed {(x,y) | x y. x \<le> (y::'a::{linorder_topology})}"
    8.57 +proof -
    8.58 +  have "{(x,y) | x y. x \<le> (y::'a)} = UNIV - {(x,y) | x y. x > (y::'a)}" by auto
    8.59 +  then show ?thesis using open_superdiagonal closed_Diff by auto
    8.60 +qed
    8.61 +
    8.62 +lemma open_subdiagonal:
    8.63 +  "open {(x,y) | x y. x < (y::'a::{linorder_topology})}"
    8.64 +proof (rule topological_space_class.openI)
    8.65 +  fix t assume "t \<in> {(x, y) | x y. y > (x::'a)}"
    8.66 +  then obtain x y where "t = (x, y)" "x < y" by blast
    8.67 +  show "\<exists>T. open T \<and> t \<in> T \<and> T \<subseteq> {(x, y) | x y. y > x}"
    8.68 +  proof (cases)
    8.69 +    assume "\<exists>z. y > z \<and> z > x"
    8.70 +    then obtain z where z: "y > z \<and> z > x" by blast
    8.71 +    define T where "T = {..<z} \<times> {z<..}"
    8.72 +    have "open T" unfolding T_def by (simp add: open_Times)
    8.73 +    moreover have "t \<in> T" using T_def z `t = (x,y)` by auto
    8.74 +    moreover have "T \<subseteq> {(x, y) |x y. y > x}" unfolding T_def by auto
    8.75 +    ultimately show ?thesis by auto
    8.76 +  next
    8.77 +    assume "\<not>(\<exists>z. y > z \<and> z > x)"
    8.78 +    then have *: "{..x} = {..<y}" "{x<..} = {y..}"
    8.79 +      using `x < y` apply auto using leI by blast
    8.80 +    define T where "T = {..x} \<times> {y..}"
    8.81 +    then have "T = {..<y} \<times> {x<..}" using * by simp
    8.82 +    then have "open T" unfolding T_def by (simp add: open_Times)
    8.83 +    moreover have "t \<in> T" using T_def `t = (x,y)` by auto
    8.84 +    moreover have "T \<subseteq> {(x, y) |x y. y > x}" unfolding T_def using `x < y` by auto
    8.85 +    ultimately show ?thesis by auto
    8.86 +  qed
    8.87 +qed
    8.88 +
    8.89 +lemma closed_superdiagonal:
    8.90 +  "closed {(x,y) | x y. x \<ge> (y::('a::{linorder_topology}))}"
    8.91 +proof -
    8.92 +  have "{(x,y) | x y. x \<ge> (y::'a)} = UNIV - {(x,y) | x y. x < y}" by auto
    8.93 +  then show ?thesis using open_subdiagonal closed_Diff by auto
    8.94 +qed
    8.95 +
    8.96  end