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