src/HOL/Analysis/Extended_Real_Limits.thy
author wenzelm
Mon Mar 25 17:21:26 2019 +0100 (3 weeks ago)
changeset 69981 3dced198b9ec
parent 69722 b5163b2132c5
child 70136 f03a01a18c6e
permissions -rw-r--r--
more strict AFP properties;
     1 (*  Title:      HOL/Analysis/Extended_Real_Limits.thy
     2     Author:     Johannes Hölzl, TU München
     3     Author:     Robert Himmelmann, TU München
     4     Author:     Armin Heller, TU München
     5     Author:     Bogdan Grechuk, University of Edinburgh
     6 *)
     7 
     8 section \<open>Limits on the Extended Real Number Line\<close> (* TO FIX: perhaps put all Nonstandard Analysis related
     9 topics together? *)
    10 
    11 theory Extended_Real_Limits
    12 imports
    13   Topology_Euclidean_Space
    14   "HOL-Library.Extended_Real"
    15   "HOL-Library.Extended_Nonnegative_Real"
    16   "HOL-Library.Indicator_Function"
    17 begin
    18 
    19 lemma compact_UNIV:
    20   "compact (UNIV :: 'a::{complete_linorder,linorder_topology,second_countable_topology} set)"
    21   using compact_complete_linorder
    22   by (auto simp: seq_compact_eq_compact[symmetric] seq_compact_def)
    23 
    24 lemma compact_eq_closed:
    25   fixes S :: "'a::{complete_linorder,linorder_topology,second_countable_topology} set"
    26   shows "compact S \<longleftrightarrow> closed S"
    27   using%unimportant closed_Int_compact[of S, OF _ compact_UNIV] compact_imp_closed
    28   by auto
    29 
    30 lemma closed_contains_Sup_cl:
    31   fixes S :: "'a::{complete_linorder,linorder_topology,second_countable_topology} set"
    32   assumes "closed S"
    33     and "S \<noteq> {}"
    34   shows "Sup S \<in> S"
    35 proof -
    36   from compact_eq_closed[of S] compact_attains_sup[of S] assms
    37   obtain s where S: "s \<in> S" "\<forall>t\<in>S. t \<le> s"
    38     by auto
    39   then have "Sup S = s"
    40     by (auto intro!: Sup_eqI)
    41   with S show ?thesis
    42     by simp
    43 qed
    44 
    45 lemma closed_contains_Inf_cl:
    46   fixes S :: "'a::{complete_linorder,linorder_topology,second_countable_topology} set"
    47   assumes "closed S"
    48     and "S \<noteq> {}"
    49   shows "Inf S \<in> S"
    50 proof -
    51   from compact_eq_closed[of S] compact_attains_inf[of S] assms
    52   obtain s where S: "s \<in> S" "\<forall>t\<in>S. s \<le> t"
    53     by auto
    54   then have "Inf S = s"
    55     by (auto intro!: Inf_eqI)
    56   with S show ?thesis
    57     by simp
    58 qed
    59 
    60 instance%unimportant enat :: second_countable_topology
    61 proof
    62   show "\<exists>B::enat set set. countable B \<and> open = generate_topology B"
    63   proof (intro exI conjI)
    64     show "countable (range lessThan \<union> range greaterThan::enat set set)"
    65       by auto
    66   qed (simp add: open_enat_def)
    67 qed
    68 
    69 instance%unimportant ereal :: second_countable_topology
    70 proof (standard, intro exI conjI)
    71   let ?B = "(\<Union>r\<in>\<rat>. {{..< r}, {r <..}} :: ereal set set)"
    72   show "countable ?B"
    73     by (auto intro: countable_rat)
    74   show "open = generate_topology ?B"
    75   proof (intro ext iffI)
    76     fix S :: "ereal set"
    77     assume "open S"
    78     then show "generate_topology ?B S"
    79       unfolding open_generated_order
    80     proof induct
    81       case (Basis b)
    82       then obtain e where "b = {..<e} \<or> b = {e<..}"
    83         by auto
    84       moreover have "{..<e} = \<Union>{{..<x}|x. x \<in> \<rat> \<and> x < e}" "{e<..} = \<Union>{{x<..}|x. x \<in> \<rat> \<and> e < x}"
    85         by (auto dest: ereal_dense3
    86                  simp del: ex_simps
    87                  simp add: ex_simps[symmetric] conj_commute Rats_def image_iff)
    88       ultimately show ?case
    89         by (auto intro: generate_topology.intros)
    90     qed (auto intro: generate_topology.intros)
    91   next
    92     fix S
    93     assume "generate_topology ?B S"
    94     then show "open S"
    95       by induct auto
    96   qed
    97 qed
    98 
    99 text \<open>This is a copy from \<open>ereal :: second_countable_topology\<close>. Maybe find a common super class of
   100 topological spaces where the rational numbers are densely embedded ?\<close>
   101 instance ennreal :: second_countable_topology
   102 proof (standard, intro exI conjI)
   103   let ?B = "(\<Union>r\<in>\<rat>. {{..< r}, {r <..}} :: ennreal set set)"
   104   show "countable ?B"
   105     by (auto intro: countable_rat)
   106   show "open = generate_topology ?B"
   107   proof (intro ext iffI)
   108     fix S :: "ennreal set"
   109     assume "open S"
   110     then show "generate_topology ?B S"
   111       unfolding open_generated_order
   112     proof induct
   113       case (Basis b)
   114       then obtain e where "b = {..<e} \<or> b = {e<..}"
   115         by auto
   116       moreover have "{..<e} = \<Union>{{..<x}|x. x \<in> \<rat> \<and> x < e}" "{e<..} = \<Union>{{x<..}|x. x \<in> \<rat> \<and> e < x}"
   117         by (auto dest: ennreal_rat_dense
   118                  simp del: ex_simps
   119                  simp add: ex_simps[symmetric] conj_commute Rats_def image_iff)
   120       ultimately show ?case
   121         by (auto intro: generate_topology.intros)
   122     qed (auto intro: generate_topology.intros)
   123   next
   124     fix S
   125     assume "generate_topology ?B S"
   126     then show "open S"
   127       by induct auto
   128   qed
   129 qed
   130 
   131 lemma ereal_open_closed_aux:
   132   fixes S :: "ereal set"
   133   assumes "open S"
   134     and "closed S"
   135     and S: "(-\<infinity>) \<notin> S"
   136   shows "S = {}"
   137 proof (rule ccontr)
   138   assume "\<not> ?thesis"
   139   then have *: "Inf S \<in> S"
   140 
   141     by (metis assms(2) closed_contains_Inf_cl)
   142   {
   143     assume "Inf S = -\<infinity>"
   144     then have False
   145       using * assms(3) by auto
   146   }
   147   moreover
   148   {
   149     assume "Inf S = \<infinity>"
   150     then have "S = {\<infinity>}"
   151       by (metis Inf_eq_PInfty \<open>S \<noteq> {}\<close>)
   152     then have False
   153       by (metis assms(1) not_open_singleton)
   154   }
   155   moreover
   156   {
   157     assume fin: "\<bar>Inf S\<bar> \<noteq> \<infinity>"
   158     from ereal_open_cont_interval[OF assms(1) * fin]
   159     obtain e where e: "e > 0" "{Inf S - e<..<Inf S + e} \<subseteq> S" .
   160     then obtain b where b: "Inf S - e < b" "b < Inf S"
   161       using fin ereal_between[of "Inf S" e] dense[of "Inf S - e"]
   162       by auto
   163     then have "b \<in> {Inf S - e <..< Inf S + e}"
   164       using e fin ereal_between[of "Inf S" e]
   165       by auto
   166     then have "b \<in> S"
   167       using e by auto
   168     then have False
   169       using b by (metis complete_lattice_class.Inf_lower leD)
   170   }
   171   ultimately show False
   172     by auto
   173 qed
   174 
   175 lemma ereal_open_closed:
   176   fixes S :: "ereal set"
   177   shows "open S \<and> closed S \<longleftrightarrow> S = {} \<or> S = UNIV"
   178 proof -
   179   {
   180     assume lhs: "open S \<and> closed S"
   181     {
   182       assume "-\<infinity> \<notin> S"
   183       then have "S = {}"
   184         using lhs ereal_open_closed_aux by auto
   185     }
   186     moreover
   187     {
   188       assume "-\<infinity> \<in> S"
   189       then have "- S = {}"
   190         using lhs ereal_open_closed_aux[of "-S"] by auto
   191     }
   192     ultimately have "S = {} \<or> S = UNIV"
   193       by auto
   194   }
   195   then show ?thesis
   196     by auto
   197 qed
   198 
   199 lemma ereal_open_atLeast:
   200   fixes x :: ereal
   201   shows "open {x..} \<longleftrightarrow> x = -\<infinity>"
   202 proof
   203   assume "x = -\<infinity>"
   204   then have "{x..} = UNIV"
   205     by auto
   206   then show "open {x..}"
   207     by auto
   208 next
   209   assume "open {x..}"
   210   then have "open {x..} \<and> closed {x..}"
   211     by auto
   212   then have "{x..} = UNIV"
   213     unfolding ereal_open_closed by auto
   214   then show "x = -\<infinity>"
   215     by (simp add: bot_ereal_def atLeast_eq_UNIV_iff)
   216 qed
   217 
   218 lemma mono_closed_real:
   219   fixes S :: "real set"
   220   assumes mono: "\<forall>y z. y \<in> S \<and> y \<le> z \<longrightarrow> z \<in> S"
   221     and "closed S"
   222   shows "S = {} \<or> S = UNIV \<or> (\<exists>a. S = {a..})"
   223 proof -
   224   {
   225     assume "S \<noteq> {}"
   226     { assume ex: "\<exists>B. \<forall>x\<in>S. B \<le> x"
   227       then have *: "\<forall>x\<in>S. Inf S \<le> x"
   228         using cInf_lower[of _ S] ex by (metis bdd_below_def)
   229       then have "Inf S \<in> S"
   230         apply (subst closed_contains_Inf)
   231         using ex \<open>S \<noteq> {}\<close> \<open>closed S\<close>
   232         apply auto
   233         done
   234       then have "\<forall>x. Inf S \<le> x \<longleftrightarrow> x \<in> S"
   235         using mono[rule_format, of "Inf S"] *
   236         by auto
   237       then have "S = {Inf S ..}"
   238         by auto
   239       then have "\<exists>a. S = {a ..}"
   240         by auto
   241     }
   242     moreover
   243     {
   244       assume "\<not> (\<exists>B. \<forall>x\<in>S. B \<le> x)"
   245       then have nex: "\<forall>B. \<exists>x\<in>S. x < B"
   246         by (simp add: not_le)
   247       {
   248         fix y
   249         obtain x where "x\<in>S" and "x < y"
   250           using nex by auto
   251         then have "y \<in> S"
   252           using mono[rule_format, of x y] by auto
   253       }
   254       then have "S = UNIV"
   255         by auto
   256     }
   257     ultimately have "S = UNIV \<or> (\<exists>a. S = {a ..})"
   258       by blast
   259   }
   260   then show ?thesis
   261     by blast
   262 qed
   263 
   264 lemma mono_closed_ereal:
   265   fixes S :: "real set"
   266   assumes mono: "\<forall>y z. y \<in> S \<and> y \<le> z \<longrightarrow> z \<in> S"
   267     and "closed S"
   268   shows "\<exists>a. S = {x. a \<le> ereal x}"
   269 proof -
   270   {
   271     assume "S = {}"
   272     then have ?thesis
   273       apply (rule_tac x=PInfty in exI)
   274       apply auto
   275       done
   276   }
   277   moreover
   278   {
   279     assume "S = UNIV"
   280     then have ?thesis
   281       apply (rule_tac x="-\<infinity>" in exI)
   282       apply auto
   283       done
   284   }
   285   moreover
   286   {
   287     assume "\<exists>a. S = {a ..}"
   288     then obtain a where "S = {a ..}"
   289       by auto
   290     then have ?thesis
   291       apply (rule_tac x="ereal a" in exI)
   292       apply auto
   293       done
   294   }
   295   ultimately show ?thesis
   296     using mono_closed_real[of S] assms by auto
   297 qed
   298 
   299 lemma Liminf_within:
   300   fixes f :: "'a::metric_space \<Rightarrow> 'b::complete_lattice"
   301   shows "Liminf (at x within S) f = (SUP e\<in>{0<..}. INF y\<in>(S \<inter> ball x e - {x}). f y)"
   302   unfolding Liminf_def eventually_at
   303 proof (rule SUP_eq, simp_all add: Ball_def Bex_def, safe)
   304   fix P d
   305   assume "0 < d" and "\<forall>y. y \<in> S \<longrightarrow> y \<noteq> x \<and> dist y x < d \<longrightarrow> P y"
   306   then have "S \<inter> ball x d - {x} \<subseteq> {x. P x}"
   307     by (auto simp: zero_less_dist_iff dist_commute)
   308   then show "\<exists>r>0. Inf (f ` (Collect P)) \<le> Inf (f ` (S \<inter> ball x r - {x}))"
   309     by (intro exI[of _ d] INF_mono conjI \<open>0 < d\<close>) auto
   310 next
   311   fix d :: real
   312   assume "0 < d"
   313   then show "\<exists>P. (\<exists>d>0. \<forall>xa. xa \<in> S \<longrightarrow> xa \<noteq> x \<and> dist xa x < d \<longrightarrow> P xa) \<and>
   314     Inf (f ` (S \<inter> ball x d - {x})) \<le> Inf (f ` (Collect P))"
   315     by (intro exI[of _ "\<lambda>y. y \<in> S \<inter> ball x d - {x}"])
   316        (auto intro!: INF_mono exI[of _ d] simp: dist_commute)
   317 qed
   318 
   319 lemma Limsup_within:
   320   fixes f :: "'a::metric_space \<Rightarrow> 'b::complete_lattice"
   321   shows "Limsup (at x within S) f = (INF e\<in>{0<..}. SUP y\<in>(S \<inter> ball x e - {x}). f y)"
   322   unfolding Limsup_def eventually_at
   323 proof (rule INF_eq, simp_all add: Ball_def Bex_def, safe)
   324   fix P d
   325   assume "0 < d" and "\<forall>y. y \<in> S \<longrightarrow> y \<noteq> x \<and> dist y x < d \<longrightarrow> P y"
   326   then have "S \<inter> ball x d - {x} \<subseteq> {x. P x}"
   327     by (auto simp: zero_less_dist_iff dist_commute)
   328   then show "\<exists>r>0. Sup (f ` (S \<inter> ball x r - {x})) \<le> Sup (f ` (Collect P))"
   329     by (intro exI[of _ d] SUP_mono conjI \<open>0 < d\<close>) auto
   330 next
   331   fix d :: real
   332   assume "0 < d"
   333   then show "\<exists>P. (\<exists>d>0. \<forall>xa. xa \<in> S \<longrightarrow> xa \<noteq> x \<and> dist xa x < d \<longrightarrow> P xa) \<and>
   334     Sup (f ` (Collect P)) \<le> Sup (f ` (S \<inter> ball x d - {x}))"
   335     by (intro exI[of _ "\<lambda>y. y \<in> S \<inter> ball x d - {x}"])
   336        (auto intro!: SUP_mono exI[of _ d] simp: dist_commute)
   337 qed
   338 
   339 lemma Liminf_at:
   340   fixes f :: "'a::metric_space \<Rightarrow> 'b::complete_lattice"
   341   shows "Liminf (at x) f = (SUP e\<in>{0<..}. INF y\<in>(ball x e - {x}). f y)"
   342   using Liminf_within[of x UNIV f] by simp
   343 
   344 lemma Limsup_at:
   345   fixes f :: "'a::metric_space \<Rightarrow> 'b::complete_lattice"
   346   shows "Limsup (at x) f = (INF e\<in>{0<..}. SUP y\<in>(ball x e - {x}). f y)"
   347   using Limsup_within[of x UNIV f] by simp
   348 
   349 lemma min_Liminf_at:
   350   fixes f :: "'a::metric_space \<Rightarrow> 'b::complete_linorder"
   351   shows "min (f x) (Liminf (at x) f) = (SUP e\<in>{0<..}. INF y\<in>ball x e. f y)"
   352   apply (simp add: inf_min [symmetric] Liminf_at)
   353   apply (subst inf_commute)
   354   apply (subst SUP_inf)
   355   apply auto
   356   apply (metis (no_types, lifting) INF_insert centre_in_ball greaterThan_iff image_cong inf_commute insert_Diff)
   357   done
   358 
   359 
   360 subsection \<open>Extended-Real.thy\<close> (*FIX ME change title *)
   361 
   362 lemma sum_constant_ereal:
   363   fixes a::ereal
   364   shows "(\<Sum>i\<in>I. a) = a * card I"
   365 apply (cases "finite I", induct set: finite, simp_all)
   366 apply (cases a, auto, metis (no_types, hide_lams) add.commute mult.commute semiring_normalization_rules(3))
   367 done
   368 
   369 lemma real_lim_then_eventually_real:
   370   assumes "(u \<longlongrightarrow> ereal l) F"
   371   shows "eventually (\<lambda>n. u n = ereal(real_of_ereal(u n))) F"
   372 proof -
   373   have "ereal l \<in> {-\<infinity><..<(\<infinity>::ereal)}" by simp
   374   moreover have "open {-\<infinity><..<(\<infinity>::ereal)}" by simp
   375   ultimately have "eventually (\<lambda>n. u n \<in> {-\<infinity><..<(\<infinity>::ereal)}) F" using assms tendsto_def by blast
   376   moreover have "\<And>x. x \<in> {-\<infinity><..<(\<infinity>::ereal)} \<Longrightarrow> x = ereal(real_of_ereal x)" using ereal_real by auto
   377   ultimately show ?thesis by (metis (mono_tags, lifting) eventually_mono)
   378 qed
   379 
   380 lemma ereal_Inf_cmult:
   381   assumes "c>(0::real)"
   382   shows "Inf {ereal c * x |x. P x} = ereal c * Inf {x. P x}"
   383 proof -
   384   have "(\<lambda>x::ereal. c * x) (Inf {x::ereal. P x}) = Inf ((\<lambda>x::ereal. c * x)`{x::ereal. P x})"
   385     apply (rule mono_bij_Inf)
   386     apply (simp add: assms ereal_mult_left_mono less_imp_le mono_def)
   387     apply (rule bij_betw_byWitness[of _ "\<lambda>x. (x::ereal) / c"], auto simp add: assms ereal_mult_divide)
   388     using assms ereal_divide_eq apply auto
   389     done
   390   then show ?thesis by (simp only: setcompr_eq_image[symmetric])
   391 qed
   392 
   393 
   394 subsubsection%important \<open>Continuity of addition\<close>
   395 
   396 text \<open>The next few lemmas remove an unnecessary assumption in \<open>tendsto_add_ereal\<close>, culminating
   397 in \<open>tendsto_add_ereal_general\<close> which essentially says that the addition
   398 is continuous on ereal times ereal, except at \<open>(-\<infinity>, \<infinity>)\<close> and \<open>(\<infinity>, -\<infinity>)\<close>.
   399 It is much more convenient in many situations, see for instance the proof of
   400 \<open>tendsto_sum_ereal\<close> below.\<close>
   401 
   402 lemma tendsto_add_ereal_PInf:
   403   fixes y :: ereal
   404   assumes y: "y \<noteq> -\<infinity>"
   405   assumes f: "(f \<longlongrightarrow> \<infinity>) F" and g: "(g \<longlongrightarrow> y) F"
   406   shows "((\<lambda>x. f x + g x) \<longlongrightarrow> \<infinity>) F"
   407 proof -
   408   have "\<exists>C. eventually (\<lambda>x. g x > ereal C) F"
   409   proof (cases y)
   410     case (real r)
   411     have "y > y-1" using y real by (simp add: ereal_between(1))
   412     then have "eventually (\<lambda>x. g x > y - 1) F" using g y order_tendsto_iff by auto
   413     moreover have "y-1 = ereal(real_of_ereal(y-1))"
   414       by (metis real ereal_eq_1(1) ereal_minus(1) real_of_ereal.simps(1))
   415     ultimately have "eventually (\<lambda>x. g x > ereal(real_of_ereal(y - 1))) F" by simp
   416     then show ?thesis by auto
   417   next
   418     case (PInf)
   419     have "eventually (\<lambda>x. g x > ereal 0) F" using g PInf by (simp add: tendsto_PInfty)
   420     then show ?thesis by auto
   421   qed (simp add: y)
   422   then obtain C::real where ge: "eventually (\<lambda>x. g x > ereal C) F" by auto
   423 
   424   {
   425     fix M::real
   426     have "eventually (\<lambda>x. f x > ereal(M - C)) F" using f by (simp add: tendsto_PInfty)
   427     then have "eventually (\<lambda>x. (f x > ereal (M-C)) \<and> (g x > ereal C)) F"
   428       by (auto simp add: ge eventually_conj_iff)
   429     moreover have "\<And>x. ((f x > ereal (M-C)) \<and> (g x > ereal C)) \<Longrightarrow> (f x + g x > ereal M)"
   430       using ereal_add_strict_mono2 by fastforce
   431     ultimately have "eventually (\<lambda>x. f x + g x > ereal M) F" using eventually_mono by force
   432   }
   433   then show ?thesis by (simp add: tendsto_PInfty)
   434 qed
   435 
   436 text\<open>One would like to deduce the next lemma from the previous one, but the fact
   437 that \<open>- (x + y)\<close> is in general different from \<open>(- x) + (- y)\<close> in ereal creates difficulties,
   438 so it is more efficient to copy the previous proof.\<close>
   439 
   440 lemma tendsto_add_ereal_MInf:
   441   fixes y :: ereal
   442   assumes y: "y \<noteq> \<infinity>"
   443   assumes f: "(f \<longlongrightarrow> -\<infinity>) F" and g: "(g \<longlongrightarrow> y) F"
   444   shows "((\<lambda>x. f x + g x) \<longlongrightarrow> -\<infinity>) F"
   445 proof -
   446   have "\<exists>C. eventually (\<lambda>x. g x < ereal C) F"
   447   proof (cases y)
   448     case (real r)
   449     have "y < y+1" using y real by (simp add: ereal_between(1))
   450     then have "eventually (\<lambda>x. g x < y + 1) F" using g y order_tendsto_iff by force
   451     moreover have "y+1 = ereal(real_of_ereal (y+1))" by (simp add: real)
   452     ultimately have "eventually (\<lambda>x. g x < ereal(real_of_ereal(y + 1))) F" by simp
   453     then show ?thesis by auto
   454   next
   455     case (MInf)
   456     have "eventually (\<lambda>x. g x < ereal 0) F" using g MInf by (simp add: tendsto_MInfty)
   457     then show ?thesis by auto
   458   qed (simp add: y)
   459   then obtain C::real where ge: "eventually (\<lambda>x. g x < ereal C) F" by auto
   460 
   461   {
   462     fix M::real
   463     have "eventually (\<lambda>x. f x < ereal(M - C)) F" using f by (simp add: tendsto_MInfty)
   464     then have "eventually (\<lambda>x. (f x < ereal (M- C)) \<and> (g x < ereal C)) F"
   465       by (auto simp add: ge eventually_conj_iff)
   466     moreover have "\<And>x. ((f x < ereal (M-C)) \<and> (g x < ereal C)) \<Longrightarrow> (f x + g x < ereal M)"
   467       using ereal_add_strict_mono2 by fastforce
   468     ultimately have "eventually (\<lambda>x. f x + g x < ereal M) F" using eventually_mono by force
   469   }
   470   then show ?thesis by (simp add: tendsto_MInfty)
   471 qed
   472 
   473 lemma tendsto_add_ereal_general1:
   474   fixes x y :: ereal
   475   assumes y: "\<bar>y\<bar> \<noteq> \<infinity>"
   476   assumes f: "(f \<longlongrightarrow> x) F" and g: "(g \<longlongrightarrow> y) F"
   477   shows "((\<lambda>x. f x + g x) \<longlongrightarrow> x + y) F"
   478 proof (cases x)
   479   case (real r)
   480   have a: "\<bar>x\<bar> \<noteq> \<infinity>" by (simp add: real)
   481   show ?thesis by (rule tendsto_add_ereal[OF a, OF y, OF f, OF g])
   482 next
   483   case PInf
   484   then show ?thesis using tendsto_add_ereal_PInf assms by force
   485 next
   486   case MInf
   487   then show ?thesis using tendsto_add_ereal_MInf assms
   488     by (metis abs_ereal.simps(3) ereal_MInfty_eq_plus)
   489 qed
   490 
   491 lemma tendsto_add_ereal_general2:
   492   fixes x y :: ereal
   493   assumes x: "\<bar>x\<bar> \<noteq> \<infinity>"
   494       and f: "(f \<longlongrightarrow> x) F" and g: "(g \<longlongrightarrow> y) F"
   495   shows "((\<lambda>x. f x + g x) \<longlongrightarrow> x + y) F"
   496 proof -
   497   have "((\<lambda>x. g x + f x) \<longlongrightarrow> x + y) F"
   498     using tendsto_add_ereal_general1[OF x, OF g, OF f] add.commute[of "y", of "x"] by simp
   499   moreover have "\<And>x. g x + f x = f x + g x" using add.commute by auto
   500   ultimately show ?thesis by simp
   501 qed
   502 
   503 text \<open>The next lemma says that the addition is continuous on \<open>ereal\<close>, except at
   504 the pairs \<open>(-\<infinity>, \<infinity>)\<close> and \<open>(\<infinity>, -\<infinity>)\<close>.\<close>
   505 
   506 lemma tendsto_add_ereal_general [tendsto_intros]:
   507   fixes x y :: ereal
   508   assumes "\<not>((x=\<infinity> \<and> y=-\<infinity>) \<or> (x=-\<infinity> \<and> y=\<infinity>))"
   509       and f: "(f \<longlongrightarrow> x) F" and g: "(g \<longlongrightarrow> y) F"
   510   shows "((\<lambda>x. f x + g x) \<longlongrightarrow> x + y) F"
   511 proof (cases x)
   512   case (real r)
   513   show ?thesis
   514     apply (rule tendsto_add_ereal_general2) using real assms by auto
   515 next
   516   case (PInf)
   517   then have "y \<noteq> -\<infinity>" using assms by simp
   518   then show ?thesis using tendsto_add_ereal_PInf PInf assms by auto
   519 next
   520   case (MInf)
   521   then have "y \<noteq> \<infinity>" using assms by simp
   522   then show ?thesis using tendsto_add_ereal_MInf MInf f g by (metis ereal_MInfty_eq_plus)
   523 qed
   524 
   525 subsubsection%important \<open>Continuity of multiplication\<close>
   526 
   527 text \<open>In the same way as for addition, we prove that the multiplication is continuous on
   528 ereal times ereal, except at \<open>(\<infinity>, 0)\<close> and \<open>(-\<infinity>, 0)\<close> and \<open>(0, \<infinity>)\<close> and \<open>(0, -\<infinity>)\<close>,
   529 starting with specific situations.\<close>
   530 
   531 lemma tendsto_mult_real_ereal:
   532   assumes "(u \<longlongrightarrow> ereal l) F" "(v \<longlongrightarrow> ereal m) F"
   533   shows "((\<lambda>n. u n * v n) \<longlongrightarrow> ereal l * ereal m) F"
   534 proof -
   535   have ureal: "eventually (\<lambda>n. u n = ereal(real_of_ereal(u n))) F" by (rule real_lim_then_eventually_real[OF assms(1)])
   536   then have "((\<lambda>n. ereal(real_of_ereal(u n))) \<longlongrightarrow> ereal l) F" using assms by auto
   537   then have limu: "((\<lambda>n. real_of_ereal(u n)) \<longlongrightarrow> l) F" by auto
   538   have vreal: "eventually (\<lambda>n. v n = ereal(real_of_ereal(v n))) F" by (rule real_lim_then_eventually_real[OF assms(2)])
   539   then have "((\<lambda>n. ereal(real_of_ereal(v n))) \<longlongrightarrow> ereal m) F" using assms by auto
   540   then have limv: "((\<lambda>n. real_of_ereal(v n)) \<longlongrightarrow> m) F" by auto
   541 
   542   {
   543     fix n assume "u n = ereal(real_of_ereal(u n))" "v n = ereal(real_of_ereal(v n))"
   544     then have "ereal(real_of_ereal(u n) * real_of_ereal(v n)) = u n * v n" by (metis times_ereal.simps(1))
   545   }
   546   then have *: "eventually (\<lambda>n. ereal(real_of_ereal(u n) * real_of_ereal(v n)) = u n * v n) F"
   547     using eventually_elim2[OF ureal vreal] by auto
   548 
   549   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
   550   then have "((\<lambda>n. ereal(real_of_ereal(u n)) * real_of_ereal(v n)) \<longlongrightarrow> ereal(l * m)) F" by auto
   551   then show ?thesis using * filterlim_cong by fastforce
   552 qed
   553 
   554 lemma tendsto_mult_ereal_PInf:
   555   fixes f g::"_ \<Rightarrow> ereal"
   556   assumes "(f \<longlongrightarrow> l) F" "l>0" "(g \<longlongrightarrow> \<infinity>) F"
   557   shows "((\<lambda>x. f x * g x) \<longlongrightarrow> \<infinity>) F"
   558 proof -
   559   obtain a::real where "0 < ereal a" "a < l" using assms(2) using ereal_dense2 by blast
   560   have *: "eventually (\<lambda>x. f x > a) F" using \<open>a < l\<close> assms(1) by (simp add: order_tendsto_iff)
   561   {
   562     fix K::real
   563     define M where "M = max K 1"
   564     then have "M > 0" by simp
   565     then have "ereal(M/a) > 0" using \<open>ereal a > 0\<close> by simp
   566     then have "\<And>x. ((f x > a) \<and> (g x > M/a)) \<Longrightarrow> (f x * g x > ereal a * ereal(M/a))"
   567       using ereal_mult_mono_strict'[where ?c = "M/a", OF \<open>0 < ereal a\<close>] by auto
   568     moreover have "ereal a * ereal(M/a) = M" using \<open>ereal a > 0\<close> by simp
   569     ultimately have "\<And>x. ((f x > a) \<and> (g x > M/a)) \<Longrightarrow> (f x * g x > M)" by simp
   570     moreover have "M \<ge> K" unfolding M_def by simp
   571     ultimately have Imp: "\<And>x. ((f x > a) \<and> (g x > M/a)) \<Longrightarrow> (f x * g x > K)"
   572       using ereal_less_eq(3) le_less_trans by blast
   573 
   574     have "eventually (\<lambda>x. g x > M/a) F" using assms(3) by (simp add: tendsto_PInfty)
   575     then have "eventually (\<lambda>x. (f x > a) \<and> (g x > M/a)) F"
   576       using * by (auto simp add: eventually_conj_iff)
   577     then have "eventually (\<lambda>x. f x * g x > K) F" using eventually_mono Imp by force
   578   }
   579   then show ?thesis by (auto simp add: tendsto_PInfty)
   580 qed
   581 
   582 lemma tendsto_mult_ereal_pos:
   583   fixes f g::"_ \<Rightarrow> ereal"
   584   assumes "(f \<longlongrightarrow> l) F" "(g \<longlongrightarrow> m) F" "l>0" "m>0"
   585   shows "((\<lambda>x. f x * g x) \<longlongrightarrow> l * m) F"
   586 proof (cases)
   587   assume *: "l = \<infinity> \<or> m = \<infinity>"
   588   then show ?thesis
   589   proof (cases)
   590     assume "m = \<infinity>"
   591     then show ?thesis using tendsto_mult_ereal_PInf assms by auto
   592   next
   593     assume "\<not>(m = \<infinity>)"
   594     then have "l = \<infinity>" using * by simp
   595     then have "((\<lambda>x. g x * f x) \<longlongrightarrow> l * m) F" using tendsto_mult_ereal_PInf assms by auto
   596     moreover have "\<And>x. g x * f x = f x * g x" using mult.commute by auto
   597     ultimately show ?thesis by simp
   598   qed
   599 next
   600   assume "\<not>(l = \<infinity> \<or> m = \<infinity>)"
   601   then have "l < \<infinity>" "m < \<infinity>" by auto
   602   then obtain lr mr where "l = ereal lr" "m = ereal mr"
   603     using \<open>l>0\<close> \<open>m>0\<close> by (metis ereal_cases ereal_less(6) not_less_iff_gr_or_eq)
   604   then show ?thesis using tendsto_mult_real_ereal assms by auto
   605 qed
   606 
   607 text \<open>We reduce the general situation to the positive case by multiplying by suitable signs.
   608 Unfortunately, as ereal is not a ring, all the neat sign lemmas are not available there. We
   609 give the bare minimum we need.\<close>
   610 
   611 lemma ereal_sgn_abs:
   612   fixes l::ereal
   613   shows "sgn(l) * l = abs(l)"
   614 apply (cases l) by (auto simp add: sgn_if ereal_less_uminus_reorder)
   615 
   616 lemma sgn_squared_ereal:
   617   assumes "l \<noteq> (0::ereal)"
   618   shows "sgn(l) * sgn(l) = 1"
   619 apply (cases l) using assms by (auto simp add: one_ereal_def sgn_if)
   620 
   621 lemma tendsto_mult_ereal [tendsto_intros]:
   622   fixes f g::"_ \<Rightarrow> ereal"
   623   assumes "(f \<longlongrightarrow> l) F" "(g \<longlongrightarrow> m) F" "\<not>((l=0 \<and> abs(m) = \<infinity>) \<or> (m=0 \<and> abs(l) = \<infinity>))"
   624   shows "((\<lambda>x. f x * g x) \<longlongrightarrow> l * m) F"
   625 proof (cases)
   626   assume "l=0 \<or> m=0"
   627   then have "abs(l) \<noteq> \<infinity>" "abs(m) \<noteq> \<infinity>" using assms(3) by auto
   628   then obtain lr mr where "l = ereal lr" "m = ereal mr" by auto
   629   then show ?thesis using tendsto_mult_real_ereal assms by auto
   630 next
   631   have sgn_finite: "\<And>a::ereal. abs(sgn a) \<noteq> \<infinity>"
   632     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)
   633   then have sgn_finite2: "\<And>a b::ereal. abs(sgn a * sgn b) \<noteq> \<infinity>"
   634     by (metis abs_eq_infinity_cases abs_ereal.simps(2) abs_ereal.simps(3) ereal_mult_eq_MInfty ereal_mult_eq_PInfty)
   635   assume "\<not>(l=0 \<or> m=0)"
   636   then have "l \<noteq> 0" "m \<noteq> 0" by auto
   637   then have "abs(l) > 0" "abs(m) > 0"
   638     by (metis abs_ereal_ge0 abs_ereal_less0 abs_ereal_pos ereal_uminus_uminus ereal_uminus_zero less_le not_less)+
   639   then have "sgn(l) * l > 0" "sgn(m) * m > 0" using ereal_sgn_abs by auto
   640   moreover have "((\<lambda>x. sgn(l) * f x) \<longlongrightarrow> (sgn(l) * l)) F"
   641     by (rule tendsto_cmult_ereal, auto simp add: sgn_finite assms(1))
   642   moreover have "((\<lambda>x. sgn(m) * g x) \<longlongrightarrow> (sgn(m) * m)) F"
   643     by (rule tendsto_cmult_ereal, auto simp add: sgn_finite assms(2))
   644   ultimately have *: "((\<lambda>x. (sgn(l) * f x) * (sgn(m) * g x)) \<longlongrightarrow> (sgn(l) * l) * (sgn(m) * m)) F"
   645     using tendsto_mult_ereal_pos by force
   646   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"
   647     by (rule tendsto_cmult_ereal, auto simp add: sgn_finite2 *)
   648   moreover have "\<And>x. (sgn(l) * sgn(m)) * ((sgn(l) * f x) * (sgn(m) * g x)) = f x * g x"
   649     by (metis mult.left_neutral sgn_squared_ereal[OF \<open>l \<noteq> 0\<close>] sgn_squared_ereal[OF \<open>m \<noteq> 0\<close>] mult.assoc mult.commute)
   650   moreover have "(sgn(l) * sgn(m)) * ((sgn(l) * l) * (sgn(m) * m)) = l * m"
   651     by (metis mult.left_neutral sgn_squared_ereal[OF \<open>l \<noteq> 0\<close>] sgn_squared_ereal[OF \<open>m \<noteq> 0\<close>] mult.assoc mult.commute)
   652   ultimately show ?thesis by auto
   653 qed
   654 
   655 lemma tendsto_cmult_ereal_general [tendsto_intros]:
   656   fixes f::"_ \<Rightarrow> ereal" and c::ereal
   657   assumes "(f \<longlongrightarrow> l) F" "\<not> (l=0 \<and> abs(c) = \<infinity>)"
   658   shows "((\<lambda>x. c * f x) \<longlongrightarrow> c * l) F"
   659 by (cases "c = 0", auto simp add: assms tendsto_mult_ereal)
   660 
   661 
   662 subsubsection%important \<open>Continuity of division\<close>
   663 
   664 lemma tendsto_inverse_ereal_PInf:
   665   fixes u::"_ \<Rightarrow> ereal"
   666   assumes "(u \<longlongrightarrow> \<infinity>) F"
   667   shows "((\<lambda>x. 1/ u x) \<longlongrightarrow> 0) F"
   668 proof -
   669   {
   670     fix e::real assume "e>0"
   671     have "1/e < \<infinity>" by auto
   672     then have "eventually (\<lambda>n. u n > 1/e) F" using assms(1) by (simp add: tendsto_PInfty)
   673     moreover
   674     {
   675       fix z::ereal assume "z>1/e"
   676       then have "z>0" using \<open>e>0\<close> using less_le_trans not_le by fastforce
   677       then have "1/z \<ge> 0" by auto
   678       moreover have "1/z < e" using \<open>e>0\<close> \<open>z>1/e\<close>
   679         apply (cases z) apply auto
   680         by (metis (mono_tags, hide_lams) less_ereal.simps(2) less_ereal.simps(4) divide_less_eq ereal_divide_less_pos ereal_less(4)
   681             ereal_less_eq(4) less_le_trans mult_eq_0_iff not_le not_one_less_zero times_ereal.simps(1))
   682       ultimately have "1/z \<ge> 0" "1/z < e" by auto
   683     }
   684     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)
   685   } note * = this
   686   show ?thesis
   687   proof (subst order_tendsto_iff, auto)
   688     fix a::ereal assume "a<0"
   689     then show "eventually (\<lambda>n. 1/u n > a) F" using *(2) eventually_mono less_le_trans linordered_field_no_ub by fastforce
   690   next
   691     fix a::ereal assume "a>0"
   692     then obtain e::real where "e>0" "a>e" using ereal_dense2 ereal_less(2) by blast
   693     then have "eventually (\<lambda>n. 1/u n < e) F" using *(1) by auto
   694     then show "eventually (\<lambda>n. 1/u n < a) F" using \<open>a>e\<close> by (metis (mono_tags, lifting) eventually_mono less_trans)
   695   qed
   696 qed
   697 
   698 text \<open>The next lemma deserves to exist by itself, as it is so common and useful.\<close>
   699 
   700 lemma tendsto_inverse_real [tendsto_intros]:
   701   fixes u::"_ \<Rightarrow> real"
   702   shows "(u \<longlongrightarrow> l) F \<Longrightarrow> l \<noteq> 0 \<Longrightarrow> ((\<lambda>x. 1/ u x) \<longlongrightarrow> 1/l) F"
   703   using tendsto_inverse unfolding inverse_eq_divide .
   704 
   705 lemma tendsto_inverse_ereal [tendsto_intros]:
   706   fixes u::"_ \<Rightarrow> ereal"
   707   assumes "(u \<longlongrightarrow> l) F" "l \<noteq> 0"
   708   shows "((\<lambda>x. 1/ u x) \<longlongrightarrow> 1/l) F"
   709 proof (cases l)
   710   case (real r)
   711   then have "r \<noteq> 0" using assms(2) by auto
   712   then have "1/l = ereal(1/r)" using real by (simp add: one_ereal_def)
   713   define v where "v = (\<lambda>n. real_of_ereal(u n))"
   714   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
   715   then have "((\<lambda>n. ereal(v n)) \<longlongrightarrow> ereal r) F" using assms real v_def by auto
   716   then have *: "((\<lambda>n. v n) \<longlongrightarrow> r) F" by auto
   717   then have "((\<lambda>n. 1/v n) \<longlongrightarrow> 1/r) F" using \<open>r \<noteq> 0\<close> tendsto_inverse_real by auto
   718   then have lim: "((\<lambda>n. ereal(1/v n)) \<longlongrightarrow> 1/l) F" using \<open>1/l = ereal(1/r)\<close> by auto
   719 
   720   have "r \<in> -{0}" "open (-{(0::real)})" using \<open>r \<noteq> 0\<close> by auto
   721   then have "eventually (\<lambda>n. v n \<in> -{0}) F" using * using topological_tendstoD by blast
   722   then have "eventually (\<lambda>n. v n \<noteq> 0) F" by auto
   723   moreover
   724   {
   725     fix n assume H: "v n \<noteq> 0" "u n = ereal(v n)"
   726     then have "ereal(1/v n) = 1/ereal(v n)" by (simp add: one_ereal_def)
   727     then have "ereal(1/v n) = 1/u n" using H(2) by simp
   728   }
   729   ultimately have "eventually (\<lambda>n. ereal(1/v n) = 1/u n) F" using ureal eventually_elim2 by force
   730   with Lim_transform_eventually[OF this lim] show ?thesis by simp
   731 next
   732   case (PInf)
   733   then have "1/l = 0" by auto
   734   then show ?thesis using tendsto_inverse_ereal_PInf assms PInf by auto
   735 next
   736   case (MInf)
   737   then have "1/l = 0" by auto
   738   have "1/z = -1/ -z" if "z < 0" for z::ereal
   739     apply (cases z) using divide_ereal_def \<open> z < 0 \<close> by auto
   740   moreover have "eventually (\<lambda>n. u n < 0) F" by (metis (no_types) MInf assms(1) tendsto_MInfty zero_ereal_def)
   741   ultimately have *: "eventually (\<lambda>n. -1/-u n = 1/u n) F" by (simp add: eventually_mono)
   742 
   743   define v where "v = (\<lambda>n. - u n)"
   744   have "(v \<longlongrightarrow> \<infinity>) F" unfolding v_def using MInf assms(1) tendsto_uminus_ereal by fastforce
   745   then have "((\<lambda>n. 1/v n) \<longlongrightarrow> 0) F" using tendsto_inverse_ereal_PInf by auto
   746   then have "((\<lambda>n. -1/v n) \<longlongrightarrow> 0) F" using tendsto_uminus_ereal by fastforce
   747   then show ?thesis unfolding v_def using Lim_transform_eventually[OF *] \<open> 1/l = 0 \<close> by auto
   748 qed
   749 
   750 lemma tendsto_divide_ereal [tendsto_intros]:
   751   fixes f g::"_ \<Rightarrow> ereal"
   752   assumes "(f \<longlongrightarrow> l) F" "(g \<longlongrightarrow> m) F" "m \<noteq> 0" "\<not>(abs(l) = \<infinity> \<and> abs(m) = \<infinity>)"
   753   shows "((\<lambda>x. f x / g x) \<longlongrightarrow> l / m) F"
   754 proof -
   755   define h where "h = (\<lambda>x. 1/ g x)"
   756   have *: "(h \<longlongrightarrow> 1/m) F" unfolding h_def using assms(2) assms(3) tendsto_inverse_ereal by auto
   757   have "((\<lambda>x. f x * h x) \<longlongrightarrow> l * (1/m)) F"
   758     apply (rule tendsto_mult_ereal[OF assms(1) *]) using assms(3) assms(4) by (auto simp add: divide_ereal_def)
   759   moreover have "f x * h x = f x / g x" for x unfolding h_def by (simp add: divide_ereal_def)
   760   moreover have "l * (1/m) = l/m" by (simp add: divide_ereal_def)
   761   ultimately show ?thesis unfolding h_def using Lim_transform_eventually by auto
   762 qed
   763 
   764 
   765 subsubsection \<open>Further limits\<close>
   766 
   767 text \<open>The assumptions of @{thm tendsto_diff_ereal} are too strong, we weaken them here.\<close>
   768 
   769 lemma tendsto_diff_ereal_general [tendsto_intros]:
   770   fixes u v::"'a \<Rightarrow> ereal"
   771   assumes "(u \<longlongrightarrow> l) F" "(v \<longlongrightarrow> m) F" "\<not>((l = \<infinity> \<and> m = \<infinity>) \<or> (l = -\<infinity> \<and> m = -\<infinity>))"
   772   shows "((\<lambda>n. u n - v n) \<longlongrightarrow> l - m) F"
   773 proof -
   774   have "((\<lambda>n. u n + (-v n)) \<longlongrightarrow> l + (-m)) F"
   775     apply (intro tendsto_intros assms) using assms by (auto simp add: ereal_uminus_eq_reorder)
   776   then show ?thesis by (simp add: minus_ereal_def)
   777 qed
   778 
   779 lemma id_nat_ereal_tendsto_PInf [tendsto_intros]:
   780   "(\<lambda> n::nat. real n) \<longlonglongrightarrow> \<infinity>"
   781 by (simp add: filterlim_real_sequentially tendsto_PInfty_eq_at_top)
   782 
   783 lemma tendsto_at_top_pseudo_inverse [tendsto_intros]:
   784   fixes u::"nat \<Rightarrow> nat"
   785   assumes "LIM n sequentially. u n :> at_top"
   786   shows "LIM n sequentially. Inf {N. u N \<ge> n} :> at_top"
   787 proof -
   788   {
   789     fix C::nat
   790     define M where "M = Max {u n| n. n \<le> C}+1"
   791     {
   792       fix n assume "n \<ge> M"
   793       have "eventually (\<lambda>N. u N \<ge> n) sequentially" using assms
   794         by (simp add: filterlim_at_top)
   795       then have *: "{N. u N \<ge> n} \<noteq> {}" by force
   796 
   797       have "N > C" if "u N \<ge> n" for N
   798       proof (rule ccontr)
   799         assume "\<not>(N > C)"
   800         have "u N \<le> Max {u n| n. n \<le> C}"
   801           apply (rule Max_ge) using \<open>\<not>(N > C)\<close> by auto
   802         then show False using \<open>u N \<ge> n\<close> \<open>n \<ge> M\<close> unfolding M_def by auto
   803       qed
   804       then have **: "{N. u N \<ge> n} \<subseteq> {C..}" by fastforce
   805       have "Inf {N. u N \<ge> n} \<ge> C"
   806         by (metis "*" "**" Inf_nat_def1 atLeast_iff subset_eq)
   807     }
   808     then have "eventually (\<lambda>n. Inf {N. u N \<ge> n} \<ge> C) sequentially"
   809       using eventually_sequentially by auto
   810   }
   811   then show ?thesis using filterlim_at_top by auto
   812 qed
   813 
   814 lemma pseudo_inverse_finite_set:
   815   fixes u::"nat \<Rightarrow> nat"
   816   assumes "LIM n sequentially. u n :> at_top"
   817   shows "finite {N. u N \<le> n}"
   818 proof -
   819   fix n
   820   have "eventually (\<lambda>N. u N \<ge> n+1) sequentially" using assms
   821     by (simp add: filterlim_at_top)
   822   then obtain N1 where N1: "\<And>N. N \<ge> N1 \<Longrightarrow> u N \<ge> n + 1"
   823     using eventually_sequentially by auto
   824   have "{N. u N \<le> n} \<subseteq> {..<N1}"
   825     apply auto using N1 by (metis Suc_eq_plus1 not_less not_less_eq_eq)
   826   then show "finite {N. u N \<le> n}" by (simp add: finite_subset)
   827 qed
   828 
   829 lemma tendsto_at_top_pseudo_inverse2 [tendsto_intros]:
   830   fixes u::"nat \<Rightarrow> nat"
   831   assumes "LIM n sequentially. u n :> at_top"
   832   shows "LIM n sequentially. Max {N. u N \<le> n} :> at_top"
   833 proof -
   834   {
   835     fix N0::nat
   836     have "N0 \<le> Max {N. u N \<le> n}" if "n \<ge> u N0" for n
   837       apply (rule Max.coboundedI) using pseudo_inverse_finite_set[OF assms] that by auto
   838     then have "eventually (\<lambda>n. N0 \<le> Max {N. u N \<le> n}) sequentially"
   839       using eventually_sequentially by blast
   840   }
   841   then show ?thesis using filterlim_at_top by auto
   842 qed
   843 
   844 lemma ereal_truncation_top [tendsto_intros]:
   845   fixes x::ereal
   846   shows "(\<lambda>n::nat. min x n) \<longlonglongrightarrow> x"
   847 proof (cases x)
   848   case (real r)
   849   then obtain K::nat where "K>0" "K > abs(r)" using reals_Archimedean2 gr0I by auto
   850   then have "min x n = x" if "n \<ge> K" for n apply (subst real, subst real, auto) using that eq_iff by fastforce
   851   then have "eventually (\<lambda>n. min x n = x) sequentially" using eventually_at_top_linorder by blast
   852   then show ?thesis by (simp add: Lim_eventually)
   853 next
   854   case (PInf)
   855   then have "min x n = n" for n::nat by (auto simp add: min_def)
   856   then show ?thesis using id_nat_ereal_tendsto_PInf PInf by auto
   857 next
   858   case (MInf)
   859   then have "min x n = x" for n::nat by (auto simp add: min_def)
   860   then show ?thesis by auto
   861 qed
   862 
   863 lemma ereal_truncation_real_top [tendsto_intros]:
   864   fixes x::ereal
   865   assumes "x \<noteq> - \<infinity>"
   866   shows "(\<lambda>n::nat. real_of_ereal(min x n)) \<longlonglongrightarrow> x"
   867 proof (cases x)
   868   case (real r)
   869   then obtain K::nat where "K>0" "K > abs(r)" using reals_Archimedean2 gr0I by auto
   870   then have "min x n = x" if "n \<ge> K" for n apply (subst real, subst real, auto) using that eq_iff by fastforce
   871   then have "real_of_ereal(min x n) = r" if "n \<ge> K" for n using real that by auto
   872   then have "eventually (\<lambda>n. real_of_ereal(min x n) = r) sequentially" using eventually_at_top_linorder by blast
   873   then have "(\<lambda>n. real_of_ereal(min x n)) \<longlonglongrightarrow> r" by (simp add: Lim_eventually)
   874   then show ?thesis using real by auto
   875 next
   876   case (PInf)
   877   then have "real_of_ereal(min x n) = n" for n::nat by (auto simp add: min_def)
   878   then show ?thesis using id_nat_ereal_tendsto_PInf PInf by auto
   879 qed (simp add: assms)
   880 
   881 lemma ereal_truncation_bottom [tendsto_intros]:
   882   fixes x::ereal
   883   shows "(\<lambda>n::nat. max x (- real n)) \<longlonglongrightarrow> x"
   884 proof (cases x)
   885   case (real r)
   886   then obtain K::nat where "K>0" "K > abs(r)" using reals_Archimedean2 gr0I by auto
   887   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
   888   then have "eventually (\<lambda>n. max x (-real n) = x) sequentially" using eventually_at_top_linorder by blast
   889   then show ?thesis by (simp add: Lim_eventually)
   890 next
   891   case (MInf)
   892   then have "max x (-real n) = (-1)* ereal(real n)" for n::nat by (auto simp add: max_def)
   893   moreover have "(\<lambda>n. (-1)* ereal(real n)) \<longlonglongrightarrow> -\<infinity>"
   894     using tendsto_cmult_ereal[of "-1", OF _ id_nat_ereal_tendsto_PInf] by (simp add: one_ereal_def)
   895   ultimately show ?thesis using MInf by auto
   896 next
   897   case (PInf)
   898   then have "max x (-real n) = x" for n::nat by (auto simp add: max_def)
   899   then show ?thesis by auto
   900 qed
   901 
   902 lemma ereal_truncation_real_bottom [tendsto_intros]:
   903   fixes x::ereal
   904   assumes "x \<noteq> \<infinity>"
   905   shows "(\<lambda>n::nat. real_of_ereal(max x (- real n))) \<longlonglongrightarrow> x"
   906 proof (cases x)
   907   case (real r)
   908   then obtain K::nat where "K>0" "K > abs(r)" using reals_Archimedean2 gr0I by auto
   909   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
   910   then have "real_of_ereal(max x (-real n)) = r" if "n \<ge> K" for n using real that by auto
   911   then have "eventually (\<lambda>n. real_of_ereal(max x (-real n)) = r) sequentially" using eventually_at_top_linorder by blast
   912   then have "(\<lambda>n. real_of_ereal(max x (-real n))) \<longlonglongrightarrow> r" by (simp add: Lim_eventually)
   913   then show ?thesis using real by auto
   914 next
   915   case (MInf)
   916   then have "real_of_ereal(max x (-real n)) = (-1)* ereal(real n)" for n::nat by (auto simp add: max_def)
   917   moreover have "(\<lambda>n. (-1)* ereal(real n)) \<longlonglongrightarrow> -\<infinity>"
   918     using tendsto_cmult_ereal[of "-1", OF _ id_nat_ereal_tendsto_PInf] by (simp add: one_ereal_def)
   919   ultimately show ?thesis using MInf by auto
   920 qed (simp add: assms)
   921 
   922 text \<open>the next one is copied from \<open>tendsto_sum\<close>.\<close>
   923 lemma tendsto_sum_ereal [tendsto_intros]:
   924   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> ereal"
   925   assumes "\<And>i. i \<in> S \<Longrightarrow> (f i \<longlongrightarrow> a i) F"
   926           "\<And>i. abs(a i) \<noteq> \<infinity>"
   927   shows "((\<lambda>x. \<Sum>i\<in>S. f i x) \<longlongrightarrow> (\<Sum>i\<in>S. a i)) F"
   928 proof (cases "finite S")
   929   assume "finite S" then show ?thesis using assms
   930     by (induct, simp, simp add: tendsto_add_ereal_general2 assms)
   931 qed(simp)
   932 
   933 
   934 lemma continuous_ereal_abs:
   935   "continuous_on (UNIV::ereal set) abs"
   936 proof -
   937   have "continuous_on ({..0} \<union> {(0::ereal)..}) abs"
   938     apply (rule continuous_on_closed_Un, auto)
   939     apply (rule iffD1[OF continuous_on_cong, of "{..0}" _ "\<lambda>x. -x"])
   940     using less_eq_ereal_def apply (auto simp add: continuous_uminus_ereal)
   941     apply (rule iffD1[OF continuous_on_cong, of "{0..}" _ "\<lambda>x. x"])
   942       apply (auto simp add: continuous_on_id)
   943     done
   944   moreover have "(UNIV::ereal set) = {..0} \<union> {(0::ereal)..}" by auto
   945   ultimately show ?thesis by auto
   946 qed
   947 
   948 lemmas continuous_on_compose_ereal_abs[continuous_intros] =
   949   continuous_on_compose2[OF continuous_ereal_abs _ subset_UNIV]
   950 
   951 lemma tendsto_abs_ereal [tendsto_intros]:
   952   assumes "(u \<longlongrightarrow> (l::ereal)) F"
   953   shows "((\<lambda>n. abs(u n)) \<longlongrightarrow> abs l) F"
   954 using continuous_ereal_abs assms by (metis UNIV_I continuous_on tendsto_compose)
   955 
   956 lemma ereal_minus_real_tendsto_MInf [tendsto_intros]:
   957   "(\<lambda>x. ereal (- real x)) \<longlonglongrightarrow> - \<infinity>"
   958 by (subst uminus_ereal.simps(1)[symmetric], intro tendsto_intros)
   959 
   960 
   961 subsection \<open>Extended-Nonnegative-Real.thy\<close> (*FIX title *)
   962 
   963 lemma tendsto_diff_ennreal_general [tendsto_intros]:
   964   fixes u v::"'a \<Rightarrow> ennreal"
   965   assumes "(u \<longlongrightarrow> l) F" "(v \<longlongrightarrow> m) F" "\<not>(l = \<infinity> \<and> m = \<infinity>)"
   966   shows "((\<lambda>n. u n - v n) \<longlongrightarrow> l - m) F"
   967 proof -
   968   have "((\<lambda>n. e2ennreal(enn2ereal(u n) - enn2ereal(v n))) \<longlongrightarrow> e2ennreal(enn2ereal l - enn2ereal m)) F"
   969     apply (intro tendsto_intros) using assms by  auto
   970   then show ?thesis by auto
   971 qed
   972 
   973 lemma tendsto_mult_ennreal [tendsto_intros]:
   974   fixes l m::ennreal
   975   assumes "(u \<longlongrightarrow> l) F" "(v \<longlongrightarrow> m) F" "\<not>((l = 0 \<and> m = \<infinity>) \<or> (l = \<infinity> \<and> m = 0))"
   976   shows "((\<lambda>n. u n * v n) \<longlongrightarrow> l * m) F"
   977 proof -
   978   have "((\<lambda>n. e2ennreal(enn2ereal (u n) * enn2ereal (v n))) \<longlongrightarrow> e2ennreal(enn2ereal l * enn2ereal m)) F"
   979     apply (intro tendsto_intros) using assms apply auto
   980     using enn2ereal_inject zero_ennreal.rep_eq by fastforce+
   981   moreover have "e2ennreal(enn2ereal (u n) * enn2ereal (v n)) = u n * v n" for n
   982     by (subst times_ennreal.abs_eq[symmetric], auto simp add: eq_onp_same_args)
   983   moreover have "e2ennreal(enn2ereal l * enn2ereal m)  = l * m"
   984     by (subst times_ennreal.abs_eq[symmetric], auto simp add: eq_onp_same_args)
   985   ultimately show ?thesis
   986     by auto
   987 qed
   988 
   989 
   990 subsection \<open>monoset\<close> (*FIX ME title *)
   991 
   992 definition (in order) mono_set:
   993   "mono_set S \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> x \<in> S \<longrightarrow> y \<in> S)"
   994 
   995 lemma (in order) mono_greaterThan [intro, simp]: "mono_set {B<..}" unfolding mono_set by auto
   996 lemma (in order) mono_atLeast [intro, simp]: "mono_set {B..}" unfolding mono_set by auto
   997 lemma (in order) mono_UNIV [intro, simp]: "mono_set UNIV" unfolding mono_set by auto
   998 lemma (in order) mono_empty [intro, simp]: "mono_set {}" unfolding mono_set by auto
   999 
  1000 lemma (in complete_linorder) mono_set_iff:
  1001   fixes S :: "'a set"
  1002   defines "a \<equiv> Inf S"
  1003   shows "mono_set S \<longleftrightarrow> S = {a <..} \<or> S = {a..}" (is "_ = ?c")
  1004 proof
  1005   assume "mono_set S"
  1006   then have mono: "\<And>x y. x \<le> y \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S"
  1007     by (auto simp: mono_set)
  1008   show ?c
  1009   proof cases
  1010     assume "a \<in> S"
  1011     show ?c
  1012       using mono[OF _ \<open>a \<in> S\<close>]
  1013       by (auto intro: Inf_lower simp: a_def)
  1014   next
  1015     assume "a \<notin> S"
  1016     have "S = {a <..}"
  1017     proof safe
  1018       fix x assume "x \<in> S"
  1019       then have "a \<le> x"
  1020         unfolding a_def by (rule Inf_lower)
  1021       then show "a < x"
  1022         using \<open>x \<in> S\<close> \<open>a \<notin> S\<close> by (cases "a = x") auto
  1023     next
  1024       fix x assume "a < x"
  1025       then obtain y where "y < x" "y \<in> S"
  1026         unfolding a_def Inf_less_iff ..
  1027       with mono[of y x] show "x \<in> S"
  1028         by auto
  1029     qed
  1030     then show ?c ..
  1031   qed
  1032 qed auto
  1033 
  1034 lemma ereal_open_mono_set:
  1035   fixes S :: "ereal set"
  1036   shows "open S \<and> mono_set S \<longleftrightarrow> S = UNIV \<or> S = {Inf S <..}"
  1037   by (metis Inf_UNIV atLeast_eq_UNIV_iff ereal_open_atLeast
  1038     ereal_open_closed mono_set_iff open_ereal_greaterThan)
  1039 
  1040 lemma ereal_closed_mono_set:
  1041   fixes S :: "ereal set"
  1042   shows "closed S \<and> mono_set S \<longleftrightarrow> S = {} \<or> S = {Inf S ..}"
  1043   by (metis Inf_UNIV atLeast_eq_UNIV_iff closed_ereal_atLeast
  1044     ereal_open_closed mono_empty mono_set_iff open_ereal_greaterThan)
  1045 
  1046 lemma ereal_Liminf_Sup_monoset:
  1047   fixes f :: "'a \<Rightarrow> ereal"
  1048   shows "Liminf net f =
  1049     Sup {l. \<forall>S. open S \<longrightarrow> mono_set S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) net}"
  1050     (is "_ = Sup ?A")
  1051 proof (safe intro!: Liminf_eqI complete_lattice_class.Sup_upper complete_lattice_class.Sup_least)
  1052   fix P
  1053   assume P: "eventually P net"
  1054   fix S
  1055   assume S: "mono_set S" "Inf (f ` (Collect P)) \<in> S"
  1056   {
  1057     fix x
  1058     assume "P x"
  1059     then have "Inf (f ` (Collect P)) \<le> f x"
  1060       by (intro complete_lattice_class.INF_lower) simp
  1061     with S have "f x \<in> S"
  1062       by (simp add: mono_set)
  1063   }
  1064   with P show "eventually (\<lambda>x. f x \<in> S) net"
  1065     by (auto elim: eventually_mono)
  1066 next
  1067   fix y l
  1068   assume S: "\<forall>S. open S \<longrightarrow> mono_set S \<longrightarrow> l \<in> S \<longrightarrow> eventually  (\<lambda>x. f x \<in> S) net"
  1069   assume P: "\<forall>P. eventually P net \<longrightarrow> Inf (f ` (Collect P)) \<le> y"
  1070   show "l \<le> y"
  1071   proof (rule dense_le)
  1072     fix B
  1073     assume "B < l"
  1074     then have "eventually (\<lambda>x. f x \<in> {B <..}) net"
  1075       by (intro S[rule_format]) auto
  1076     then have "Inf (f ` {x. B < f x}) \<le> y"
  1077       using P by auto
  1078     moreover have "B \<le> Inf (f ` {x. B < f x})"
  1079       by (intro INF_greatest) auto
  1080     ultimately show "B \<le> y"
  1081       by simp
  1082   qed
  1083 qed
  1084 
  1085 lemma ereal_Limsup_Inf_monoset:
  1086   fixes f :: "'a \<Rightarrow> ereal"
  1087   shows "Limsup net f =
  1088     Inf {l. \<forall>S. open S \<longrightarrow> mono_set (uminus ` S) \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) net}"
  1089     (is "_ = Inf ?A")
  1090 proof (safe intro!: Limsup_eqI complete_lattice_class.Inf_lower complete_lattice_class.Inf_greatest)
  1091   fix P
  1092   assume P: "eventually P net"
  1093   fix S
  1094   assume S: "mono_set (uminus`S)" "Sup (f ` (Collect P)) \<in> S"
  1095   {
  1096     fix x
  1097     assume "P x"
  1098     then have "f x \<le> Sup (f ` (Collect P))"
  1099       by (intro complete_lattice_class.SUP_upper) simp
  1100     with S(1)[unfolded mono_set, rule_format, of "- Sup (f ` (Collect P))" "- f x"] S(2)
  1101     have "f x \<in> S"
  1102       by (simp add: inj_image_mem_iff) }
  1103   with P show "eventually (\<lambda>x. f x \<in> S) net"
  1104     by (auto elim: eventually_mono)
  1105 next
  1106   fix y l
  1107   assume S: "\<forall>S. open S \<longrightarrow> mono_set (uminus ` S) \<longrightarrow> l \<in> S \<longrightarrow> eventually  (\<lambda>x. f x \<in> S) net"
  1108   assume P: "\<forall>P. eventually P net \<longrightarrow> y \<le> Sup (f ` (Collect P))"
  1109   show "y \<le> l"
  1110   proof (rule dense_ge)
  1111     fix B
  1112     assume "l < B"
  1113     then have "eventually (\<lambda>x. f x \<in> {..< B}) net"
  1114       by (intro S[rule_format]) auto
  1115     then have "y \<le> Sup (f ` {x. f x < B})"
  1116       using P by auto
  1117     moreover have "Sup (f ` {x. f x < B}) \<le> B"
  1118       by (intro SUP_least) auto
  1119     ultimately show "y \<le> B"
  1120       by simp
  1121   qed
  1122 qed
  1123 
  1124 lemma liminf_bounded_open:
  1125   fixes x :: "nat \<Rightarrow> ereal"
  1126   shows "x0 \<le> liminf x \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> mono_set S \<longrightarrow> x0 \<in> S \<longrightarrow> (\<exists>N. \<forall>n\<ge>N. x n \<in> S))"
  1127   (is "_ \<longleftrightarrow> ?P x0")
  1128 proof
  1129   assume "?P x0"
  1130   then show "x0 \<le> liminf x"
  1131     unfolding ereal_Liminf_Sup_monoset eventually_sequentially
  1132     by (intro complete_lattice_class.Sup_upper) auto
  1133 next
  1134   assume "x0 \<le> liminf x"
  1135   {
  1136     fix S :: "ereal set"
  1137     assume om: "open S" "mono_set S" "x0 \<in> S"
  1138     {
  1139       assume "S = UNIV"
  1140       then have "\<exists>N. \<forall>n\<ge>N. x n \<in> S"
  1141         by auto
  1142     }
  1143     moreover
  1144     {
  1145       assume "S \<noteq> UNIV"
  1146       then obtain B where B: "S = {B<..}"
  1147         using om ereal_open_mono_set by auto
  1148       then have "B < x0"
  1149         using om by auto
  1150       then have "\<exists>N. \<forall>n\<ge>N. x n \<in> S"
  1151         unfolding B
  1152         using \<open>x0 \<le> liminf x\<close> liminf_bounded_iff
  1153         by auto
  1154     }
  1155     ultimately have "\<exists>N. \<forall>n\<ge>N. x n \<in> S"
  1156       by auto
  1157   }
  1158   then show "?P x0"
  1159     by auto
  1160 qed
  1161 
  1162 lemma limsup_finite_then_bounded:
  1163   fixes u::"nat \<Rightarrow> real"
  1164   assumes "limsup u < \<infinity>"
  1165   shows "\<exists>C. \<forall>n. u n \<le> C"
  1166 proof -
  1167   obtain C where C: "limsup u < C" "C < \<infinity>" using assms ereal_dense2 by blast
  1168   then have "C = ereal(real_of_ereal C)" using ereal_real by force
  1169   have "eventually (\<lambda>n. u n < C) sequentially" using C(1) unfolding Limsup_def
  1170     apply (auto simp add: INF_less_iff)
  1171     using SUP_lessD eventually_mono by fastforce
  1172   then obtain N where N: "\<And>n. n \<ge> N \<Longrightarrow> u n < C" using eventually_sequentially by auto
  1173   define D where "D = max (real_of_ereal C) (Max {u n |n. n \<le> N})"
  1174   have "\<And>n. u n \<le> D"
  1175   proof -
  1176     fix n show "u n \<le> D"
  1177     proof (cases)
  1178       assume *: "n \<le> N"
  1179       have "u n \<le> Max {u n |n. n \<le> N}" by (rule Max_ge, auto simp add: *)
  1180       then show "u n \<le> D" unfolding D_def by linarith
  1181     next
  1182       assume "\<not>(n \<le> N)"
  1183       then have "n \<ge> N" by simp
  1184       then have "u n < C" using N by auto
  1185       then have "u n < real_of_ereal C" using \<open>C = ereal(real_of_ereal C)\<close> less_ereal.simps(1) by fastforce
  1186       then show "u n \<le> D" unfolding D_def by linarith
  1187     qed
  1188   qed
  1189   then show ?thesis by blast
  1190 qed
  1191 
  1192 lemma liminf_finite_then_bounded_below:
  1193   fixes u::"nat \<Rightarrow> real"
  1194   assumes "liminf u > -\<infinity>"
  1195   shows "\<exists>C. \<forall>n. u n \<ge> C"
  1196 proof -
  1197   obtain C where C: "liminf u > C" "C > -\<infinity>" using assms using ereal_dense2 by blast
  1198   then have "C = ereal(real_of_ereal C)" using ereal_real by force
  1199   have "eventually (\<lambda>n. u n > C) sequentially" using C(1) unfolding Liminf_def
  1200     apply (auto simp add: less_SUP_iff)
  1201     using eventually_elim2 less_INF_D by fastforce
  1202   then obtain N where N: "\<And>n. n \<ge> N \<Longrightarrow> u n > C" using eventually_sequentially by auto
  1203   define D where "D = min (real_of_ereal C) (Min {u n |n. n \<le> N})"
  1204   have "\<And>n. u n \<ge> D"
  1205   proof -
  1206     fix n show "u n \<ge> D"
  1207     proof (cases)
  1208       assume *: "n \<le> N"
  1209       have "u n \<ge> Min {u n |n. n \<le> N}" by (rule Min_le, auto simp add: *)
  1210       then show "u n \<ge> D" unfolding D_def by linarith
  1211     next
  1212       assume "\<not>(n \<le> N)"
  1213       then have "n \<ge> N" by simp
  1214       then have "u n > C" using N by auto
  1215       then have "u n > real_of_ereal C" using \<open>C = ereal(real_of_ereal C)\<close> less_ereal.simps(1) by fastforce
  1216       then show "u n \<ge> D" unfolding D_def by linarith
  1217     qed
  1218   qed
  1219   then show ?thesis by blast
  1220 qed
  1221 
  1222 lemma liminf_upper_bound:
  1223   fixes u:: "nat \<Rightarrow> ereal"
  1224   assumes "liminf u < l"
  1225   shows "\<exists>N>k. u N < l"
  1226 by (metis assms gt_ex less_le_trans liminf_bounded_iff not_less)
  1227 
  1228 lemma limsup_shift:
  1229   "limsup (\<lambda>n. u (n+1)) = limsup u"
  1230 proof -
  1231   have "(SUP m\<in>{n+1..}. u m) = (SUP m\<in>{n..}. u (m + 1))" for n
  1232     apply (rule SUP_eq) using Suc_le_D by auto
  1233   then have a: "(INF n. SUP m\<in>{n..}. u (m + 1)) = (INF n. (SUP m\<in>{n+1..}. u m))" by auto
  1234   have b: "(INF n. (SUP m\<in>{n+1..}. u m)) = (INF n\<in>{1..}. (SUP m\<in>{n..}. u m))"
  1235     apply (rule INF_eq) using Suc_le_D by auto
  1236   have "(INF n\<in>{1..}. v n) = (INF n. v n)" if "decseq v" for v::"nat \<Rightarrow> 'a"
  1237     apply (rule INF_eq) using \<open>decseq v\<close> decseq_Suc_iff by auto
  1238   moreover have "decseq (\<lambda>n. (SUP m\<in>{n..}. u m))" by (simp add: SUP_subset_mono decseq_def)
  1239   ultimately have c: "(INF n\<in>{1..}. (SUP m\<in>{n..}. u m)) = (INF n. (SUP m\<in>{n..}. u m))" by simp
  1240   have "(INF n. Sup (u ` {n..})) = (INF n. SUP m\<in>{n..}. u (m + 1))" using a b c by simp
  1241   then show ?thesis by (auto cong: limsup_INF_SUP)
  1242 qed
  1243 
  1244 lemma limsup_shift_k:
  1245   "limsup (\<lambda>n. u (n+k)) = limsup u"
  1246 proof (induction k)
  1247   case (Suc k)
  1248   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
  1249   then show ?case using Suc.IH by simp
  1250 qed (auto)
  1251 
  1252 lemma liminf_shift:
  1253   "liminf (\<lambda>n. u (n+1)) = liminf u"
  1254 proof -
  1255   have "(INF m\<in>{n+1..}. u m) = (INF m\<in>{n..}. u (m + 1))" for n
  1256     apply (rule INF_eq) using Suc_le_D by (auto)
  1257   then have a: "(SUP n. INF m\<in>{n..}. u (m + 1)) = (SUP n. (INF m\<in>{n+1..}. u m))" by auto
  1258   have b: "(SUP n. (INF m\<in>{n+1..}. u m)) = (SUP n\<in>{1..}. (INF m\<in>{n..}. u m))"
  1259     apply (rule SUP_eq) using Suc_le_D by (auto)
  1260   have "(SUP n\<in>{1..}. v n) = (SUP n. v n)" if "incseq v" for v::"nat \<Rightarrow> 'a"
  1261     apply (rule SUP_eq) using \<open>incseq v\<close> incseq_Suc_iff by auto
  1262   moreover have "incseq (\<lambda>n. (INF m\<in>{n..}. u m))" by (simp add: INF_superset_mono mono_def)
  1263   ultimately have c: "(SUP n\<in>{1..}. (INF m\<in>{n..}. u m)) = (SUP n. (INF m\<in>{n..}. u m))" by simp
  1264   have "(SUP n. Inf (u ` {n..})) = (SUP n. INF m\<in>{n..}. u (m + 1))" using a b c by simp
  1265   then show ?thesis by (auto cong: liminf_SUP_INF)
  1266 qed
  1267 
  1268 lemma liminf_shift_k:
  1269   "liminf (\<lambda>n. u (n+k)) = liminf u"
  1270 proof (induction k)
  1271   case (Suc k)
  1272   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
  1273   then show ?case using Suc.IH by simp
  1274 qed (auto)
  1275 
  1276 lemma Limsup_obtain:
  1277   fixes u::"_ \<Rightarrow> 'a :: complete_linorder"
  1278   assumes "Limsup F u > c"
  1279   shows "\<exists>i. u i > c"
  1280 proof -
  1281   have "(INF P\<in>{P. eventually P F}. SUP x\<in>{x. P x}. u x) > c" using assms by (simp add: Limsup_def)
  1282   then show ?thesis by (metis eventually_True mem_Collect_eq less_INF_D less_SUP_iff)
  1283 qed
  1284 
  1285 text \<open>The next lemma is extremely useful, as it often makes it possible to reduce statements
  1286 about limsups to statements about limits.\<close>
  1287 
  1288 lemma limsup_subseq_lim:
  1289   fixes u::"nat \<Rightarrow> 'a :: {complete_linorder, linorder_topology}"
  1290   shows "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (u o r) \<longlonglongrightarrow> limsup u"
  1291 proof (cases)
  1292   assume "\<forall>n. \<exists>p>n. \<forall>m\<ge>p. u m \<le> u p"
  1293   then have "\<exists>r. \<forall>n. (\<forall>m\<ge>r n. u m \<le> u (r n)) \<and> r n < r (Suc n)"
  1294     by (intro dependent_nat_choice) (auto simp: conj_commute)
  1295   then obtain r :: "nat \<Rightarrow> nat" where "strict_mono r" and mono: "\<And>n m. r n \<le> m \<Longrightarrow> u m \<le> u (r n)"
  1296     by (auto simp: strict_mono_Suc_iff)
  1297   define umax where "umax = (\<lambda>n. (SUP m\<in>{n..}. u m))"
  1298   have "decseq umax" unfolding umax_def by (simp add: SUP_subset_mono antimono_def)
  1299   then have "umax \<longlonglongrightarrow> limsup u" unfolding umax_def by (metis LIMSEQ_INF limsup_INF_SUP)
  1300   then have *: "(umax o r) \<longlonglongrightarrow> limsup u" by (simp add: LIMSEQ_subseq_LIMSEQ \<open>strict_mono r\<close>)
  1301   have "\<And>n. umax(r n) = u(r n)" unfolding umax_def using mono
  1302     by (metis SUP_le_iff antisym atLeast_def mem_Collect_eq order_refl)
  1303   then have "umax o r = u o r" unfolding o_def by simp
  1304   then have "(u o r) \<longlonglongrightarrow> limsup u" using * by simp
  1305   then show ?thesis using \<open>strict_mono r\<close> by blast
  1306 next
  1307   assume "\<not> (\<forall>n. \<exists>p>n. (\<forall>m\<ge>p. u m \<le> u p))"
  1308   then obtain N where N: "\<And>p. p > N \<Longrightarrow> \<exists>m>p. u p < u m" by (force simp: not_le le_less)
  1309   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)))"
  1310   proof (rule dependent_nat_choice)
  1311     fix x assume "N < x"
  1312     then have a: "finite {N<..x}" "{N<..x} \<noteq> {}" by simp_all
  1313     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)
  1314     then obtain p where "p \<in> {N<..x}" and upmax: "u p = Max{u i |i. i \<in> {N<..x}}" by auto
  1315     define U where "U = {m. m > p \<and> u p < u m}"
  1316     have "U \<noteq> {}" unfolding U_def using N[of p] \<open>p \<in> {N<..x}\<close> by auto
  1317     define y where "y = Inf U"
  1318     then have "y \<in> U" using \<open>U \<noteq> {}\<close> by (simp add: Inf_nat_def1)
  1319     have a: "\<And>i. i \<in> {N<..x} \<Longrightarrow> u i \<le> u p"
  1320     proof -
  1321       fix i assume "i \<in> {N<..x}"
  1322       then have "u i \<in> {u i |i. i \<in> {N<..x}}" by blast
  1323       then show "u i \<le> u p" using upmax by simp
  1324     qed
  1325     moreover have "u p < u y" using \<open>y \<in> U\<close> U_def by auto
  1326     ultimately have "y \<notin> {N<..x}" using not_le by blast
  1327     moreover have "y > N" using \<open>y \<in> U\<close> U_def \<open>p \<in> {N<..x}\<close> by auto
  1328     ultimately have "y > x" by auto
  1329 
  1330     have "\<And>i. i \<in> {N<..y} \<Longrightarrow> u i \<le> u y"
  1331     proof -
  1332       fix i assume "i \<in> {N<..y}" show "u i \<le> u y"
  1333       proof (cases)
  1334         assume "i = y"
  1335         then show ?thesis by simp
  1336       next
  1337         assume "\<not>(i=y)"
  1338         then have i:"i \<in> {N<..<y}" using \<open>i \<in> {N<..y}\<close> by simp
  1339         have "u i \<le> u p"
  1340         proof (cases)
  1341           assume "i \<le> x"
  1342           then have "i \<in> {N<..x}" using i by simp
  1343           then show ?thesis using a by simp
  1344         next
  1345           assume "\<not>(i \<le> x)"
  1346           then have "i > x" by simp
  1347           then have *: "i > p" using \<open>p \<in> {N<..x}\<close> by simp
  1348           have "i < Inf U" using i y_def by simp
  1349           then have "i \<notin> U" using Inf_nat_def not_less_Least by auto
  1350           then show ?thesis using U_def * by auto
  1351         qed
  1352         then show "u i \<le> u y" using \<open>u p < u y\<close> by auto
  1353       qed
  1354     qed
  1355     then have "N < y \<and> x < y \<and> (\<forall>i\<in>{N<..y}. u i \<le> u y)" using \<open>y > x\<close> \<open>y > N\<close> by auto
  1356     then show "\<exists>y>N. x < y \<and> (\<forall>i\<in>{N<..y}. u i \<le> u y)" by auto
  1357   qed (auto)
  1358   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
  1359   have "strict_mono r" using r by (auto simp: strict_mono_Suc_iff)
  1360   have "incseq (u o r)" unfolding o_def using r by (simp add: incseq_SucI order.strict_implies_order)
  1361   then have "(u o r) \<longlonglongrightarrow> (SUP n. (u o r) n)" using LIMSEQ_SUP by blast
  1362   then have "limsup (u o r) = (SUP n. (u o r) n)" by (simp add: lim_imp_Limsup)
  1363   moreover have "limsup (u o r) \<le> limsup u" using \<open>strict_mono r\<close> by (simp add: limsup_subseq_mono)
  1364   ultimately have "(SUP n. (u o r) n) \<le> limsup u" by simp
  1365 
  1366   {
  1367     fix i assume i: "i \<in> {N<..}"
  1368     obtain n where "i < r (Suc n)" using \<open>strict_mono r\<close> using Suc_le_eq seq_suble by blast
  1369     then have "i \<in> {N<..r(Suc n)}" using i by simp
  1370     then have "u i \<le> u (r(Suc n))" using r by simp
  1371     then have "u i \<le> (SUP n. (u o r) n)" unfolding o_def by (meson SUP_upper2 UNIV_I)
  1372   }
  1373   then have "(SUP i\<in>{N<..}. u i) \<le> (SUP n. (u o r) n)" using SUP_least by blast
  1374   then have "limsup u \<le> (SUP n. (u o r) n)" unfolding Limsup_def
  1375     by (metis (mono_tags, lifting) INF_lower2 atLeast_Suc_greaterThan atLeast_def eventually_ge_at_top mem_Collect_eq)
  1376   then have "limsup u = (SUP n. (u o r) n)" using \<open>(SUP n. (u o r) n) \<le> limsup u\<close> by simp
  1377   then have "(u o r) \<longlonglongrightarrow> limsup u" using \<open>(u o r) \<longlonglongrightarrow> (SUP n. (u o r) n)\<close> by simp
  1378   then show ?thesis using \<open>strict_mono r\<close> by auto
  1379 qed
  1380 
  1381 lemma liminf_subseq_lim:
  1382   fixes u::"nat \<Rightarrow> 'a :: {complete_linorder, linorder_topology}"
  1383   shows "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (u o r) \<longlonglongrightarrow> liminf u"
  1384 proof (cases)
  1385   assume "\<forall>n. \<exists>p>n. \<forall>m\<ge>p. u m \<ge> u p"
  1386   then have "\<exists>r. \<forall>n. (\<forall>m\<ge>r n. u m \<ge> u (r n)) \<and> r n < r (Suc n)"
  1387     by (intro dependent_nat_choice) (auto simp: conj_commute)
  1388   then obtain r :: "nat \<Rightarrow> nat" where "strict_mono r" and mono: "\<And>n m. r n \<le> m \<Longrightarrow> u m \<ge> u (r n)"
  1389     by (auto simp: strict_mono_Suc_iff)
  1390   define umin where "umin = (\<lambda>n. (INF m\<in>{n..}. u m))"
  1391   have "incseq umin" unfolding umin_def by (simp add: INF_superset_mono incseq_def)
  1392   then have "umin \<longlonglongrightarrow> liminf u" unfolding umin_def by (metis LIMSEQ_SUP liminf_SUP_INF)
  1393   then have *: "(umin o r) \<longlonglongrightarrow> liminf u" by (simp add: LIMSEQ_subseq_LIMSEQ \<open>strict_mono r\<close>)
  1394   have "\<And>n. umin(r n) = u(r n)" unfolding umin_def using mono
  1395     by (metis le_INF_iff antisym atLeast_def mem_Collect_eq order_refl)
  1396   then have "umin o r = u o r" unfolding o_def by simp
  1397   then have "(u o r) \<longlonglongrightarrow> liminf u" using * by simp
  1398   then show ?thesis using \<open>strict_mono r\<close> by blast
  1399 next
  1400   assume "\<not> (\<forall>n. \<exists>p>n. (\<forall>m\<ge>p. u m \<ge> u p))"
  1401   then obtain N where N: "\<And>p. p > N \<Longrightarrow> \<exists>m>p. u p > u m" by (force simp: not_le le_less)
  1402   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)))"
  1403   proof (rule dependent_nat_choice)
  1404     fix x assume "N < x"
  1405     then have a: "finite {N<..x}" "{N<..x} \<noteq> {}" by simp_all
  1406     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)
  1407     then obtain p where "p \<in> {N<..x}" and upmin: "u p = Min{u i |i. i \<in> {N<..x}}" by auto
  1408     define U where "U = {m. m > p \<and> u p > u m}"
  1409     have "U \<noteq> {}" unfolding U_def using N[of p] \<open>p \<in> {N<..x}\<close> by auto
  1410     define y where "y = Inf U"
  1411     then have "y \<in> U" using \<open>U \<noteq> {}\<close> by (simp add: Inf_nat_def1)
  1412     have a: "\<And>i. i \<in> {N<..x} \<Longrightarrow> u i \<ge> u p"
  1413     proof -
  1414       fix i assume "i \<in> {N<..x}"
  1415       then have "u i \<in> {u i |i. i \<in> {N<..x}}" by blast
  1416       then show "u i \<ge> u p" using upmin by simp
  1417     qed
  1418     moreover have "u p > u y" using \<open>y \<in> U\<close> U_def by auto
  1419     ultimately have "y \<notin> {N<..x}" using not_le by blast
  1420     moreover have "y > N" using \<open>y \<in> U\<close> U_def \<open>p \<in> {N<..x}\<close> by auto
  1421     ultimately have "y > x" by auto
  1422 
  1423     have "\<And>i. i \<in> {N<..y} \<Longrightarrow> u i \<ge> u y"
  1424     proof -
  1425       fix i assume "i \<in> {N<..y}" show "u i \<ge> u y"
  1426       proof (cases)
  1427         assume "i = y"
  1428         then show ?thesis by simp
  1429       next
  1430         assume "\<not>(i=y)"
  1431         then have i:"i \<in> {N<..<y}" using \<open>i \<in> {N<..y}\<close> by simp
  1432         have "u i \<ge> u p"
  1433         proof (cases)
  1434           assume "i \<le> x"
  1435           then have "i \<in> {N<..x}" using i by simp
  1436           then show ?thesis using a by simp
  1437         next
  1438           assume "\<not>(i \<le> x)"
  1439           then have "i > x" by simp
  1440           then have *: "i > p" using \<open>p \<in> {N<..x}\<close> by simp
  1441           have "i < Inf U" using i y_def by simp
  1442           then have "i \<notin> U" using Inf_nat_def not_less_Least by auto
  1443           then show ?thesis using U_def * by auto
  1444         qed
  1445         then show "u i \<ge> u y" using \<open>u p > u y\<close> by auto
  1446       qed
  1447     qed
  1448     then have "N < y \<and> x < y \<and> (\<forall>i\<in>{N<..y}. u i \<ge> u y)" using \<open>y > x\<close> \<open>y > N\<close> by auto
  1449     then show "\<exists>y>N. x < y \<and> (\<forall>i\<in>{N<..y}. u i \<ge> u y)" by auto
  1450   qed (auto)
  1451   then obtain r :: "nat \<Rightarrow> nat" 
  1452     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
  1453   have "strict_mono r" using r by (auto simp: strict_mono_Suc_iff)
  1454   have "decseq (u o r)" unfolding o_def using r by (simp add: decseq_SucI order.strict_implies_order)
  1455   then have "(u o r) \<longlonglongrightarrow> (INF n. (u o r) n)" using LIMSEQ_INF by blast
  1456   then have "liminf (u o r) = (INF n. (u o r) n)" by (simp add: lim_imp_Liminf)
  1457   moreover have "liminf (u o r) \<ge> liminf u" using \<open>strict_mono r\<close> by (simp add: liminf_subseq_mono)
  1458   ultimately have "(INF n. (u o r) n) \<ge> liminf u" by simp
  1459 
  1460   {
  1461     fix i assume i: "i \<in> {N<..}"
  1462     obtain n where "i < r (Suc n)" using \<open>strict_mono r\<close> using Suc_le_eq seq_suble by blast
  1463     then have "i \<in> {N<..r(Suc n)}" using i by simp
  1464     then have "u i \<ge> u (r(Suc n))" using r by simp
  1465     then have "u i \<ge> (INF n. (u o r) n)" unfolding o_def by (meson INF_lower2 UNIV_I)
  1466   }
  1467   then have "(INF i\<in>{N<..}. u i) \<ge> (INF n. (u o r) n)" using INF_greatest by blast
  1468   then have "liminf u \<ge> (INF n. (u o r) n)" unfolding Liminf_def
  1469     by (metis (mono_tags, lifting) SUP_upper2 atLeast_Suc_greaterThan atLeast_def eventually_ge_at_top mem_Collect_eq)
  1470   then have "liminf u = (INF n. (u o r) n)" using \<open>(INF n. (u o r) n) \<ge> liminf u\<close> by simp
  1471   then have "(u o r) \<longlonglongrightarrow> liminf u" using \<open>(u o r) \<longlonglongrightarrow> (INF n. (u o r) n)\<close> by simp
  1472   then show ?thesis using \<open>strict_mono r\<close> by auto
  1473 qed
  1474 
  1475 text \<open>The following statement about limsups is reduced to a statement about limits using
  1476 subsequences thanks to \<open>limsup_subseq_lim\<close>. The statement for limits follows for instance from
  1477 \<open>tendsto_add_ereal_general\<close>.\<close>
  1478 
  1479 lemma ereal_limsup_add_mono:
  1480   fixes u v::"nat \<Rightarrow> ereal"
  1481   shows "limsup (\<lambda>n. u n + v n) \<le> limsup u + limsup v"
  1482 proof (cases)
  1483   assume "(limsup u = \<infinity>) \<or> (limsup v = \<infinity>)"
  1484   then have "limsup u + limsup v = \<infinity>" by simp
  1485   then show ?thesis by auto
  1486 next
  1487   assume "\<not>((limsup u = \<infinity>) \<or> (limsup v = \<infinity>))"
  1488   then have "limsup u < \<infinity>" "limsup v < \<infinity>" by auto
  1489 
  1490   define w where "w = (\<lambda>n. u n + v n)"
  1491   obtain r where r: "strict_mono r" "(w o r) \<longlonglongrightarrow> limsup w" using limsup_subseq_lim by auto
  1492   obtain s where s: "strict_mono s" "(u o r o s) \<longlonglongrightarrow> limsup (u o r)" using limsup_subseq_lim by auto
  1493   obtain t where t: "strict_mono t" "(v o r o s o t) \<longlonglongrightarrow> limsup (v o r o s)" using limsup_subseq_lim by auto
  1494 
  1495   define a where "a = r o s o t"
  1496   have "strict_mono a" using r s t by (simp add: a_def strict_mono_o)
  1497   have l:"(w o a) \<longlonglongrightarrow> limsup w"
  1498          "(u o a) \<longlonglongrightarrow> limsup (u o r)"
  1499          "(v o a) \<longlonglongrightarrow> limsup (v o r o s)"
  1500   apply (metis (no_types, lifting) r(2) s(1) t(1) LIMSEQ_subseq_LIMSEQ a_def comp_assoc)
  1501   apply (metis (no_types, lifting) s(2) t(1) LIMSEQ_subseq_LIMSEQ a_def comp_assoc)
  1502   apply (metis (no_types, lifting) t(2) a_def comp_assoc)
  1503   done
  1504 
  1505   have "limsup (u o r) \<le> limsup u" by (simp add: limsup_subseq_mono r(1))
  1506   then have a: "limsup (u o r) \<noteq> \<infinity>" using \<open>limsup u < \<infinity>\<close> by auto
  1507   have "limsup (v o r o s) \<le> limsup v" 
  1508     by (simp add: comp_assoc limsup_subseq_mono r(1) s(1) strict_mono_o)
  1509   then have b: "limsup (v o r o s) \<noteq> \<infinity>" using \<open>limsup v < \<infinity>\<close> by auto
  1510 
  1511   have "(\<lambda>n. (u o a) n + (v o a) n) \<longlonglongrightarrow> limsup (u o r) + limsup (v o r o s)"
  1512     using l tendsto_add_ereal_general a b by fastforce
  1513   moreover have "(\<lambda>n. (u o a) n + (v o a) n) = (w o a)" unfolding w_def by auto
  1514   ultimately have "(w o a) \<longlonglongrightarrow> limsup (u o r) + limsup (v o r o s)" by simp
  1515   then have "limsup w = limsup (u o r) + limsup (v o r o s)" using l(1) LIMSEQ_unique by blast
  1516   then have "limsup w \<le> limsup u + limsup v"
  1517     using \<open>limsup (u o r) \<le> limsup u\<close> \<open>limsup (v o r o s) \<le> limsup v\<close> add_mono by simp
  1518   then show ?thesis unfolding w_def by simp
  1519 qed
  1520 
  1521 text \<open>There is an asymmetry between liminfs and limsups in \<open>ereal\<close>, as \<open>\<infinity> + (-\<infinity>) = \<infinity>\<close>.
  1522 This explains why there are more assumptions in the next lemma dealing with liminfs that in the
  1523 previous one about limsups.\<close>
  1524 
  1525 lemma ereal_liminf_add_mono:
  1526   fixes u v::"nat \<Rightarrow> ereal"
  1527   assumes "\<not>((liminf u = \<infinity> \<and> liminf v = -\<infinity>) \<or> (liminf u = -\<infinity> \<and> liminf v = \<infinity>))"
  1528   shows "liminf (\<lambda>n. u n + v n) \<ge> liminf u + liminf v"
  1529 proof (cases)
  1530   assume "(liminf u = -\<infinity>) \<or> (liminf v = -\<infinity>)"
  1531   then have *: "liminf u + liminf v = -\<infinity>" using assms by auto
  1532   show ?thesis by (simp add: *)
  1533 next
  1534   assume "\<not>((liminf u = -\<infinity>) \<or> (liminf v = -\<infinity>))"
  1535   then have "liminf u > -\<infinity>" "liminf v > -\<infinity>" by auto
  1536 
  1537   define w where "w = (\<lambda>n. u n + v n)"
  1538   obtain r where r: "strict_mono r" "(w o r) \<longlonglongrightarrow> liminf w" using liminf_subseq_lim by auto
  1539   obtain s where s: "strict_mono s" "(u o r o s) \<longlonglongrightarrow> liminf (u o r)" using liminf_subseq_lim by auto
  1540   obtain t where t: "strict_mono t" "(v o r o s o t) \<longlonglongrightarrow> liminf (v o r o s)" using liminf_subseq_lim by auto
  1541 
  1542   define a where "a = r o s o t"
  1543   have "strict_mono a" using r s t by (simp add: a_def strict_mono_o)
  1544   have l:"(w o a) \<longlonglongrightarrow> liminf w"
  1545          "(u o a) \<longlonglongrightarrow> liminf (u o r)"
  1546          "(v o a) \<longlonglongrightarrow> liminf (v o r o s)"
  1547   apply (metis (no_types, lifting) r(2) s(1) t(1) LIMSEQ_subseq_LIMSEQ a_def comp_assoc)
  1548   apply (metis (no_types, lifting) s(2) t(1) LIMSEQ_subseq_LIMSEQ a_def comp_assoc)
  1549   apply (metis (no_types, lifting) t(2) a_def comp_assoc)
  1550   done
  1551 
  1552   have "liminf (u o r) \<ge> liminf u" by (simp add: liminf_subseq_mono r(1))
  1553   then have a: "liminf (u o r) \<noteq> -\<infinity>" using \<open>liminf u > -\<infinity>\<close> by auto
  1554   have "liminf (v o r o s) \<ge> liminf v" 
  1555     by (simp add: comp_assoc liminf_subseq_mono r(1) s(1) strict_mono_o)
  1556   then have b: "liminf (v o r o s) \<noteq> -\<infinity>" using \<open>liminf v > -\<infinity>\<close> by auto
  1557 
  1558   have "(\<lambda>n. (u o a) n + (v o a) n) \<longlonglongrightarrow> liminf (u o r) + liminf (v o r o s)"
  1559     using l tendsto_add_ereal_general a b by fastforce
  1560   moreover have "(\<lambda>n. (u o a) n + (v o a) n) = (w o a)" unfolding w_def by auto
  1561   ultimately have "(w o a) \<longlonglongrightarrow> liminf (u o r) + liminf (v o r o s)" by simp
  1562   then have "liminf w = liminf (u o r) + liminf (v o r o s)" using l(1) LIMSEQ_unique by blast
  1563   then have "liminf w \<ge> liminf u + liminf v"
  1564     using \<open>liminf (u o r) \<ge> liminf u\<close> \<open>liminf (v o r o s) \<ge> liminf v\<close> add_mono by simp
  1565   then show ?thesis unfolding w_def by simp
  1566 qed
  1567 
  1568 lemma ereal_limsup_lim_add:
  1569   fixes u v::"nat \<Rightarrow> ereal"
  1570   assumes "u \<longlonglongrightarrow> a" "abs(a) \<noteq> \<infinity>"
  1571   shows "limsup (\<lambda>n. u n + v n) = a + limsup v"
  1572 proof -
  1573   have "limsup u = a" using assms(1) using tendsto_iff_Liminf_eq_Limsup trivial_limit_at_top_linorder by blast
  1574   have "(\<lambda>n. -u n) \<longlonglongrightarrow> -a" using assms(1) by auto
  1575   then have "limsup (\<lambda>n. -u n) = -a" using tendsto_iff_Liminf_eq_Limsup trivial_limit_at_top_linorder by blast
  1576 
  1577   have "limsup (\<lambda>n. u n + v n) \<le> limsup u + limsup v"
  1578     by (rule ereal_limsup_add_mono)
  1579   then have up: "limsup (\<lambda>n. u n + v n) \<le> a + limsup v" using \<open>limsup u = a\<close> by simp
  1580 
  1581   have a: "limsup (\<lambda>n. (u n + v n) + (-u n)) \<le> limsup (\<lambda>n. u n + v n) + limsup (\<lambda>n. -u n)"
  1582     by (rule ereal_limsup_add_mono)
  1583   have "eventually (\<lambda>n. u n = ereal(real_of_ereal(u n))) sequentially" using assms
  1584     real_lim_then_eventually_real by auto
  1585   moreover have "\<And>x. x = ereal(real_of_ereal(x)) \<Longrightarrow> x + (-x) = 0"
  1586     by (metis plus_ereal.simps(1) right_minus uminus_ereal.simps(1) zero_ereal_def)
  1587   ultimately have "eventually (\<lambda>n. u n + (-u n) = 0) sequentially"
  1588     by (metis (mono_tags, lifting) eventually_mono)
  1589   moreover have "\<And>n. u n + (-u n) = 0 \<Longrightarrow> u n + v n + (-u n) = v n"
  1590     by (metis add.commute add.left_commute add.left_neutral)
  1591   ultimately have "eventually (\<lambda>n. u n + v n + (-u n) = v n) sequentially"
  1592     using eventually_mono by force
  1593   then have "limsup v = limsup (\<lambda>n. u n + v n + (-u n))" using Limsup_eq by force
  1594   then have "limsup v \<le> limsup (\<lambda>n. u n + v n) -a" using a \<open>limsup (\<lambda>n. -u n) = -a\<close> by (simp add: minus_ereal_def)
  1595   then have "limsup (\<lambda>n. u n + v n) \<ge> a + limsup v" using assms(2) by (metis add.commute ereal_le_minus)
  1596   then show ?thesis using up by simp
  1597 qed
  1598 
  1599 lemma ereal_limsup_lim_mult:
  1600   fixes u v::"nat \<Rightarrow> ereal"
  1601   assumes "u \<longlonglongrightarrow> a" "a>0" "a \<noteq> \<infinity>"
  1602   shows "limsup (\<lambda>n. u n * v n) = a * limsup v"
  1603 proof -
  1604   define w where "w = (\<lambda>n. u n * v n)"
  1605   obtain r where r: "strict_mono r" "(v o r) \<longlonglongrightarrow> limsup v" using limsup_subseq_lim by auto
  1606   have "(u o r) \<longlonglongrightarrow> a" using assms(1) LIMSEQ_subseq_LIMSEQ r by auto
  1607   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
  1608   moreover have "\<And>n. (w o r) n = (u o r) n * (v o r) n" unfolding w_def by auto
  1609   ultimately have "(w o r) \<longlonglongrightarrow> a * limsup v" unfolding w_def by presburger
  1610   then have "limsup (w o r) = a * limsup v" by (simp add: tendsto_iff_Liminf_eq_Limsup)
  1611   then have I: "limsup w \<ge> a * limsup v" by (metis limsup_subseq_mono r(1))
  1612 
  1613   obtain s where s: "strict_mono s" "(w o s) \<longlonglongrightarrow> limsup w" using limsup_subseq_lim by auto
  1614   have *: "(u o s) \<longlonglongrightarrow> a" using assms(1) LIMSEQ_subseq_LIMSEQ s by auto
  1615   have "eventually (\<lambda>n. (u o s) n > 0) sequentially" using assms(2) * order_tendsto_iff by blast
  1616   moreover have "eventually (\<lambda>n. (u o s) n < \<infinity>) sequentially" using assms(3) * order_tendsto_iff by blast
  1617   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
  1618     unfolding w_def using that by (auto simp add: ereal_divide_eq)
  1619   ultimately have "eventually (\<lambda>n. (w o s) n / (u o s) n = (v o s) n) sequentially" using eventually_elim2 by force
  1620   moreover have "(\<lambda>n. (w o s) n / (u o s) n) \<longlonglongrightarrow> (limsup w) / a"
  1621     apply (rule tendsto_divide_ereal[OF s(2) *]) using assms(2) assms(3) by auto
  1622   ultimately have "(v o s) \<longlonglongrightarrow> (limsup w) / a" using Lim_transform_eventually by fastforce
  1623   then have "limsup (v o s) = (limsup w) / a" by (simp add: tendsto_iff_Liminf_eq_Limsup)
  1624   then have "limsup v \<ge> (limsup w) / a" by (metis limsup_subseq_mono s(1))
  1625   then have "a * limsup v \<ge> limsup w" using assms(2) assms(3) by (simp add: ereal_divide_le_pos)
  1626   then show ?thesis using I unfolding w_def by auto
  1627 qed
  1628 
  1629 lemma ereal_liminf_lim_mult:
  1630   fixes u v::"nat \<Rightarrow> ereal"
  1631   assumes "u \<longlonglongrightarrow> a" "a>0" "a \<noteq> \<infinity>"
  1632   shows "liminf (\<lambda>n. u n * v n) = a * liminf v"
  1633 proof -
  1634   define w where "w = (\<lambda>n. u n * v n)"
  1635   obtain r where r: "strict_mono r" "(v o r) \<longlonglongrightarrow> liminf v" using liminf_subseq_lim by auto
  1636   have "(u o r) \<longlonglongrightarrow> a" using assms(1) LIMSEQ_subseq_LIMSEQ r by auto
  1637   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
  1638   moreover have "\<And>n. (w o r) n = (u o r) n * (v o r) n" unfolding w_def by auto
  1639   ultimately have "(w o r) \<longlonglongrightarrow> a * liminf v" unfolding w_def by presburger
  1640   then have "liminf (w o r) = a * liminf v" by (simp add: tendsto_iff_Liminf_eq_Limsup)
  1641   then have I: "liminf w \<le> a * liminf v" by (metis liminf_subseq_mono r(1))
  1642 
  1643   obtain s where s: "strict_mono s" "(w o s) \<longlonglongrightarrow> liminf w" using liminf_subseq_lim by auto
  1644   have *: "(u o s) \<longlonglongrightarrow> a" using assms(1) LIMSEQ_subseq_LIMSEQ s by auto
  1645   have "eventually (\<lambda>n. (u o s) n > 0) sequentially" using assms(2) * order_tendsto_iff by blast
  1646   moreover have "eventually (\<lambda>n. (u o s) n < \<infinity>) sequentially" using assms(3) * order_tendsto_iff by blast
  1647   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
  1648     unfolding w_def using that by (auto simp add: ereal_divide_eq)
  1649   ultimately have "eventually (\<lambda>n. (w o s) n / (u o s) n = (v o s) n) sequentially" using eventually_elim2 by force
  1650   moreover have "(\<lambda>n. (w o s) n / (u o s) n) \<longlonglongrightarrow> (liminf w) / a"
  1651     apply (rule tendsto_divide_ereal[OF s(2) *]) using assms(2) assms(3) by auto
  1652   ultimately have "(v o s) \<longlonglongrightarrow> (liminf w) / a" using Lim_transform_eventually by fastforce
  1653   then have "liminf (v o s) = (liminf w) / a" by (simp add: tendsto_iff_Liminf_eq_Limsup)
  1654   then have "liminf v \<le> (liminf w) / a" by (metis liminf_subseq_mono s(1))
  1655   then have "a * liminf v \<le> liminf w" using assms(2) assms(3) by (simp add: ereal_le_divide_pos)
  1656   then show ?thesis using I unfolding w_def by auto
  1657 qed
  1658 
  1659 lemma ereal_liminf_lim_add:
  1660   fixes u v::"nat \<Rightarrow> ereal"
  1661   assumes "u \<longlonglongrightarrow> a" "abs(a) \<noteq> \<infinity>"
  1662   shows "liminf (\<lambda>n. u n + v n) = a + liminf v"
  1663 proof -
  1664   have "liminf u = a" using assms(1) tendsto_iff_Liminf_eq_Limsup trivial_limit_at_top_linorder by blast
  1665   then have *: "abs(liminf u) \<noteq> \<infinity>" using assms(2) by auto
  1666   have "(\<lambda>n. -u n) \<longlonglongrightarrow> -a" using assms(1) by auto
  1667   then have "liminf (\<lambda>n. -u n) = -a" using tendsto_iff_Liminf_eq_Limsup trivial_limit_at_top_linorder by blast
  1668   then have **: "abs(liminf (\<lambda>n. -u n)) \<noteq> \<infinity>" using assms(2) by auto
  1669 
  1670   have "liminf (\<lambda>n. u n + v n) \<ge> liminf u + liminf v"
  1671     apply (rule ereal_liminf_add_mono) using * by auto
  1672   then have up: "liminf (\<lambda>n. u n + v n) \<ge> a + liminf v" using \<open>liminf u = a\<close> by simp
  1673 
  1674   have a: "liminf (\<lambda>n. (u n + v n) + (-u n)) \<ge> liminf (\<lambda>n. u n + v n) + liminf (\<lambda>n. -u n)"
  1675     apply (rule ereal_liminf_add_mono) using ** by auto
  1676   have "eventually (\<lambda>n. u n = ereal(real_of_ereal(u n))) sequentially" using assms
  1677     real_lim_then_eventually_real by auto
  1678   moreover have "\<And>x. x = ereal(real_of_ereal(x)) \<Longrightarrow> x + (-x) = 0"
  1679     by (metis plus_ereal.simps(1) right_minus uminus_ereal.simps(1) zero_ereal_def)
  1680   ultimately have "eventually (\<lambda>n. u n + (-u n) = 0) sequentially"
  1681     by (metis (mono_tags, lifting) eventually_mono)
  1682   moreover have "\<And>n. u n + (-u n) = 0 \<Longrightarrow> u n + v n + (-u n) = v n"
  1683     by (metis add.commute add.left_commute add.left_neutral)
  1684   ultimately have "eventually (\<lambda>n. u n + v n + (-u n) = v n) sequentially"
  1685     using eventually_mono by force
  1686   then have "liminf v = liminf (\<lambda>n. u n + v n + (-u n))" using Liminf_eq by force
  1687   then have "liminf v \<ge> liminf (\<lambda>n. u n + v n) -a" using a \<open>liminf (\<lambda>n. -u n) = -a\<close> by (simp add: minus_ereal_def)
  1688   then have "liminf (\<lambda>n. u n + v n) \<le> a + liminf v" using assms(2) by (metis add.commute ereal_minus_le)
  1689   then show ?thesis using up by simp
  1690 qed
  1691 
  1692 lemma ereal_liminf_limsup_add:
  1693   fixes u v::"nat \<Rightarrow> ereal"
  1694   shows "liminf (\<lambda>n. u n + v n) \<le> liminf u + limsup v"
  1695 proof (cases)
  1696   assume "limsup v = \<infinity> \<or> liminf u = \<infinity>"
  1697   then show ?thesis by auto
  1698 next
  1699   assume "\<not>(limsup v = \<infinity> \<or> liminf u = \<infinity>)"
  1700   then have "limsup v < \<infinity>" "liminf u < \<infinity>" by auto
  1701 
  1702   define w where "w = (\<lambda>n. u n + v n)"
  1703   obtain r where r: "strict_mono r" "(u o r) \<longlonglongrightarrow> liminf u" using liminf_subseq_lim by auto
  1704   obtain s where s: "strict_mono s" "(w o r o s) \<longlonglongrightarrow> liminf (w o r)" using liminf_subseq_lim by auto
  1705   obtain t where t: "strict_mono t" "(v o r o s o t) \<longlonglongrightarrow> limsup (v o r o s)" using limsup_subseq_lim by auto
  1706 
  1707   define a where "a = r o s o t"
  1708   have "strict_mono a" using r s t by (simp add: a_def strict_mono_o)
  1709   have l:"(u o a) \<longlonglongrightarrow> liminf u"
  1710          "(w o a) \<longlonglongrightarrow> liminf (w o r)"
  1711          "(v o a) \<longlonglongrightarrow> limsup (v o r o s)"
  1712   apply (metis (no_types, lifting) r(2) s(1) t(1) LIMSEQ_subseq_LIMSEQ a_def comp_assoc)
  1713   apply (metis (no_types, lifting) s(2) t(1) LIMSEQ_subseq_LIMSEQ a_def comp_assoc)
  1714   apply (metis (no_types, lifting) t(2) a_def comp_assoc)
  1715   done
  1716 
  1717   have "liminf (w o r) \<ge> liminf w" by (simp add: liminf_subseq_mono r(1))
  1718   have "limsup (v o r o s) \<le> limsup v" 
  1719     by (simp add: comp_assoc limsup_subseq_mono r(1) s(1) strict_mono_o)
  1720   then have b: "limsup (v o r o s) < \<infinity>" using \<open>limsup v < \<infinity>\<close> by auto
  1721 
  1722   have "(\<lambda>n. (u o a) n + (v o a) n) \<longlonglongrightarrow> liminf u + limsup (v o r o s)"
  1723     apply (rule tendsto_add_ereal_general) using b \<open>liminf u < \<infinity>\<close> l(1) l(3) by force+
  1724   moreover have "(\<lambda>n. (u o a) n + (v o a) n) = (w o a)" unfolding w_def by auto
  1725   ultimately have "(w o a) \<longlonglongrightarrow> liminf u + limsup (v o r o s)" by simp
  1726   then have "liminf (w o r) = liminf u + limsup (v o r o s)" using l(2) using LIMSEQ_unique by blast
  1727   then have "liminf w \<le> liminf u + limsup v"
  1728     using \<open>liminf (w o r) \<ge> liminf w\<close> \<open>limsup (v o r o s) \<le> limsup v\<close>
  1729     by (metis add_mono_thms_linordered_semiring(2) le_less_trans not_less)
  1730   then show ?thesis unfolding w_def by simp
  1731 qed
  1732 
  1733 lemma ereal_liminf_limsup_minus:
  1734   fixes u v::"nat \<Rightarrow> ereal"
  1735   shows "liminf (\<lambda>n. u n - v n) \<le> limsup u - limsup v"
  1736   unfolding minus_ereal_def
  1737   apply (subst add.commute)
  1738   apply (rule order_trans[OF ereal_liminf_limsup_add])
  1739   using ereal_Limsup_uminus[of sequentially "\<lambda>n. - v n"]
  1740   apply (simp add: add.commute)
  1741   done
  1742 
  1743 
  1744 lemma liminf_minus_ennreal:
  1745   fixes u v::"nat \<Rightarrow> ennreal"
  1746   shows "(\<And>n. v n \<le> u n) \<Longrightarrow> liminf (\<lambda>n. u n - v n) \<le> limsup u - limsup v"
  1747   unfolding liminf_SUP_INF limsup_INF_SUP
  1748   including ennreal.lifting
  1749 proof (transfer, clarsimp)
  1750   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"
  1751   moreover have "0 \<le> limsup u - limsup v"
  1752     using * by (intro ereal_diff_positive Limsup_mono always_eventually) simp
  1753   moreover have "0 \<le> Sup (u ` {x..})" for x
  1754     using * by (intro SUP_upper2[of x]) auto
  1755   moreover have "0 \<le> Sup (v ` {x..})" for x
  1756     using * by (intro SUP_upper2[of x]) auto
  1757   ultimately show "(SUP n. INF n\<in>{n..}. max 0 (u n - v n))
  1758             \<le> max 0 ((INF x. max 0 (Sup (u ` {x..}))) - (INF x. max 0 (Sup (v ` {x..}))))"
  1759     by (auto simp: * ereal_diff_positive max.absorb2 liminf_SUP_INF[symmetric] limsup_INF_SUP[symmetric] ereal_liminf_limsup_minus)
  1760 qed
  1761 
  1762 subsection "Relate extended reals and the indicator function"
  1763 
  1764 lemma ereal_indicator_le_0: "(indicator S x::ereal) \<le> 0 \<longleftrightarrow> x \<notin> S"
  1765   by (auto split: split_indicator simp: one_ereal_def)
  1766 
  1767 lemma ereal_indicator: "ereal (indicator A x) = indicator A x"
  1768   by (auto simp: indicator_def one_ereal_def)
  1769 
  1770 lemma ereal_mult_indicator: "ereal (x * indicator A y) = ereal x * indicator A y"
  1771   by (simp split: split_indicator)
  1772 
  1773 lemma ereal_indicator_mult: "ereal (indicator A y * x) = indicator A y * ereal x"
  1774   by (simp split: split_indicator)
  1775 
  1776 lemma ereal_indicator_nonneg[simp, intro]: "0 \<le> (indicator A x ::ereal)"
  1777   unfolding indicator_def by auto
  1778 
  1779 lemma indicator_inter_arith_ereal: "indicator A x * indicator B x = (indicator (A \<inter> B) x :: ereal)"
  1780   by (simp split: split_indicator)
  1781 
  1782 end