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