src/HOL/Library/Extended_Real.thy
author hoelzl
Tue Jul 19 14:38:48 2011 +0200 (2011-07-19)
changeset 43924 1165fe965da8
parent 43923 ab93d0190a5d
child 43933 6cc1875cf35d
permissions -rw-r--r--
rename Fin to enat
     1 (*  Title:      HOL/Library/Extended_Real.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 header {* Extended real number line *}
     9 
    10 theory Extended_Real
    11   imports Complex_Main Extended_Nat
    12 begin
    13 
    14 text {*
    15 
    16 For more lemmas about the extended real numbers go to
    17   @{text "src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy"}
    18 
    19 *}
    20 
    21 lemma (in complete_lattice) atLeast_eq_UNIV_iff: "{x..} = UNIV \<longleftrightarrow> x = bot"
    22 proof
    23   assume "{x..} = UNIV"
    24   show "x = bot"
    25   proof (rule ccontr)
    26     assume "x \<noteq> bot" then have "bot \<notin> {x..}" by (simp add: le_less)
    27     then show False using `{x..} = UNIV` by simp
    28   qed
    29 qed auto
    30 
    31 lemma SUPR_pair:
    32   "(SUP i : A. SUP j : B. f i j) = (SUP p : A \<times> B. f (fst p) (snd p))"
    33   by (rule antisym) (auto intro!: SUP_leI le_SUPI2)
    34 
    35 lemma INFI_pair:
    36   "(INF i : A. INF j : B. f i j) = (INF p : A \<times> B. f (fst p) (snd p))"
    37   by (rule antisym) (auto intro!: le_INFI INF_leI2)
    38 
    39 subsection {* Definition and basic properties *}
    40 
    41 datatype ereal = ereal real | PInfty | MInfty
    42 
    43 instantiation ereal :: uminus
    44 begin
    45   fun uminus_ereal where
    46     "- (ereal r) = ereal (- r)"
    47   | "- PInfty = MInfty"
    48   | "- MInfty = PInfty"
    49   instance ..
    50 end
    51 
    52 instantiation ereal :: infinity
    53 begin
    54   definition "(\<infinity>::ereal) = PInfty"
    55   instance ..
    56 end
    57 
    58 definition "ereal_of_enat n = (case n of enat n \<Rightarrow> ereal (real n) | \<infinity> \<Rightarrow> \<infinity>)"
    59 
    60 declare [[coercion "ereal :: real \<Rightarrow> ereal"]]
    61 declare [[coercion "ereal_of_enat :: enat \<Rightarrow> ereal"]]
    62 declare [[coercion "(\<lambda>n. ereal (of_nat n)) :: nat \<Rightarrow> ereal"]]
    63 
    64 lemma ereal_uminus_uminus[simp]:
    65   fixes a :: ereal shows "- (- a) = a"
    66   by (cases a) simp_all
    67 
    68 lemma
    69   shows PInfty_eq_infinity[simp]: "PInfty = \<infinity>"
    70     and MInfty_eq_minfinity[simp]: "MInfty = - \<infinity>"
    71     and MInfty_neq_PInfty[simp]: "\<infinity> \<noteq> - (\<infinity>::ereal)" "- \<infinity> \<noteq> (\<infinity>::ereal)"
    72     and MInfty_neq_ereal[simp]: "ereal r \<noteq> - \<infinity>" "- \<infinity> \<noteq> ereal r"
    73     and PInfty_neq_ereal[simp]: "ereal r \<noteq> \<infinity>" "\<infinity> \<noteq> ereal r"
    74     and PInfty_cases[simp]: "(case \<infinity> of ereal r \<Rightarrow> f r | PInfty \<Rightarrow> y | MInfty \<Rightarrow> z) = y"
    75     and MInfty_cases[simp]: "(case - \<infinity> of ereal r \<Rightarrow> f r | PInfty \<Rightarrow> y | MInfty \<Rightarrow> z) = z"
    76   by (simp_all add: infinity_ereal_def)
    77 
    78 lemma inj_ereal[simp]: "inj_on ereal A"
    79   unfolding inj_on_def by auto
    80 
    81 lemma ereal_cases[case_names real PInf MInf, cases type: ereal]:
    82   assumes "\<And>r. x = ereal r \<Longrightarrow> P"
    83   assumes "x = \<infinity> \<Longrightarrow> P"
    84   assumes "x = -\<infinity> \<Longrightarrow> P"
    85   shows P
    86   using assms by (cases x) auto
    87 
    88 lemmas ereal2_cases = ereal_cases[case_product ereal_cases]
    89 lemmas ereal3_cases = ereal2_cases[case_product ereal_cases]
    90 
    91 lemma ereal_uminus_eq_iff[simp]:
    92   fixes a b :: ereal shows "-a = -b \<longleftrightarrow> a = b"
    93   by (cases rule: ereal2_cases[of a b]) simp_all
    94 
    95 function of_ereal :: "ereal \<Rightarrow> real" where
    96 "of_ereal (ereal r) = r" |
    97 "of_ereal \<infinity> = 0" |
    98 "of_ereal (-\<infinity>) = 0"
    99   by (auto intro: ereal_cases)
   100 termination proof qed (rule wf_empty)
   101 
   102 defs (overloaded)
   103   real_of_ereal_def [code_unfold]: "real \<equiv> of_ereal"
   104 
   105 lemma real_of_ereal[simp]:
   106     "real (- x :: ereal) = - (real x)"
   107     "real (ereal r) = r"
   108     "real (\<infinity>::ereal) = 0"
   109   by (cases x) (simp_all add: real_of_ereal_def)
   110 
   111 lemma range_ereal[simp]: "range ereal = UNIV - {\<infinity>, -\<infinity>}"
   112 proof safe
   113   fix x assume "x \<notin> range ereal" "x \<noteq> \<infinity>"
   114   then show "x = -\<infinity>" by (cases x) auto
   115 qed auto
   116 
   117 lemma ereal_range_uminus[simp]: "range uminus = (UNIV::ereal set)"
   118 proof safe
   119   fix x :: ereal show "x \<in> range uminus" by (intro image_eqI[of _ _ "-x"]) auto
   120 qed auto
   121 
   122 instantiation ereal :: number
   123 begin
   124 definition [simp]: "number_of x = ereal (number_of x)"
   125 instance proof qed
   126 end
   127 
   128 instantiation ereal :: abs
   129 begin
   130   function abs_ereal where
   131     "\<bar>ereal r\<bar> = ereal \<bar>r\<bar>"
   132   | "\<bar>-\<infinity>\<bar> = (\<infinity>::ereal)"
   133   | "\<bar>\<infinity>\<bar> = (\<infinity>::ereal)"
   134   by (auto intro: ereal_cases)
   135   termination proof qed (rule wf_empty)
   136   instance ..
   137 end
   138 
   139 lemma abs_eq_infinity_cases[elim!]: "\<lbrakk> \<bar>x :: ereal\<bar> = \<infinity> ; x = \<infinity> \<Longrightarrow> P ; x = -\<infinity> \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
   140   by (cases x) auto
   141 
   142 lemma abs_neq_infinity_cases[elim!]: "\<lbrakk> \<bar>x :: ereal\<bar> \<noteq> \<infinity> ; \<And>r. x = ereal r \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
   143   by (cases x) auto
   144 
   145 lemma abs_ereal_uminus[simp]: "\<bar>- x\<bar> = \<bar>x::ereal\<bar>"
   146   by (cases x) auto
   147 
   148 subsubsection "Addition"
   149 
   150 instantiation ereal :: comm_monoid_add
   151 begin
   152 
   153 definition "0 = ereal 0"
   154 
   155 function plus_ereal where
   156 "ereal r + ereal p = ereal (r + p)" |
   157 "\<infinity> + a = (\<infinity>::ereal)" |
   158 "a + \<infinity> = (\<infinity>::ereal)" |
   159 "ereal r + -\<infinity> = - \<infinity>" |
   160 "-\<infinity> + ereal p = -(\<infinity>::ereal)" |
   161 "-\<infinity> + -\<infinity> = -(\<infinity>::ereal)"
   162 proof -
   163   case (goal1 P x)
   164   moreover then obtain a b where "x = (a, b)" by (cases x) auto
   165   ultimately show P
   166    by (cases rule: ereal2_cases[of a b]) auto
   167 qed auto
   168 termination proof qed (rule wf_empty)
   169 
   170 lemma Infty_neq_0[simp]:
   171   "(\<infinity>::ereal) \<noteq> 0" "0 \<noteq> (\<infinity>::ereal)"
   172   "-(\<infinity>::ereal) \<noteq> 0" "0 \<noteq> -(\<infinity>::ereal)"
   173   by (simp_all add: zero_ereal_def)
   174 
   175 lemma ereal_eq_0[simp]:
   176   "ereal r = 0 \<longleftrightarrow> r = 0"
   177   "0 = ereal r \<longleftrightarrow> r = 0"
   178   unfolding zero_ereal_def by simp_all
   179 
   180 instance
   181 proof
   182   fix a :: ereal show "0 + a = a"
   183     by (cases a) (simp_all add: zero_ereal_def)
   184   fix b :: ereal show "a + b = b + a"
   185     by (cases rule: ereal2_cases[of a b]) simp_all
   186   fix c :: ereal show "a + b + c = a + (b + c)"
   187     by (cases rule: ereal3_cases[of a b c]) simp_all
   188 qed
   189 end
   190 
   191 lemma real_of_ereal_0[simp]: "real (0::ereal) = 0"
   192   unfolding real_of_ereal_def zero_ereal_def by simp
   193 
   194 lemma abs_ereal_zero[simp]: "\<bar>0\<bar> = (0::ereal)"
   195   unfolding zero_ereal_def abs_ereal.simps by simp
   196 
   197 lemma ereal_uminus_zero[simp]:
   198   "- 0 = (0::ereal)"
   199   by (simp add: zero_ereal_def)
   200 
   201 lemma ereal_uminus_zero_iff[simp]:
   202   fixes a :: ereal shows "-a = 0 \<longleftrightarrow> a = 0"
   203   by (cases a) simp_all
   204 
   205 lemma ereal_plus_eq_PInfty[simp]:
   206   fixes a b :: ereal shows "a + b = \<infinity> \<longleftrightarrow> a = \<infinity> \<or> b = \<infinity>"
   207   by (cases rule: ereal2_cases[of a b]) auto
   208 
   209 lemma ereal_plus_eq_MInfty[simp]:
   210   fixes a b :: ereal shows "a + b = -\<infinity> \<longleftrightarrow>
   211     (a = -\<infinity> \<or> b = -\<infinity>) \<and> a \<noteq> \<infinity> \<and> b \<noteq> \<infinity>"
   212   by (cases rule: ereal2_cases[of a b]) auto
   213 
   214 lemma ereal_add_cancel_left:
   215   fixes a b :: ereal assumes "a \<noteq> -\<infinity>"
   216   shows "a + b = a + c \<longleftrightarrow> (a = \<infinity> \<or> b = c)"
   217   using assms by (cases rule: ereal3_cases[of a b c]) auto
   218 
   219 lemma ereal_add_cancel_right:
   220   fixes a b :: ereal assumes "a \<noteq> -\<infinity>"
   221   shows "b + a = c + a \<longleftrightarrow> (a = \<infinity> \<or> b = c)"
   222   using assms by (cases rule: ereal3_cases[of a b c]) auto
   223 
   224 lemma ereal_real:
   225   "ereal (real x) = (if \<bar>x\<bar> = \<infinity> then 0 else x)"
   226   by (cases x) simp_all
   227 
   228 lemma real_of_ereal_add:
   229   fixes a b :: ereal
   230   shows "real (a + b) = (if (\<bar>a\<bar> = \<infinity>) \<and> (\<bar>b\<bar> = \<infinity>) \<or> (\<bar>a\<bar> \<noteq> \<infinity>) \<and> (\<bar>b\<bar> \<noteq> \<infinity>) then real a + real b else 0)"
   231   by (cases rule: ereal2_cases[of a b]) auto
   232 
   233 subsubsection "Linear order on @{typ ereal}"
   234 
   235 instantiation ereal :: linorder
   236 begin
   237 
   238 function less_ereal where
   239 "   ereal x < ereal y     \<longleftrightarrow> x < y" |
   240 "(\<infinity>::ereal) < a           \<longleftrightarrow> False" |
   241 "         a < -(\<infinity>::ereal) \<longleftrightarrow> False" |
   242 "ereal x    < \<infinity>           \<longleftrightarrow> True" |
   243 "        -\<infinity> < ereal r     \<longleftrightarrow> True" |
   244 "        -\<infinity> < (\<infinity>::ereal) \<longleftrightarrow> True"
   245 proof -
   246   case (goal1 P x)
   247   moreover then obtain a b where "x = (a,b)" by (cases x) auto
   248   ultimately show P by (cases rule: ereal2_cases[of a b]) auto
   249 qed simp_all
   250 termination by (relation "{}") simp
   251 
   252 definition "x \<le> (y::ereal) \<longleftrightarrow> x < y \<or> x = y"
   253 
   254 lemma ereal_infty_less[simp]:
   255   fixes x :: ereal
   256   shows "x < \<infinity> \<longleftrightarrow> (x \<noteq> \<infinity>)"
   257     "-\<infinity> < x \<longleftrightarrow> (x \<noteq> -\<infinity>)"
   258   by (cases x, simp_all) (cases x, simp_all)
   259 
   260 lemma ereal_infty_less_eq[simp]:
   261   fixes x :: ereal
   262   shows "\<infinity> \<le> x \<longleftrightarrow> x = \<infinity>"
   263   "x \<le> -\<infinity> \<longleftrightarrow> x = -\<infinity>"
   264   by (auto simp add: less_eq_ereal_def)
   265 
   266 lemma ereal_less[simp]:
   267   "ereal r < 0 \<longleftrightarrow> (r < 0)"
   268   "0 < ereal r \<longleftrightarrow> (0 < r)"
   269   "0 < (\<infinity>::ereal)"
   270   "-(\<infinity>::ereal) < 0"
   271   by (simp_all add: zero_ereal_def)
   272 
   273 lemma ereal_less_eq[simp]:
   274   "x \<le> (\<infinity>::ereal)"
   275   "-(\<infinity>::ereal) \<le> x"
   276   "ereal r \<le> ereal p \<longleftrightarrow> r \<le> p"
   277   "ereal r \<le> 0 \<longleftrightarrow> r \<le> 0"
   278   "0 \<le> ereal r \<longleftrightarrow> 0 \<le> r"
   279   by (auto simp add: less_eq_ereal_def zero_ereal_def)
   280 
   281 lemma ereal_infty_less_eq2:
   282   "a \<le> b \<Longrightarrow> a = \<infinity> \<Longrightarrow> b = (\<infinity>::ereal)"
   283   "a \<le> b \<Longrightarrow> b = -\<infinity> \<Longrightarrow> a = -(\<infinity>::ereal)"
   284   by simp_all
   285 
   286 instance
   287 proof
   288   fix x :: ereal show "x \<le> x"
   289     by (cases x) simp_all
   290   fix y :: ereal show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
   291     by (cases rule: ereal2_cases[of x y]) auto
   292   show "x \<le> y \<or> y \<le> x "
   293     by (cases rule: ereal2_cases[of x y]) auto
   294   { assume "x \<le> y" "y \<le> x" then show "x = y"
   295     by (cases rule: ereal2_cases[of x y]) auto }
   296   { fix z assume "x \<le> y" "y \<le> z" then show "x \<le> z"
   297     by (cases rule: ereal3_cases[of x y z]) auto }
   298 qed
   299 end
   300 
   301 instance ereal :: ordered_ab_semigroup_add
   302 proof
   303   fix a b c :: ereal assume "a \<le> b" then show "c + a \<le> c + b"
   304     by (cases rule: ereal3_cases[of a b c]) auto
   305 qed
   306 
   307 lemma real_of_ereal_positive_mono:
   308   fixes x y :: ereal shows "\<lbrakk>0 \<le> x; x \<le> y; y \<noteq> \<infinity>\<rbrakk> \<Longrightarrow> real x \<le> real y"
   309   by (cases rule: ereal2_cases[of x y]) auto
   310 
   311 lemma ereal_MInfty_lessI[intro, simp]:
   312   fixes a :: ereal shows "a \<noteq> -\<infinity> \<Longrightarrow> -\<infinity> < a"
   313   by (cases a) auto
   314 
   315 lemma ereal_less_PInfty[intro, simp]:
   316   fixes a :: ereal shows "a \<noteq> \<infinity> \<Longrightarrow> a < \<infinity>"
   317   by (cases a) auto
   318 
   319 lemma ereal_less_ereal_Ex:
   320   fixes a b :: ereal
   321   shows "x < ereal r \<longleftrightarrow> x = -\<infinity> \<or> (\<exists>p. p < r \<and> x = ereal p)"
   322   by (cases x) auto
   323 
   324 lemma less_PInf_Ex_of_nat: "x \<noteq> \<infinity> \<longleftrightarrow> (\<exists>n::nat. x < ereal (real n))"
   325 proof (cases x)
   326   case (real r) then show ?thesis
   327     using reals_Archimedean2[of r] by simp
   328 qed simp_all
   329 
   330 lemma ereal_add_mono:
   331   fixes a b c d :: ereal assumes "a \<le> b" "c \<le> d" shows "a + c \<le> b + d"
   332   using assms
   333   apply (cases a)
   334   apply (cases rule: ereal3_cases[of b c d], auto)
   335   apply (cases rule: ereal3_cases[of b c d], auto)
   336   done
   337 
   338 lemma ereal_minus_le_minus[simp]:
   339   fixes a b :: ereal shows "- a \<le> - b \<longleftrightarrow> b \<le> a"
   340   by (cases rule: ereal2_cases[of a b]) auto
   341 
   342 lemma ereal_minus_less_minus[simp]:
   343   fixes a b :: ereal shows "- a < - b \<longleftrightarrow> b < a"
   344   by (cases rule: ereal2_cases[of a b]) auto
   345 
   346 lemma ereal_le_real_iff:
   347   "x \<le> real y \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> ereal x \<le> y) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> x \<le> 0))"
   348   by (cases y) auto
   349 
   350 lemma real_le_ereal_iff:
   351   "real y \<le> x \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> y \<le> ereal x) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> 0 \<le> x))"
   352   by (cases y) auto
   353 
   354 lemma ereal_less_real_iff:
   355   "x < real y \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> ereal x < y) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> x < 0))"
   356   by (cases y) auto
   357 
   358 lemma real_less_ereal_iff:
   359   "real y < x \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> y < ereal x) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> 0 < x))"
   360   by (cases y) auto
   361 
   362 lemma real_of_ereal_pos:
   363   fixes x :: ereal shows "0 \<le> x \<Longrightarrow> 0 \<le> real x" by (cases x) auto
   364 
   365 lemmas real_of_ereal_ord_simps =
   366   ereal_le_real_iff real_le_ereal_iff ereal_less_real_iff real_less_ereal_iff
   367 
   368 lemma abs_ereal_ge0[simp]: "0 \<le> x \<Longrightarrow> \<bar>x :: ereal\<bar> = x"
   369   by (cases x) auto
   370 
   371 lemma abs_ereal_less0[simp]: "x < 0 \<Longrightarrow> \<bar>x :: ereal\<bar> = -x"
   372   by (cases x) auto
   373 
   374 lemma abs_ereal_pos[simp]: "0 \<le> \<bar>x :: ereal\<bar>"
   375   by (cases x) auto
   376 
   377 lemma real_of_ereal_le_0[simp]: "real (x :: ereal) \<le> 0 \<longleftrightarrow> (x \<le> 0 \<or> x = \<infinity>)"
   378   by (cases x) auto
   379 
   380 lemma abs_real_of_ereal[simp]: "\<bar>real (x :: ereal)\<bar> = real \<bar>x\<bar>"
   381   by (cases x) auto
   382 
   383 lemma zero_less_real_of_ereal:
   384   fixes x :: ereal shows "0 < real x \<longleftrightarrow> (0 < x \<and> x \<noteq> \<infinity>)"
   385   by (cases x) auto
   386 
   387 lemma ereal_0_le_uminus_iff[simp]:
   388   fixes a :: ereal shows "0 \<le> -a \<longleftrightarrow> a \<le> 0"
   389   by (cases rule: ereal2_cases[of a]) auto
   390 
   391 lemma ereal_uminus_le_0_iff[simp]:
   392   fixes a :: ereal shows "-a \<le> 0 \<longleftrightarrow> 0 \<le> a"
   393   by (cases rule: ereal2_cases[of a]) auto
   394 
   395 lemma ereal_dense2: "x < y \<Longrightarrow> \<exists>z. x < ereal z \<and> ereal z < y"
   396   using lt_ex gt_ex dense by (cases x y rule: ereal2_cases) auto
   397 
   398 lemma ereal_dense:
   399   fixes x y :: ereal assumes "x < y"
   400   shows "\<exists>z. x < z \<and> z < y"
   401   using ereal_dense2[OF `x < y`] by blast
   402 
   403 lemma ereal_add_strict_mono:
   404   fixes a b c d :: ereal
   405   assumes "a = b" "0 \<le> a" "a \<noteq> \<infinity>" "c < d"
   406   shows "a + c < b + d"
   407   using assms by (cases rule: ereal3_cases[case_product ereal_cases, of a b c d]) auto
   408 
   409 lemma ereal_less_add: 
   410   fixes a b c :: ereal shows "\<bar>a\<bar> \<noteq> \<infinity> \<Longrightarrow> c < b \<Longrightarrow> a + c < a + b"
   411   by (cases rule: ereal2_cases[of b c]) auto
   412 
   413 lemma ereal_uminus_eq_reorder: "- a = b \<longleftrightarrow> a = (-b::ereal)" by auto
   414 
   415 lemma ereal_uminus_less_reorder: "- a < b \<longleftrightarrow> -b < (a::ereal)"
   416   by (subst (3) ereal_uminus_uminus[symmetric]) (simp only: ereal_minus_less_minus)
   417 
   418 lemma ereal_uminus_le_reorder: "- a \<le> b \<longleftrightarrow> -b \<le> (a::ereal)"
   419   by (subst (3) ereal_uminus_uminus[symmetric]) (simp only: ereal_minus_le_minus)
   420 
   421 lemmas ereal_uminus_reorder =
   422   ereal_uminus_eq_reorder ereal_uminus_less_reorder ereal_uminus_le_reorder
   423 
   424 lemma ereal_bot:
   425   fixes x :: ereal assumes "\<And>B. x \<le> ereal B" shows "x = - \<infinity>"
   426 proof (cases x)
   427   case (real r) with assms[of "r - 1"] show ?thesis by auto
   428 next case PInf with assms[of 0] show ?thesis by auto
   429 next case MInf then show ?thesis by simp
   430 qed
   431 
   432 lemma ereal_top:
   433   fixes x :: ereal assumes "\<And>B. x \<ge> ereal B" shows "x = \<infinity>"
   434 proof (cases x)
   435   case (real r) with assms[of "r + 1"] show ?thesis by auto
   436 next case MInf with assms[of 0] show ?thesis by auto
   437 next case PInf then show ?thesis by simp
   438 qed
   439 
   440 lemma
   441   shows ereal_max[simp]: "ereal (max x y) = max (ereal x) (ereal y)"
   442     and ereal_min[simp]: "ereal (min x y) = min (ereal x) (ereal y)"
   443   by (simp_all add: min_def max_def)
   444 
   445 lemma ereal_max_0: "max 0 (ereal r) = ereal (max 0 r)"
   446   by (auto simp: zero_ereal_def)
   447 
   448 lemma
   449   fixes f :: "nat \<Rightarrow> ereal"
   450   shows incseq_uminus[simp]: "incseq (\<lambda>x. - f x) \<longleftrightarrow> decseq f"
   451   and decseq_uminus[simp]: "decseq (\<lambda>x. - f x) \<longleftrightarrow> incseq f"
   452   unfolding decseq_def incseq_def by auto
   453 
   454 lemma incseq_ereal: "incseq f \<Longrightarrow> incseq (\<lambda>x. ereal (f x))"
   455   unfolding incseq_def by auto
   456 
   457 lemma ereal_add_nonneg_nonneg:
   458   fixes a b :: ereal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a + b"
   459   using add_mono[of 0 a 0 b] by simp
   460 
   461 lemma image_eqD: "f ` A = B \<Longrightarrow> (\<forall>x\<in>A. f x \<in> B)"
   462   by auto
   463 
   464 lemma incseq_setsumI:
   465   fixes f :: "nat \<Rightarrow> 'a::{comm_monoid_add, ordered_ab_semigroup_add}"
   466   assumes "\<And>i. 0 \<le> f i"
   467   shows "incseq (\<lambda>i. setsum f {..< i})"
   468 proof (intro incseq_SucI)
   469   fix n have "setsum f {..< n} + 0 \<le> setsum f {..<n} + f n"
   470     using assms by (rule add_left_mono)
   471   then show "setsum f {..< n} \<le> setsum f {..< Suc n}"
   472     by auto
   473 qed
   474 
   475 lemma incseq_setsumI2:
   476   fixes f :: "'i \<Rightarrow> nat \<Rightarrow> 'a::{comm_monoid_add, ordered_ab_semigroup_add}"
   477   assumes "\<And>n. n \<in> A \<Longrightarrow> incseq (f n)"
   478   shows "incseq (\<lambda>i. \<Sum>n\<in>A. f n i)"
   479   using assms unfolding incseq_def by (auto intro: setsum_mono)
   480 
   481 subsubsection "Multiplication"
   482 
   483 instantiation ereal :: "{comm_monoid_mult, sgn}"
   484 begin
   485 
   486 definition "1 = ereal 1"
   487 
   488 function sgn_ereal where
   489   "sgn (ereal r) = ereal (sgn r)"
   490 | "sgn (\<infinity>::ereal) = 1"
   491 | "sgn (-\<infinity>::ereal) = -1"
   492 by (auto intro: ereal_cases)
   493 termination proof qed (rule wf_empty)
   494 
   495 function times_ereal where
   496 "ereal r * ereal p = ereal (r * p)" |
   497 "ereal r * \<infinity> = (if r = 0 then 0 else if r > 0 then \<infinity> else -\<infinity>)" |
   498 "\<infinity> * ereal r = (if r = 0 then 0 else if r > 0 then \<infinity> else -\<infinity>)" |
   499 "ereal r * -\<infinity> = (if r = 0 then 0 else if r > 0 then -\<infinity> else \<infinity>)" |
   500 "-\<infinity> * ereal r = (if r = 0 then 0 else if r > 0 then -\<infinity> else \<infinity>)" |
   501 "(\<infinity>::ereal) * \<infinity> = \<infinity>" |
   502 "-(\<infinity>::ereal) * \<infinity> = -\<infinity>" |
   503 "(\<infinity>::ereal) * -\<infinity> = -\<infinity>" |
   504 "-(\<infinity>::ereal) * -\<infinity> = \<infinity>"
   505 proof -
   506   case (goal1 P x)
   507   moreover then obtain a b where "x = (a, b)" by (cases x) auto
   508   ultimately show P by (cases rule: ereal2_cases[of a b]) auto
   509 qed simp_all
   510 termination by (relation "{}") simp
   511 
   512 instance
   513 proof
   514   fix a :: ereal show "1 * a = a"
   515     by (cases a) (simp_all add: one_ereal_def)
   516   fix b :: ereal show "a * b = b * a"
   517     by (cases rule: ereal2_cases[of a b]) simp_all
   518   fix c :: ereal show "a * b * c = a * (b * c)"
   519     by (cases rule: ereal3_cases[of a b c])
   520        (simp_all add: zero_ereal_def zero_less_mult_iff)
   521 qed
   522 end
   523 
   524 lemma real_of_ereal_le_1:
   525   fixes a :: ereal shows "a \<le> 1 \<Longrightarrow> real a \<le> 1"
   526   by (cases a) (auto simp: one_ereal_def)
   527 
   528 lemma abs_ereal_one[simp]: "\<bar>1\<bar> = (1::ereal)"
   529   unfolding one_ereal_def by simp
   530 
   531 lemma ereal_mult_zero[simp]:
   532   fixes a :: ereal shows "a * 0 = 0"
   533   by (cases a) (simp_all add: zero_ereal_def)
   534 
   535 lemma ereal_zero_mult[simp]:
   536   fixes a :: ereal shows "0 * a = 0"
   537   by (cases a) (simp_all add: zero_ereal_def)
   538 
   539 lemma ereal_m1_less_0[simp]:
   540   "-(1::ereal) < 0"
   541   by (simp add: zero_ereal_def one_ereal_def)
   542 
   543 lemma ereal_zero_m1[simp]:
   544   "1 \<noteq> (0::ereal)"
   545   by (simp add: zero_ereal_def one_ereal_def)
   546 
   547 lemma ereal_times_0[simp]:
   548   fixes x :: ereal shows "0 * x = 0"
   549   by (cases x) (auto simp: zero_ereal_def)
   550 
   551 lemma ereal_times[simp]:
   552   "1 \<noteq> (\<infinity>::ereal)" "(\<infinity>::ereal) \<noteq> 1"
   553   "1 \<noteq> -(\<infinity>::ereal)" "-(\<infinity>::ereal) \<noteq> 1"
   554   by (auto simp add: times_ereal_def one_ereal_def)
   555 
   556 lemma ereal_plus_1[simp]:
   557   "1 + ereal r = ereal (r + 1)" "ereal r + 1 = ereal (r + 1)"
   558   "1 + -(\<infinity>::ereal) = -\<infinity>" "-(\<infinity>::ereal) + 1 = -\<infinity>"
   559   unfolding one_ereal_def by auto
   560 
   561 lemma ereal_zero_times[simp]:
   562   fixes a b :: ereal shows "a * b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
   563   by (cases rule: ereal2_cases[of a b]) auto
   564 
   565 lemma ereal_mult_eq_PInfty[simp]:
   566   shows "a * b = (\<infinity>::ereal) \<longleftrightarrow>
   567     (a = \<infinity> \<and> b > 0) \<or> (a > 0 \<and> b = \<infinity>) \<or> (a = -\<infinity> \<and> b < 0) \<or> (a < 0 \<and> b = -\<infinity>)"
   568   by (cases rule: ereal2_cases[of a b]) auto
   569 
   570 lemma ereal_mult_eq_MInfty[simp]:
   571   shows "a * b = -(\<infinity>::ereal) \<longleftrightarrow>
   572     (a = \<infinity> \<and> b < 0) \<or> (a < 0 \<and> b = \<infinity>) \<or> (a = -\<infinity> \<and> b > 0) \<or> (a > 0 \<and> b = -\<infinity>)"
   573   by (cases rule: ereal2_cases[of a b]) auto
   574 
   575 lemma ereal_0_less_1[simp]: "0 < (1::ereal)"
   576   by (simp_all add: zero_ereal_def one_ereal_def)
   577 
   578 lemma ereal_zero_one[simp]: "0 \<noteq> (1::ereal)"
   579   by (simp_all add: zero_ereal_def one_ereal_def)
   580 
   581 lemma ereal_mult_minus_left[simp]:
   582   fixes a b :: ereal shows "-a * b = - (a * b)"
   583   by (cases rule: ereal2_cases[of a b]) auto
   584 
   585 lemma ereal_mult_minus_right[simp]:
   586   fixes a b :: ereal shows "a * -b = - (a * b)"
   587   by (cases rule: ereal2_cases[of a b]) auto
   588 
   589 lemma ereal_mult_infty[simp]:
   590   "a * (\<infinity>::ereal) = (if a = 0 then 0 else if 0 < a then \<infinity> else - \<infinity>)"
   591   by (cases a) auto
   592 
   593 lemma ereal_infty_mult[simp]:
   594   "(\<infinity>::ereal) * a = (if a = 0 then 0 else if 0 < a then \<infinity> else - \<infinity>)"
   595   by (cases a) auto
   596 
   597 lemma ereal_mult_strict_right_mono:
   598   assumes "a < b" and "0 < c" "c < (\<infinity>::ereal)"
   599   shows "a * c < b * c"
   600   using assms
   601   by (cases rule: ereal3_cases[of a b c])
   602      (auto simp: zero_le_mult_iff ereal_less_PInfty)
   603 
   604 lemma ereal_mult_strict_left_mono:
   605   "\<lbrakk> a < b ; 0 < c ; c < (\<infinity>::ereal)\<rbrakk> \<Longrightarrow> c * a < c * b"
   606   using ereal_mult_strict_right_mono by (simp add: mult_commute[of c])
   607 
   608 lemma ereal_mult_right_mono:
   609   fixes a b c :: ereal shows "\<lbrakk>a \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> a*c \<le> b*c"
   610   using assms
   611   apply (cases "c = 0") apply simp
   612   by (cases rule: ereal3_cases[of a b c])
   613      (auto simp: zero_le_mult_iff ereal_less_PInfty)
   614 
   615 lemma ereal_mult_left_mono:
   616   fixes a b c :: ereal shows "\<lbrakk>a \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> c * a \<le> c * b"
   617   using ereal_mult_right_mono by (simp add: mult_commute[of c])
   618 
   619 lemma zero_less_one_ereal[simp]: "0 \<le> (1::ereal)"
   620   by (simp add: one_ereal_def zero_ereal_def)
   621 
   622 lemma ereal_0_le_mult[simp]: "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a * (b :: ereal)"
   623   by (cases rule: ereal2_cases[of a b]) (auto simp: mult_nonneg_nonneg)
   624 
   625 lemma ereal_right_distrib:
   626   fixes r a b :: ereal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> r * (a + b) = r * a + r * b"
   627   by (cases rule: ereal3_cases[of r a b]) (simp_all add: field_simps)
   628 
   629 lemma ereal_left_distrib:
   630   fixes r a b :: ereal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> (a + b) * r = a * r + b * r"
   631   by (cases rule: ereal3_cases[of r a b]) (simp_all add: field_simps)
   632 
   633 lemma ereal_mult_le_0_iff:
   634   fixes a b :: ereal
   635   shows "a * b \<le> 0 \<longleftrightarrow> (0 \<le> a \<and> b \<le> 0) \<or> (a \<le> 0 \<and> 0 \<le> b)"
   636   by (cases rule: ereal2_cases[of a b]) (simp_all add: mult_le_0_iff)
   637 
   638 lemma ereal_zero_le_0_iff:
   639   fixes a b :: ereal
   640   shows "0 \<le> a * b \<longleftrightarrow> (0 \<le> a \<and> 0 \<le> b) \<or> (a \<le> 0 \<and> b \<le> 0)"
   641   by (cases rule: ereal2_cases[of a b]) (simp_all add: zero_le_mult_iff)
   642 
   643 lemma ereal_mult_less_0_iff:
   644   fixes a b :: ereal
   645   shows "a * b < 0 \<longleftrightarrow> (0 < a \<and> b < 0) \<or> (a < 0 \<and> 0 < b)"
   646   by (cases rule: ereal2_cases[of a b]) (simp_all add: mult_less_0_iff)
   647 
   648 lemma ereal_zero_less_0_iff:
   649   fixes a b :: ereal
   650   shows "0 < a * b \<longleftrightarrow> (0 < a \<and> 0 < b) \<or> (a < 0 \<and> b < 0)"
   651   by (cases rule: ereal2_cases[of a b]) (simp_all add: zero_less_mult_iff)
   652 
   653 lemma ereal_distrib:
   654   fixes a b c :: ereal
   655   assumes "a \<noteq> \<infinity> \<or> b \<noteq> -\<infinity>" "a \<noteq> -\<infinity> \<or> b \<noteq> \<infinity>" "\<bar>c\<bar> \<noteq> \<infinity>"
   656   shows "(a + b) * c = a * c + b * c"
   657   using assms
   658   by (cases rule: ereal3_cases[of a b c]) (simp_all add: field_simps)
   659 
   660 lemma ereal_le_epsilon:
   661   fixes x y :: ereal
   662   assumes "ALL e. 0 < e --> x <= y + e"
   663   shows "x <= y"
   664 proof-
   665 { assume a: "EX r. y = ereal r"
   666   from this obtain r where r_def: "y = ereal r" by auto
   667   { assume "x=(-\<infinity>)" hence ?thesis by auto }
   668   moreover
   669   { assume "~(x=(-\<infinity>))"
   670     from this obtain p where p_def: "x = ereal p"
   671     using a assms[rule_format, of 1] by (cases x) auto
   672     { fix e have "0 < e --> p <= r + e"
   673       using assms[rule_format, of "ereal e"] p_def r_def by auto }
   674     hence "p <= r" apply (subst field_le_epsilon) by auto
   675     hence ?thesis using r_def p_def by auto
   676   } ultimately have ?thesis by blast
   677 }
   678 moreover
   679 { assume "y=(-\<infinity>) | y=\<infinity>" hence ?thesis
   680     using assms[rule_format, of 1] by (cases x) auto
   681 } ultimately show ?thesis by (cases y) auto
   682 qed
   683 
   684 
   685 lemma ereal_le_epsilon2:
   686   fixes x y :: ereal
   687   assumes "ALL e. 0 < e --> x <= y + ereal e"
   688   shows "x <= y"
   689 proof-
   690 { fix e :: ereal assume "e>0"
   691   { assume "e=\<infinity>" hence "x<=y+e" by auto }
   692   moreover
   693   { assume "e~=\<infinity>"
   694     from this obtain r where "e = ereal r" using `e>0` apply (cases e) by auto
   695     hence "x<=y+e" using assms[rule_format, of r] `e>0` by auto
   696   } ultimately have "x<=y+e" by blast
   697 } from this show ?thesis using ereal_le_epsilon by auto
   698 qed
   699 
   700 lemma ereal_le_real:
   701   fixes x y :: ereal
   702   assumes "ALL z. x <= ereal z --> y <= ereal z"
   703   shows "y <= x"
   704 by (metis assms ereal_bot ereal_cases ereal_infty_less_eq ereal_less_eq linorder_le_cases)
   705 
   706 lemma ereal_le_ereal:
   707   fixes x y :: ereal
   708   assumes "\<And>B. B < x \<Longrightarrow> B <= y"
   709   shows "x <= y"
   710 by (metis assms ereal_dense leD linorder_le_less_linear)
   711 
   712 lemma ereal_ge_ereal:
   713   fixes x y :: ereal
   714   assumes "ALL B. B>x --> B >= y"
   715   shows "x >= y"
   716 by (metis assms ereal_dense leD linorder_le_less_linear)
   717 
   718 lemma setprod_ereal_0:
   719   fixes f :: "'a \<Rightarrow> ereal"
   720   shows "(\<Prod>i\<in>A. f i) = 0 \<longleftrightarrow> (finite A \<and> (\<exists>i\<in>A. f i = 0))"
   721 proof cases
   722   assume "finite A"
   723   then show ?thesis by (induct A) auto
   724 qed auto
   725 
   726 lemma setprod_ereal_pos:
   727   fixes f :: "'a \<Rightarrow> ereal" assumes pos: "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i" shows "0 \<le> (\<Prod>i\<in>I. f i)"
   728 proof cases
   729   assume "finite I" from this pos show ?thesis by induct auto
   730 qed simp
   731 
   732 lemma setprod_PInf:
   733   fixes f :: "'a \<Rightarrow> ereal"
   734   assumes "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i"
   735   shows "(\<Prod>i\<in>I. f i) = \<infinity> \<longleftrightarrow> finite I \<and> (\<exists>i\<in>I. f i = \<infinity>) \<and> (\<forall>i\<in>I. f i \<noteq> 0)"
   736 proof cases
   737   assume "finite I" from this assms show ?thesis
   738   proof (induct I)
   739     case (insert i I)
   740     then have pos: "0 \<le> f i" "0 \<le> setprod f I" by (auto intro!: setprod_ereal_pos)
   741     from insert have "(\<Prod>j\<in>insert i I. f j) = \<infinity> \<longleftrightarrow> setprod f I * f i = \<infinity>" by auto
   742     also have "\<dots> \<longleftrightarrow> (setprod f I = \<infinity> \<or> f i = \<infinity>) \<and> f i \<noteq> 0 \<and> setprod f I \<noteq> 0"
   743       using setprod_ereal_pos[of I f] pos
   744       by (cases rule: ereal2_cases[of "f i" "setprod f I"]) auto
   745     also have "\<dots> \<longleftrightarrow> finite (insert i I) \<and> (\<exists>j\<in>insert i I. f j = \<infinity>) \<and> (\<forall>j\<in>insert i I. f j \<noteq> 0)"
   746       using insert by (auto simp: setprod_ereal_0)
   747     finally show ?case .
   748   qed simp
   749 qed simp
   750 
   751 lemma setprod_ereal: "(\<Prod>i\<in>A. ereal (f i)) = ereal (setprod f A)"
   752 proof cases
   753   assume "finite A" then show ?thesis
   754     by induct (auto simp: one_ereal_def)
   755 qed (simp add: one_ereal_def)
   756 
   757 subsubsection {* Power *}
   758 
   759 instantiation ereal :: power
   760 begin
   761 primrec power_ereal where
   762   "power_ereal x 0 = 1" |
   763   "power_ereal x (Suc n) = x * x ^ n"
   764 instance ..
   765 end
   766 
   767 lemma ereal_power[simp]: "(ereal x) ^ n = ereal (x^n)"
   768   by (induct n) (auto simp: one_ereal_def)
   769 
   770 lemma ereal_power_PInf[simp]: "(\<infinity>::ereal) ^ n = (if n = 0 then 1 else \<infinity>)"
   771   by (induct n) (auto simp: one_ereal_def)
   772 
   773 lemma ereal_power_uminus[simp]:
   774   fixes x :: ereal
   775   shows "(- x) ^ n = (if even n then x ^ n else - (x^n))"
   776   by (induct n) (auto simp: one_ereal_def)
   777 
   778 lemma ereal_power_number_of[simp]:
   779   "(number_of num :: ereal) ^ n = ereal (number_of num ^ n)"
   780   by (induct n) (auto simp: one_ereal_def)
   781 
   782 lemma zero_le_power_ereal[simp]:
   783   fixes a :: ereal assumes "0 \<le> a"
   784   shows "0 \<le> a ^ n"
   785   using assms by (induct n) (auto simp: ereal_zero_le_0_iff)
   786 
   787 subsubsection {* Subtraction *}
   788 
   789 lemma ereal_minus_minus_image[simp]:
   790   fixes S :: "ereal set"
   791   shows "uminus ` uminus ` S = S"
   792   by (auto simp: image_iff)
   793 
   794 lemma ereal_uminus_lessThan[simp]:
   795   fixes a :: ereal shows "uminus ` {..<a} = {-a<..}"
   796 proof (safe intro!: image_eqI)
   797   fix x assume "-a < x"
   798   then have "- x < - (- a)" by (simp del: ereal_uminus_uminus)
   799   then show "- x < a" by simp
   800 qed auto
   801 
   802 lemma ereal_uminus_greaterThan[simp]:
   803   "uminus ` {(a::ereal)<..} = {..<-a}"
   804   by (metis ereal_uminus_lessThan ereal_uminus_uminus
   805             ereal_minus_minus_image)
   806 
   807 instantiation ereal :: minus
   808 begin
   809 definition "x - y = x + -(y::ereal)"
   810 instance ..
   811 end
   812 
   813 lemma ereal_minus[simp]:
   814   "ereal r - ereal p = ereal (r - p)"
   815   "-\<infinity> - ereal r = -\<infinity>"
   816   "ereal r - \<infinity> = -\<infinity>"
   817   "(\<infinity>::ereal) - x = \<infinity>"
   818   "-(\<infinity>::ereal) - \<infinity> = -\<infinity>"
   819   "x - -y = x + y"
   820   "x - 0 = x"
   821   "0 - x = -x"
   822   by (simp_all add: minus_ereal_def)
   823 
   824 lemma ereal_x_minus_x[simp]:
   825   "x - x = (if \<bar>x\<bar> = \<infinity> then \<infinity> else 0::ereal)"
   826   by (cases x) simp_all
   827 
   828 lemma ereal_eq_minus_iff:
   829   fixes x y z :: ereal
   830   shows "x = z - y \<longleftrightarrow>
   831     (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x + y = z) \<and>
   832     (y = -\<infinity> \<longrightarrow> x = \<infinity>) \<and>
   833     (y = \<infinity> \<longrightarrow> z = \<infinity> \<longrightarrow> x = \<infinity>) \<and>
   834     (y = \<infinity> \<longrightarrow> z \<noteq> \<infinity> \<longrightarrow> x = -\<infinity>)"
   835   by (cases rule: ereal3_cases[of x y z]) auto
   836 
   837 lemma ereal_eq_minus:
   838   fixes x y z :: ereal
   839   shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x = z - y \<longleftrightarrow> x + y = z"
   840   by (auto simp: ereal_eq_minus_iff)
   841 
   842 lemma ereal_less_minus_iff:
   843   fixes x y z :: ereal
   844   shows "x < z - y \<longleftrightarrow>
   845     (y = \<infinity> \<longrightarrow> z = \<infinity> \<and> x \<noteq> \<infinity>) \<and>
   846     (y = -\<infinity> \<longrightarrow> x \<noteq> \<infinity>) \<and>
   847     (\<bar>y\<bar> \<noteq> \<infinity>\<longrightarrow> x + y < z)"
   848   by (cases rule: ereal3_cases[of x y z]) auto
   849 
   850 lemma ereal_less_minus:
   851   fixes x y z :: ereal
   852   shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x < z - y \<longleftrightarrow> x + y < z"
   853   by (auto simp: ereal_less_minus_iff)
   854 
   855 lemma ereal_le_minus_iff:
   856   fixes x y z :: ereal
   857   shows "x \<le> z - y \<longleftrightarrow>
   858     (y = \<infinity> \<longrightarrow> z \<noteq> \<infinity> \<longrightarrow> x = -\<infinity>) \<and>
   859     (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x + y \<le> z)"
   860   by (cases rule: ereal3_cases[of x y z]) auto
   861 
   862 lemma ereal_le_minus:
   863   fixes x y z :: ereal
   864   shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x \<le> z - y \<longleftrightarrow> x + y \<le> z"
   865   by (auto simp: ereal_le_minus_iff)
   866 
   867 lemma ereal_minus_less_iff:
   868   fixes x y z :: ereal
   869   shows "x - y < z \<longleftrightarrow>
   870     y \<noteq> -\<infinity> \<and> (y = \<infinity> \<longrightarrow> x \<noteq> \<infinity> \<and> z \<noteq> -\<infinity>) \<and>
   871     (y \<noteq> \<infinity> \<longrightarrow> x < z + y)"
   872   by (cases rule: ereal3_cases[of x y z]) auto
   873 
   874 lemma ereal_minus_less:
   875   fixes x y z :: ereal
   876   shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x - y < z \<longleftrightarrow> x < z + y"
   877   by (auto simp: ereal_minus_less_iff)
   878 
   879 lemma ereal_minus_le_iff:
   880   fixes x y z :: ereal
   881   shows "x - y \<le> z \<longleftrightarrow>
   882     (y = -\<infinity> \<longrightarrow> z = \<infinity>) \<and>
   883     (y = \<infinity> \<longrightarrow> x = \<infinity> \<longrightarrow> z = \<infinity>) \<and>
   884     (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x \<le> z + y)"
   885   by (cases rule: ereal3_cases[of x y z]) auto
   886 
   887 lemma ereal_minus_le:
   888   fixes x y z :: ereal
   889   shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x - y \<le> z \<longleftrightarrow> x \<le> z + y"
   890   by (auto simp: ereal_minus_le_iff)
   891 
   892 lemma ereal_minus_eq_minus_iff:
   893   fixes a b c :: ereal
   894   shows "a - b = a - c \<longleftrightarrow>
   895     b = c \<or> a = \<infinity> \<or> (a = -\<infinity> \<and> b \<noteq> -\<infinity> \<and> c \<noteq> -\<infinity>)"
   896   by (cases rule: ereal3_cases[of a b c]) auto
   897 
   898 lemma ereal_add_le_add_iff:
   899   fixes a b c :: ereal
   900   shows "c + a \<le> c + b \<longleftrightarrow>
   901     a \<le> b \<or> c = \<infinity> \<or> (c = -\<infinity> \<and> a \<noteq> \<infinity> \<and> b \<noteq> \<infinity>)"
   902   by (cases rule: ereal3_cases[of a b c]) (simp_all add: field_simps)
   903 
   904 lemma ereal_mult_le_mult_iff:
   905   fixes a b c :: ereal
   906   shows "\<bar>c\<bar> \<noteq> \<infinity> \<Longrightarrow> c * a \<le> c * b \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
   907   by (cases rule: ereal3_cases[of a b c]) (simp_all add: mult_le_cancel_left)
   908 
   909 lemma ereal_minus_mono:
   910   fixes A B C D :: ereal assumes "A \<le> B" "D \<le> C"
   911   shows "A - C \<le> B - D"
   912   using assms
   913   by (cases rule: ereal3_cases[case_product ereal_cases, of A B C D]) simp_all
   914 
   915 lemma real_of_ereal_minus:
   916   fixes a b :: ereal
   917   shows "real (a - b) = (if \<bar>a\<bar> = \<infinity> \<or> \<bar>b\<bar> = \<infinity> then 0 else real a - real b)"
   918   by (cases rule: ereal2_cases[of a b]) auto
   919 
   920 lemma ereal_diff_positive:
   921   fixes a b :: ereal shows "a \<le> b \<Longrightarrow> 0 \<le> b - a"
   922   by (cases rule: ereal2_cases[of a b]) auto
   923 
   924 lemma ereal_between:
   925   fixes x e :: ereal
   926   assumes "\<bar>x\<bar> \<noteq> \<infinity>" "0 < e"
   927   shows "x - e < x" "x < x + e"
   928 using assms apply (cases x, cases e) apply auto
   929 using assms by (cases x, cases e) auto
   930 
   931 subsubsection {* Division *}
   932 
   933 instantiation ereal :: inverse
   934 begin
   935 
   936 function inverse_ereal where
   937 "inverse (ereal r) = (if r = 0 then \<infinity> else ereal (inverse r))" |
   938 "inverse (\<infinity>::ereal) = 0" |
   939 "inverse (-\<infinity>::ereal) = 0"
   940   by (auto intro: ereal_cases)
   941 termination by (relation "{}") simp
   942 
   943 definition "x / y = x * inverse (y :: ereal)"
   944 
   945 instance proof qed
   946 end
   947 
   948 lemma real_of_ereal_inverse[simp]:
   949   fixes a :: ereal
   950   shows "real (inverse a) = 1 / real a"
   951   by (cases a) (auto simp: inverse_eq_divide)
   952 
   953 lemma ereal_inverse[simp]:
   954   "inverse (0::ereal) = \<infinity>"
   955   "inverse (1::ereal) = 1"
   956   by (simp_all add: one_ereal_def zero_ereal_def)
   957 
   958 lemma ereal_divide[simp]:
   959   "ereal r / ereal p = (if p = 0 then ereal r * \<infinity> else ereal (r / p))"
   960   unfolding divide_ereal_def by (auto simp: divide_real_def)
   961 
   962 lemma ereal_divide_same[simp]:
   963   fixes x :: ereal shows "x / x = (if \<bar>x\<bar> = \<infinity> \<or> x = 0 then 0 else 1)"
   964   by (cases x)
   965      (simp_all add: divide_real_def divide_ereal_def one_ereal_def)
   966 
   967 lemma ereal_inv_inv[simp]:
   968   fixes x :: ereal shows "inverse (inverse x) = (if x \<noteq> -\<infinity> then x else \<infinity>)"
   969   by (cases x) auto
   970 
   971 lemma ereal_inverse_minus[simp]:
   972   fixes x :: ereal shows "inverse (- x) = (if x = 0 then \<infinity> else -inverse x)"
   973   by (cases x) simp_all
   974 
   975 lemma ereal_uminus_divide[simp]:
   976   fixes x y :: ereal shows "- x / y = - (x / y)"
   977   unfolding divide_ereal_def by simp
   978 
   979 lemma ereal_divide_Infty[simp]:
   980   fixes x :: ereal shows "x / \<infinity> = 0" "x / -\<infinity> = 0"
   981   unfolding divide_ereal_def by simp_all
   982 
   983 lemma ereal_divide_one[simp]:
   984   "x / 1 = (x::ereal)"
   985   unfolding divide_ereal_def by simp
   986 
   987 lemma ereal_divide_ereal[simp]:
   988   "\<infinity> / ereal r = (if 0 \<le> r then \<infinity> else -\<infinity>)"
   989   unfolding divide_ereal_def by simp
   990 
   991 lemma zero_le_divide_ereal[simp]:
   992   fixes a :: ereal assumes "0 \<le> a" "0 \<le> b"
   993   shows "0 \<le> a / b"
   994   using assms by (cases rule: ereal2_cases[of a b]) (auto simp: zero_le_divide_iff)
   995 
   996 lemma ereal_le_divide_pos:
   997   fixes x y z :: ereal shows "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y \<le> z / x \<longleftrightarrow> x * y \<le> z"
   998   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
   999 
  1000 lemma ereal_divide_le_pos:
  1001   fixes x y z :: ereal shows "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> z / x \<le> y \<longleftrightarrow> z \<le> x * y"
  1002   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
  1003 
  1004 lemma ereal_le_divide_neg:
  1005   fixes x y z :: ereal shows "x < 0 \<Longrightarrow> x \<noteq> -\<infinity> \<Longrightarrow> y \<le> z / x \<longleftrightarrow> z \<le> x * y"
  1006   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
  1007 
  1008 lemma ereal_divide_le_neg:
  1009   fixes x y z :: ereal shows "x < 0 \<Longrightarrow> x \<noteq> -\<infinity> \<Longrightarrow> z / x \<le> y \<longleftrightarrow> x * y \<le> z"
  1010   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
  1011 
  1012 lemma ereal_inverse_antimono_strict:
  1013   fixes x y :: ereal
  1014   shows "0 \<le> x \<Longrightarrow> x < y \<Longrightarrow> inverse y < inverse x"
  1015   by (cases rule: ereal2_cases[of x y]) auto
  1016 
  1017 lemma ereal_inverse_antimono:
  1018   fixes x y :: ereal
  1019   shows "0 \<le> x \<Longrightarrow> x <= y \<Longrightarrow> inverse y <= inverse x"
  1020   by (cases rule: ereal2_cases[of x y]) auto
  1021 
  1022 lemma inverse_inverse_Pinfty_iff[simp]:
  1023   fixes x :: ereal shows "inverse x = \<infinity> \<longleftrightarrow> x = 0"
  1024   by (cases x) auto
  1025 
  1026 lemma ereal_inverse_eq_0:
  1027   fixes x :: ereal shows "inverse x = 0 \<longleftrightarrow> x = \<infinity> \<or> x = -\<infinity>"
  1028   by (cases x) auto
  1029 
  1030 lemma ereal_0_gt_inverse:
  1031   fixes x :: ereal shows "0 < inverse x \<longleftrightarrow> x \<noteq> \<infinity> \<and> 0 \<le> x"
  1032   by (cases x) auto
  1033 
  1034 lemma ereal_mult_less_right:
  1035   fixes a b c :: ereal
  1036   assumes "b * a < c * a" "0 < a" "a < \<infinity>"
  1037   shows "b < c"
  1038   using assms
  1039   by (cases rule: ereal3_cases[of a b c])
  1040      (auto split: split_if_asm simp: zero_less_mult_iff zero_le_mult_iff)
  1041 
  1042 lemma ereal_power_divide:
  1043   fixes x y :: ereal shows "y \<noteq> 0 \<Longrightarrow> (x / y) ^ n = x^n / y^n"
  1044   by (cases rule: ereal2_cases[of x y])
  1045      (auto simp: one_ereal_def zero_ereal_def power_divide not_le
  1046                  power_less_zero_eq zero_le_power_iff)
  1047 
  1048 lemma ereal_le_mult_one_interval:
  1049   fixes x y :: ereal
  1050   assumes y: "y \<noteq> -\<infinity>"
  1051   assumes z: "\<And>z. \<lbrakk> 0 < z ; z < 1 \<rbrakk> \<Longrightarrow> z * x \<le> y"
  1052   shows "x \<le> y"
  1053 proof (cases x)
  1054   case PInf with z[of "1 / 2"] show "x \<le> y" by (simp add: one_ereal_def)
  1055 next
  1056   case (real r) note r = this
  1057   show "x \<le> y"
  1058   proof (cases y)
  1059     case (real p) note p = this
  1060     have "r \<le> p"
  1061     proof (rule field_le_mult_one_interval)
  1062       fix z :: real assume "0 < z" and "z < 1"
  1063       with z[of "ereal z"]
  1064       show "z * r \<le> p" using p r by (auto simp: zero_le_mult_iff one_ereal_def)
  1065     qed
  1066     then show "x \<le> y" using p r by simp
  1067   qed (insert y, simp_all)
  1068 qed simp
  1069 
  1070 subsection "Complete lattice"
  1071 
  1072 instantiation ereal :: lattice
  1073 begin
  1074 definition [simp]: "sup x y = (max x y :: ereal)"
  1075 definition [simp]: "inf x y = (min x y :: ereal)"
  1076 instance proof qed simp_all
  1077 end
  1078 
  1079 instantiation ereal :: complete_lattice
  1080 begin
  1081 
  1082 definition "bot = (-\<infinity>::ereal)"
  1083 definition "top = (\<infinity>::ereal)"
  1084 
  1085 definition "Sup S = (LEAST z. \<forall>x\<in>S. x \<le> z :: ereal)"
  1086 definition "Inf S = (GREATEST z. \<forall>x\<in>S. z \<le> x :: ereal)"
  1087 
  1088 lemma ereal_complete_Sup:
  1089   fixes S :: "ereal set" assumes "S \<noteq> {}"
  1090   shows "\<exists>x. (\<forall>y\<in>S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>S. y \<le> z) \<longrightarrow> x \<le> z)"
  1091 proof cases
  1092   assume "\<exists>x. \<forall>a\<in>S. a \<le> ereal x"
  1093   then obtain y where y: "\<And>a. a\<in>S \<Longrightarrow> a \<le> ereal y" by auto
  1094   then have "\<infinity> \<notin> S" by force
  1095   show ?thesis
  1096   proof cases
  1097     assume "S = {-\<infinity>}"
  1098     then show ?thesis by (auto intro!: exI[of _ "-\<infinity>"])
  1099   next
  1100     assume "S \<noteq> {-\<infinity>}"
  1101     with `S \<noteq> {}` `\<infinity> \<notin> S` obtain x where "x \<in> S - {-\<infinity>}" "x \<noteq> \<infinity>" by auto
  1102     with y `\<infinity> \<notin> S` have "\<forall>z\<in>real ` (S - {-\<infinity>}). z \<le> y"
  1103       by (auto simp: real_of_ereal_ord_simps)
  1104     with reals_complete2[of "real ` (S - {-\<infinity>})"] `x \<in> S - {-\<infinity>}`
  1105     obtain s where s:
  1106        "\<forall>y\<in>S - {-\<infinity>}. real y \<le> s" "\<And>z. (\<forall>y\<in>S - {-\<infinity>}. real y \<le> z) \<Longrightarrow> s \<le> z"
  1107        by auto
  1108     show ?thesis
  1109     proof (safe intro!: exI[of _ "ereal s"])
  1110       fix z assume "z \<in> S" with `\<infinity> \<notin> S` show "z \<le> ereal s"
  1111       proof (cases z)
  1112         case (real r)
  1113         then show ?thesis
  1114           using s(1)[rule_format, of z] `z \<in> S` `z = ereal r` by auto
  1115       qed auto
  1116     next
  1117       fix z assume *: "\<forall>y\<in>S. y \<le> z"
  1118       with `S \<noteq> {-\<infinity>}` `S \<noteq> {}` show "ereal s \<le> z"
  1119       proof (cases z)
  1120         case (real u)
  1121         with * have "s \<le> u"
  1122           by (intro s(2)[of u]) (auto simp: real_of_ereal_ord_simps)
  1123         then show ?thesis using real by simp
  1124       qed auto
  1125     qed
  1126   qed
  1127 next
  1128   assume *: "\<not> (\<exists>x. \<forall>a\<in>S. a \<le> ereal x)"
  1129   show ?thesis
  1130   proof (safe intro!: exI[of _ \<infinity>])
  1131     fix y assume **: "\<forall>z\<in>S. z \<le> y"
  1132     with * show "\<infinity> \<le> y"
  1133     proof (cases y)
  1134       case MInf with * ** show ?thesis by (force simp: not_le)
  1135     qed auto
  1136   qed simp
  1137 qed
  1138 
  1139 lemma ereal_complete_Inf:
  1140   fixes S :: "ereal set" assumes "S ~= {}"
  1141   shows "EX x. (ALL y:S. x <= y) & (ALL z. (ALL y:S. z <= y) --> z <= x)"
  1142 proof-
  1143 def S1 == "uminus ` S"
  1144 hence "S1 ~= {}" using assms by auto
  1145 from this obtain x where x_def: "(ALL y:S1. y <= x) & (ALL z. (ALL y:S1. y <= z) --> x <= z)"
  1146    using ereal_complete_Sup[of S1] by auto
  1147 { fix z assume "ALL y:S. z <= y"
  1148   hence "ALL y:S1. y <= -z" unfolding S1_def by auto
  1149   hence "x <= -z" using x_def by auto
  1150   hence "z <= -x"
  1151     apply (subst ereal_uminus_uminus[symmetric])
  1152     unfolding ereal_minus_le_minus . }
  1153 moreover have "(ALL y:S. -x <= y)"
  1154    using x_def unfolding S1_def
  1155    apply simp
  1156    apply (subst (3) ereal_uminus_uminus[symmetric])
  1157    unfolding ereal_minus_le_minus by simp
  1158 ultimately show ?thesis by auto
  1159 qed
  1160 
  1161 lemma ereal_complete_uminus_eq:
  1162   fixes S :: "ereal set"
  1163   shows "(\<forall>y\<in>uminus`S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>uminus`S. y \<le> z) \<longrightarrow> x \<le> z)
  1164      \<longleftrightarrow> (\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> -x)"
  1165   by simp (metis ereal_minus_le_minus ereal_uminus_uminus)
  1166 
  1167 lemma ereal_Sup_uminus_image_eq:
  1168   fixes S :: "ereal set"
  1169   shows "Sup (uminus ` S) = - Inf S"
  1170 proof cases
  1171   assume "S = {}"
  1172   moreover have "(THE x. All (op \<le> x)) = (-\<infinity>::ereal)"
  1173     by (rule the_equality) (auto intro!: ereal_bot)
  1174   moreover have "(SOME x. \<forall>y. y \<le> x) = (\<infinity>::ereal)"
  1175     by (rule some_equality) (auto intro!: ereal_top)
  1176   ultimately show ?thesis unfolding Inf_ereal_def Sup_ereal_def
  1177     Least_def Greatest_def GreatestM_def by simp
  1178 next
  1179   assume "S \<noteq> {}"
  1180   with ereal_complete_Sup[of "uminus`S"]
  1181   obtain x where x: "(\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> -x)"
  1182     unfolding ereal_complete_uminus_eq by auto
  1183   show "Sup (uminus ` S) = - Inf S"
  1184     unfolding Inf_ereal_def Greatest_def GreatestM_def
  1185   proof (intro someI2[of _ _ "\<lambda>x. Sup (uminus`S) = - x"])
  1186     show "(\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>y. (\<forall>z\<in>S. y \<le> z) \<longrightarrow> y \<le> -x)"
  1187       using x .
  1188     fix x' assume "(\<forall>y\<in>S. x' \<le> y) \<and> (\<forall>y. (\<forall>z\<in>S. y \<le> z) \<longrightarrow> y \<le> x')"
  1189     then have "(\<forall>y\<in>uminus`S. y \<le> - x') \<and> (\<forall>y. (\<forall>z\<in>uminus`S. z \<le> y) \<longrightarrow> - x' \<le> y)"
  1190       unfolding ereal_complete_uminus_eq by simp
  1191     then show "Sup (uminus ` S) = -x'"
  1192       unfolding Sup_ereal_def ereal_uminus_eq_iff
  1193       by (intro Least_equality) auto
  1194   qed
  1195 qed
  1196 
  1197 instance
  1198 proof
  1199   { fix x :: ereal and A
  1200     show "bot <= x" by (cases x) (simp_all add: bot_ereal_def)
  1201     show "x <= top" by (simp add: top_ereal_def) }
  1202 
  1203   { fix x :: ereal and A assume "x : A"
  1204     with ereal_complete_Sup[of A]
  1205     obtain s where s: "\<forall>y\<in>A. y <= s" "\<forall>z. (\<forall>y\<in>A. y <= z) \<longrightarrow> s <= z" by auto
  1206     hence "x <= s" using `x : A` by auto
  1207     also have "... = Sup A" using s unfolding Sup_ereal_def
  1208       by (auto intro!: Least_equality[symmetric])
  1209     finally show "x <= Sup A" . }
  1210   note le_Sup = this
  1211 
  1212   { fix x :: ereal and A assume *: "!!z. (z : A ==> z <= x)"
  1213     show "Sup A <= x"
  1214     proof (cases "A = {}")
  1215       case True
  1216       hence "Sup A = -\<infinity>" unfolding Sup_ereal_def
  1217         by (auto intro!: Least_equality)
  1218       thus "Sup A <= x" by simp
  1219     next
  1220       case False
  1221       with ereal_complete_Sup[of A]
  1222       obtain s where s: "\<forall>y\<in>A. y <= s" "\<forall>z. (\<forall>y\<in>A. y <= z) \<longrightarrow> s <= z" by auto
  1223       hence "Sup A = s"
  1224         unfolding Sup_ereal_def by (auto intro!: Least_equality)
  1225       also have "s <= x" using * s by auto
  1226       finally show "Sup A <= x" .
  1227     qed }
  1228   note Sup_le = this
  1229 
  1230   { fix x :: ereal and A assume "x \<in> A"
  1231     with le_Sup[of "-x" "uminus`A"] show "Inf A \<le> x"
  1232       unfolding ereal_Sup_uminus_image_eq by simp }
  1233 
  1234   { fix x :: ereal and A assume *: "!!z. (z : A ==> x <= z)"
  1235     with Sup_le[of "uminus`A" "-x"] show "x \<le> Inf A"
  1236       unfolding ereal_Sup_uminus_image_eq by force }
  1237 qed
  1238 end
  1239 
  1240 lemma ereal_SUPR_uminus:
  1241   fixes f :: "'a => ereal"
  1242   shows "(SUP i : R. -(f i)) = -(INF i : R. f i)"
  1243   unfolding SUPR_def INFI_def
  1244   using ereal_Sup_uminus_image_eq[of "f`R"]
  1245   by (simp add: image_image)
  1246 
  1247 lemma ereal_INFI_uminus:
  1248   fixes f :: "'a => ereal"
  1249   shows "(INF i : R. -(f i)) = -(SUP i : R. f i)"
  1250   using ereal_SUPR_uminus[of _ "\<lambda>x. - f x"] by simp
  1251 
  1252 lemma ereal_Inf_uminus_image_eq: "Inf (uminus ` S) = - Sup (S::ereal set)"
  1253   using ereal_Sup_uminus_image_eq[of "uminus ` S"] by (simp add: image_image)
  1254 
  1255 lemma ereal_inj_on_uminus[intro, simp]: "inj_on uminus (A :: ereal set)"
  1256   by (auto intro!: inj_onI)
  1257 
  1258 lemma ereal_image_uminus_shift:
  1259   fixes X Y :: "ereal set" shows "uminus ` X = Y \<longleftrightarrow> X = uminus ` Y"
  1260 proof
  1261   assume "uminus ` X = Y"
  1262   then have "uminus ` uminus ` X = uminus ` Y"
  1263     by (simp add: inj_image_eq_iff)
  1264   then show "X = uminus ` Y" by (simp add: image_image)
  1265 qed (simp add: image_image)
  1266 
  1267 lemma Inf_ereal_iff:
  1268   fixes z :: ereal
  1269   shows "(!!x. x:X ==> z <= x) ==> (EX x:X. x<y) <-> Inf X < y"
  1270   by (metis complete_lattice_class.Inf_greatest complete_lattice_class.Inf_lower less_le_not_le linear
  1271             order_less_le_trans)
  1272 
  1273 lemma Sup_eq_MInfty:
  1274   fixes S :: "ereal set" shows "Sup S = -\<infinity> \<longleftrightarrow> S = {} \<or> S = {-\<infinity>}"
  1275 proof
  1276   assume a: "Sup S = -\<infinity>"
  1277   with complete_lattice_class.Sup_upper[of _ S]
  1278   show "S={} \<or> S={-\<infinity>}" by auto
  1279 next
  1280   assume "S={} \<or> S={-\<infinity>}" then show "Sup S = -\<infinity>"
  1281     unfolding Sup_ereal_def by (auto intro!: Least_equality)
  1282 qed
  1283 
  1284 lemma Inf_eq_PInfty:
  1285   fixes S :: "ereal set" shows "Inf S = \<infinity> \<longleftrightarrow> S = {} \<or> S = {\<infinity>}"
  1286   using Sup_eq_MInfty[of "uminus`S"]
  1287   unfolding ereal_Sup_uminus_image_eq ereal_image_uminus_shift by simp
  1288 
  1289 lemma Inf_eq_MInfty: 
  1290   fixes S :: "ereal set" shows "-\<infinity> \<in> S \<Longrightarrow> Inf S = -\<infinity>"
  1291   unfolding Inf_ereal_def
  1292   by (auto intro!: Greatest_equality)
  1293 
  1294 lemma Sup_eq_PInfty:
  1295   fixes S :: "ereal set" shows "\<infinity> \<in> S \<Longrightarrow> Sup S = \<infinity>"
  1296   unfolding Sup_ereal_def
  1297   by (auto intro!: Least_equality)
  1298 
  1299 lemma ereal_SUPI:
  1300   fixes x :: ereal
  1301   assumes "!!i. i : A ==> f i <= x"
  1302   assumes "!!y. (!!i. i : A ==> f i <= y) ==> x <= y"
  1303   shows "(SUP i:A. f i) = x"
  1304   unfolding SUPR_def Sup_ereal_def
  1305   using assms by (auto intro!: Least_equality)
  1306 
  1307 lemma ereal_INFI:
  1308   fixes x :: ereal
  1309   assumes "!!i. i : A ==> f i >= x"
  1310   assumes "!!y. (!!i. i : A ==> f i >= y) ==> x >= y"
  1311   shows "(INF i:A. f i) = x"
  1312   unfolding INFI_def Inf_ereal_def
  1313   using assms by (auto intro!: Greatest_equality)
  1314 
  1315 lemma Sup_ereal_close:
  1316   fixes e :: ereal
  1317   assumes "0 < e" and S: "\<bar>Sup S\<bar> \<noteq> \<infinity>" "S \<noteq> {}"
  1318   shows "\<exists>x\<in>S. Sup S - e < x"
  1319   using assms by (cases e) (auto intro!: less_Sup_iff[THEN iffD1])
  1320 
  1321 lemma Inf_ereal_close:
  1322   fixes e :: ereal assumes "\<bar>Inf X\<bar> \<noteq> \<infinity>" "0 < e"
  1323   shows "\<exists>x\<in>X. x < Inf X + e"
  1324 proof (rule Inf_less_iff[THEN iffD1])
  1325   show "Inf X < Inf X + e" using assms
  1326     by (cases e) auto
  1327 qed
  1328 
  1329 lemma Sup_eq_top_iff:
  1330   fixes A :: "'a::{complete_lattice, linorder} set"
  1331   shows "Sup A = top \<longleftrightarrow> (\<forall>x<top. \<exists>i\<in>A. x < i)"
  1332 proof
  1333   assume *: "Sup A = top"
  1334   show "(\<forall>x<top. \<exists>i\<in>A. x < i)" unfolding *[symmetric]
  1335   proof (intro allI impI)
  1336     fix x assume "x < Sup A" then show "\<exists>i\<in>A. x < i"
  1337       unfolding less_Sup_iff by auto
  1338   qed
  1339 next
  1340   assume *: "\<forall>x<top. \<exists>i\<in>A. x < i"
  1341   show "Sup A = top"
  1342   proof (rule ccontr)
  1343     assume "Sup A \<noteq> top"
  1344     with top_greatest[of "Sup A"]
  1345     have "Sup A < top" unfolding le_less by auto
  1346     then have "Sup A < Sup A"
  1347       using * unfolding less_Sup_iff by auto
  1348     then show False by auto
  1349   qed
  1350 qed
  1351 
  1352 lemma SUP_eq_top_iff:
  1353   fixes f :: "'a \<Rightarrow> 'b::{complete_lattice, linorder}"
  1354   shows "(SUP i:A. f i) = top \<longleftrightarrow> (\<forall>x<top. \<exists>i\<in>A. x < f i)"
  1355   unfolding SUPR_def Sup_eq_top_iff by auto
  1356 
  1357 lemma SUP_nat_Infty: "(SUP i::nat. ereal (real i)) = \<infinity>"
  1358 proof -
  1359   { fix x ::ereal assume "x \<noteq> \<infinity>"
  1360     then have "\<exists>k::nat. x < ereal (real k)"
  1361     proof (cases x)
  1362       case MInf then show ?thesis by (intro exI[of _ 0]) auto
  1363     next
  1364       case (real r)
  1365       moreover obtain k :: nat where "r < real k"
  1366         using ex_less_of_nat by (auto simp: real_eq_of_nat)
  1367       ultimately show ?thesis by auto
  1368     qed simp }
  1369   then show ?thesis
  1370     using SUP_eq_top_iff[of UNIV "\<lambda>n::nat. ereal (real n)"]
  1371     by (auto simp: top_ereal_def)
  1372 qed
  1373 
  1374 lemma ereal_le_Sup:
  1375   fixes x :: ereal
  1376   shows "(x <= (SUP i:A. f i)) <-> (ALL y. y < x --> (EX i. i : A & y <= f i))"
  1377 (is "?lhs <-> ?rhs")
  1378 proof-
  1379 { assume "?rhs"
  1380   { assume "~(x <= (SUP i:A. f i))" hence "(SUP i:A. f i)<x" by (simp add: not_le)
  1381     from this obtain y where y_def: "(SUP i:A. f i)<y & y<x" using ereal_dense by auto
  1382     from this obtain i where "i : A & y <= f i" using `?rhs` by auto
  1383     hence "y <= (SUP i:A. f i)" using le_SUPI[of i A f] by auto
  1384     hence False using y_def by auto
  1385   } hence "?lhs" by auto
  1386 }
  1387 moreover
  1388 { assume "?lhs" hence "?rhs"
  1389   by (metis Collect_def Collect_mem_eq SUP_leI assms atLeastatMost_empty atLeastatMost_empty_iff
  1390       inf_sup_ord(4) linorder_le_cases sup_absorb1 xt1(8))
  1391 } ultimately show ?thesis by auto
  1392 qed
  1393 
  1394 lemma ereal_Inf_le:
  1395   fixes x :: ereal
  1396   shows "((INF i:A. f i) <= x) <-> (ALL y. x < y --> (EX i. i : A & f i <= y))"
  1397 (is "?lhs <-> ?rhs")
  1398 proof-
  1399 { assume "?rhs"
  1400   { assume "~((INF i:A. f i) <= x)" hence "x < (INF i:A. f i)" by (simp add: not_le)
  1401     from this obtain y where y_def: "x<y & y<(INF i:A. f i)" using ereal_dense by auto
  1402     from this obtain i where "i : A & f i <= y" using `?rhs` by auto
  1403     hence "(INF i:A. f i) <= y" using INF_leI[of i A f] by auto
  1404     hence False using y_def by auto
  1405   } hence "?lhs" by auto
  1406 }
  1407 moreover
  1408 { assume "?lhs" hence "?rhs"
  1409   by (metis Collect_def Collect_mem_eq le_INFI assms atLeastatMost_empty atLeastatMost_empty_iff
  1410       inf_sup_ord(4) linorder_le_cases sup_absorb1 xt1(8))
  1411 } ultimately show ?thesis by auto
  1412 qed
  1413 
  1414 lemma Inf_less:
  1415   fixes x :: ereal
  1416   assumes "(INF i:A. f i) < x"
  1417   shows "EX i. i : A & f i <= x"
  1418 proof(rule ccontr)
  1419   assume "~ (EX i. i : A & f i <= x)"
  1420   hence "ALL i:A. f i > x" by auto
  1421   hence "(INF i:A. f i) >= x" apply (subst le_INFI) by auto
  1422   thus False using assms by auto
  1423 qed
  1424 
  1425 lemma same_INF:
  1426   assumes "ALL e:A. f e = g e"
  1427   shows "(INF e:A. f e) = (INF e:A. g e)"
  1428 proof-
  1429 have "f ` A = g ` A" unfolding image_def using assms by auto
  1430 thus ?thesis unfolding INFI_def by auto
  1431 qed
  1432 
  1433 lemma same_SUP:
  1434   assumes "ALL e:A. f e = g e"
  1435   shows "(SUP e:A. f e) = (SUP e:A. g e)"
  1436 proof-
  1437 have "f ` A = g ` A" unfolding image_def using assms by auto
  1438 thus ?thesis unfolding SUPR_def by auto
  1439 qed
  1440 
  1441 lemma SUPR_eq:
  1442   assumes "\<forall>i\<in>A. \<exists>j\<in>B. f i \<le> g j"
  1443   assumes "\<forall>j\<in>B. \<exists>i\<in>A. g j \<le> f i"
  1444   shows "(SUP i:A. f i) = (SUP j:B. g j)"
  1445 proof (intro antisym)
  1446   show "(SUP i:A. f i) \<le> (SUP j:B. g j)"
  1447     using assms by (metis SUP_leI le_SUPI2)
  1448   show "(SUP i:B. g i) \<le> (SUP j:A. f j)"
  1449     using assms by (metis SUP_leI le_SUPI2)
  1450 qed
  1451 
  1452 lemma SUP_ereal_le_addI:
  1453   fixes f :: "'i \<Rightarrow> ereal"
  1454   assumes "\<And>i. f i + y \<le> z" and "y \<noteq> -\<infinity>"
  1455   shows "SUPR UNIV f + y \<le> z"
  1456 proof (cases y)
  1457   case (real r)
  1458   then have "\<And>i. f i \<le> z - y" using assms by (simp add: ereal_le_minus_iff)
  1459   then have "SUPR UNIV f \<le> z - y" by (rule SUP_leI)
  1460   then show ?thesis using real by (simp add: ereal_le_minus_iff)
  1461 qed (insert assms, auto)
  1462 
  1463 lemma SUPR_ereal_add:
  1464   fixes f g :: "nat \<Rightarrow> ereal"
  1465   assumes "incseq f" "incseq g" and pos: "\<And>i. f i \<noteq> -\<infinity>" "\<And>i. g i \<noteq> -\<infinity>"
  1466   shows "(SUP i. f i + g i) = SUPR UNIV f + SUPR UNIV g"
  1467 proof (rule ereal_SUPI)
  1468   fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> f i + g i \<le> y"
  1469   have f: "SUPR UNIV f \<noteq> -\<infinity>" using pos
  1470     unfolding SUPR_def Sup_eq_MInfty by (auto dest: image_eqD)
  1471   { fix j
  1472     { fix i
  1473       have "f i + g j \<le> f i + g (max i j)"
  1474         using `incseq g`[THEN incseqD] by (rule add_left_mono) auto
  1475       also have "\<dots> \<le> f (max i j) + g (max i j)"
  1476         using `incseq f`[THEN incseqD] by (rule add_right_mono) auto
  1477       also have "\<dots> \<le> y" using * by auto
  1478       finally have "f i + g j \<le> y" . }
  1479     then have "SUPR UNIV f + g j \<le> y"
  1480       using assms(4)[of j] by (intro SUP_ereal_le_addI) auto
  1481     then have "g j + SUPR UNIV f \<le> y" by (simp add: ac_simps) }
  1482   then have "SUPR UNIV g + SUPR UNIV f \<le> y"
  1483     using f by (rule SUP_ereal_le_addI)
  1484   then show "SUPR UNIV f + SUPR UNIV g \<le> y" by (simp add: ac_simps)
  1485 qed (auto intro!: add_mono le_SUPI)
  1486 
  1487 lemma SUPR_ereal_add_pos:
  1488   fixes f g :: "nat \<Rightarrow> ereal"
  1489   assumes inc: "incseq f" "incseq g" and pos: "\<And>i. 0 \<le> f i" "\<And>i. 0 \<le> g i"
  1490   shows "(SUP i. f i + g i) = SUPR UNIV f + SUPR UNIV g"
  1491 proof (intro SUPR_ereal_add inc)
  1492   fix i show "f i \<noteq> -\<infinity>" "g i \<noteq> -\<infinity>" using pos[of i] by auto
  1493 qed
  1494 
  1495 lemma SUPR_ereal_setsum:
  1496   fixes f g :: "'a \<Rightarrow> nat \<Rightarrow> ereal"
  1497   assumes "\<And>n. n \<in> A \<Longrightarrow> incseq (f n)" and pos: "\<And>n i. n \<in> A \<Longrightarrow> 0 \<le> f n i"
  1498   shows "(SUP i. \<Sum>n\<in>A. f n i) = (\<Sum>n\<in>A. SUPR UNIV (f n))"
  1499 proof cases
  1500   assume "finite A" then show ?thesis using assms
  1501     by induct (auto simp: incseq_setsumI2 setsum_nonneg SUPR_ereal_add_pos)
  1502 qed simp
  1503 
  1504 lemma SUPR_ereal_cmult:
  1505   fixes f :: "nat \<Rightarrow> ereal" assumes "\<And>i. 0 \<le> f i" "0 \<le> c"
  1506   shows "(SUP i. c * f i) = c * SUPR UNIV f"
  1507 proof (rule ereal_SUPI)
  1508   fix i have "f i \<le> SUPR UNIV f" by (rule le_SUPI) auto
  1509   then show "c * f i \<le> c * SUPR UNIV f"
  1510     using `0 \<le> c` by (rule ereal_mult_left_mono)
  1511 next
  1512   fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> c * f i \<le> y"
  1513   show "c * SUPR UNIV f \<le> y"
  1514   proof cases
  1515     assume c: "0 < c \<and> c \<noteq> \<infinity>"
  1516     with * have "SUPR UNIV f \<le> y / c"
  1517       by (intro SUP_leI) (auto simp: ereal_le_divide_pos)
  1518     with c show ?thesis
  1519       by (auto simp: ereal_le_divide_pos)
  1520   next
  1521     { assume "c = \<infinity>" have ?thesis
  1522       proof cases
  1523         assume "\<forall>i. f i = 0"
  1524         moreover then have "range f = {0}" by auto
  1525         ultimately show "c * SUPR UNIV f \<le> y" using * by (auto simp: SUPR_def)
  1526       next
  1527         assume "\<not> (\<forall>i. f i = 0)"
  1528         then obtain i where "f i \<noteq> 0" by auto
  1529         with *[of i] `c = \<infinity>` `0 \<le> f i` show ?thesis by (auto split: split_if_asm)
  1530       qed }
  1531     moreover assume "\<not> (0 < c \<and> c \<noteq> \<infinity>)"
  1532     ultimately show ?thesis using * `0 \<le> c` by auto
  1533   qed
  1534 qed
  1535 
  1536 lemma SUP_PInfty:
  1537   fixes f :: "'a \<Rightarrow> ereal"
  1538   assumes "\<And>n::nat. \<exists>i\<in>A. ereal (real n) \<le> f i"
  1539   shows "(SUP i:A. f i) = \<infinity>"
  1540   unfolding SUPR_def Sup_eq_top_iff[where 'a=ereal, unfolded top_ereal_def]
  1541   apply simp
  1542 proof safe
  1543   fix x :: ereal assume "x \<noteq> \<infinity>"
  1544   show "\<exists>i\<in>A. x < f i"
  1545   proof (cases x)
  1546     case PInf with `x \<noteq> \<infinity>` show ?thesis by simp
  1547   next
  1548     case MInf with assms[of "0"] show ?thesis by force
  1549   next
  1550     case (real r)
  1551     with less_PInf_Ex_of_nat[of x] obtain n :: nat where "x < ereal (real n)" by auto
  1552     moreover from assms[of n] guess i ..
  1553     ultimately show ?thesis
  1554       by (auto intro!: bexI[of _ i])
  1555   qed
  1556 qed
  1557 
  1558 lemma Sup_countable_SUPR:
  1559   assumes "A \<noteq> {}"
  1560   shows "\<exists>f::nat \<Rightarrow> ereal. range f \<subseteq> A \<and> Sup A = SUPR UNIV f"
  1561 proof (cases "Sup A")
  1562   case (real r)
  1563   have "\<forall>n::nat. \<exists>x. x \<in> A \<and> Sup A < x + 1 / ereal (real n)"
  1564   proof
  1565     fix n ::nat have "\<exists>x\<in>A. Sup A - 1 / ereal (real n) < x"
  1566       using assms real by (intro Sup_ereal_close) (auto simp: one_ereal_def)
  1567     then guess x ..
  1568     then show "\<exists>x. x \<in> A \<and> Sup A < x + 1 / ereal (real n)"
  1569       by (auto intro!: exI[of _ x] simp: ereal_minus_less_iff)
  1570   qed
  1571   from choice[OF this] guess f .. note f = this
  1572   have "SUPR UNIV f = Sup A"
  1573   proof (rule ereal_SUPI)
  1574     fix i show "f i \<le> Sup A" using f
  1575       by (auto intro!: complete_lattice_class.Sup_upper)
  1576   next
  1577     fix y assume bound: "\<And>i. i \<in> UNIV \<Longrightarrow> f i \<le> y"
  1578     show "Sup A \<le> y"
  1579     proof (rule ereal_le_epsilon, intro allI impI)
  1580       fix e :: ereal assume "0 < e"
  1581       show "Sup A \<le> y + e"
  1582       proof (cases e)
  1583         case (real r)
  1584         hence "0 < r" using `0 < e` by auto
  1585         then obtain n ::nat where *: "1 / real n < r" "0 < n"
  1586           using ex_inverse_of_nat_less by (auto simp: real_eq_of_nat inverse_eq_divide)
  1587         have "Sup A \<le> f n + 1 / ereal (real n)" using f[THEN spec, of n] by auto
  1588         also have "1 / ereal (real n) \<le> e" using real * by (auto simp: one_ereal_def )
  1589         with bound have "f n + 1 / ereal (real n) \<le> y + e" by (rule add_mono) simp
  1590         finally show "Sup A \<le> y + e" .
  1591       qed (insert `0 < e`, auto)
  1592     qed
  1593   qed
  1594   with f show ?thesis by (auto intro!: exI[of _ f])
  1595 next
  1596   case PInf
  1597   from `A \<noteq> {}` obtain x where "x \<in> A" by auto
  1598   show ?thesis
  1599   proof cases
  1600     assume "\<infinity> \<in> A"
  1601     moreover then have "\<infinity> \<le> Sup A" by (intro complete_lattice_class.Sup_upper)
  1602     ultimately show ?thesis by (auto intro!: exI[of _ "\<lambda>x. \<infinity>"])
  1603   next
  1604     assume "\<infinity> \<notin> A"
  1605     have "\<exists>x\<in>A. 0 \<le> x"
  1606       by (metis Infty_neq_0 PInf complete_lattice_class.Sup_least ereal_infty_less_eq2 linorder_linear)
  1607     then obtain x where "x \<in> A" "0 \<le> x" by auto
  1608     have "\<forall>n::nat. \<exists>f. f \<in> A \<and> x + ereal (real n) \<le> f"
  1609     proof (rule ccontr)
  1610       assume "\<not> ?thesis"
  1611       then have "\<exists>n::nat. Sup A \<le> x + ereal (real n)"
  1612         by (simp add: Sup_le_iff not_le less_imp_le Ball_def) (metis less_imp_le)
  1613       then show False using `x \<in> A` `\<infinity> \<notin> A` PInf
  1614         by(cases x) auto
  1615     qed
  1616     from choice[OF this] guess f .. note f = this
  1617     have "SUPR UNIV f = \<infinity>"
  1618     proof (rule SUP_PInfty)
  1619       fix n :: nat show "\<exists>i\<in>UNIV. ereal (real n) \<le> f i"
  1620         using f[THEN spec, of n] `0 \<le> x`
  1621         by (cases rule: ereal2_cases[of "f n" x]) (auto intro!: exI[of _ n])
  1622     qed
  1623     then show ?thesis using f PInf by (auto intro!: exI[of _ f])
  1624   qed
  1625 next
  1626   case MInf
  1627   with `A \<noteq> {}` have "A = {-\<infinity>}" by (auto simp: Sup_eq_MInfty)
  1628   then show ?thesis using MInf by (auto intro!: exI[of _ "\<lambda>x. -\<infinity>"])
  1629 qed
  1630 
  1631 lemma SUPR_countable_SUPR:
  1632   "A \<noteq> {} \<Longrightarrow> \<exists>f::nat \<Rightarrow> ereal. range f \<subseteq> g`A \<and> SUPR A g = SUPR UNIV f"
  1633   using Sup_countable_SUPR[of "g`A"] by (auto simp: SUPR_def)
  1634 
  1635 lemma Sup_ereal_cadd:
  1636   fixes A :: "ereal set" assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
  1637   shows "Sup ((\<lambda>x. a + x) ` A) = a + Sup A"
  1638 proof (rule antisym)
  1639   have *: "\<And>a::ereal. \<And>A. Sup ((\<lambda>x. a + x) ` A) \<le> a + Sup A"
  1640     by (auto intro!: add_mono complete_lattice_class.Sup_least complete_lattice_class.Sup_upper)
  1641   then show "Sup ((\<lambda>x. a + x) ` A) \<le> a + Sup A" .
  1642   show "a + Sup A \<le> Sup ((\<lambda>x. a + x) ` A)"
  1643   proof (cases a)
  1644     case PInf with `A \<noteq> {}` show ?thesis by (auto simp: image_constant)
  1645   next
  1646     case (real r)
  1647     then have **: "op + (- a) ` op + a ` A = A"
  1648       by (auto simp: image_iff ac_simps zero_ereal_def[symmetric])
  1649     from *[of "-a" "(\<lambda>x. a + x) ` A"] real show ?thesis unfolding **
  1650       by (cases rule: ereal2_cases[of "Sup A" "Sup (op + a ` A)"]) auto
  1651   qed (insert `a \<noteq> -\<infinity>`, auto)
  1652 qed
  1653 
  1654 lemma Sup_ereal_cminus:
  1655   fixes A :: "ereal set" assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
  1656   shows "Sup ((\<lambda>x. a - x) ` A) = a - Inf A"
  1657   using Sup_ereal_cadd[of "uminus ` A" a] assms
  1658   by (simp add: comp_def image_image minus_ereal_def
  1659                  ereal_Sup_uminus_image_eq)
  1660 
  1661 lemma SUPR_ereal_cminus:
  1662   fixes f :: "'i \<Rightarrow> ereal"
  1663   fixes A assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
  1664   shows "(SUP x:A. a - f x) = a - (INF x:A. f x)"
  1665   using Sup_ereal_cminus[of "f`A" a] assms
  1666   unfolding SUPR_def INFI_def image_image by auto
  1667 
  1668 lemma Inf_ereal_cminus:
  1669   fixes A :: "ereal set" assumes "A \<noteq> {}" and "\<bar>a\<bar> \<noteq> \<infinity>"
  1670   shows "Inf ((\<lambda>x. a - x) ` A) = a - Sup A"
  1671 proof -
  1672   { fix x have "-a - -x = -(a - x)" using assms by (cases x) auto }
  1673   moreover then have "(\<lambda>x. -a - x)`uminus`A = uminus ` (\<lambda>x. a - x) ` A"
  1674     by (auto simp: image_image)
  1675   ultimately show ?thesis
  1676     using Sup_ereal_cminus[of "uminus ` A" "-a"] assms
  1677     by (auto simp add: ereal_Sup_uminus_image_eq ereal_Inf_uminus_image_eq)
  1678 qed
  1679 
  1680 lemma INFI_ereal_cminus:
  1681   fixes a :: ereal assumes "A \<noteq> {}" and "\<bar>a\<bar> \<noteq> \<infinity>"
  1682   shows "(INF x:A. a - f x) = a - (SUP x:A. f x)"
  1683   using Inf_ereal_cminus[of "f`A" a] assms
  1684   unfolding SUPR_def INFI_def image_image
  1685   by auto
  1686 
  1687 lemma uminus_ereal_add_uminus_uminus:
  1688   fixes a b :: ereal shows "a \<noteq> \<infinity> \<Longrightarrow> b \<noteq> \<infinity> \<Longrightarrow> - (- a + - b) = a + b"
  1689   by (cases rule: ereal2_cases[of a b]) auto
  1690 
  1691 lemma INFI_ereal_add:
  1692   fixes f :: "nat \<Rightarrow> ereal"
  1693   assumes "decseq f" "decseq g" and fin: "\<And>i. f i \<noteq> \<infinity>" "\<And>i. g i \<noteq> \<infinity>"
  1694   shows "(INF i. f i + g i) = INFI UNIV f + INFI UNIV g"
  1695 proof -
  1696   have INF_less: "(INF i. f i) < \<infinity>" "(INF i. g i) < \<infinity>"
  1697     using assms unfolding INF_less_iff by auto
  1698   { fix i from fin[of i] have "- ((- f i) + (- g i)) = f i + g i"
  1699       by (rule uminus_ereal_add_uminus_uminus) }
  1700   then have "(INF i. f i + g i) = (INF i. - ((- f i) + (- g i)))"
  1701     by simp
  1702   also have "\<dots> = INFI UNIV f + INFI UNIV g"
  1703     unfolding ereal_INFI_uminus
  1704     using assms INF_less
  1705     by (subst SUPR_ereal_add)
  1706        (auto simp: ereal_SUPR_uminus intro!: uminus_ereal_add_uminus_uminus)
  1707   finally show ?thesis .
  1708 qed
  1709 
  1710 subsection "Limits on @{typ ereal}"
  1711 
  1712 subsubsection "Topological space"
  1713 
  1714 instantiation ereal :: topological_space
  1715 begin
  1716 
  1717 definition "open A \<longleftrightarrow> open (ereal -` A)
  1718        \<and> (\<infinity> \<in> A \<longrightarrow> (\<exists>x. {ereal x <..} \<subseteq> A))
  1719        \<and> (-\<infinity> \<in> A \<longrightarrow> (\<exists>x. {..<ereal x} \<subseteq> A))"
  1720 
  1721 lemma open_PInfty: "open A \<Longrightarrow> \<infinity> \<in> A \<Longrightarrow> (\<exists>x. {ereal x<..} \<subseteq> A)"
  1722   unfolding open_ereal_def by auto
  1723 
  1724 lemma open_MInfty: "open A \<Longrightarrow> -\<infinity> \<in> A \<Longrightarrow> (\<exists>x. {..<ereal x} \<subseteq> A)"
  1725   unfolding open_ereal_def by auto
  1726 
  1727 lemma open_PInfty2: assumes "open A" "\<infinity> \<in> A" obtains x where "{ereal x<..} \<subseteq> A"
  1728   using open_PInfty[OF assms] by auto
  1729 
  1730 lemma open_MInfty2: assumes "open A" "-\<infinity> \<in> A" obtains x where "{..<ereal x} \<subseteq> A"
  1731   using open_MInfty[OF assms] by auto
  1732 
  1733 lemma ereal_openE: assumes "open A" obtains x y where
  1734   "open (ereal -` A)"
  1735   "\<infinity> \<in> A \<Longrightarrow> {ereal x<..} \<subseteq> A"
  1736   "-\<infinity> \<in> A \<Longrightarrow> {..<ereal y} \<subseteq> A"
  1737   using assms open_ereal_def by auto
  1738 
  1739 instance
  1740 proof
  1741   let ?U = "UNIV::ereal set"
  1742   show "open ?U" unfolding open_ereal_def
  1743     by (auto intro!: exI[of _ 0])
  1744 next
  1745   fix S T::"ereal set" assume "open S" and "open T"
  1746   from `open S`[THEN ereal_openE] guess xS yS .
  1747   moreover from `open T`[THEN ereal_openE] guess xT yT .
  1748   ultimately have
  1749     "open (ereal -` (S \<inter> T))"
  1750     "\<infinity> \<in> S \<inter> T \<Longrightarrow> {ereal (max xS xT) <..} \<subseteq> S \<inter> T"
  1751     "-\<infinity> \<in> S \<inter> T \<Longrightarrow> {..< ereal (min yS yT)} \<subseteq> S \<inter> T"
  1752     by auto
  1753   then show "open (S Int T)" unfolding open_ereal_def by blast
  1754 next
  1755   fix K :: "ereal set set" assume "\<forall>S\<in>K. open S"
  1756   then have *: "\<forall>S. \<exists>x y. S \<in> K \<longrightarrow> open (ereal -` S) \<and>
  1757     (\<infinity> \<in> S \<longrightarrow> {ereal x <..} \<subseteq> S) \<and> (-\<infinity> \<in> S \<longrightarrow> {..< ereal y} \<subseteq> S)"
  1758     by (auto simp: open_ereal_def)
  1759   then show "open (Union K)" unfolding open_ereal_def
  1760   proof (intro conjI impI)
  1761     show "open (ereal -` \<Union>K)"
  1762       using *[THEN choice] by (auto simp: vimage_Union)
  1763   qed ((metis UnionE Union_upper subset_trans *)+)
  1764 qed
  1765 end
  1766 
  1767 lemma open_ereal: "open S \<Longrightarrow> open (ereal ` S)"
  1768   by (auto simp: inj_vimage_image_eq open_ereal_def)
  1769 
  1770 lemma open_ereal_vimage: "open S \<Longrightarrow> open (ereal -` S)"
  1771   unfolding open_ereal_def by auto
  1772 
  1773 lemma open_ereal_lessThan[intro, simp]: "open {..< a :: ereal}"
  1774 proof -
  1775   have "\<And>x. ereal -` {..<ereal x} = {..< x}"
  1776     "ereal -` {..< \<infinity>} = UNIV" "ereal -` {..< -\<infinity>} = {}" by auto
  1777   then show ?thesis by (cases a) (auto simp: open_ereal_def)
  1778 qed
  1779 
  1780 lemma open_ereal_greaterThan[intro, simp]:
  1781   "open {a :: ereal <..}"
  1782 proof -
  1783   have "\<And>x. ereal -` {ereal x<..} = {x<..}"
  1784     "ereal -` {\<infinity><..} = {}" "ereal -` {-\<infinity><..} = UNIV" by auto
  1785   then show ?thesis by (cases a) (auto simp: open_ereal_def)
  1786 qed
  1787 
  1788 lemma ereal_open_greaterThanLessThan[intro, simp]: "open {a::ereal <..< b}"
  1789   unfolding greaterThanLessThan_def by auto
  1790 
  1791 lemma closed_ereal_atLeast[simp, intro]: "closed {a :: ereal ..}"
  1792 proof -
  1793   have "- {a ..} = {..< a}" by auto
  1794   then show "closed {a ..}"
  1795     unfolding closed_def using open_ereal_lessThan by auto
  1796 qed
  1797 
  1798 lemma closed_ereal_atMost[simp, intro]: "closed {.. b :: ereal}"
  1799 proof -
  1800   have "- {.. b} = {b <..}" by auto
  1801   then show "closed {.. b}"
  1802     unfolding closed_def using open_ereal_greaterThan by auto
  1803 qed
  1804 
  1805 lemma closed_ereal_atLeastAtMost[simp, intro]:
  1806   shows "closed {a :: ereal .. b}"
  1807   unfolding atLeastAtMost_def by auto
  1808 
  1809 lemma closed_ereal_singleton:
  1810   "closed {a :: ereal}"
  1811 by (metis atLeastAtMost_singleton closed_ereal_atLeastAtMost)
  1812 
  1813 lemma ereal_open_cont_interval:
  1814   fixes S :: "ereal set"
  1815   assumes "open S" "x \<in> S" "\<bar>x\<bar> \<noteq> \<infinity>"
  1816   obtains e where "e>0" "{x-e <..< x+e} \<subseteq> S"
  1817 proof-
  1818   from `open S` have "open (ereal -` S)" by (rule ereal_openE)
  1819   then obtain e where "0 < e" and e: "\<And>y. dist y (real x) < e \<Longrightarrow> ereal y \<in> S"
  1820     using assms unfolding open_dist by force
  1821   show thesis
  1822   proof (intro that subsetI)
  1823     show "0 < ereal e" using `0 < e` by auto
  1824     fix y assume "y \<in> {x - ereal e<..<x + ereal e}"
  1825     with assms obtain t where "y = ereal t" "dist t (real x) < e"
  1826       apply (cases y) by (auto simp: dist_real_def)
  1827     then show "y \<in> S" using e[of t] by auto
  1828   qed
  1829 qed
  1830 
  1831 lemma ereal_open_cont_interval2:
  1832   fixes S :: "ereal set"
  1833   assumes "open S" "x \<in> S" and x: "\<bar>x\<bar> \<noteq> \<infinity>"
  1834   obtains a b where "a < x" "x < b" "{a <..< b} \<subseteq> S"
  1835 proof-
  1836   guess e using ereal_open_cont_interval[OF assms] .
  1837   with that[of "x-e" "x+e"] ereal_between[OF x, of e]
  1838   show thesis by auto
  1839 qed
  1840 
  1841 instance ereal :: t2_space
  1842 proof
  1843   fix x y :: ereal assume "x ~= y"
  1844   let "?P x (y::ereal)" = "EX U V. open U & open V & x : U & y : V & U Int V = {}"
  1845 
  1846   { fix x y :: ereal assume "x < y"
  1847     from ereal_dense[OF this] obtain z where z: "x < z" "z < y" by auto
  1848     have "?P x y"
  1849       apply (rule exI[of _ "{..<z}"])
  1850       apply (rule exI[of _ "{z<..}"])
  1851       using z by auto }
  1852   note * = this
  1853 
  1854   from `x ~= y`
  1855   show "EX U V. open U & open V & x : U & y : V & U Int V = {}"
  1856   proof (cases rule: linorder_cases)
  1857     assume "x = y" with `x ~= y` show ?thesis by simp
  1858   next assume "x < y" from *[OF this] show ?thesis by auto
  1859   next assume "y < x" from *[OF this] show ?thesis by auto
  1860   qed
  1861 qed
  1862 
  1863 subsubsection {* Convergent sequences *}
  1864 
  1865 lemma lim_ereal[simp]:
  1866   "((\<lambda>n. ereal (f n)) ---> ereal x) net \<longleftrightarrow> (f ---> x) net" (is "?l = ?r")
  1867 proof (intro iffI topological_tendstoI)
  1868   fix S assume "?l" "open S" "x \<in> S"
  1869   then show "eventually (\<lambda>x. f x \<in> S) net"
  1870     using `?l`[THEN topological_tendstoD, OF open_ereal, OF `open S`]
  1871     by (simp add: inj_image_mem_iff)
  1872 next
  1873   fix S assume "?r" "open S" "ereal x \<in> S"
  1874   show "eventually (\<lambda>x. ereal (f x) \<in> S) net"
  1875     using `?r`[THEN topological_tendstoD, OF open_ereal_vimage, OF `open S`]
  1876     using `ereal x \<in> S` by auto
  1877 qed
  1878 
  1879 lemma lim_real_of_ereal[simp]:
  1880   assumes lim: "(f ---> ereal x) net"
  1881   shows "((\<lambda>x. real (f x)) ---> x) net"
  1882 proof (intro topological_tendstoI)
  1883   fix S assume "open S" "x \<in> S"
  1884   then have S: "open S" "ereal x \<in> ereal ` S"
  1885     by (simp_all add: inj_image_mem_iff)
  1886   have "\<forall>x. f x \<in> ereal ` S \<longrightarrow> real (f x) \<in> S" by auto
  1887   from this lim[THEN topological_tendstoD, OF open_ereal, OF S]
  1888   show "eventually (\<lambda>x. real (f x) \<in> S) net"
  1889     by (rule eventually_mono)
  1890 qed
  1891 
  1892 lemma Lim_PInfty: "f ----> \<infinity> <-> (ALL B. EX N. ALL n>=N. f n >= ereal B)" (is "?l = ?r")
  1893 proof
  1894   assume ?r
  1895   show ?l
  1896     apply(rule topological_tendstoI)
  1897     unfolding eventually_sequentially
  1898   proof-
  1899     fix S :: "ereal set" assume "open S" "\<infinity> : S"
  1900     from open_PInfty[OF this] guess B .. note B=this
  1901     from `?r`[rule_format,of "B+1"] guess N .. note N=this
  1902     show "EX N. ALL n>=N. f n : S" apply(rule_tac x=N in exI)
  1903     proof safe case goal1
  1904       have "ereal B < ereal (B + 1)" by auto
  1905       also have "... <= f n" using goal1 N by auto
  1906       finally show ?case using B by fastsimp
  1907     qed
  1908   qed
  1909 next
  1910   assume ?l
  1911   show ?r
  1912   proof fix B::real have "open {ereal B<..}" "\<infinity> : {ereal B<..}" by auto
  1913     from topological_tendstoD[OF `?l` this,unfolded eventually_sequentially]
  1914     guess N .. note N=this
  1915     show "EX N. ALL n>=N. ereal B <= f n" apply(rule_tac x=N in exI) using N by auto
  1916   qed
  1917 qed
  1918 
  1919 
  1920 lemma Lim_MInfty: "f ----> (-\<infinity>) <-> (ALL B. EX N. ALL n>=N. f n <= ereal B)" (is "?l = ?r")
  1921 proof
  1922   assume ?r
  1923   show ?l
  1924     apply(rule topological_tendstoI)
  1925     unfolding eventually_sequentially
  1926   proof-
  1927     fix S :: "ereal set"
  1928     assume "open S" "(-\<infinity>) : S"
  1929     from open_MInfty[OF this] guess B .. note B=this
  1930     from `?r`[rule_format,of "B-(1::real)"] guess N .. note N=this
  1931     show "EX N. ALL n>=N. f n : S" apply(rule_tac x=N in exI)
  1932     proof safe case goal1
  1933       have "ereal (B - 1) >= f n" using goal1 N by auto
  1934       also have "... < ereal B" by auto
  1935       finally show ?case using B by fastsimp
  1936     qed
  1937   qed
  1938 next assume ?l show ?r
  1939   proof fix B::real have "open {..<ereal B}" "(-\<infinity>) : {..<ereal B}" by auto
  1940     from topological_tendstoD[OF `?l` this,unfolded eventually_sequentially]
  1941     guess N .. note N=this
  1942     show "EX N. ALL n>=N. ereal B >= f n" apply(rule_tac x=N in exI) using N by auto
  1943   qed
  1944 qed
  1945 
  1946 
  1947 lemma Lim_bounded_PInfty: assumes lim:"f ----> l" and "!!n. f n <= ereal B" shows "l ~= \<infinity>"
  1948 proof(rule ccontr,unfold not_not) let ?B = "B + 1" assume as:"l=\<infinity>"
  1949   from lim[unfolded this Lim_PInfty,rule_format,of "?B"]
  1950   guess N .. note N=this[rule_format,OF le_refl]
  1951   hence "ereal ?B <= ereal B" using assms(2)[of N] by(rule order_trans)
  1952   hence "ereal ?B < ereal ?B" apply (rule le_less_trans) by auto
  1953   thus False by auto
  1954 qed
  1955 
  1956 
  1957 lemma Lim_bounded_MInfty: assumes lim:"f ----> l" and "!!n. f n >= ereal B" shows "l ~= (-\<infinity>)"
  1958 proof(rule ccontr,unfold not_not) let ?B = "B - 1" assume as:"l=(-\<infinity>)"
  1959   from lim[unfolded this Lim_MInfty,rule_format,of "?B"]
  1960   guess N .. note N=this[rule_format,OF le_refl]
  1961   hence "ereal B <= ereal ?B" using assms(2)[of N] order_trans[of "ereal B" "f N" "ereal(B - 1)"] by blast
  1962   thus False by auto
  1963 qed
  1964 
  1965 
  1966 lemma tendsto_explicit:
  1967   "f ----> f0 <-> (ALL S. open S --> f0 : S --> (EX N. ALL n>=N. f n : S))"
  1968   unfolding tendsto_def eventually_sequentially by auto
  1969 
  1970 
  1971 lemma tendsto_obtains_N:
  1972   assumes "f ----> f0"
  1973   assumes "open S" "f0 : S"
  1974   obtains N where "ALL n>=N. f n : S"
  1975   using tendsto_explicit[of f f0] assms by auto
  1976 
  1977 
  1978 lemma tail_same_limit:
  1979   fixes X Y N
  1980   assumes "X ----> L" "ALL n>=N. X n = Y n"
  1981   shows "Y ----> L"
  1982 proof-
  1983 { fix S assume "open S" and "L:S"
  1984   from this obtain N1 where "ALL n>=N1. X n : S"
  1985      using assms unfolding tendsto_def eventually_sequentially by auto
  1986   hence "ALL n>=max N N1. Y n : S" using assms by auto
  1987   hence "EX N. ALL n>=N. Y n : S" apply(rule_tac x="max N N1" in exI) by auto
  1988 }
  1989 thus ?thesis using tendsto_explicit by auto
  1990 qed
  1991 
  1992 
  1993 lemma Lim_bounded_PInfty2:
  1994 assumes lim:"f ----> l" and "ALL n>=N. f n <= ereal B"
  1995 shows "l ~= \<infinity>"
  1996 proof-
  1997   def g == "(%n. if n>=N then f n else ereal B)"
  1998   hence "g ----> l" using tail_same_limit[of f l N g] lim by auto
  1999   moreover have "!!n. g n <= ereal B" using g_def assms by auto
  2000   ultimately show ?thesis using  Lim_bounded_PInfty by auto
  2001 qed
  2002 
  2003 lemma Lim_bounded_ereal:
  2004   assumes lim:"f ----> (l :: ereal)"
  2005   and "ALL n>=M. f n <= C"
  2006   shows "l<=C"
  2007 proof-
  2008 { assume "l=(-\<infinity>)" hence ?thesis by auto }
  2009 moreover
  2010 { assume "~(l=(-\<infinity>))"
  2011   { assume "C=\<infinity>" hence ?thesis by auto }
  2012   moreover
  2013   { assume "C=(-\<infinity>)" hence "ALL n>=M. f n = (-\<infinity>)" using assms by auto
  2014     hence "l=(-\<infinity>)" using assms
  2015        tendsto_unique[OF trivial_limit_sequentially] tail_same_limit[of "\<lambda>n. -\<infinity>" "-\<infinity>" M f, OF tendsto_const] by auto
  2016     hence ?thesis by auto }
  2017   moreover
  2018   { assume "EX B. C = ereal B"
  2019     from this obtain B where B_def: "C=ereal B" by auto
  2020     hence "~(l=\<infinity>)" using Lim_bounded_PInfty2 assms by auto
  2021     from this obtain m where m_def: "ereal m=l" using `~(l=(-\<infinity>))` by (cases l) auto
  2022     from this obtain N where N_def: "ALL n>=N. f n : {ereal(m - 1) <..< ereal(m+1)}"
  2023        apply (subst tendsto_obtains_N[of f l "{ereal(m - 1) <..< ereal(m+1)}"]) using assms by auto
  2024     { fix n assume "n>=N"
  2025       hence "EX r. ereal r = f n" using N_def by (cases "f n") auto
  2026     } from this obtain g where g_def: "ALL n>=N. ereal (g n) = f n" by metis
  2027     hence "(%n. ereal (g n)) ----> l" using tail_same_limit[of f l N] assms by auto
  2028     hence *: "(%n. g n) ----> m" using m_def by auto
  2029     { fix n assume "n>=max N M"
  2030       hence "ereal (g n) <= ereal B" using assms g_def B_def by auto
  2031       hence "g n <= B" by auto
  2032     } hence "EX N. ALL n>=N. g n <= B" by blast
  2033     hence "m<=B" using * LIMSEQ_le_const2[of g m B] by auto
  2034     hence ?thesis using m_def B_def by auto
  2035   } ultimately have ?thesis by (cases C) auto
  2036 } ultimately show ?thesis by blast
  2037 qed
  2038 
  2039 lemma real_of_ereal_mult[simp]:
  2040   fixes a b :: ereal shows "real (a * b) = real a * real b"
  2041   by (cases rule: ereal2_cases[of a b]) auto
  2042 
  2043 lemma real_of_ereal_eq_0:
  2044   fixes x :: ereal shows "real x = 0 \<longleftrightarrow> x = \<infinity> \<or> x = -\<infinity> \<or> x = 0"
  2045   by (cases x) auto
  2046 
  2047 lemma tendsto_ereal_realD:
  2048   fixes f :: "'a \<Rightarrow> ereal"
  2049   assumes "x \<noteq> 0" and tendsto: "((\<lambda>x. ereal (real (f x))) ---> x) net"
  2050   shows "(f ---> x) net"
  2051 proof (intro topological_tendstoI)
  2052   fix S assume S: "open S" "x \<in> S"
  2053   with `x \<noteq> 0` have "open (S - {0})" "x \<in> S - {0}" by auto
  2054   from tendsto[THEN topological_tendstoD, OF this]
  2055   show "eventually (\<lambda>x. f x \<in> S) net"
  2056     by (rule eventually_rev_mp) (auto simp: ereal_real real_of_ereal_0)
  2057 qed
  2058 
  2059 lemma tendsto_ereal_realI:
  2060   fixes f :: "'a \<Rightarrow> ereal"
  2061   assumes x: "\<bar>x\<bar> \<noteq> \<infinity>" and tendsto: "(f ---> x) net"
  2062   shows "((\<lambda>x. ereal (real (f x))) ---> x) net"
  2063 proof (intro topological_tendstoI)
  2064   fix S assume "open S" "x \<in> S"
  2065   with x have "open (S - {\<infinity>, -\<infinity>})" "x \<in> S - {\<infinity>, -\<infinity>}" by auto
  2066   from tendsto[THEN topological_tendstoD, OF this]
  2067   show "eventually (\<lambda>x. ereal (real (f x)) \<in> S) net"
  2068     by (elim eventually_elim1) (auto simp: ereal_real)
  2069 qed
  2070 
  2071 lemma ereal_mult_cancel_left:
  2072   fixes a b c :: ereal shows "a * b = a * c \<longleftrightarrow>
  2073     ((\<bar>a\<bar> = \<infinity> \<and> 0 < b * c) \<or> a = 0 \<or> b = c)"
  2074   by (cases rule: ereal3_cases[of a b c])
  2075      (simp_all add: zero_less_mult_iff)
  2076 
  2077 lemma ereal_inj_affinity:
  2078   fixes m t :: ereal
  2079   assumes "\<bar>m\<bar> \<noteq> \<infinity>" "m \<noteq> 0" "\<bar>t\<bar> \<noteq> \<infinity>"
  2080   shows "inj_on (\<lambda>x. m * x + t) A"
  2081   using assms
  2082   by (cases rule: ereal2_cases[of m t])
  2083      (auto intro!: inj_onI simp: ereal_add_cancel_right ereal_mult_cancel_left)
  2084 
  2085 lemma ereal_PInfty_eq_plus[simp]:
  2086   fixes a b :: ereal
  2087   shows "\<infinity> = a + b \<longleftrightarrow> a = \<infinity> \<or> b = \<infinity>"
  2088   by (cases rule: ereal2_cases[of a b]) auto
  2089 
  2090 lemma ereal_MInfty_eq_plus[simp]:
  2091   fixes a b :: ereal
  2092   shows "-\<infinity> = a + b \<longleftrightarrow> (a = -\<infinity> \<and> b \<noteq> \<infinity>) \<or> (b = -\<infinity> \<and> a \<noteq> \<infinity>)"
  2093   by (cases rule: ereal2_cases[of a b]) auto
  2094 
  2095 lemma ereal_less_divide_pos:
  2096   fixes x y :: ereal
  2097   shows "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y < z / x \<longleftrightarrow> x * y < z"
  2098   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
  2099 
  2100 lemma ereal_divide_less_pos:
  2101   fixes x y z :: ereal
  2102   shows "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y / x < z \<longleftrightarrow> y < x * z"
  2103   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
  2104 
  2105 lemma ereal_divide_eq:
  2106   fixes a b c :: ereal
  2107   shows "b \<noteq> 0 \<Longrightarrow> \<bar>b\<bar> \<noteq> \<infinity> \<Longrightarrow> a / b = c \<longleftrightarrow> a = b * c"
  2108   by (cases rule: ereal3_cases[of a b c])
  2109      (simp_all add: field_simps)
  2110 
  2111 lemma ereal_inverse_not_MInfty[simp]: "inverse (a::ereal) \<noteq> -\<infinity>"
  2112   by (cases a) auto
  2113 
  2114 lemma ereal_mult_m1[simp]: "x * ereal (-1) = -x"
  2115   by (cases x) auto
  2116 
  2117 lemma ereal_LimI_finite:
  2118   fixes x :: ereal
  2119   assumes "\<bar>x\<bar> \<noteq> \<infinity>"
  2120   assumes "!!r. 0 < r ==> EX N. ALL n>=N. u n < x + r & x < u n + r"
  2121   shows "u ----> x"
  2122 proof (rule topological_tendstoI, unfold eventually_sequentially)
  2123   obtain rx where rx_def: "x=ereal rx" using assms by (cases x) auto
  2124   fix S assume "open S" "x : S"
  2125   then have "open (ereal -` S)" unfolding open_ereal_def by auto
  2126   with `x \<in> S` obtain r where "0 < r" and dist: "!!y. dist y rx < r ==> ereal y \<in> S"
  2127     unfolding open_real_def rx_def by auto
  2128   then obtain n where
  2129     upper: "!!N. n <= N ==> u N < x + ereal r" and
  2130     lower: "!!N. n <= N ==> x < u N + ereal r" using assms(2)[of "ereal r"] by auto
  2131   show "EX N. ALL n>=N. u n : S"
  2132   proof (safe intro!: exI[of _ n])
  2133     fix N assume "n <= N"
  2134     from upper[OF this] lower[OF this] assms `0 < r`
  2135     have "u N ~: {\<infinity>,(-\<infinity>)}" by auto
  2136     from this obtain ra where ra_def: "(u N) = ereal ra" by (cases "u N") auto
  2137     hence "rx < ra + r" and "ra < rx + r"
  2138        using rx_def assms `0 < r` lower[OF `n <= N`] upper[OF `n <= N`] by auto
  2139     hence "dist (real (u N)) rx < r"
  2140       using rx_def ra_def
  2141       by (auto simp: dist_real_def abs_diff_less_iff field_simps)
  2142     from dist[OF this] show "u N : S" using `u N  ~: {\<infinity>, -\<infinity>}`
  2143       by (auto simp: ereal_real split: split_if_asm)
  2144   qed
  2145 qed
  2146 
  2147 lemma ereal_LimI_finite_iff:
  2148   fixes x :: ereal
  2149   assumes "\<bar>x\<bar> \<noteq> \<infinity>"
  2150   shows "u ----> x <-> (ALL r. 0 < r --> (EX N. ALL n>=N. u n < x + r & x < u n + r))"
  2151   (is "?lhs <-> ?rhs")
  2152 proof
  2153   assume lim: "u ----> x"
  2154   { fix r assume "(r::ereal)>0"
  2155     from this obtain N where N_def: "ALL n>=N. u n : {x - r <..< x + r}"
  2156        apply (subst tendsto_obtains_N[of u x "{x - r <..< x + r}"])
  2157        using lim ereal_between[of x r] assms `r>0` by auto
  2158     hence "EX N. ALL n>=N. u n < x + r & x < u n + r"
  2159       using ereal_minus_less[of r x] by (cases r) auto
  2160   } then show "?rhs" by auto
  2161 next
  2162   assume ?rhs then show "u ----> x"
  2163     using ereal_LimI_finite[of x] assms by auto
  2164 qed
  2165 
  2166 
  2167 subsubsection {* @{text Liminf} and @{text Limsup} *}
  2168 
  2169 definition
  2170   "Liminf net f = (GREATEST l. \<forall>y<l. eventually (\<lambda>x. y < f x) net)"
  2171 
  2172 definition
  2173   "Limsup net f = (LEAST l. \<forall>y>l. eventually (\<lambda>x. f x < y) net)"
  2174 
  2175 lemma Liminf_Sup:
  2176   fixes f :: "'a => 'b::{complete_lattice, linorder}"
  2177   shows "Liminf net f = Sup {l. \<forall>y<l. eventually (\<lambda>x. y < f x) net}"
  2178   by (auto intro!: Greatest_equality complete_lattice_class.Sup_upper simp: less_Sup_iff Liminf_def)
  2179 
  2180 lemma Limsup_Inf:
  2181   fixes f :: "'a => 'b::{complete_lattice, linorder}"
  2182   shows "Limsup net f = Inf {l. \<forall>y>l. eventually (\<lambda>x. f x < y) net}"
  2183   by (auto intro!: Least_equality complete_lattice_class.Inf_lower simp: Inf_less_iff Limsup_def)
  2184 
  2185 lemma ereal_SupI:
  2186   fixes x :: ereal
  2187   assumes "\<And>y. y \<in> A \<Longrightarrow> y \<le> x"
  2188   assumes "\<And>y. (\<And>z. z \<in> A \<Longrightarrow> z \<le> y) \<Longrightarrow> x \<le> y"
  2189   shows "Sup A = x"
  2190   unfolding Sup_ereal_def
  2191   using assms by (auto intro!: Least_equality)
  2192 
  2193 lemma ereal_InfI:
  2194   fixes x :: ereal
  2195   assumes "\<And>i. i \<in> A \<Longrightarrow> x \<le> i"
  2196   assumes "\<And>y. (\<And>i. i \<in> A \<Longrightarrow> y \<le> i) \<Longrightarrow> y \<le> x"
  2197   shows "Inf A = x"
  2198   unfolding Inf_ereal_def
  2199   using assms by (auto intro!: Greatest_equality)
  2200 
  2201 lemma Limsup_const:
  2202   fixes c :: "'a::{complete_lattice, linorder}"
  2203   assumes ntriv: "\<not> trivial_limit net"
  2204   shows "Limsup net (\<lambda>x. c) = c"
  2205   unfolding Limsup_Inf
  2206 proof (safe intro!: antisym complete_lattice_class.Inf_greatest complete_lattice_class.Inf_lower)
  2207   fix x assume *: "\<forall>y>x. eventually (\<lambda>_. c < y) net"
  2208   show "c \<le> x"
  2209   proof (rule ccontr)
  2210     assume "\<not> c \<le> x" then have "x < c" by auto
  2211     then show False using ntriv * by (auto simp: trivial_limit_def)
  2212   qed
  2213 qed auto
  2214 
  2215 lemma Liminf_const:
  2216   fixes c :: "'a::{complete_lattice, linorder}"
  2217   assumes ntriv: "\<not> trivial_limit net"
  2218   shows "Liminf net (\<lambda>x. c) = c"
  2219   unfolding Liminf_Sup
  2220 proof (safe intro!: antisym complete_lattice_class.Sup_least complete_lattice_class.Sup_upper)
  2221   fix x assume *: "\<forall>y<x. eventually (\<lambda>_. y < c) net"
  2222   show "x \<le> c"
  2223   proof (rule ccontr)
  2224     assume "\<not> x \<le> c" then have "c < x" by auto
  2225     then show False using ntriv * by (auto simp: trivial_limit_def)
  2226   qed
  2227 qed auto
  2228 
  2229 lemma mono_set:
  2230   fixes S :: "('a::order) set"
  2231   shows "mono S \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> x \<in> S \<longrightarrow> y \<in> S)"
  2232   by (auto simp: mono_def mem_def)
  2233 
  2234 lemma mono_greaterThan[intro, simp]: "mono {B<..}" unfolding mono_set by auto
  2235 lemma mono_atLeast[intro, simp]: "mono {B..}" unfolding mono_set by auto
  2236 lemma mono_UNIV[intro, simp]: "mono UNIV" unfolding mono_set by auto
  2237 lemma mono_empty[intro, simp]: "mono {}" unfolding mono_set by auto
  2238 
  2239 lemma mono_set_iff:
  2240   fixes S :: "'a::{linorder,complete_lattice} set"
  2241   defines "a \<equiv> Inf S"
  2242   shows "mono S \<longleftrightarrow> (S = {a <..} \<or> S = {a..})" (is "_ = ?c")
  2243 proof
  2244   assume "mono S"
  2245   then have mono: "\<And>x y. x \<le> y \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S" by (auto simp: mono_set)
  2246   show ?c
  2247   proof cases
  2248     assume "a \<in> S"
  2249     show ?c
  2250       using mono[OF _ `a \<in> S`]
  2251       by (auto intro: complete_lattice_class.Inf_lower simp: a_def)
  2252   next
  2253     assume "a \<notin> S"
  2254     have "S = {a <..}"
  2255     proof safe
  2256       fix x assume "x \<in> S"
  2257       then have "a \<le> x" unfolding a_def by (rule complete_lattice_class.Inf_lower)
  2258       then show "a < x" using `x \<in> S` `a \<notin> S` by (cases "a = x") auto
  2259     next
  2260       fix x assume "a < x"
  2261       then obtain y where "y < x" "y \<in> S" unfolding a_def Inf_less_iff ..
  2262       with mono[of y x] show "x \<in> S" by auto
  2263     qed
  2264     then show ?c ..
  2265   qed
  2266 qed auto
  2267 
  2268 lemma lim_imp_Liminf:
  2269   fixes f :: "'a \<Rightarrow> ereal"
  2270   assumes ntriv: "\<not> trivial_limit net"
  2271   assumes lim: "(f ---> f0) net"
  2272   shows "Liminf net f = f0"
  2273   unfolding Liminf_Sup
  2274 proof (safe intro!: ereal_SupI)
  2275   fix y assume *: "\<forall>y'<y. eventually (\<lambda>x. y' < f x) net"
  2276   show "y \<le> f0"
  2277   proof (rule ereal_le_ereal)
  2278     fix B assume "B < y"
  2279     { assume "f0 < B"
  2280       then have "eventually (\<lambda>x. f x < B \<and> B < f x) net"
  2281          using topological_tendstoD[OF lim, of "{..<B}"] *[rule_format, OF `B < y`]
  2282          by (auto intro: eventually_conj)
  2283       also have "(\<lambda>x. f x < B \<and> B < f x) = (\<lambda>x. False)" by (auto simp: fun_eq_iff)
  2284       finally have False using ntriv[unfolded trivial_limit_def] by auto
  2285     } then show "B \<le> f0" by (metis linorder_le_less_linear)
  2286   qed
  2287 next
  2288   fix y assume *: "\<forall>z. z \<in> {l. \<forall>y<l. eventually (\<lambda>x. y < f x) net} \<longrightarrow> z \<le> y"
  2289   show "f0 \<le> y"
  2290   proof (safe intro!: *[rule_format])
  2291     fix y assume "y < f0" then show "eventually (\<lambda>x. y < f x) net"
  2292       using lim[THEN topological_tendstoD, of "{y <..}"] by auto
  2293   qed
  2294 qed
  2295 
  2296 lemma ereal_Liminf_le_Limsup:
  2297   fixes f :: "'a \<Rightarrow> ereal"
  2298   assumes ntriv: "\<not> trivial_limit net"
  2299   shows "Liminf net f \<le> Limsup net f"
  2300   unfolding Limsup_Inf Liminf_Sup
  2301 proof (safe intro!: complete_lattice_class.Inf_greatest  complete_lattice_class.Sup_least)
  2302   fix u v assume *: "\<forall>y<u. eventually (\<lambda>x. y < f x) net" "\<forall>y>v. eventually (\<lambda>x. f x < y) net"
  2303   show "u \<le> v"
  2304   proof (rule ccontr)
  2305     assume "\<not> u \<le> v"
  2306     then obtain t where "t < u" "v < t"
  2307       using ereal_dense[of v u] by (auto simp: not_le)
  2308     then have "eventually (\<lambda>x. t < f x \<and> f x < t) net"
  2309       using * by (auto intro: eventually_conj)
  2310     also have "(\<lambda>x. t < f x \<and> f x < t) = (\<lambda>x. False)" by (auto simp: fun_eq_iff)
  2311     finally show False using ntriv by (auto simp: trivial_limit_def)
  2312   qed
  2313 qed
  2314 
  2315 lemma Liminf_mono:
  2316   fixes f g :: "'a => ereal"
  2317   assumes ev: "eventually (\<lambda>x. f x \<le> g x) net"
  2318   shows "Liminf net f \<le> Liminf net g"
  2319   unfolding Liminf_Sup
  2320 proof (safe intro!: Sup_mono bexI)
  2321   fix a y assume "\<forall>y<a. eventually (\<lambda>x. y < f x) net" and "y < a"
  2322   then have "eventually (\<lambda>x. y < f x) net" by auto
  2323   then show "eventually (\<lambda>x. y < g x) net"
  2324     by (rule eventually_rev_mp) (rule eventually_mono[OF _ ev], auto)
  2325 qed simp
  2326 
  2327 lemma Liminf_eq:
  2328   fixes f g :: "'a \<Rightarrow> ereal"
  2329   assumes "eventually (\<lambda>x. f x = g x) net"
  2330   shows "Liminf net f = Liminf net g"
  2331   by (intro antisym Liminf_mono eventually_mono[OF _ assms]) auto
  2332 
  2333 lemma Liminf_mono_all:
  2334   fixes f g :: "'a \<Rightarrow> ereal"
  2335   assumes "\<And>x. f x \<le> g x"
  2336   shows "Liminf net f \<le> Liminf net g"
  2337   using assms by (intro Liminf_mono always_eventually) auto
  2338 
  2339 lemma Limsup_mono:
  2340   fixes f g :: "'a \<Rightarrow> ereal"
  2341   assumes ev: "eventually (\<lambda>x. f x \<le> g x) net"
  2342   shows "Limsup net f \<le> Limsup net g"
  2343   unfolding Limsup_Inf
  2344 proof (safe intro!: Inf_mono bexI)
  2345   fix a y assume "\<forall>y>a. eventually (\<lambda>x. g x < y) net" and "a < y"
  2346   then have "eventually (\<lambda>x. g x < y) net" by auto
  2347   then show "eventually (\<lambda>x. f x < y) net"
  2348     by (rule eventually_rev_mp) (rule eventually_mono[OF _ ev], auto)
  2349 qed simp
  2350 
  2351 lemma Limsup_mono_all:
  2352   fixes f g :: "'a \<Rightarrow> ereal"
  2353   assumes "\<And>x. f x \<le> g x"
  2354   shows "Limsup net f \<le> Limsup net g"
  2355   using assms by (intro Limsup_mono always_eventually) auto
  2356 
  2357 lemma Limsup_eq:
  2358   fixes f g :: "'a \<Rightarrow> ereal"
  2359   assumes "eventually (\<lambda>x. f x = g x) net"
  2360   shows "Limsup net f = Limsup net g"
  2361   by (intro antisym Limsup_mono eventually_mono[OF _ assms]) auto
  2362 
  2363 abbreviation "liminf \<equiv> Liminf sequentially"
  2364 
  2365 abbreviation "limsup \<equiv> Limsup sequentially"
  2366 
  2367 lemma (in complete_lattice) less_INFD:
  2368   assumes "y < INFI A f"" i \<in> A" shows "y < f i"
  2369 proof -
  2370   note `y < INFI A f`
  2371   also have "INFI A f \<le> f i" using `i \<in> A` by (rule INF_leI)
  2372   finally show "y < f i" .
  2373 qed
  2374 
  2375 lemma liminf_SUPR_INFI:
  2376   fixes f :: "nat \<Rightarrow> ereal"
  2377   shows "liminf f = (SUP n. INF m:{n..}. f m)"
  2378   unfolding Liminf_Sup eventually_sequentially
  2379 proof (safe intro!: antisym complete_lattice_class.Sup_least)
  2380   fix x assume *: "\<forall>y<x. \<exists>N. \<forall>n\<ge>N. y < f n" show "x \<le> (SUP n. INF m:{n..}. f m)"
  2381   proof (rule ereal_le_ereal)
  2382     fix y assume "y < x"
  2383     with * obtain N where "\<And>n. N \<le> n \<Longrightarrow> y < f n" by auto
  2384     then have "y \<le> (INF m:{N..}. f m)" by (force simp: le_INF_iff)
  2385     also have "\<dots> \<le> (SUP n. INF m:{n..}. f m)" by (intro le_SUPI) auto
  2386     finally show "y \<le> (SUP n. INF m:{n..}. f m)" .
  2387   qed
  2388 next
  2389   show "(SUP n. INF m:{n..}. f m) \<le> Sup {l. \<forall>y<l. \<exists>N. \<forall>n\<ge>N. y < f n}"
  2390   proof (unfold SUPR_def, safe intro!: Sup_mono bexI)
  2391     fix y n assume "y < INFI {n..} f"
  2392     from less_INFD[OF this] show "\<exists>N. \<forall>n\<ge>N. y < f n" by (intro exI[of _ n]) auto
  2393   qed (rule order_refl)
  2394 qed
  2395 
  2396 lemma tail_same_limsup:
  2397   fixes X Y :: "nat => ereal"
  2398   assumes "\<And>n. N \<le> n \<Longrightarrow> X n = Y n"
  2399   shows "limsup X = limsup Y"
  2400   using Limsup_eq[of X Y sequentially] eventually_sequentially assms by auto
  2401 
  2402 lemma tail_same_liminf:
  2403   fixes X Y :: "nat => ereal"
  2404   assumes "\<And>n. N \<le> n \<Longrightarrow> X n = Y n"
  2405   shows "liminf X = liminf Y"
  2406   using Liminf_eq[of X Y sequentially] eventually_sequentially assms by auto
  2407 
  2408 lemma liminf_mono:
  2409   fixes X Y :: "nat \<Rightarrow> ereal"
  2410   assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= Y n"
  2411   shows "liminf X \<le> liminf Y"
  2412   using Liminf_mono[of X Y sequentially] eventually_sequentially assms by auto
  2413 
  2414 lemma limsup_mono:
  2415   fixes X Y :: "nat => ereal"
  2416   assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= Y n"
  2417   shows "limsup X \<le> limsup Y"
  2418   using Limsup_mono[of X Y sequentially] eventually_sequentially assms by auto
  2419 
  2420 declare trivial_limit_sequentially[simp]
  2421 
  2422 lemma
  2423   fixes X :: "nat \<Rightarrow> ereal"
  2424   shows ereal_incseq_uminus[simp]: "incseq (\<lambda>i. - X i) = decseq X"
  2425     and ereal_decseq_uminus[simp]: "decseq (\<lambda>i. - X i) = incseq X"
  2426   unfolding incseq_def decseq_def by auto
  2427 
  2428 lemma liminf_bounded:
  2429   fixes X Y :: "nat \<Rightarrow> ereal"
  2430   assumes "\<And>n. N \<le> n \<Longrightarrow> C \<le> X n"
  2431   shows "C \<le> liminf X"
  2432   using liminf_mono[of N "\<lambda>n. C" X] assms Liminf_const[of sequentially C] by simp
  2433 
  2434 lemma limsup_bounded:
  2435   fixes X Y :: "nat => ereal"
  2436   assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= C"
  2437   shows "limsup X \<le> C"
  2438   using limsup_mono[of N X "\<lambda>n. C"] assms Limsup_const[of sequentially C] by simp
  2439 
  2440 lemma liminf_bounded_iff:
  2441   fixes x :: "nat \<Rightarrow> ereal"
  2442   shows "C \<le> liminf x \<longleftrightarrow> (\<forall>B<C. \<exists>N. \<forall>n\<ge>N. B < x n)" (is "?lhs <-> ?rhs")
  2443 proof safe
  2444   fix B assume "B < C" "C \<le> liminf x"
  2445   then have "B < liminf x" by auto
  2446   then obtain N where "B < (INF m:{N..}. x m)"
  2447     unfolding liminf_SUPR_INFI SUPR_def less_Sup_iff by auto
  2448   from less_INFD[OF this] show "\<exists>N. \<forall>n\<ge>N. B < x n" by auto
  2449 next
  2450   assume *: "\<forall>B<C. \<exists>N. \<forall>n\<ge>N. B < x n"
  2451   { fix B assume "B<C"
  2452     then obtain N where "\<forall>n\<ge>N. B < x n" using `?rhs` by auto
  2453     hence "B \<le> (INF m:{N..}. x m)" by (intro le_INFI) auto
  2454     also have "... \<le> liminf x" unfolding liminf_SUPR_INFI by (intro le_SUPI) simp
  2455     finally have "B \<le> liminf x" .
  2456   } then show "?lhs" by (metis * leD liminf_bounded linorder_le_less_linear)
  2457 qed
  2458 
  2459 lemma liminf_subseq_mono:
  2460   fixes X :: "nat \<Rightarrow> ereal"
  2461   assumes "subseq r"
  2462   shows "liminf X \<le> liminf (X \<circ> r) "
  2463 proof-
  2464   have "\<And>n. (INF m:{n..}. X m) \<le> (INF m:{n..}. (X \<circ> r) m)"
  2465   proof (safe intro!: INF_mono)
  2466     fix n m :: nat assume "n \<le> m" then show "\<exists>ma\<in>{n..}. X ma \<le> (X \<circ> r) m"
  2467       using seq_suble[OF `subseq r`, of m] by (intro bexI[of _ "r m"]) auto
  2468   qed
  2469   then show ?thesis by (auto intro!: SUP_mono simp: liminf_SUPR_INFI comp_def)
  2470 qed
  2471 
  2472 lemma ereal_real': assumes "\<bar>x\<bar> \<noteq> \<infinity>" shows "ereal (real x) = x"
  2473   using assms by auto
  2474 
  2475 lemma ereal_le_ereal_bounded:
  2476   fixes x y z :: ereal
  2477   assumes "z \<le> y"
  2478   assumes *: "\<And>B. z < B \<Longrightarrow> B < x \<Longrightarrow> B \<le> y"
  2479   shows "x \<le> y"
  2480 proof (rule ereal_le_ereal)
  2481   fix B assume "B < x"
  2482   show "B \<le> y"
  2483   proof cases
  2484     assume "z < B" from *[OF this `B < x`] show "B \<le> y" .
  2485   next
  2486     assume "\<not> z < B" with `z \<le> y` show "B \<le> y" by auto
  2487   qed
  2488 qed
  2489 
  2490 lemma fixes x y :: ereal
  2491   shows Sup_atMost[simp]: "Sup {.. y} = y"
  2492     and Sup_lessThan[simp]: "Sup {..< y} = y"
  2493     and Sup_atLeastAtMost[simp]: "x \<le> y \<Longrightarrow> Sup { x .. y} = y"
  2494     and Sup_greaterThanAtMost[simp]: "x < y \<Longrightarrow> Sup { x <.. y} = y"
  2495     and Sup_atLeastLessThan[simp]: "x < y \<Longrightarrow> Sup { x ..< y} = y"
  2496   by (auto simp: Sup_ereal_def intro!: Least_equality
  2497            intro: ereal_le_ereal ereal_le_ereal_bounded[of x])
  2498 
  2499 lemma Sup_greaterThanlessThan[simp]:
  2500   fixes x y :: ereal assumes "x < y" shows "Sup { x <..< y} = y"
  2501   unfolding Sup_ereal_def
  2502 proof (intro Least_equality ereal_le_ereal_bounded[of _ _ y])
  2503   fix z assume z: "\<forall>u\<in>{x<..<y}. u \<le> z"
  2504   from ereal_dense[OF `x < y`] guess w .. note w = this
  2505   with z[THEN bspec, of w] show "x \<le> z" by auto
  2506 qed auto
  2507 
  2508 lemma real_ereal_id: "real o ereal = id"
  2509 proof-
  2510 { fix x have "(real o ereal) x = id x" by auto }
  2511 from this show ?thesis using ext by blast
  2512 qed
  2513 
  2514 lemma open_image_ereal: "open(UNIV-{ \<infinity> , (-\<infinity> :: ereal)})"
  2515 by (metis range_ereal open_ereal open_UNIV)
  2516 
  2517 lemma ereal_le_distrib:
  2518   fixes a b c :: ereal shows "c * (a + b) \<le> c * a + c * b"
  2519   by (cases rule: ereal3_cases[of a b c])
  2520      (auto simp add: field_simps not_le mult_le_0_iff mult_less_0_iff)
  2521 
  2522 lemma ereal_pos_distrib:
  2523   fixes a b c :: ereal assumes "0 \<le> c" "c \<noteq> \<infinity>" shows "c * (a + b) = c * a + c * b"
  2524   using assms by (cases rule: ereal3_cases[of a b c])
  2525                  (auto simp add: field_simps not_le mult_le_0_iff mult_less_0_iff)
  2526 
  2527 lemma ereal_pos_le_distrib:
  2528 fixes a b c :: ereal
  2529 assumes "c>=0"
  2530 shows "c * (a + b) <= c * a + c * b"
  2531   using assms by (cases rule: ereal3_cases[of a b c])
  2532                  (auto simp add: field_simps)
  2533 
  2534 lemma ereal_max_mono:
  2535   "[| (a::ereal) <= b; c <= d |] ==> max a c <= max b d"
  2536   by (metis sup_ereal_def sup_mono)
  2537 
  2538 
  2539 lemma ereal_max_least:
  2540   "[| (a::ereal) <= x; c <= x |] ==> max a c <= x"
  2541   by (metis sup_ereal_def sup_least)
  2542 
  2543 end