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