src/HOL/Library/Extended_Real.thy
author Andreas Lochbihler
Fri Sep 20 10:09:16 2013 +0200 (2013-09-20)
changeset 53745 788730ab7da4
parent 53381 355a4cac5440
child 53873 08594daabcd9
permissions -rw-r--r--
prefer Code.abort over code_abort
     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 Liminf_Limsup
    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 subsection {* Definition and basic properties *}
    22 
    23 datatype ereal = ereal real | PInfty | MInfty
    24 
    25 instantiation ereal :: uminus
    26 begin
    27   fun uminus_ereal where
    28     "- (ereal r) = ereal (- r)"
    29   | "- PInfty = MInfty"
    30   | "- MInfty = PInfty"
    31   instance ..
    32 end
    33 
    34 instantiation ereal :: infinity
    35 begin
    36   definition "(\<infinity>::ereal) = PInfty"
    37   instance ..
    38 end
    39 
    40 declare [[coercion "ereal :: real \<Rightarrow> ereal"]]
    41 
    42 lemma ereal_uminus_uminus[simp]:
    43   fixes a :: ereal shows "- (- a) = a"
    44   by (cases a) simp_all
    45 
    46 lemma
    47   shows PInfty_eq_infinity[simp]: "PInfty = \<infinity>"
    48     and MInfty_eq_minfinity[simp]: "MInfty = - \<infinity>"
    49     and MInfty_neq_PInfty[simp]: "\<infinity> \<noteq> - (\<infinity>::ereal)" "- \<infinity> \<noteq> (\<infinity>::ereal)"
    50     and MInfty_neq_ereal[simp]: "ereal r \<noteq> - \<infinity>" "- \<infinity> \<noteq> ereal r"
    51     and PInfty_neq_ereal[simp]: "ereal r \<noteq> \<infinity>" "\<infinity> \<noteq> ereal r"
    52     and PInfty_cases[simp]: "(case \<infinity> of ereal r \<Rightarrow> f r | PInfty \<Rightarrow> y | MInfty \<Rightarrow> z) = y"
    53     and MInfty_cases[simp]: "(case - \<infinity> of ereal r \<Rightarrow> f r | PInfty \<Rightarrow> y | MInfty \<Rightarrow> z) = z"
    54   by (simp_all add: infinity_ereal_def)
    55 
    56 declare
    57   PInfty_eq_infinity[code_post]
    58   MInfty_eq_minfinity[code_post]
    59 
    60 lemma [code_unfold]:
    61   "\<infinity> = PInfty"
    62   "-PInfty = MInfty"
    63   by simp_all
    64 
    65 lemma inj_ereal[simp]: "inj_on ereal A"
    66   unfolding inj_on_def by auto
    67 
    68 lemma ereal_cases[case_names real PInf MInf, cases type: ereal]:
    69   assumes "\<And>r. x = ereal r \<Longrightarrow> P"
    70   assumes "x = \<infinity> \<Longrightarrow> P"
    71   assumes "x = -\<infinity> \<Longrightarrow> P"
    72   shows P
    73   using assms by (cases x) auto
    74 
    75 lemmas ereal2_cases = ereal_cases[case_product ereal_cases]
    76 lemmas ereal3_cases = ereal2_cases[case_product ereal_cases]
    77 
    78 lemma ereal_uminus_eq_iff[simp]:
    79   fixes a b :: ereal shows "-a = -b \<longleftrightarrow> a = b"
    80   by (cases rule: ereal2_cases[of a b]) simp_all
    81 
    82 function of_ereal :: "ereal \<Rightarrow> real" where
    83 "of_ereal (ereal r) = r" |
    84 "of_ereal \<infinity> = 0" |
    85 "of_ereal (-\<infinity>) = 0"
    86   by (auto intro: ereal_cases)
    87 termination proof qed (rule wf_empty)
    88 
    89 defs (overloaded)
    90   real_of_ereal_def [code_unfold]: "real \<equiv> of_ereal"
    91 
    92 lemma real_of_ereal[simp]:
    93     "real (- x :: ereal) = - (real x)"
    94     "real (ereal r) = r"
    95     "real (\<infinity>::ereal) = 0"
    96   by (cases x) (simp_all add: real_of_ereal_def)
    97 
    98 lemma range_ereal[simp]: "range ereal = UNIV - {\<infinity>, -\<infinity>}"
    99 proof safe
   100   fix x assume "x \<notin> range ereal" "x \<noteq> \<infinity>"
   101   then show "x = -\<infinity>" by (cases x) auto
   102 qed auto
   103 
   104 lemma ereal_range_uminus[simp]: "range uminus = (UNIV::ereal set)"
   105 proof safe
   106   fix x :: ereal show "x \<in> range uminus" by (intro image_eqI[of _ _ "-x"]) auto
   107 qed auto
   108 
   109 instantiation ereal :: abs
   110 begin
   111   function abs_ereal where
   112     "\<bar>ereal r\<bar> = ereal \<bar>r\<bar>"
   113   | "\<bar>-\<infinity>\<bar> = (\<infinity>::ereal)"
   114   | "\<bar>\<infinity>\<bar> = (\<infinity>::ereal)"
   115   by (auto intro: ereal_cases)
   116   termination proof qed (rule wf_empty)
   117   instance ..
   118 end
   119 
   120 lemma abs_eq_infinity_cases[elim!]: "\<lbrakk> \<bar>x :: ereal\<bar> = \<infinity> ; x = \<infinity> \<Longrightarrow> P ; x = -\<infinity> \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
   121   by (cases x) auto
   122 
   123 lemma abs_neq_infinity_cases[elim!]: "\<lbrakk> \<bar>x :: ereal\<bar> \<noteq> \<infinity> ; \<And>r. x = ereal r \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
   124   by (cases x) auto
   125 
   126 lemma abs_ereal_uminus[simp]: "\<bar>- x\<bar> = \<bar>x::ereal\<bar>"
   127   by (cases x) auto
   128 
   129 lemma ereal_infinity_cases: "(a::ereal) \<noteq> \<infinity> \<Longrightarrow> a \<noteq> -\<infinity> \<Longrightarrow> \<bar>a\<bar> \<noteq> \<infinity>"
   130   by auto
   131 
   132 subsubsection "Addition"
   133 
   134 instantiation ereal :: "{one, comm_monoid_add}"
   135 begin
   136 
   137 definition "0 = ereal 0"
   138 definition "1 = ereal 1"
   139 
   140 function plus_ereal where
   141 "ereal r + ereal p = ereal (r + p)" |
   142 "\<infinity> + a = (\<infinity>::ereal)" |
   143 "a + \<infinity> = (\<infinity>::ereal)" |
   144 "ereal r + -\<infinity> = - \<infinity>" |
   145 "-\<infinity> + ereal p = -(\<infinity>::ereal)" |
   146 "-\<infinity> + -\<infinity> = -(\<infinity>::ereal)"
   147 proof -
   148   case (goal1 P x)
   149   then obtain a b where "x = (a, b)" by (cases x) auto
   150   with goal1 show P
   151    by (cases rule: ereal2_cases[of a b]) auto
   152 qed auto
   153 termination by default (rule wf_empty)
   154 
   155 lemma Infty_neq_0[simp]:
   156   "(\<infinity>::ereal) \<noteq> 0" "0 \<noteq> (\<infinity>::ereal)"
   157   "-(\<infinity>::ereal) \<noteq> 0" "0 \<noteq> -(\<infinity>::ereal)"
   158   by (simp_all add: zero_ereal_def)
   159 
   160 lemma ereal_eq_0[simp]:
   161   "ereal r = 0 \<longleftrightarrow> r = 0"
   162   "0 = ereal r \<longleftrightarrow> r = 0"
   163   unfolding zero_ereal_def by simp_all
   164 
   165 instance
   166 proof
   167   fix a b c :: ereal
   168   show "0 + a = a"
   169     by (cases a) (simp_all add: zero_ereal_def)
   170   show "a + b = b + a"
   171     by (cases rule: ereal2_cases[of a b]) simp_all
   172   show "a + b + c = a + (b + c)"
   173     by (cases rule: ereal3_cases[of a b c]) simp_all
   174 qed
   175 end
   176 
   177 instance ereal :: numeral ..
   178 
   179 lemma real_of_ereal_0[simp]: "real (0::ereal) = 0"
   180   unfolding real_of_ereal_def zero_ereal_def by simp
   181 
   182 lemma abs_ereal_zero[simp]: "\<bar>0\<bar> = (0::ereal)"
   183   unfolding zero_ereal_def abs_ereal.simps by simp
   184 
   185 lemma ereal_uminus_zero[simp]:
   186   "- 0 = (0::ereal)"
   187   by (simp add: zero_ereal_def)
   188 
   189 lemma ereal_uminus_zero_iff[simp]:
   190   fixes a :: ereal shows "-a = 0 \<longleftrightarrow> a = 0"
   191   by (cases a) simp_all
   192 
   193 lemma ereal_plus_eq_PInfty[simp]:
   194   fixes a b :: ereal shows "a + b = \<infinity> \<longleftrightarrow> a = \<infinity> \<or> b = \<infinity>"
   195   by (cases rule: ereal2_cases[of a b]) auto
   196 
   197 lemma ereal_plus_eq_MInfty[simp]:
   198   fixes a b :: ereal shows "a + b = -\<infinity> \<longleftrightarrow>
   199     (a = -\<infinity> \<or> b = -\<infinity>) \<and> a \<noteq> \<infinity> \<and> b \<noteq> \<infinity>"
   200   by (cases rule: ereal2_cases[of a b]) auto
   201 
   202 lemma ereal_add_cancel_left:
   203   fixes a b :: ereal assumes "a \<noteq> -\<infinity>"
   204   shows "a + b = a + c \<longleftrightarrow> (a = \<infinity> \<or> b = c)"
   205   using assms by (cases rule: ereal3_cases[of a b c]) auto
   206 
   207 lemma ereal_add_cancel_right:
   208   fixes a b :: ereal assumes "a \<noteq> -\<infinity>"
   209   shows "b + a = c + a \<longleftrightarrow> (a = \<infinity> \<or> b = c)"
   210   using assms by (cases rule: ereal3_cases[of a b c]) auto
   211 
   212 lemma ereal_real:
   213   "ereal (real x) = (if \<bar>x\<bar> = \<infinity> then 0 else x)"
   214   by (cases x) simp_all
   215 
   216 lemma real_of_ereal_add:
   217   fixes a b :: ereal
   218   shows "real (a + b) =
   219     (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)"
   220   by (cases rule: ereal2_cases[of a b]) auto
   221 
   222 subsubsection "Linear order on @{typ ereal}"
   223 
   224 instantiation ereal :: linorder
   225 begin
   226 
   227 function less_ereal
   228 where
   229   "   ereal x < ereal y     \<longleftrightarrow> x < y"
   230 | "(\<infinity>::ereal) < a           \<longleftrightarrow> False"
   231 | "         a < -(\<infinity>::ereal) \<longleftrightarrow> False"
   232 | "ereal x    < \<infinity>           \<longleftrightarrow> True"
   233 | "        -\<infinity> < ereal r     \<longleftrightarrow> True"
   234 | "        -\<infinity> < (\<infinity>::ereal) \<longleftrightarrow> True"
   235 proof -
   236   case (goal1 P x)
   237   then obtain a b where "x = (a,b)" by (cases x) auto
   238   with goal1 show P by (cases rule: ereal2_cases[of a b]) auto
   239 qed simp_all
   240 termination by (relation "{}") simp
   241 
   242 definition "x \<le> (y::ereal) \<longleftrightarrow> x < y \<or> x = y"
   243 
   244 lemma ereal_infty_less[simp]:
   245   fixes x :: ereal
   246   shows "x < \<infinity> \<longleftrightarrow> (x \<noteq> \<infinity>)"
   247     "-\<infinity> < x \<longleftrightarrow> (x \<noteq> -\<infinity>)"
   248   by (cases x, simp_all) (cases x, simp_all)
   249 
   250 lemma ereal_infty_less_eq[simp]:
   251   fixes x :: ereal
   252   shows "\<infinity> \<le> x \<longleftrightarrow> x = \<infinity>"
   253   "x \<le> -\<infinity> \<longleftrightarrow> x = -\<infinity>"
   254   by (auto simp add: less_eq_ereal_def)
   255 
   256 lemma ereal_less[simp]:
   257   "ereal r < 0 \<longleftrightarrow> (r < 0)"
   258   "0 < ereal r \<longleftrightarrow> (0 < r)"
   259   "0 < (\<infinity>::ereal)"
   260   "-(\<infinity>::ereal) < 0"
   261   by (simp_all add: zero_ereal_def)
   262 
   263 lemma ereal_less_eq[simp]:
   264   "x \<le> (\<infinity>::ereal)"
   265   "-(\<infinity>::ereal) \<le> x"
   266   "ereal r \<le> ereal p \<longleftrightarrow> r \<le> p"
   267   "ereal r \<le> 0 \<longleftrightarrow> r \<le> 0"
   268   "0 \<le> ereal r \<longleftrightarrow> 0 \<le> r"
   269   by (auto simp add: less_eq_ereal_def zero_ereal_def)
   270 
   271 lemma ereal_infty_less_eq2:
   272   "a \<le> b \<Longrightarrow> a = \<infinity> \<Longrightarrow> b = (\<infinity>::ereal)"
   273   "a \<le> b \<Longrightarrow> b = -\<infinity> \<Longrightarrow> a = -(\<infinity>::ereal)"
   274   by simp_all
   275 
   276 instance
   277 proof
   278   fix x y z :: ereal
   279   show "x \<le> x"
   280     by (cases x) simp_all
   281   show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
   282     by (cases rule: ereal2_cases[of x y]) auto
   283   show "x \<le> y \<or> y \<le> x "
   284     by (cases rule: ereal2_cases[of x y]) auto
   285   { assume "x \<le> y" "y \<le> x" then show "x = y"
   286     by (cases rule: ereal2_cases[of x y]) auto }
   287   { assume "x \<le> y" "y \<le> z" then show "x \<le> z"
   288     by (cases rule: ereal3_cases[of x y z]) auto }
   289 qed
   290 
   291 end
   292 
   293 lemma ereal_dense2: "x < y \<Longrightarrow> \<exists>z. x < ereal z \<and> ereal z < y"
   294   using lt_ex gt_ex dense by (cases x y rule: ereal2_cases) auto
   295 
   296 instance ereal :: dense_linorder
   297   by default (blast dest: ereal_dense2)
   298 
   299 instance ereal :: ordered_ab_semigroup_add
   300 proof
   301   fix a b c :: ereal assume "a \<le> b" then show "c + a \<le> c + b"
   302     by (cases rule: ereal3_cases[of a b c]) auto
   303 qed
   304 
   305 lemma real_of_ereal_positive_mono:
   306   fixes x y :: ereal shows "\<lbrakk>0 \<le> x; x \<le> y; y \<noteq> \<infinity>\<rbrakk> \<Longrightarrow> real x \<le> real y"
   307   by (cases rule: ereal2_cases[of x y]) auto
   308 
   309 lemma ereal_MInfty_lessI[intro, simp]:
   310   fixes a :: ereal shows "a \<noteq> -\<infinity> \<Longrightarrow> -\<infinity> < a"
   311   by (cases a) auto
   312 
   313 lemma ereal_less_PInfty[intro, simp]:
   314   fixes a :: ereal shows "a \<noteq> \<infinity> \<Longrightarrow> a < \<infinity>"
   315   by (cases a) auto
   316 
   317 lemma ereal_less_ereal_Ex:
   318   fixes a b :: ereal
   319   shows "x < ereal r \<longleftrightarrow> x = -\<infinity> \<or> (\<exists>p. p < r \<and> x = ereal p)"
   320   by (cases x) auto
   321 
   322 lemma less_PInf_Ex_of_nat: "x \<noteq> \<infinity> \<longleftrightarrow> (\<exists>n::nat. x < ereal (real n))"
   323 proof (cases x)
   324   case (real r) then show ?thesis
   325     using reals_Archimedean2[of r] by simp
   326 qed simp_all
   327 
   328 lemma ereal_add_mono:
   329   fixes a b c d :: ereal assumes "a \<le> b" "c \<le> d" shows "a + c \<le> b + d"
   330   using assms
   331   apply (cases a)
   332   apply (cases rule: ereal3_cases[of b c d], auto)
   333   apply (cases rule: ereal3_cases[of b c d], auto)
   334   done
   335 
   336 lemma ereal_minus_le_minus[simp]:
   337   fixes a b :: ereal shows "- a \<le> - b \<longleftrightarrow> b \<le> a"
   338   by (cases rule: ereal2_cases[of a b]) auto
   339 
   340 lemma ereal_minus_less_minus[simp]:
   341   fixes a b :: ereal shows "- a < - b \<longleftrightarrow> b < a"
   342   by (cases rule: ereal2_cases[of a b]) auto
   343 
   344 lemma ereal_le_real_iff:
   345   "x \<le> real y \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> ereal x \<le> y) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> x \<le> 0))"
   346   by (cases y) auto
   347 
   348 lemma real_le_ereal_iff:
   349   "real y \<le> x \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> y \<le> ereal x) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> 0 \<le> x))"
   350   by (cases y) auto
   351 
   352 lemma ereal_less_real_iff:
   353   "x < real y \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> ereal x < y) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> x < 0))"
   354   by (cases y) auto
   355 
   356 lemma real_less_ereal_iff:
   357   "real y < x \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> y < ereal x) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> 0 < x))"
   358   by (cases y) auto
   359 
   360 lemma real_of_ereal_pos:
   361   fixes x :: ereal shows "0 \<le> x \<Longrightarrow> 0 \<le> real x" by (cases x) auto
   362 
   363 lemmas real_of_ereal_ord_simps =
   364   ereal_le_real_iff real_le_ereal_iff ereal_less_real_iff real_less_ereal_iff
   365 
   366 lemma abs_ereal_ge0[simp]: "0 \<le> x \<Longrightarrow> \<bar>x :: ereal\<bar> = x"
   367   by (cases x) auto
   368 
   369 lemma abs_ereal_less0[simp]: "x < 0 \<Longrightarrow> \<bar>x :: ereal\<bar> = -x"
   370   by (cases x) auto
   371 
   372 lemma abs_ereal_pos[simp]: "0 \<le> \<bar>x :: ereal\<bar>"
   373   by (cases x) auto
   374 
   375 lemma real_of_ereal_le_0[simp]: "real (x :: ereal) \<le> 0 \<longleftrightarrow> (x \<le> 0 \<or> x = \<infinity>)"
   376   by (cases x) auto
   377 
   378 lemma abs_real_of_ereal[simp]: "\<bar>real (x :: ereal)\<bar> = real \<bar>x\<bar>"
   379   by (cases x) auto
   380 
   381 lemma zero_less_real_of_ereal:
   382   fixes x :: ereal shows "0 < real x \<longleftrightarrow> (0 < x \<and> x \<noteq> \<infinity>)"
   383   by (cases x) auto
   384 
   385 lemma ereal_0_le_uminus_iff[simp]:
   386   fixes a :: ereal shows "0 \<le> -a \<longleftrightarrow> a \<le> 0"
   387   by (cases rule: ereal2_cases[of a]) auto
   388 
   389 lemma ereal_uminus_le_0_iff[simp]:
   390   fixes a :: ereal shows "-a \<le> 0 \<longleftrightarrow> 0 \<le> a"
   391   by (cases rule: ereal2_cases[of a]) auto
   392 
   393 lemma ereal_add_strict_mono:
   394   fixes a b c d :: ereal
   395   assumes "a = b" "0 \<le> a" "a \<noteq> \<infinity>" "c < d"
   396   shows "a + c < b + d"
   397   using assms by (cases rule: ereal3_cases[case_product ereal_cases, of a b c d]) auto
   398 
   399 lemma ereal_less_add: 
   400   fixes a b c :: ereal shows "\<bar>a\<bar> \<noteq> \<infinity> \<Longrightarrow> c < b \<Longrightarrow> a + c < a + b"
   401   by (cases rule: ereal2_cases[of b c]) auto
   402 
   403 lemma ereal_uminus_eq_reorder: "- a = b \<longleftrightarrow> a = (-b::ereal)" by auto
   404 
   405 lemma ereal_uminus_less_reorder: "- a < b \<longleftrightarrow> -b < (a::ereal)"
   406   by (subst (3) ereal_uminus_uminus[symmetric]) (simp only: ereal_minus_less_minus)
   407 
   408 lemma ereal_uminus_le_reorder: "- a \<le> b \<longleftrightarrow> -b \<le> (a::ereal)"
   409   by (subst (3) ereal_uminus_uminus[symmetric]) (simp only: ereal_minus_le_minus)
   410 
   411 lemmas ereal_uminus_reorder =
   412   ereal_uminus_eq_reorder ereal_uminus_less_reorder ereal_uminus_le_reorder
   413 
   414 lemma ereal_bot:
   415   fixes x :: ereal assumes "\<And>B. x \<le> ereal B" shows "x = - \<infinity>"
   416 proof (cases x)
   417   case (real r) with assms[of "r - 1"] show ?thesis by auto
   418 next
   419   case PInf with assms[of 0] show ?thesis by auto
   420 next
   421   case MInf then show ?thesis by simp
   422 qed
   423 
   424 lemma ereal_top:
   425   fixes x :: ereal assumes "\<And>B. x \<ge> ereal B" shows "x = \<infinity>"
   426 proof (cases x)
   427   case (real r) with assms[of "r + 1"] show ?thesis by auto
   428 next
   429   case MInf with assms[of 0] show ?thesis by auto
   430 next
   431   case PInf then show ?thesis by simp
   432 qed
   433 
   434 lemma
   435   shows ereal_max[simp]: "ereal (max x y) = max (ereal x) (ereal y)"
   436     and ereal_min[simp]: "ereal (min x y) = min (ereal x) (ereal y)"
   437   by (simp_all add: min_def max_def)
   438 
   439 lemma ereal_max_0: "max 0 (ereal r) = ereal (max 0 r)"
   440   by (auto simp: zero_ereal_def)
   441 
   442 lemma
   443   fixes f :: "nat \<Rightarrow> ereal"
   444   shows incseq_uminus[simp]: "incseq (\<lambda>x. - f x) \<longleftrightarrow> decseq f"
   445   and decseq_uminus[simp]: "decseq (\<lambda>x. - f x) \<longleftrightarrow> incseq f"
   446   unfolding decseq_def incseq_def by auto
   447 
   448 lemma incseq_ereal: "incseq f \<Longrightarrow> incseq (\<lambda>x. ereal (f x))"
   449   unfolding incseq_def by auto
   450 
   451 lemma ereal_add_nonneg_nonneg:
   452   fixes a b :: ereal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a + b"
   453   using add_mono[of 0 a 0 b] by simp
   454 
   455 lemma image_eqD: "f ` A = B \<Longrightarrow> (\<forall>x\<in>A. f x \<in> B)"
   456   by auto
   457 
   458 lemma incseq_setsumI:
   459   fixes f :: "nat \<Rightarrow> 'a::{comm_monoid_add, ordered_ab_semigroup_add}"
   460   assumes "\<And>i. 0 \<le> f i"
   461   shows "incseq (\<lambda>i. setsum f {..< i})"
   462 proof (intro incseq_SucI)
   463   fix n have "setsum f {..< n} + 0 \<le> setsum f {..<n} + f n"
   464     using assms by (rule add_left_mono)
   465   then show "setsum f {..< n} \<le> setsum f {..< Suc n}"
   466     by auto
   467 qed
   468 
   469 lemma incseq_setsumI2:
   470   fixes f :: "'i \<Rightarrow> nat \<Rightarrow> 'a::{comm_monoid_add, ordered_ab_semigroup_add}"
   471   assumes "\<And>n. n \<in> A \<Longrightarrow> incseq (f n)"
   472   shows "incseq (\<lambda>i. \<Sum>n\<in>A. f n i)"
   473   using assms unfolding incseq_def by (auto intro: setsum_mono)
   474 
   475 subsubsection "Multiplication"
   476 
   477 instantiation ereal :: "{comm_monoid_mult, sgn}"
   478 begin
   479 
   480 function sgn_ereal :: "ereal \<Rightarrow> ereal" where
   481   "sgn (ereal r) = ereal (sgn r)"
   482 | "sgn (\<infinity>::ereal) = 1"
   483 | "sgn (-\<infinity>::ereal) = -1"
   484 by (auto intro: ereal_cases)
   485 termination proof qed (rule wf_empty)
   486 
   487 function times_ereal where
   488 "ereal r * ereal p = ereal (r * p)" |
   489 "ereal r * \<infinity> = (if r = 0 then 0 else if r > 0 then \<infinity> else -\<infinity>)" |
   490 "\<infinity> * ereal r = (if r = 0 then 0 else if r > 0 then \<infinity> else -\<infinity>)" |
   491 "ereal r * -\<infinity> = (if r = 0 then 0 else if r > 0 then -\<infinity> else \<infinity>)" |
   492 "-\<infinity> * ereal r = (if r = 0 then 0 else if r > 0 then -\<infinity> else \<infinity>)" |
   493 "(\<infinity>::ereal) * \<infinity> = \<infinity>" |
   494 "-(\<infinity>::ereal) * \<infinity> = -\<infinity>" |
   495 "(\<infinity>::ereal) * -\<infinity> = -\<infinity>" |
   496 "-(\<infinity>::ereal) * -\<infinity> = \<infinity>"
   497 proof -
   498   case (goal1 P x)
   499   then obtain a b where "x = (a, b)" by (cases x) auto
   500   with goal1 show P by (cases rule: ereal2_cases[of a b]) auto
   501 qed simp_all
   502 termination by (relation "{}") simp
   503 
   504 instance
   505 proof
   506   fix a b c :: ereal show "1 * a = a"
   507     by (cases a) (simp_all add: one_ereal_def)
   508   show "a * b = b * a"
   509     by (cases rule: ereal2_cases[of a b]) simp_all
   510   show "a * b * c = a * (b * c)"
   511     by (cases rule: ereal3_cases[of a b c])
   512        (simp_all add: zero_ereal_def zero_less_mult_iff)
   513 qed
   514 end
   515 
   516 lemma real_ereal_1[simp]: "real (1::ereal) = 1"
   517   unfolding one_ereal_def by simp
   518 
   519 lemma real_of_ereal_le_1:
   520   fixes a :: ereal shows "a \<le> 1 \<Longrightarrow> real a \<le> 1"
   521   by (cases a) (auto simp: one_ereal_def)
   522 
   523 lemma abs_ereal_one[simp]: "\<bar>1\<bar> = (1::ereal)"
   524   unfolding one_ereal_def by simp
   525 
   526 lemma ereal_mult_zero[simp]:
   527   fixes a :: ereal shows "a * 0 = 0"
   528   by (cases a) (simp_all add: zero_ereal_def)
   529 
   530 lemma ereal_zero_mult[simp]:
   531   fixes a :: ereal shows "0 * a = 0"
   532   by (cases a) (simp_all add: zero_ereal_def)
   533 
   534 lemma ereal_m1_less_0[simp]:
   535   "-(1::ereal) < 0"
   536   by (simp add: zero_ereal_def one_ereal_def)
   537 
   538 lemma ereal_zero_m1[simp]:
   539   "1 \<noteq> (0::ereal)"
   540   by (simp add: zero_ereal_def one_ereal_def)
   541 
   542 lemma ereal_times_0[simp]:
   543   fixes x :: ereal shows "0 * x = 0"
   544   by (cases x) (auto simp: zero_ereal_def)
   545 
   546 lemma ereal_times[simp]:
   547   "1 \<noteq> (\<infinity>::ereal)" "(\<infinity>::ereal) \<noteq> 1"
   548   "1 \<noteq> -(\<infinity>::ereal)" "-(\<infinity>::ereal) \<noteq> 1"
   549   by (auto simp add: times_ereal_def one_ereal_def)
   550 
   551 lemma ereal_plus_1[simp]:
   552   "1 + ereal r = ereal (r + 1)" "ereal r + 1 = ereal (r + 1)"
   553   "1 + -(\<infinity>::ereal) = -\<infinity>" "-(\<infinity>::ereal) + 1 = -\<infinity>"
   554   unfolding one_ereal_def by auto
   555 
   556 lemma ereal_zero_times[simp]:
   557   fixes a b :: ereal shows "a * b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
   558   by (cases rule: ereal2_cases[of a b]) auto
   559 
   560 lemma ereal_mult_eq_PInfty[simp]:
   561   shows "a * b = (\<infinity>::ereal) \<longleftrightarrow>
   562     (a = \<infinity> \<and> b > 0) \<or> (a > 0 \<and> b = \<infinity>) \<or> (a = -\<infinity> \<and> b < 0) \<or> (a < 0 \<and> b = -\<infinity>)"
   563   by (cases rule: ereal2_cases[of a b]) auto
   564 
   565 lemma ereal_mult_eq_MInfty[simp]:
   566   shows "a * b = -(\<infinity>::ereal) \<longleftrightarrow>
   567     (a = \<infinity> \<and> b < 0) \<or> (a < 0 \<and> b = \<infinity>) \<or> (a = -\<infinity> \<and> b > 0) \<or> (a > 0 \<and> b = -\<infinity>)"
   568   by (cases rule: ereal2_cases[of a b]) auto
   569 
   570 lemma ereal_0_less_1[simp]: "0 < (1::ereal)"
   571   by (simp_all add: zero_ereal_def one_ereal_def)
   572 
   573 lemma ereal_zero_one[simp]: "0 \<noteq> (1::ereal)"
   574   by (simp_all add: zero_ereal_def one_ereal_def)
   575 
   576 lemma ereal_mult_minus_left[simp]:
   577   fixes a b :: ereal shows "-a * b = - (a * b)"
   578   by (cases rule: ereal2_cases[of a b]) auto
   579 
   580 lemma ereal_mult_minus_right[simp]:
   581   fixes a b :: ereal shows "a * -b = - (a * b)"
   582   by (cases rule: ereal2_cases[of a b]) auto
   583 
   584 lemma ereal_mult_infty[simp]:
   585   "a * (\<infinity>::ereal) = (if a = 0 then 0 else if 0 < a then \<infinity> else - \<infinity>)"
   586   by (cases a) auto
   587 
   588 lemma ereal_infty_mult[simp]:
   589   "(\<infinity>::ereal) * a = (if a = 0 then 0 else if 0 < a then \<infinity> else - \<infinity>)"
   590   by (cases a) auto
   591 
   592 lemma ereal_mult_strict_right_mono:
   593   assumes "a < b" and "0 < c" "c < (\<infinity>::ereal)"
   594   shows "a * c < b * c"
   595   using assms
   596   by (cases rule: ereal3_cases[of a b c])
   597      (auto simp: zero_le_mult_iff)
   598 
   599 lemma ereal_mult_strict_left_mono:
   600   "\<lbrakk> a < b ; 0 < c ; c < (\<infinity>::ereal)\<rbrakk> \<Longrightarrow> c * a < c * b"
   601   using ereal_mult_strict_right_mono by (simp add: mult_commute[of c])
   602 
   603 lemma ereal_mult_right_mono:
   604   fixes a b c :: ereal shows "\<lbrakk>a \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> a*c \<le> b*c"
   605   using assms
   606   apply (cases "c = 0") apply simp
   607   by (cases rule: ereal3_cases[of a b c])
   608      (auto simp: zero_le_mult_iff)
   609 
   610 lemma ereal_mult_left_mono:
   611   fixes a b c :: ereal shows "\<lbrakk>a \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> c * a \<le> c * b"
   612   using ereal_mult_right_mono by (simp add: mult_commute[of c])
   613 
   614 lemma zero_less_one_ereal[simp]: "0 \<le> (1::ereal)"
   615   by (simp add: one_ereal_def zero_ereal_def)
   616 
   617 lemma ereal_0_le_mult[simp]: "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a * (b :: ereal)"
   618   by (cases rule: ereal2_cases[of a b]) (auto simp: mult_nonneg_nonneg)
   619 
   620 lemma ereal_right_distrib:
   621   fixes r a b :: ereal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> r * (a + b) = r * a + r * b"
   622   by (cases rule: ereal3_cases[of r a b]) (simp_all add: field_simps)
   623 
   624 lemma ereal_left_distrib:
   625   fixes r a b :: ereal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> (a + b) * r = a * r + b * r"
   626   by (cases rule: ereal3_cases[of r a b]) (simp_all add: field_simps)
   627 
   628 lemma ereal_mult_le_0_iff:
   629   fixes a b :: ereal
   630   shows "a * b \<le> 0 \<longleftrightarrow> (0 \<le> a \<and> b \<le> 0) \<or> (a \<le> 0 \<and> 0 \<le> b)"
   631   by (cases rule: ereal2_cases[of a b]) (simp_all add: mult_le_0_iff)
   632 
   633 lemma ereal_zero_le_0_iff:
   634   fixes a b :: ereal
   635   shows "0 \<le> a * b \<longleftrightarrow> (0 \<le> a \<and> 0 \<le> b) \<or> (a \<le> 0 \<and> b \<le> 0)"
   636   by (cases rule: ereal2_cases[of a b]) (simp_all add: zero_le_mult_iff)
   637 
   638 lemma ereal_mult_less_0_iff:
   639   fixes a b :: ereal
   640   shows "a * b < 0 \<longleftrightarrow> (0 < a \<and> b < 0) \<or> (a < 0 \<and> 0 < b)"
   641   by (cases rule: ereal2_cases[of a b]) (simp_all add: mult_less_0_iff)
   642 
   643 lemma ereal_zero_less_0_iff:
   644   fixes a b :: ereal
   645   shows "0 < a * b \<longleftrightarrow> (0 < a \<and> 0 < b) \<or> (a < 0 \<and> b < 0)"
   646   by (cases rule: ereal2_cases[of a b]) (simp_all add: zero_less_mult_iff)
   647 
   648 lemma ereal_left_mult_cong:
   649   fixes a b c :: ereal
   650   shows "(c \<noteq> 0 \<Longrightarrow> a = b) \<Longrightarrow> c * a = c * b"
   651   by (cases "c = 0") simp_all
   652 
   653 lemma ereal_right_mult_cong:
   654   fixes a b c :: ereal
   655   shows "(c \<noteq> 0 \<Longrightarrow> a = b) \<Longrightarrow> a * c = b * c"
   656   by (cases "c = 0") simp_all
   657 
   658 lemma ereal_distrib:
   659   fixes a b c :: ereal
   660   assumes "a \<noteq> \<infinity> \<or> b \<noteq> -\<infinity>" "a \<noteq> -\<infinity> \<or> b \<noteq> \<infinity>" "\<bar>c\<bar> \<noteq> \<infinity>"
   661   shows "(a + b) * c = a * c + b * c"
   662   using assms
   663   by (cases rule: ereal3_cases[of a b c]) (simp_all add: field_simps)
   664 
   665 lemma numeral_eq_ereal [simp]: "numeral w = ereal (numeral w)"
   666   apply (induct w rule: num_induct)
   667   apply (simp only: numeral_One one_ereal_def)
   668   apply (simp only: numeral_inc ereal_plus_1)
   669   done
   670 
   671 lemma ereal_le_epsilon:
   672   fixes x y :: ereal
   673   assumes "ALL e. 0 < e --> x <= y + e"
   674   shows "x <= y"
   675 proof-
   676 { assume a: "EX r. y = ereal r"
   677   then obtain r where r_def: "y = ereal r" by auto
   678   { assume "x=(-\<infinity>)" hence ?thesis by auto }
   679   moreover
   680   { assume "~(x=(-\<infinity>))"
   681     then obtain p where p_def: "x = ereal p"
   682     using a assms[rule_format, of 1] by (cases x) auto
   683     { fix e have "0 < e --> p <= r + e"
   684       using assms[rule_format, of "ereal e"] p_def r_def by auto }
   685     hence "p <= r" apply (subst field_le_epsilon) by auto
   686     hence ?thesis using r_def p_def by auto
   687   } ultimately have ?thesis by blast
   688 }
   689 moreover
   690 { assume "y=(-\<infinity>) | y=\<infinity>" hence ?thesis
   691     using assms[rule_format, of 1] by (cases x) auto
   692 } ultimately show ?thesis by (cases y) auto
   693 qed
   694 
   695 
   696 lemma ereal_le_epsilon2:
   697   fixes x y :: ereal
   698   assumes "ALL e. 0 < e --> x <= y + ereal e"
   699   shows "x <= y"
   700 proof-
   701 { fix e :: ereal assume "e>0"
   702   { assume "e=\<infinity>" hence "x<=y+e" by auto }
   703   moreover
   704   { assume "e~=\<infinity>"
   705     then obtain r where "e = ereal r" using `e>0` apply (cases e) by auto
   706     hence "x<=y+e" using assms[rule_format, of r] `e>0` by auto
   707   } ultimately have "x<=y+e" by blast
   708 } then show ?thesis using ereal_le_epsilon by auto
   709 qed
   710 
   711 lemma ereal_le_real:
   712   fixes x y :: ereal
   713   assumes "ALL z. x <= ereal z --> y <= ereal z"
   714   shows "y <= x"
   715 by (metis assms ereal_bot ereal_cases ereal_infty_less_eq(2) ereal_less_eq(1) linorder_le_cases)
   716 
   717 lemma setprod_ereal_0:
   718   fixes f :: "'a \<Rightarrow> ereal"
   719   shows "(\<Prod>i\<in>A. f i) = 0 \<longleftrightarrow> (finite A \<and> (\<exists>i\<in>A. f i = 0))"
   720 proof cases
   721   assume "finite A"
   722   then show ?thesis by (induct A) auto
   723 qed auto
   724 
   725 lemma setprod_ereal_pos:
   726   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)"
   727 proof cases
   728   assume "finite I" from this pos show ?thesis by induct auto
   729 qed simp
   730 
   731 lemma setprod_PInf:
   732   fixes f :: "'a \<Rightarrow> ereal"
   733   assumes "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i"
   734   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)"
   735 proof cases
   736   assume "finite I" from this assms show ?thesis
   737   proof (induct I)
   738     case (insert i I)
   739     then have pos: "0 \<le> f i" "0 \<le> setprod f I" by (auto intro!: setprod_ereal_pos)
   740     from insert have "(\<Prod>j\<in>insert i I. f j) = \<infinity> \<longleftrightarrow> setprod f I * f i = \<infinity>" by auto
   741     also have "\<dots> \<longleftrightarrow> (setprod f I = \<infinity> \<or> f i = \<infinity>) \<and> f i \<noteq> 0 \<and> setprod f I \<noteq> 0"
   742       using setprod_ereal_pos[of I f] pos
   743       by (cases rule: ereal2_cases[of "f i" "setprod f I"]) auto
   744     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)"
   745       using insert by (auto simp: setprod_ereal_0)
   746     finally show ?case .
   747   qed simp
   748 qed simp
   749 
   750 lemma setprod_ereal: "(\<Prod>i\<in>A. ereal (f i)) = ereal (setprod f A)"
   751 proof cases
   752   assume "finite A" then show ?thesis
   753     by induct (auto simp: one_ereal_def)
   754 qed (simp add: one_ereal_def)
   755 
   756 subsubsection {* Power *}
   757 
   758 lemma ereal_power[simp]: "(ereal x) ^ n = ereal (x^n)"
   759   by (induct n) (auto simp: one_ereal_def)
   760 
   761 lemma ereal_power_PInf[simp]: "(\<infinity>::ereal) ^ n = (if n = 0 then 1 else \<infinity>)"
   762   by (induct n) (auto simp: one_ereal_def)
   763 
   764 lemma ereal_power_uminus[simp]:
   765   fixes x :: ereal
   766   shows "(- x) ^ n = (if even n then x ^ n else - (x^n))"
   767   by (induct n) (auto simp: one_ereal_def)
   768 
   769 lemma ereal_power_numeral[simp]:
   770   "(numeral num :: ereal) ^ n = ereal (numeral num ^ n)"
   771   by (induct n) (auto simp: one_ereal_def)
   772 
   773 lemma zero_le_power_ereal[simp]:
   774   fixes a :: ereal assumes "0 \<le> a"
   775   shows "0 \<le> a ^ n"
   776   using assms by (induct n) (auto simp: ereal_zero_le_0_iff)
   777 
   778 subsubsection {* Subtraction *}
   779 
   780 lemma ereal_minus_minus_image[simp]:
   781   fixes S :: "ereal set"
   782   shows "uminus ` uminus ` S = S"
   783   by (auto simp: image_iff)
   784 
   785 lemma ereal_uminus_lessThan[simp]:
   786   fixes a :: ereal shows "uminus ` {..<a} = {-a<..}"
   787 proof -
   788   {
   789     fix x assume "-a < x"
   790     then have "- x < - (- a)" by (simp del: ereal_uminus_uminus)
   791     then have "- x < a" by simp
   792   }
   793   then show ?thesis by (auto intro!: image_eqI)
   794 qed
   795 
   796 lemma ereal_uminus_greaterThan[simp]:
   797   "uminus ` {(a::ereal)<..} = {..<-a}"
   798   by (metis ereal_uminus_lessThan ereal_uminus_uminus
   799             ereal_minus_minus_image)
   800 
   801 instantiation ereal :: minus
   802 begin
   803 definition "x - y = x + -(y::ereal)"
   804 instance ..
   805 end
   806 
   807 lemma ereal_minus[simp]:
   808   "ereal r - ereal p = ereal (r - p)"
   809   "-\<infinity> - ereal r = -\<infinity>"
   810   "ereal r - \<infinity> = -\<infinity>"
   811   "(\<infinity>::ereal) - x = \<infinity>"
   812   "-(\<infinity>::ereal) - \<infinity> = -\<infinity>"
   813   "x - -y = x + y"
   814   "x - 0 = x"
   815   "0 - x = -x"
   816   by (simp_all add: minus_ereal_def)
   817 
   818 lemma ereal_x_minus_x[simp]:
   819   "x - x = (if \<bar>x\<bar> = \<infinity> then \<infinity> else 0::ereal)"
   820   by (cases x) simp_all
   821 
   822 lemma ereal_eq_minus_iff:
   823   fixes x y z :: ereal
   824   shows "x = z - y \<longleftrightarrow>
   825     (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x + y = z) \<and>
   826     (y = -\<infinity> \<longrightarrow> x = \<infinity>) \<and>
   827     (y = \<infinity> \<longrightarrow> z = \<infinity> \<longrightarrow> x = \<infinity>) \<and>
   828     (y = \<infinity> \<longrightarrow> z \<noteq> \<infinity> \<longrightarrow> x = -\<infinity>)"
   829   by (cases rule: ereal3_cases[of x y z]) auto
   830 
   831 lemma ereal_eq_minus:
   832   fixes x y z :: ereal
   833   shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x = z - y \<longleftrightarrow> x + y = z"
   834   by (auto simp: ereal_eq_minus_iff)
   835 
   836 lemma ereal_less_minus_iff:
   837   fixes x y z :: ereal
   838   shows "x < z - y \<longleftrightarrow>
   839     (y = \<infinity> \<longrightarrow> z = \<infinity> \<and> x \<noteq> \<infinity>) \<and>
   840     (y = -\<infinity> \<longrightarrow> x \<noteq> \<infinity>) \<and>
   841     (\<bar>y\<bar> \<noteq> \<infinity>\<longrightarrow> x + y < z)"
   842   by (cases rule: ereal3_cases[of x y z]) auto
   843 
   844 lemma ereal_less_minus:
   845   fixes x y z :: ereal
   846   shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x < z - y \<longleftrightarrow> x + y < z"
   847   by (auto simp: ereal_less_minus_iff)
   848 
   849 lemma ereal_le_minus_iff:
   850   fixes x y z :: ereal
   851   shows "x \<le> z - y \<longleftrightarrow>
   852     (y = \<infinity> \<longrightarrow> z \<noteq> \<infinity> \<longrightarrow> x = -\<infinity>) \<and>
   853     (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x + y \<le> z)"
   854   by (cases rule: ereal3_cases[of x y z]) auto
   855 
   856 lemma ereal_le_minus:
   857   fixes x y z :: ereal
   858   shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x \<le> z - y \<longleftrightarrow> x + y \<le> z"
   859   by (auto simp: ereal_le_minus_iff)
   860 
   861 lemma ereal_minus_less_iff:
   862   fixes x y z :: ereal
   863   shows "x - y < z \<longleftrightarrow>
   864     y \<noteq> -\<infinity> \<and> (y = \<infinity> \<longrightarrow> x \<noteq> \<infinity> \<and> z \<noteq> -\<infinity>) \<and>
   865     (y \<noteq> \<infinity> \<longrightarrow> x < z + y)"
   866   by (cases rule: ereal3_cases[of x y z]) auto
   867 
   868 lemma ereal_minus_less:
   869   fixes x y z :: ereal
   870   shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x - y < z \<longleftrightarrow> x < z + y"
   871   by (auto simp: ereal_minus_less_iff)
   872 
   873 lemma ereal_minus_le_iff:
   874   fixes x y z :: ereal
   875   shows "x - y \<le> z \<longleftrightarrow>
   876     (y = -\<infinity> \<longrightarrow> z = \<infinity>) \<and>
   877     (y = \<infinity> \<longrightarrow> x = \<infinity> \<longrightarrow> z = \<infinity>) \<and>
   878     (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x \<le> z + y)"
   879   by (cases rule: ereal3_cases[of x y z]) auto
   880 
   881 lemma ereal_minus_le:
   882   fixes x y z :: ereal
   883   shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x - y \<le> z \<longleftrightarrow> x \<le> z + y"
   884   by (auto simp: ereal_minus_le_iff)
   885 
   886 lemma ereal_minus_eq_minus_iff:
   887   fixes a b c :: ereal
   888   shows "a - b = a - c \<longleftrightarrow>
   889     b = c \<or> a = \<infinity> \<or> (a = -\<infinity> \<and> b \<noteq> -\<infinity> \<and> c \<noteq> -\<infinity>)"
   890   by (cases rule: ereal3_cases[of a b c]) auto
   891 
   892 lemma ereal_add_le_add_iff:
   893   fixes a b c :: ereal
   894   shows "c + a \<le> c + b \<longleftrightarrow>
   895     a \<le> b \<or> c = \<infinity> \<or> (c = -\<infinity> \<and> a \<noteq> \<infinity> \<and> b \<noteq> \<infinity>)"
   896   by (cases rule: ereal3_cases[of a b c]) (simp_all add: field_simps)
   897 
   898 lemma ereal_mult_le_mult_iff:
   899   fixes a b c :: ereal
   900   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)"
   901   by (cases rule: ereal3_cases[of a b c]) (simp_all add: mult_le_cancel_left)
   902 
   903 lemma ereal_minus_mono:
   904   fixes A B C D :: ereal assumes "A \<le> B" "D \<le> C"
   905   shows "A - C \<le> B - D"
   906   using assms
   907   by (cases rule: ereal3_cases[case_product ereal_cases, of A B C D]) simp_all
   908 
   909 lemma real_of_ereal_minus:
   910   fixes a b :: ereal
   911   shows "real (a - b) = (if \<bar>a\<bar> = \<infinity> \<or> \<bar>b\<bar> = \<infinity> then 0 else real a - real b)"
   912   by (cases rule: ereal2_cases[of a b]) auto
   913 
   914 lemma ereal_diff_positive:
   915   fixes a b :: ereal shows "a \<le> b \<Longrightarrow> 0 \<le> b - a"
   916   by (cases rule: ereal2_cases[of a b]) auto
   917 
   918 lemma ereal_between:
   919   fixes x e :: ereal
   920   assumes "\<bar>x\<bar> \<noteq> \<infinity>" "0 < e"
   921   shows "x - e < x" "x < x + e"
   922 using assms apply (cases x, cases e) apply auto
   923 using assms apply (cases x, cases e) apply auto
   924 done
   925 
   926 lemma ereal_minus_eq_PInfty_iff:
   927   fixes x y :: ereal shows "x - y = \<infinity> \<longleftrightarrow> y = -\<infinity> \<or> x = \<infinity>"
   928   by (cases x y rule: ereal2_cases) simp_all
   929 
   930 subsubsection {* Division *}
   931 
   932 instantiation ereal :: inverse
   933 begin
   934 
   935 function inverse_ereal where
   936 "inverse (ereal r) = (if r = 0 then \<infinity> else ereal (inverse r))" |
   937 "inverse (\<infinity>::ereal) = 0" |
   938 "inverse (-\<infinity>::ereal) = 0"
   939   by (auto intro: ereal_cases)
   940 termination by (relation "{}") simp
   941 
   942 definition "x / y = x * inverse (y :: ereal)"
   943 
   944 instance ..
   945 end
   946 
   947 lemma real_of_ereal_inverse[simp]:
   948   fixes a :: ereal
   949   shows "real (inverse a) = 1 / real a"
   950   by (cases a) (auto simp: inverse_eq_divide)
   951 
   952 lemma ereal_inverse[simp]:
   953   "inverse (0::ereal) = \<infinity>"
   954   "inverse (1::ereal) = 1"
   955   by (simp_all add: one_ereal_def zero_ereal_def)
   956 
   957 lemma ereal_divide[simp]:
   958   "ereal r / ereal p = (if p = 0 then ereal r * \<infinity> else ereal (r / p))"
   959   unfolding divide_ereal_def by (auto simp: divide_real_def)
   960 
   961 lemma ereal_divide_same[simp]:
   962   fixes x :: ereal shows "x / x = (if \<bar>x\<bar> = \<infinity> \<or> x = 0 then 0 else 1)"
   963   by (cases x)
   964      (simp_all add: divide_real_def divide_ereal_def one_ereal_def)
   965 
   966 lemma ereal_inv_inv[simp]:
   967   fixes x :: ereal shows "inverse (inverse x) = (if x \<noteq> -\<infinity> then x else \<infinity>)"
   968   by (cases x) auto
   969 
   970 lemma ereal_inverse_minus[simp]:
   971   fixes x :: ereal shows "inverse (- x) = (if x = 0 then \<infinity> else -inverse x)"
   972   by (cases x) simp_all
   973 
   974 lemma ereal_uminus_divide[simp]:
   975   fixes x y :: ereal shows "- x / y = - (x / y)"
   976   unfolding divide_ereal_def by simp
   977 
   978 lemma ereal_divide_Infty[simp]:
   979   fixes x :: ereal shows "x / \<infinity> = 0" "x / -\<infinity> = 0"
   980   unfolding divide_ereal_def by simp_all
   981 
   982 lemma ereal_divide_one[simp]:
   983   "x / 1 = (x::ereal)"
   984   unfolding divide_ereal_def by simp
   985 
   986 lemma ereal_divide_ereal[simp]:
   987   "\<infinity> / ereal r = (if 0 \<le> r then \<infinity> else -\<infinity>)"
   988   unfolding divide_ereal_def by simp
   989 
   990 lemma zero_le_divide_ereal[simp]:
   991   fixes a :: ereal assumes "0 \<le> a" "0 \<le> b"
   992   shows "0 \<le> a / b"
   993   using assms by (cases rule: ereal2_cases[of a b]) (auto simp: zero_le_divide_iff)
   994 
   995 lemma ereal_le_divide_pos:
   996   fixes x y z :: ereal shows "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y \<le> z / x \<longleftrightarrow> x * y \<le> z"
   997   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
   998 
   999 lemma ereal_divide_le_pos:
  1000   fixes x y z :: ereal shows "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> z / x \<le> y \<longleftrightarrow> z \<le> x * y"
  1001   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
  1002 
  1003 lemma ereal_le_divide_neg:
  1004   fixes x y z :: ereal shows "x < 0 \<Longrightarrow> x \<noteq> -\<infinity> \<Longrightarrow> y \<le> z / x \<longleftrightarrow> z \<le> x * y"
  1005   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
  1006 
  1007 lemma ereal_divide_le_neg:
  1008   fixes x y z :: ereal shows "x < 0 \<Longrightarrow> x \<noteq> -\<infinity> \<Longrightarrow> z / x \<le> y \<longleftrightarrow> x * y \<le> z"
  1009   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
  1010 
  1011 lemma ereal_inverse_antimono_strict:
  1012   fixes x y :: ereal
  1013   shows "0 \<le> x \<Longrightarrow> x < y \<Longrightarrow> inverse y < inverse x"
  1014   by (cases rule: ereal2_cases[of x y]) auto
  1015 
  1016 lemma ereal_inverse_antimono:
  1017   fixes x y :: ereal
  1018   shows "0 \<le> x \<Longrightarrow> x <= y \<Longrightarrow> inverse y <= inverse x"
  1019   by (cases rule: ereal2_cases[of x y]) auto
  1020 
  1021 lemma inverse_inverse_Pinfty_iff[simp]:
  1022   fixes x :: ereal shows "inverse x = \<infinity> \<longleftrightarrow> x = 0"
  1023   by (cases x) auto
  1024 
  1025 lemma ereal_inverse_eq_0:
  1026   fixes x :: ereal shows "inverse x = 0 \<longleftrightarrow> x = \<infinity> \<or> x = -\<infinity>"
  1027   by (cases x) auto
  1028 
  1029 lemma ereal_0_gt_inverse:
  1030   fixes x :: ereal shows "0 < inverse x \<longleftrightarrow> x \<noteq> \<infinity> \<and> 0 \<le> x"
  1031   by (cases x) auto
  1032 
  1033 lemma ereal_mult_less_right:
  1034   fixes a b c :: ereal
  1035   assumes "b * a < c * a" "0 < a" "a < \<infinity>"
  1036   shows "b < c"
  1037   using assms
  1038   by (cases rule: ereal3_cases[of a b c])
  1039      (auto split: split_if_asm simp: zero_less_mult_iff zero_le_mult_iff)
  1040 
  1041 lemma ereal_power_divide:
  1042   fixes x y :: ereal shows "y \<noteq> 0 \<Longrightarrow> (x / y) ^ n = x^n / y^n"
  1043   by (cases rule: ereal2_cases[of x y])
  1044      (auto simp: one_ereal_def zero_ereal_def power_divide not_le
  1045                  power_less_zero_eq zero_le_power_iff)
  1046 
  1047 lemma ereal_le_mult_one_interval:
  1048   fixes x y :: ereal
  1049   assumes y: "y \<noteq> -\<infinity>"
  1050   assumes z: "\<And>z. \<lbrakk> 0 < z ; z < 1 \<rbrakk> \<Longrightarrow> z * x \<le> y"
  1051   shows "x \<le> y"
  1052 proof (cases x)
  1053   case PInf with z[of "1 / 2"] show "x \<le> y" by (simp add: one_ereal_def)
  1054 next
  1055   case (real r) note r = this
  1056   show "x \<le> y"
  1057   proof (cases y)
  1058     case (real p) note p = this
  1059     have "r \<le> p"
  1060     proof (rule field_le_mult_one_interval)
  1061       fix z :: real assume "0 < z" and "z < 1"
  1062       with z[of "ereal z"]
  1063       show "z * r \<le> p" using p r by (auto simp: zero_le_mult_iff one_ereal_def)
  1064     qed
  1065     then show "x \<le> y" using p r by simp
  1066   qed (insert y, simp_all)
  1067 qed simp
  1068 
  1069 lemma ereal_divide_right_mono[simp]:
  1070   fixes x y z :: ereal
  1071   assumes "x \<le> y" "0 < z" shows "x / z \<le> y / z"
  1072 using assms by (cases x y z rule: ereal3_cases) (auto intro: divide_right_mono)
  1073 
  1074 lemma ereal_divide_left_mono[simp]:
  1075   fixes x y z :: ereal
  1076   assumes "y \<le> x" "0 < z" "0 < x * y"
  1077   shows "z / x \<le> z / y"
  1078 using assms by (cases x y z rule: ereal3_cases)
  1079   (auto intro: divide_left_mono simp: field_simps sign_simps split: split_if_asm)
  1080 
  1081 lemma ereal_divide_zero_left[simp]:
  1082   fixes a :: ereal
  1083   shows "0 / a = 0"
  1084   by (cases a) (auto simp: zero_ereal_def)
  1085 
  1086 lemma ereal_times_divide_eq_left[simp]:
  1087   fixes a b c :: ereal
  1088   shows "b / c * a = b * a / c"
  1089   by (cases a b c rule: ereal3_cases) (auto simp: field_simps sign_simps)
  1090 
  1091 subsection "Complete lattice"
  1092 
  1093 instantiation ereal :: lattice
  1094 begin
  1095 definition [simp]: "sup x y = (max x y :: ereal)"
  1096 definition [simp]: "inf x y = (min x y :: ereal)"
  1097 instance by default simp_all
  1098 end
  1099 
  1100 instantiation ereal :: complete_lattice
  1101 begin
  1102 
  1103 definition "bot = (-\<infinity>::ereal)"
  1104 definition "top = (\<infinity>::ereal)"
  1105 
  1106 definition "Sup S = (SOME x :: ereal. (\<forall>y\<in>S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>S. y \<le> z) \<longrightarrow> x \<le> z))"
  1107 definition "Inf S = (SOME x :: ereal. (\<forall>y\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> x))"
  1108 
  1109 lemma ereal_complete_Sup:
  1110   fixes S :: "ereal set"
  1111   shows "\<exists>x. (\<forall>y\<in>S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>S. y \<le> z) \<longrightarrow> x \<le> z)"
  1112 proof cases
  1113   assume "\<exists>x. \<forall>a\<in>S. a \<le> ereal x"
  1114   then obtain y where y: "\<And>a. a\<in>S \<Longrightarrow> a \<le> ereal y" by auto
  1115   then have "\<infinity> \<notin> S" by force
  1116   show ?thesis
  1117   proof cases
  1118     assume "S \<noteq> {-\<infinity>} \<and> S \<noteq> {}"
  1119     with `\<infinity> \<notin> S` obtain x where x: "x \<in> S" "\<bar>x\<bar> \<noteq> \<infinity>" by auto
  1120     obtain s where s: "\<forall>x\<in>ereal -` S. x \<le> s" "\<And>z. (\<forall>x\<in>ereal -` S. x \<le> z) \<Longrightarrow> s \<le> z"
  1121     proof (atomize_elim, rule complete_real)
  1122       show "\<exists>x. x \<in> ereal -` S" using x by auto
  1123       show "\<exists>z. \<forall>x\<in>ereal -` S. x \<le> z" by (auto dest: y intro!: exI[of _ y])
  1124     qed
  1125     show ?thesis
  1126     proof (safe intro!: exI[of _ "ereal s"])
  1127       fix y assume "y \<in> S" with s `\<infinity> \<notin> S` show "y \<le> ereal s"
  1128         by (cases y) auto
  1129     next
  1130       fix z assume "\<forall>y\<in>S. y \<le> z" with `S \<noteq> {-\<infinity>} \<and> S \<noteq> {}` show "ereal s \<le> z"
  1131         by (cases z) (auto intro!: s)
  1132     qed
  1133   qed (auto intro!: exI[of _ "-\<infinity>"])
  1134 qed (fastforce intro!: exI[of _ \<infinity>] ereal_top intro: order_trans dest: less_imp_le simp: not_le)
  1135 
  1136 lemma ereal_complete_uminus_eq:
  1137   fixes S :: "ereal set"
  1138   shows "(\<forall>y\<in>uminus`S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>uminus`S. y \<le> z) \<longrightarrow> x \<le> z)
  1139      \<longleftrightarrow> (\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> -x)"
  1140   by simp (metis ereal_minus_le_minus ereal_uminus_uminus)
  1141 
  1142 lemma ereal_complete_Inf:
  1143   "\<exists>x. (\<forall>y\<in>S::ereal set. x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> x)"
  1144   using ereal_complete_Sup[of "uminus ` S"] unfolding ereal_complete_uminus_eq by auto
  1145 
  1146 instance
  1147 proof
  1148   show "Sup {} = (bot::ereal)"
  1149   apply (auto simp: bot_ereal_def Sup_ereal_def)
  1150   apply (rule some1_equality)
  1151   apply (metis ereal_bot ereal_less_eq(2))
  1152   apply (metis ereal_less_eq(2))
  1153   done
  1154 next
  1155   show "Inf {} = (top::ereal)"
  1156   apply (auto simp: top_ereal_def Inf_ereal_def)
  1157   apply (rule some1_equality)
  1158   apply (metis ereal_top ereal_less_eq(1))
  1159   apply (metis ereal_less_eq(1))
  1160   done
  1161 qed (auto intro: someI2_ex ereal_complete_Sup ereal_complete_Inf
  1162   simp: Sup_ereal_def Inf_ereal_def bot_ereal_def top_ereal_def)
  1163 
  1164 end
  1165 
  1166 instance ereal :: complete_linorder ..
  1167 
  1168 instance ereal :: linear_continuum
  1169 proof
  1170   show "\<exists>a b::ereal. a \<noteq> b"
  1171     using ereal_zero_one by blast
  1172 qed
  1173 
  1174 lemma ereal_Sup_uminus_image_eq: "Sup (uminus ` S::ereal set) = - Inf S"
  1175   by (auto intro!: Sup_eqI
  1176            simp: Ball_def[symmetric] ereal_uminus_le_reorder le_Inf_iff
  1177            intro!: complete_lattice_class.Inf_lower2)
  1178 
  1179 lemma ereal_inj_on_uminus[intro, simp]: "inj_on uminus (A :: ereal set)"
  1180   by (auto intro!: inj_onI)
  1181 
  1182 lemma ereal_Inf_uminus_image_eq: "Inf (uminus ` S::ereal set) = - Sup S"
  1183   using ereal_Sup_uminus_image_eq[of "uminus ` S"] by simp
  1184 
  1185 lemma ereal_SUPR_uminus:
  1186   fixes f :: "'a => ereal"
  1187   shows "(SUP i : R. -(f i)) = -(INF i : R. f i)"
  1188   using ereal_Sup_uminus_image_eq[of "f`R"]
  1189   by (simp add: SUP_def INF_def image_image)
  1190 
  1191 lemma ereal_INFI_uminus:
  1192   fixes f :: "'a => ereal"
  1193   shows "(INF i : R. -(f i)) = -(SUP i : R. f i)"
  1194   using ereal_SUPR_uminus[of _ "\<lambda>x. - f x"] by simp
  1195 
  1196 lemma ereal_image_uminus_shift:
  1197   fixes X Y :: "ereal set" shows "uminus ` X = Y \<longleftrightarrow> X = uminus ` Y"
  1198 proof
  1199   assume "uminus ` X = Y"
  1200   then have "uminus ` uminus ` X = uminus ` Y"
  1201     by (simp add: inj_image_eq_iff)
  1202   then show "X = uminus ` Y" by (simp add: image_image)
  1203 qed (simp add: image_image)
  1204 
  1205 lemma Inf_ereal_iff:
  1206   fixes z :: ereal
  1207   shows "(!!x. x:X ==> z <= x) ==> (EX x:X. x<y) <-> Inf X < y"
  1208   by (metis complete_lattice_class.Inf_greatest complete_lattice_class.Inf_lower less_le_not_le linear
  1209             order_less_le_trans)
  1210 
  1211 lemma Sup_eq_MInfty:
  1212   fixes S :: "ereal set" shows "Sup S = -\<infinity> \<longleftrightarrow> S = {} \<or> S = {-\<infinity>}"
  1213   unfolding bot_ereal_def[symmetric] by auto
  1214 
  1215 lemma Inf_eq_PInfty:
  1216   fixes S :: "ereal set" shows "Inf S = \<infinity> \<longleftrightarrow> S = {} \<or> S = {\<infinity>}"
  1217   using Sup_eq_MInfty[of "uminus`S"]
  1218   unfolding ereal_Sup_uminus_image_eq ereal_image_uminus_shift by simp
  1219 
  1220 lemma Inf_eq_MInfty: 
  1221   fixes S :: "ereal set" shows "-\<infinity> \<in> S \<Longrightarrow> Inf S = -\<infinity>"
  1222   unfolding bot_ereal_def[symmetric] by auto
  1223 
  1224 lemma Sup_eq_PInfty:
  1225   fixes S :: "ereal set" shows "\<infinity> \<in> S \<Longrightarrow> Sup S = \<infinity>"
  1226   unfolding top_ereal_def[symmetric] by auto
  1227 
  1228 lemma Sup_ereal_close:
  1229   fixes e :: ereal
  1230   assumes "0 < e" and S: "\<bar>Sup S\<bar> \<noteq> \<infinity>" "S \<noteq> {}"
  1231   shows "\<exists>x\<in>S. Sup S - e < x"
  1232   using assms by (cases e) (auto intro!: less_Sup_iff[THEN iffD1])
  1233 
  1234 lemma Inf_ereal_close:
  1235   fixes e :: ereal assumes "\<bar>Inf X\<bar> \<noteq> \<infinity>" "0 < e"
  1236   shows "\<exists>x\<in>X. x < Inf X + e"
  1237 proof (rule Inf_less_iff[THEN iffD1])
  1238   show "Inf X < Inf X + e" using assms
  1239     by (cases e) auto
  1240 qed
  1241 
  1242 lemma SUP_nat_Infty: "(SUP i::nat. ereal (real i)) = \<infinity>"
  1243 proof -
  1244   { fix x ::ereal assume "x \<noteq> \<infinity>"
  1245     then have "\<exists>k::nat. x < ereal (real k)"
  1246     proof (cases x)
  1247       case MInf then show ?thesis by (intro exI[of _ 0]) auto
  1248     next
  1249       case (real r)
  1250       moreover obtain k :: nat where "r < real k"
  1251         using ex_less_of_nat by (auto simp: real_eq_of_nat)
  1252       ultimately show ?thesis by auto
  1253     qed simp }
  1254   then show ?thesis
  1255     using SUP_eq_top_iff[of UNIV "\<lambda>n::nat. ereal (real n)"]
  1256     by (auto simp: top_ereal_def)
  1257 qed
  1258 
  1259 lemma Inf_less:
  1260   fixes x :: ereal
  1261   assumes "(INF i:A. f i) < x"
  1262   shows "EX i. i : A & f i <= x"
  1263 proof(rule ccontr)
  1264   assume "~ (EX i. i : A & f i <= x)"
  1265   hence "ALL i:A. f i > x" by auto
  1266   hence "(INF i:A. f i) >= x" apply (subst INF_greatest) by auto
  1267   thus False using assms by auto
  1268 qed
  1269 
  1270 lemma SUP_ereal_le_addI:
  1271   fixes f :: "'i \<Rightarrow> ereal"
  1272   assumes "\<And>i. f i + y \<le> z" and "y \<noteq> -\<infinity>"
  1273   shows "SUPR UNIV f + y \<le> z"
  1274 proof (cases y)
  1275   case (real r)
  1276   then have "\<And>i. f i \<le> z - y" using assms by (simp add: ereal_le_minus_iff)
  1277   then have "SUPR UNIV f \<le> z - y" by (rule SUP_least)
  1278   then show ?thesis using real by (simp add: ereal_le_minus_iff)
  1279 qed (insert assms, auto)
  1280 
  1281 lemma SUPR_ereal_add:
  1282   fixes f g :: "nat \<Rightarrow> ereal"
  1283   assumes "incseq f" "incseq g" and pos: "\<And>i. f i \<noteq> -\<infinity>" "\<And>i. g i \<noteq> -\<infinity>"
  1284   shows "(SUP i. f i + g i) = SUPR UNIV f + SUPR UNIV g"
  1285 proof (rule SUP_eqI)
  1286   fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> f i + g i \<le> y"
  1287   have f: "SUPR UNIV f \<noteq> -\<infinity>" using pos
  1288     unfolding SUP_def Sup_eq_MInfty by (auto dest: image_eqD)
  1289   { fix j
  1290     { fix i
  1291       have "f i + g j \<le> f i + g (max i j)"
  1292         using `incseq g`[THEN incseqD] by (rule add_left_mono) auto
  1293       also have "\<dots> \<le> f (max i j) + g (max i j)"
  1294         using `incseq f`[THEN incseqD] by (rule add_right_mono) auto
  1295       also have "\<dots> \<le> y" using * by auto
  1296       finally have "f i + g j \<le> y" . }
  1297     then have "SUPR UNIV f + g j \<le> y"
  1298       using assms(4)[of j] by (intro SUP_ereal_le_addI) auto
  1299     then have "g j + SUPR UNIV f \<le> y" by (simp add: ac_simps) }
  1300   then have "SUPR UNIV g + SUPR UNIV f \<le> y"
  1301     using f by (rule SUP_ereal_le_addI)
  1302   then show "SUPR UNIV f + SUPR UNIV g \<le> y" by (simp add: ac_simps)
  1303 qed (auto intro!: add_mono SUP_upper)
  1304 
  1305 lemma SUPR_ereal_add_pos:
  1306   fixes f g :: "nat \<Rightarrow> ereal"
  1307   assumes inc: "incseq f" "incseq g" and pos: "\<And>i. 0 \<le> f i" "\<And>i. 0 \<le> g i"
  1308   shows "(SUP i. f i + g i) = SUPR UNIV f + SUPR UNIV g"
  1309 proof (intro SUPR_ereal_add inc)
  1310   fix i show "f i \<noteq> -\<infinity>" "g i \<noteq> -\<infinity>" using pos[of i] by auto
  1311 qed
  1312 
  1313 lemma SUPR_ereal_setsum:
  1314   fixes f g :: "'a \<Rightarrow> nat \<Rightarrow> ereal"
  1315   assumes "\<And>n. n \<in> A \<Longrightarrow> incseq (f n)" and pos: "\<And>n i. n \<in> A \<Longrightarrow> 0 \<le> f n i"
  1316   shows "(SUP i. \<Sum>n\<in>A. f n i) = (\<Sum>n\<in>A. SUPR UNIV (f n))"
  1317 proof cases
  1318   assume "finite A" then show ?thesis using assms
  1319     by induct (auto simp: incseq_setsumI2 setsum_nonneg SUPR_ereal_add_pos)
  1320 qed simp
  1321 
  1322 lemma SUPR_ereal_cmult:
  1323   fixes f :: "nat \<Rightarrow> ereal" assumes "\<And>i. 0 \<le> f i" "0 \<le> c"
  1324   shows "(SUP i. c * f i) = c * SUPR UNIV f"
  1325 proof (rule SUP_eqI)
  1326   fix i have "f i \<le> SUPR UNIV f" by (rule SUP_upper) auto
  1327   then show "c * f i \<le> c * SUPR UNIV f"
  1328     using `0 \<le> c` by (rule ereal_mult_left_mono)
  1329 next
  1330   fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> c * f i \<le> y"
  1331   show "c * SUPR UNIV f \<le> y"
  1332   proof cases
  1333     assume c: "0 < c \<and> c \<noteq> \<infinity>"
  1334     with * have "SUPR UNIV f \<le> y / c"
  1335       by (intro SUP_least) (auto simp: ereal_le_divide_pos)
  1336     with c show ?thesis
  1337       by (auto simp: ereal_le_divide_pos)
  1338   next
  1339     { assume "c = \<infinity>" have ?thesis
  1340       proof cases
  1341         assume **: "\<forall>i. f i = 0"
  1342         then have "range f = {0}" by auto
  1343         with ** show "c * SUPR UNIV f \<le> y" using *
  1344           by (auto simp: SUP_def min_max.sup_absorb1)
  1345       next
  1346         assume "\<not> (\<forall>i. f i = 0)"
  1347         then obtain i where "f i \<noteq> 0" by auto
  1348         with *[of i] `c = \<infinity>` `0 \<le> f i` show ?thesis by (auto split: split_if_asm)
  1349       qed }
  1350     moreover assume "\<not> (0 < c \<and> c \<noteq> \<infinity>)"
  1351     ultimately show ?thesis using * `0 \<le> c` by auto
  1352   qed
  1353 qed
  1354 
  1355 lemma SUP_PInfty:
  1356   fixes f :: "'a \<Rightarrow> ereal"
  1357   assumes "\<And>n::nat. \<exists>i\<in>A. ereal (real n) \<le> f i"
  1358   shows "(SUP i:A. f i) = \<infinity>"
  1359   unfolding SUP_def Sup_eq_top_iff[where 'a=ereal, unfolded top_ereal_def]
  1360   apply simp
  1361 proof safe
  1362   fix x :: ereal assume "x \<noteq> \<infinity>"
  1363   show "\<exists>i\<in>A. x < f i"
  1364   proof (cases x)
  1365     case PInf with `x \<noteq> \<infinity>` show ?thesis by simp
  1366   next
  1367     case MInf with assms[of "0"] show ?thesis by force
  1368   next
  1369     case (real r)
  1370     with less_PInf_Ex_of_nat[of x] obtain n :: nat where "x < ereal (real n)" by auto
  1371     moreover obtain i where "i \<in> A" "ereal (real n) \<le> f i"
  1372       using assms ..
  1373     ultimately show ?thesis
  1374       by (auto intro!: bexI[of _ i])
  1375   qed
  1376 qed
  1377 
  1378 lemma Sup_countable_SUPR:
  1379   assumes "A \<noteq> {}"
  1380   shows "\<exists>f::nat \<Rightarrow> ereal. range f \<subseteq> A \<and> Sup A = SUPR UNIV f"
  1381 proof (cases "Sup A")
  1382   case (real r)
  1383   have "\<forall>n::nat. \<exists>x. x \<in> A \<and> Sup A < x + 1 / ereal (real n)"
  1384   proof
  1385     fix n ::nat have "\<exists>x\<in>A. Sup A - 1 / ereal (real n) < x"
  1386       using assms real by (intro Sup_ereal_close) (auto simp: one_ereal_def)
  1387     then obtain x where "x \<in> A" "Sup A - 1 / ereal (real n) < x" ..
  1388     then show "\<exists>x. x \<in> A \<and> Sup A < x + 1 / ereal (real n)"
  1389       by (auto intro!: exI[of _ x] simp: ereal_minus_less_iff)
  1390   qed
  1391   from choice[OF this] obtain f :: "nat \<Rightarrow> ereal"
  1392     where f: "\<forall>x. f x \<in> A \<and> Sup A < f x + 1 / ereal (real x)" ..
  1393   have "SUPR UNIV f = Sup A"
  1394   proof (rule SUP_eqI)
  1395     fix i show "f i \<le> Sup A" using f
  1396       by (auto intro!: complete_lattice_class.Sup_upper)
  1397   next
  1398     fix y assume bound: "\<And>i. i \<in> UNIV \<Longrightarrow> f i \<le> y"
  1399     show "Sup A \<le> y"
  1400     proof (rule ereal_le_epsilon, intro allI impI)
  1401       fix e :: ereal assume "0 < e"
  1402       show "Sup A \<le> y + e"
  1403       proof (cases e)
  1404         case (real r)
  1405         hence "0 < r" using `0 < e` by auto
  1406         then obtain n ::nat where *: "1 / real n < r" "0 < n"
  1407           using ex_inverse_of_nat_less by (auto simp: real_eq_of_nat inverse_eq_divide)
  1408         have "Sup A \<le> f n + 1 / ereal (real n)" using f[THEN spec, of n]
  1409           by auto
  1410         also have "1 / ereal (real n) \<le> e" using real * by (auto simp: one_ereal_def )
  1411         with bound have "f n + 1 / ereal (real n) \<le> y + e" by (rule add_mono) simp
  1412         finally show "Sup A \<le> y + e" .
  1413       qed (insert `0 < e`, auto)
  1414     qed
  1415   qed
  1416   with f show ?thesis by (auto intro!: exI[of _ f])
  1417 next
  1418   case PInf
  1419   from `A \<noteq> {}` obtain x where "x \<in> A" by auto
  1420   show ?thesis
  1421   proof cases
  1422     assume *: "\<infinity> \<in> A"
  1423     then have "\<infinity> \<le> Sup A" by (intro complete_lattice_class.Sup_upper)
  1424     with * show ?thesis by (auto intro!: exI[of _ "\<lambda>x. \<infinity>"])
  1425   next
  1426     assume "\<infinity> \<notin> A"
  1427     have "\<exists>x\<in>A. 0 \<le> x"
  1428       by (metis Infty_neq_0 PInf complete_lattice_class.Sup_least ereal_infty_less_eq2 linorder_linear)
  1429     then obtain x where "x \<in> A" "0 \<le> x" by auto
  1430     have "\<forall>n::nat. \<exists>f. f \<in> A \<and> x + ereal (real n) \<le> f"
  1431     proof (rule ccontr)
  1432       assume "\<not> ?thesis"
  1433       then have "\<exists>n::nat. Sup A \<le> x + ereal (real n)"
  1434         by (simp add: Sup_le_iff not_le less_imp_le Ball_def) (metis less_imp_le)
  1435       then show False using `x \<in> A` `\<infinity> \<notin> A` PInf
  1436         by(cases x) auto
  1437     qed
  1438     from choice[OF this] obtain f :: "nat \<Rightarrow> ereal"
  1439       where f: "\<forall>z. f z \<in> A \<and> x + ereal (real z) \<le> f z" ..
  1440     have "SUPR UNIV f = \<infinity>"
  1441     proof (rule SUP_PInfty)
  1442       fix n :: nat
  1443       show "\<exists>i\<in>UNIV. ereal (real n) \<le> f i"
  1444         using f[THEN spec, of n] `0 \<le> x`
  1445         by (cases rule: ereal2_cases[of "f n" x]) (auto intro!: exI[of _ n])
  1446     qed
  1447     then show ?thesis using f PInf by (auto intro!: exI[of _ f])
  1448   qed
  1449 next
  1450   case MInf
  1451   with `A \<noteq> {}` have "A = {-\<infinity>}" by (auto simp: Sup_eq_MInfty)
  1452   then show ?thesis using MInf by (auto intro!: exI[of _ "\<lambda>x. -\<infinity>"])
  1453 qed
  1454 
  1455 lemma SUPR_countable_SUPR:
  1456   "A \<noteq> {} \<Longrightarrow> \<exists>f::nat \<Rightarrow> ereal. range f \<subseteq> g`A \<and> SUPR A g = SUPR UNIV f"
  1457   using Sup_countable_SUPR[of "g`A"] by (auto simp: SUP_def)
  1458 
  1459 lemma Sup_ereal_cadd:
  1460   fixes A :: "ereal set" assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
  1461   shows "Sup ((\<lambda>x. a + x) ` A) = a + Sup A"
  1462 proof (rule antisym)
  1463   have *: "\<And>a::ereal. \<And>A. Sup ((\<lambda>x. a + x) ` A) \<le> a + Sup A"
  1464     by (auto intro!: add_mono complete_lattice_class.Sup_least complete_lattice_class.Sup_upper)
  1465   then show "Sup ((\<lambda>x. a + x) ` A) \<le> a + Sup A" .
  1466   show "a + Sup A \<le> Sup ((\<lambda>x. a + x) ` A)"
  1467   proof (cases a)
  1468     case PInf with `A \<noteq> {}` show ?thesis by (auto simp: image_constant min_max.sup_absorb1)
  1469   next
  1470     case (real r)
  1471     then have **: "op + (- a) ` op + a ` A = A"
  1472       by (auto simp: image_iff ac_simps zero_ereal_def[symmetric])
  1473     from *[of "-a" "(\<lambda>x. a + x) ` A"] real show ?thesis unfolding **
  1474       by (cases rule: ereal2_cases[of "Sup A" "Sup (op + a ` A)"]) auto
  1475   qed (insert `a \<noteq> -\<infinity>`, auto)
  1476 qed
  1477 
  1478 lemma Sup_ereal_cminus:
  1479   fixes A :: "ereal set" assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
  1480   shows "Sup ((\<lambda>x. a - x) ` A) = a - Inf A"
  1481   using Sup_ereal_cadd[of "uminus ` A" a] assms
  1482   by (simp add: comp_def image_image minus_ereal_def
  1483                  ereal_Sup_uminus_image_eq)
  1484 
  1485 lemma SUPR_ereal_cminus:
  1486   fixes f :: "'i \<Rightarrow> ereal"
  1487   fixes A assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
  1488   shows "(SUP x:A. a - f x) = a - (INF x:A. f x)"
  1489   using Sup_ereal_cminus[of "f`A" a] assms
  1490   unfolding SUP_def INF_def image_image by auto
  1491 
  1492 lemma Inf_ereal_cminus:
  1493   fixes A :: "ereal set" assumes "A \<noteq> {}" and "\<bar>a\<bar> \<noteq> \<infinity>"
  1494   shows "Inf ((\<lambda>x. a - x) ` A) = a - Sup A"
  1495 proof -
  1496   {
  1497     fix x
  1498     have "-a - -x = -(a - x)" using assms by (cases x) auto
  1499   } note * = this
  1500   then have "(\<lambda>x. -a - x)`uminus`A = uminus ` (\<lambda>x. a - x) ` A"
  1501     by (auto simp: image_image)
  1502   with * show ?thesis
  1503     using Sup_ereal_cminus[of "uminus ` A" "-a"] assms
  1504     by (auto simp add: ereal_Sup_uminus_image_eq ereal_Inf_uminus_image_eq)
  1505 qed
  1506 
  1507 lemma INFI_ereal_cminus:
  1508   fixes a :: ereal assumes "A \<noteq> {}" and "\<bar>a\<bar> \<noteq> \<infinity>"
  1509   shows "(INF x:A. a - f x) = a - (SUP x:A. f x)"
  1510   using Inf_ereal_cminus[of "f`A" a] assms
  1511   unfolding SUP_def INF_def image_image
  1512   by auto
  1513 
  1514 lemma uminus_ereal_add_uminus_uminus:
  1515   fixes a b :: ereal shows "a \<noteq> \<infinity> \<Longrightarrow> b \<noteq> \<infinity> \<Longrightarrow> - (- a + - b) = a + b"
  1516   by (cases rule: ereal2_cases[of a b]) auto
  1517 
  1518 lemma INFI_ereal_add:
  1519   fixes f :: "nat \<Rightarrow> ereal"
  1520   assumes "decseq f" "decseq g" and fin: "\<And>i. f i \<noteq> \<infinity>" "\<And>i. g i \<noteq> \<infinity>"
  1521   shows "(INF i. f i + g i) = INFI UNIV f + INFI UNIV g"
  1522 proof -
  1523   have INF_less: "(INF i. f i) < \<infinity>" "(INF i. g i) < \<infinity>"
  1524     using assms unfolding INF_less_iff by auto
  1525   { fix i from fin[of i] have "- ((- f i) + (- g i)) = f i + g i"
  1526       by (rule uminus_ereal_add_uminus_uminus) }
  1527   then have "(INF i. f i + g i) = (INF i. - ((- f i) + (- g i)))"
  1528     by simp
  1529   also have "\<dots> = INFI UNIV f + INFI UNIV g"
  1530     unfolding ereal_INFI_uminus
  1531     using assms INF_less
  1532     by (subst SUPR_ereal_add)
  1533        (auto simp: ereal_SUPR_uminus intro!: uminus_ereal_add_uminus_uminus)
  1534   finally show ?thesis .
  1535 qed
  1536 
  1537 subsection "Relation to @{typ enat}"
  1538 
  1539 definition "ereal_of_enat n = (case n of enat n \<Rightarrow> ereal (real n) | \<infinity> \<Rightarrow> \<infinity>)"
  1540 
  1541 declare [[coercion "ereal_of_enat :: enat \<Rightarrow> ereal"]]
  1542 declare [[coercion "(\<lambda>n. ereal (real n)) :: nat \<Rightarrow> ereal"]]
  1543 
  1544 lemma ereal_of_enat_simps[simp]:
  1545   "ereal_of_enat (enat n) = ereal n"
  1546   "ereal_of_enat \<infinity> = \<infinity>"
  1547   by (simp_all add: ereal_of_enat_def)
  1548 
  1549 lemma ereal_of_enat_le_iff[simp]:
  1550   "ereal_of_enat m \<le> ereal_of_enat n \<longleftrightarrow> m \<le> n"
  1551 by (cases m n rule: enat2_cases) auto
  1552 
  1553 lemma ereal_of_enat_less_iff[simp]:
  1554   "ereal_of_enat m < ereal_of_enat n \<longleftrightarrow> m < n"
  1555 by (cases m n rule: enat2_cases) auto
  1556 
  1557 lemma numeral_le_ereal_of_enat_iff[simp]:
  1558   shows "numeral m \<le> ereal_of_enat n \<longleftrightarrow> numeral m \<le> n"
  1559 by (cases n) (auto dest: natceiling_le intro: natceiling_le_eq[THEN iffD1])
  1560 
  1561 lemma numeral_less_ereal_of_enat_iff[simp]:
  1562   shows "numeral m < ereal_of_enat n \<longleftrightarrow> numeral m < n"
  1563 by (cases n) (auto simp: real_of_nat_less_iff[symmetric])
  1564 
  1565 lemma ereal_of_enat_ge_zero_cancel_iff[simp]:
  1566   "0 \<le> ereal_of_enat n \<longleftrightarrow> 0 \<le> n"
  1567 by (cases n) (auto simp: enat_0[symmetric])
  1568 
  1569 lemma ereal_of_enat_gt_zero_cancel_iff[simp]:
  1570   "0 < ereal_of_enat n \<longleftrightarrow> 0 < n"
  1571 by (cases n) (auto simp: enat_0[symmetric])
  1572 
  1573 lemma ereal_of_enat_zero[simp]:
  1574   "ereal_of_enat 0 = 0"
  1575 by (auto simp: enat_0[symmetric])
  1576 
  1577 lemma ereal_of_enat_inf[simp]:
  1578   "ereal_of_enat n = \<infinity> \<longleftrightarrow> n = \<infinity>"
  1579   by (cases n) auto
  1580 
  1581 
  1582 lemma ereal_of_enat_add:
  1583   "ereal_of_enat (m + n) = ereal_of_enat m + ereal_of_enat n"
  1584 by (cases m n rule: enat2_cases) auto
  1585 
  1586 lemma ereal_of_enat_sub:
  1587   assumes "n \<le> m" shows "ereal_of_enat (m - n) = ereal_of_enat m - ereal_of_enat n "
  1588 using assms by (cases m n rule: enat2_cases) auto
  1589 
  1590 lemma ereal_of_enat_mult:
  1591   "ereal_of_enat (m * n) = ereal_of_enat m * ereal_of_enat n"
  1592 by (cases m n rule: enat2_cases) auto
  1593 
  1594 lemmas ereal_of_enat_pushin = ereal_of_enat_add ereal_of_enat_sub ereal_of_enat_mult
  1595 lemmas ereal_of_enat_pushout = ereal_of_enat_pushin[symmetric]
  1596 
  1597 
  1598 subsection "Limits on @{typ ereal}"
  1599 
  1600 subsubsection "Topological space"
  1601 
  1602 instantiation ereal :: linear_continuum_topology
  1603 begin
  1604 
  1605 definition "open_ereal" :: "ereal set \<Rightarrow> bool" where
  1606   open_ereal_generated: "open_ereal = generate_topology (range lessThan \<union> range greaterThan)"
  1607 
  1608 instance
  1609   by default (simp add: open_ereal_generated)
  1610 end
  1611 
  1612 lemma open_PInfty: "open A \<Longrightarrow> \<infinity> \<in> A \<Longrightarrow> (\<exists>x. {ereal x<..} \<subseteq> A)"
  1613   unfolding open_ereal_generated
  1614 proof (induct rule: generate_topology.induct)
  1615   case (Int A B)
  1616   then obtain x z where "\<infinity> \<in> A \<Longrightarrow> {ereal x <..} \<subseteq> A" "\<infinity> \<in> B \<Longrightarrow> {ereal z <..} \<subseteq> B"
  1617     by auto
  1618   with Int show ?case
  1619     by (intro exI[of _ "max x z"]) fastforce
  1620 next
  1621   { fix x have "x \<noteq> \<infinity> \<Longrightarrow> \<exists>t. x \<le> ereal t" by (cases x) auto }
  1622   moreover case (Basis S)
  1623   ultimately show ?case
  1624     by (auto split: ereal.split)
  1625 qed (fastforce simp add: vimage_Union)+
  1626 
  1627 lemma open_MInfty: "open A \<Longrightarrow> -\<infinity> \<in> A \<Longrightarrow> (\<exists>x. {..<ereal x} \<subseteq> A)"
  1628   unfolding open_ereal_generated
  1629 proof (induct rule: generate_topology.induct)
  1630   case (Int A B)
  1631   then obtain x z where "-\<infinity> \<in> A \<Longrightarrow> {..< ereal x} \<subseteq> A" "-\<infinity> \<in> B \<Longrightarrow> {..< ereal z} \<subseteq> B"
  1632     by auto
  1633   with Int show ?case
  1634     by (intro exI[of _ "min x z"]) fastforce
  1635 next
  1636   { fix x have "x \<noteq> - \<infinity> \<Longrightarrow> \<exists>t. ereal t \<le> x" by (cases x) auto }
  1637   moreover case (Basis S)
  1638   ultimately show ?case
  1639     by (auto split: ereal.split)
  1640 qed (fastforce simp add: vimage_Union)+
  1641 
  1642 lemma open_ereal_vimage: "open S \<Longrightarrow> open (ereal -` S)"
  1643   unfolding open_ereal_generated
  1644 proof (induct rule: generate_topology.induct)
  1645   case (Int A B) then show ?case by auto
  1646 next
  1647   { fix x have
  1648       "ereal -` {..<x} = (case x of PInfty \<Rightarrow> UNIV | MInfty \<Rightarrow> {} | ereal r \<Rightarrow> {..<r})"
  1649       "ereal -` {x<..} = (case x of PInfty \<Rightarrow> {} | MInfty \<Rightarrow> UNIV | ereal r \<Rightarrow> {r<..})"
  1650       by (induct x) auto }
  1651   moreover case (Basis S)
  1652   ultimately show ?case
  1653     by (auto split: ereal.split)
  1654 qed (fastforce simp add: vimage_Union)+
  1655 
  1656 lemma open_ereal: "open S \<Longrightarrow> open (ereal ` S)"
  1657   unfolding open_generated_order[where 'a=real]
  1658 proof (induct rule: generate_topology.induct)
  1659   case (Basis S)
  1660   moreover { fix x have "ereal ` {..< x} = { -\<infinity> <..< ereal x }" by auto (case_tac xa, auto) }
  1661   moreover { fix x have "ereal ` {x <..} = { ereal x <..< \<infinity> }" by auto (case_tac xa, auto) }
  1662   ultimately show ?case
  1663      by auto
  1664 qed (auto simp add: image_Union image_Int)
  1665 
  1666 lemma open_ereal_def: "open A \<longleftrightarrow> open (ereal -` A) \<and> (\<infinity> \<in> A \<longrightarrow> (\<exists>x. {ereal x <..} \<subseteq> A)) \<and> (-\<infinity> \<in> A \<longrightarrow> (\<exists>x. {..<ereal x} \<subseteq> A))"
  1667   (is "open A \<longleftrightarrow> ?rhs")
  1668 proof
  1669   assume "open A" then show ?rhs
  1670     using open_PInfty open_MInfty open_ereal_vimage by auto
  1671 next
  1672   assume "?rhs"
  1673   then obtain x y where A: "open (ereal -` A)" "\<infinity> \<in> A \<Longrightarrow> {ereal x<..} \<subseteq> A" "-\<infinity> \<in> A \<Longrightarrow> {..< ereal y} \<subseteq> A"
  1674     by auto
  1675   have *: "A = ereal ` (ereal -` A) \<union> (if \<infinity> \<in> A then {ereal x<..} else {}) \<union> (if -\<infinity> \<in> A then {..< ereal y} else {})"
  1676     using A(2,3) by auto
  1677   from open_ereal[OF A(1)] show "open A"
  1678     by (subst *) (auto simp: open_Un)
  1679 qed
  1680 
  1681 lemma open_PInfty2: assumes "open A" "\<infinity> \<in> A" obtains x where "{ereal x<..} \<subseteq> A"
  1682   using open_PInfty[OF assms] by auto
  1683 
  1684 lemma open_MInfty2: assumes "open A" "-\<infinity> \<in> A" obtains x where "{..<ereal x} \<subseteq> A"
  1685   using open_MInfty[OF assms] by auto
  1686 
  1687 lemma ereal_openE: assumes "open A" obtains x y where
  1688   "open (ereal -` A)" "\<infinity> \<in> A \<Longrightarrow> {ereal x<..} \<subseteq> A" "-\<infinity> \<in> A \<Longrightarrow> {..<ereal y} \<subseteq> A"
  1689   using assms open_ereal_def by auto
  1690 
  1691 lemmas open_ereal_lessThan = open_lessThan[where 'a=ereal]
  1692 lemmas open_ereal_greaterThan = open_greaterThan[where 'a=ereal]
  1693 lemmas ereal_open_greaterThanLessThan = open_greaterThanLessThan[where 'a=ereal]
  1694 lemmas closed_ereal_atLeast = closed_atLeast[where 'a=ereal]
  1695 lemmas closed_ereal_atMost = closed_atMost[where 'a=ereal]
  1696 lemmas closed_ereal_atLeastAtMost = closed_atLeastAtMost[where 'a=ereal]
  1697 lemmas closed_ereal_singleton = closed_singleton[where 'a=ereal]
  1698   
  1699 lemma ereal_open_cont_interval:
  1700   fixes S :: "ereal set"
  1701   assumes "open S" "x \<in> S" "\<bar>x\<bar> \<noteq> \<infinity>"
  1702   obtains e where "e>0" "{x-e <..< x+e} \<subseteq> S"
  1703 proof-
  1704   from `open S` have "open (ereal -` S)" by (rule ereal_openE)
  1705   then obtain e where "0 < e" and e: "\<And>y. dist y (real x) < e \<Longrightarrow> ereal y \<in> S"
  1706     using assms unfolding open_dist by force
  1707   show thesis
  1708   proof (intro that subsetI)
  1709     show "0 < ereal e" using `0 < e` by auto
  1710     fix y assume "y \<in> {x - ereal e<..<x + ereal e}"
  1711     with assms obtain t where "y = ereal t" "dist t (real x) < e"
  1712       apply (cases y) by (auto simp: dist_real_def)
  1713     then show "y \<in> S" using e[of t] by auto
  1714   qed
  1715 qed
  1716 
  1717 lemma ereal_open_cont_interval2:
  1718   fixes S :: "ereal set"
  1719   assumes "open S" "x \<in> S" and x: "\<bar>x\<bar> \<noteq> \<infinity>"
  1720   obtains a b where "a < x" "x < b" "{a <..< b} \<subseteq> S"
  1721 proof -
  1722   obtain e where "0 < e" "{x - e<..<x + e} \<subseteq> S"
  1723     using assms by (rule ereal_open_cont_interval)
  1724   with that[of "x-e" "x+e"] ereal_between[OF x, of e]
  1725   show thesis by auto
  1726 qed
  1727 
  1728 subsubsection {* Convergent sequences *}
  1729 
  1730 lemma lim_ereal[simp]:
  1731   "((\<lambda>n. ereal (f n)) ---> ereal x) net \<longleftrightarrow> (f ---> x) net" (is "?l = ?r")
  1732 proof (intro iffI topological_tendstoI)
  1733   fix S assume "?l" "open S" "x \<in> S"
  1734   then show "eventually (\<lambda>x. f x \<in> S) net"
  1735     using `?l`[THEN topological_tendstoD, OF open_ereal, OF `open S`]
  1736     by (simp add: inj_image_mem_iff)
  1737 next
  1738   fix S assume "?r" "open S" "ereal x \<in> S"
  1739   show "eventually (\<lambda>x. ereal (f x) \<in> S) net"
  1740     using `?r`[THEN topological_tendstoD, OF open_ereal_vimage, OF `open S`]
  1741     using `ereal x \<in> S` by auto
  1742 qed
  1743 
  1744 lemma lim_real_of_ereal[simp]:
  1745   assumes lim: "(f ---> ereal x) net"
  1746   shows "((\<lambda>x. real (f x)) ---> x) net"
  1747 proof (intro topological_tendstoI)
  1748   fix S assume "open S" "x \<in> S"
  1749   then have S: "open S" "ereal x \<in> ereal ` S"
  1750     by (simp_all add: inj_image_mem_iff)
  1751   have "\<forall>x. f x \<in> ereal ` S \<longrightarrow> real (f x) \<in> S" by auto
  1752   from this lim[THEN topological_tendstoD, OF open_ereal, OF S]
  1753   show "eventually (\<lambda>x. real (f x) \<in> S) net"
  1754     by (rule eventually_mono)
  1755 qed
  1756 
  1757 lemma tendsto_PInfty: "(f ---> \<infinity>) F \<longleftrightarrow> (\<forall>r. eventually (\<lambda>x. ereal r < f x) F)"
  1758 proof -
  1759   { fix l :: ereal assume "\<forall>r. eventually (\<lambda>x. ereal r < f x) F"
  1760     from this[THEN spec, of "real l"]
  1761     have "l \<noteq> \<infinity> \<Longrightarrow> eventually (\<lambda>x. l < f x) F"
  1762       by (cases l) (auto elim: eventually_elim1) }
  1763   then show ?thesis
  1764     by (auto simp: order_tendsto_iff)
  1765 qed
  1766 
  1767 lemma tendsto_MInfty: "(f ---> -\<infinity>) F \<longleftrightarrow> (\<forall>r. eventually (\<lambda>x. f x < ereal r) F)"
  1768   unfolding tendsto_def
  1769 proof safe
  1770   fix S :: "ereal set"
  1771   assume "open S" "-\<infinity> \<in> S"
  1772   from open_MInfty[OF this] obtain B where "{..<ereal B} \<subseteq> S" ..
  1773   moreover
  1774   assume "\<forall>r::real. eventually (\<lambda>z. f z < r) F"
  1775   then have "eventually (\<lambda>z. f z \<in> {..< B}) F" by auto
  1776   ultimately show "eventually (\<lambda>z. f z \<in> S) F" by (auto elim!: eventually_elim1)
  1777 next
  1778   fix x assume "\<forall>S. open S \<longrightarrow> -\<infinity> \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) F"
  1779   from this[rule_format, of "{..< ereal x}"]
  1780   show "eventually (\<lambda>y. f y < ereal x) F" by auto
  1781 qed
  1782 
  1783 lemma Lim_PInfty: "f ----> \<infinity> \<longleftrightarrow> (\<forall>B. \<exists>N. \<forall>n\<ge>N. f n \<ge> ereal B)"
  1784   unfolding tendsto_PInfty eventually_sequentially
  1785 proof safe
  1786   fix r assume "\<forall>r. \<exists>N. \<forall>n\<ge>N. ereal r \<le> f n"
  1787   then obtain N where "\<forall>n\<ge>N. ereal (r + 1) \<le> f n" by blast
  1788   moreover have "ereal r < ereal (r + 1)" by auto
  1789   ultimately show "\<exists>N. \<forall>n\<ge>N. ereal r < f n"
  1790     by (blast intro: less_le_trans)
  1791 qed (blast intro: less_imp_le)
  1792 
  1793 lemma Lim_MInfty: "f ----> -\<infinity> \<longleftrightarrow> (\<forall>B. \<exists>N. \<forall>n\<ge>N. ereal B \<ge> f n)"
  1794   unfolding tendsto_MInfty eventually_sequentially
  1795 proof safe
  1796   fix r assume "\<forall>r. \<exists>N. \<forall>n\<ge>N. f n \<le> ereal r"
  1797   then obtain N where "\<forall>n\<ge>N. f n \<le> ereal (r - 1)" by blast
  1798   moreover have "ereal (r - 1) < ereal r" by auto
  1799   ultimately show "\<exists>N. \<forall>n\<ge>N. f n < ereal r"
  1800     by (blast intro: le_less_trans)
  1801 qed (blast intro: less_imp_le)
  1802 
  1803 lemma Lim_bounded_PInfty: "f ----> l \<Longrightarrow> (\<And>n. f n \<le> ereal B) \<Longrightarrow> l \<noteq> \<infinity>"
  1804   using LIMSEQ_le_const2[of f l "ereal B"] by auto
  1805 
  1806 lemma Lim_bounded_MInfty: "f ----> l \<Longrightarrow> (\<And>n. ereal B \<le> f n) \<Longrightarrow> l \<noteq> -\<infinity>"
  1807   using LIMSEQ_le_const[of f l "ereal B"] by auto
  1808 
  1809 lemma tendsto_explicit:
  1810   "f ----> f0 <-> (ALL S. open S --> f0 : S --> (EX N. ALL n>=N. f n : S))"
  1811   unfolding tendsto_def eventually_sequentially by auto
  1812 
  1813 lemma Lim_bounded_PInfty2:
  1814   "f ----> l \<Longrightarrow> ALL n>=N. f n <= ereal B \<Longrightarrow> l ~= \<infinity>"
  1815   using LIMSEQ_le_const2[of f l "ereal B"] by fastforce
  1816 
  1817 lemma Lim_bounded_ereal: "f ----> (l :: 'a::linorder_topology) \<Longrightarrow> ALL n>=M. f n <= C \<Longrightarrow> l<=C"
  1818   by (intro LIMSEQ_le_const2) auto
  1819 
  1820 lemma Lim_bounded2_ereal:
  1821   assumes lim:"f ----> (l :: 'a::linorder_topology)" and ge: "ALL n>=N. f n >= C"
  1822   shows "l>=C"
  1823   using ge
  1824   by (intro tendsto_le[OF trivial_limit_sequentially lim tendsto_const])
  1825      (auto simp: eventually_sequentially)
  1826 
  1827 lemma real_of_ereal_mult[simp]:
  1828   fixes a b :: ereal shows "real (a * b) = real a * real b"
  1829   by (cases rule: ereal2_cases[of a b]) auto
  1830 
  1831 lemma real_of_ereal_eq_0:
  1832   fixes x :: ereal shows "real x = 0 \<longleftrightarrow> x = \<infinity> \<or> x = -\<infinity> \<or> x = 0"
  1833   by (cases x) auto
  1834 
  1835 lemma tendsto_ereal_realD:
  1836   fixes f :: "'a \<Rightarrow> ereal"
  1837   assumes "x \<noteq> 0" and tendsto: "((\<lambda>x. ereal (real (f x))) ---> x) net"
  1838   shows "(f ---> x) net"
  1839 proof (intro topological_tendstoI)
  1840   fix S assume S: "open S" "x \<in> S"
  1841   with `x \<noteq> 0` have "open (S - {0})" "x \<in> S - {0}" by auto
  1842   from tendsto[THEN topological_tendstoD, OF this]
  1843   show "eventually (\<lambda>x. f x \<in> S) net"
  1844     by (rule eventually_rev_mp) (auto simp: ereal_real)
  1845 qed
  1846 
  1847 lemma tendsto_ereal_realI:
  1848   fixes f :: "'a \<Rightarrow> ereal"
  1849   assumes x: "\<bar>x\<bar> \<noteq> \<infinity>" and tendsto: "(f ---> x) net"
  1850   shows "((\<lambda>x. ereal (real (f x))) ---> x) net"
  1851 proof (intro topological_tendstoI)
  1852   fix S assume "open S" "x \<in> S"
  1853   with x have "open (S - {\<infinity>, -\<infinity>})" "x \<in> S - {\<infinity>, -\<infinity>}" by auto
  1854   from tendsto[THEN topological_tendstoD, OF this]
  1855   show "eventually (\<lambda>x. ereal (real (f x)) \<in> S) net"
  1856     by (elim eventually_elim1) (auto simp: ereal_real)
  1857 qed
  1858 
  1859 lemma ereal_mult_cancel_left:
  1860   fixes a b c :: ereal shows "a * b = a * c \<longleftrightarrow>
  1861     ((\<bar>a\<bar> = \<infinity> \<and> 0 < b * c) \<or> a = 0 \<or> b = c)"
  1862   by (cases rule: ereal3_cases[of a b c])
  1863      (simp_all add: zero_less_mult_iff)
  1864 
  1865 lemma ereal_inj_affinity:
  1866   fixes m t :: ereal
  1867   assumes "\<bar>m\<bar> \<noteq> \<infinity>" "m \<noteq> 0" "\<bar>t\<bar> \<noteq> \<infinity>"
  1868   shows "inj_on (\<lambda>x. m * x + t) A"
  1869   using assms
  1870   by (cases rule: ereal2_cases[of m t])
  1871      (auto intro!: inj_onI simp: ereal_add_cancel_right ereal_mult_cancel_left)
  1872 
  1873 lemma ereal_PInfty_eq_plus[simp]:
  1874   fixes a b :: ereal
  1875   shows "\<infinity> = a + b \<longleftrightarrow> a = \<infinity> \<or> b = \<infinity>"
  1876   by (cases rule: ereal2_cases[of a b]) auto
  1877 
  1878 lemma ereal_MInfty_eq_plus[simp]:
  1879   fixes a b :: ereal
  1880   shows "-\<infinity> = a + b \<longleftrightarrow> (a = -\<infinity> \<and> b \<noteq> \<infinity>) \<or> (b = -\<infinity> \<and> a \<noteq> \<infinity>)"
  1881   by (cases rule: ereal2_cases[of a b]) auto
  1882 
  1883 lemma ereal_less_divide_pos:
  1884   fixes x y :: ereal
  1885   shows "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y < z / x \<longleftrightarrow> x * y < z"
  1886   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
  1887 
  1888 lemma ereal_divide_less_pos:
  1889   fixes x y z :: ereal
  1890   shows "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y / x < z \<longleftrightarrow> y < x * z"
  1891   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
  1892 
  1893 lemma ereal_divide_eq:
  1894   fixes a b c :: ereal
  1895   shows "b \<noteq> 0 \<Longrightarrow> \<bar>b\<bar> \<noteq> \<infinity> \<Longrightarrow> a / b = c \<longleftrightarrow> a = b * c"
  1896   by (cases rule: ereal3_cases[of a b c])
  1897      (simp_all add: field_simps)
  1898 
  1899 lemma ereal_inverse_not_MInfty[simp]: "inverse (a::ereal) \<noteq> -\<infinity>"
  1900   by (cases a) auto
  1901 
  1902 lemma ereal_mult_m1[simp]: "x * ereal (-1) = -x"
  1903   by (cases x) auto
  1904 
  1905 lemma ereal_real': assumes "\<bar>x\<bar> \<noteq> \<infinity>" shows "ereal (real x) = x"
  1906   using assms by auto
  1907 
  1908 lemma real_ereal_id: "real o ereal = id"
  1909 proof-
  1910   { fix x have "(real o ereal) x = id x" by auto }
  1911   then show ?thesis using ext by blast
  1912 qed
  1913 
  1914 lemma open_image_ereal: "open(UNIV-{ \<infinity> , (-\<infinity> :: ereal)})"
  1915 by (metis range_ereal open_ereal open_UNIV)
  1916 
  1917 lemma ereal_le_distrib:
  1918   fixes a b c :: ereal shows "c * (a + b) \<le> c * a + c * b"
  1919   by (cases rule: ereal3_cases[of a b c])
  1920      (auto simp add: field_simps not_le mult_le_0_iff mult_less_0_iff)
  1921 
  1922 lemma ereal_pos_distrib:
  1923   fixes a b c :: ereal assumes "0 \<le> c" "c \<noteq> \<infinity>" shows "c * (a + b) = c * a + c * b"
  1924   using assms by (cases rule: ereal3_cases[of a b c])
  1925                  (auto simp add: field_simps not_le mult_le_0_iff mult_less_0_iff)
  1926 
  1927 lemma ereal_pos_le_distrib:
  1928 fixes a b c :: ereal
  1929 assumes "c>=0"
  1930 shows "c * (a + b) <= c * a + c * b"
  1931   using assms by (cases rule: ereal3_cases[of a b c])
  1932                  (auto simp add: field_simps)
  1933 
  1934 lemma ereal_max_mono:
  1935   "[| (a::ereal) <= b; c <= d |] ==> max a c <= max b d"
  1936   by (metis sup_ereal_def sup_mono)
  1937 
  1938 
  1939 lemma ereal_max_least:
  1940   "[| (a::ereal) <= x; c <= x |] ==> max a c <= x"
  1941   by (metis sup_ereal_def sup_least)
  1942 
  1943 lemma ereal_LimI_finite:
  1944   fixes x :: ereal
  1945   assumes "\<bar>x\<bar> \<noteq> \<infinity>"
  1946   assumes "!!r. 0 < r ==> EX N. ALL n>=N. u n < x + r & x < u n + r"
  1947   shows "u ----> x"
  1948 proof (rule topological_tendstoI, unfold eventually_sequentially)
  1949   obtain rx where rx_def: "x=ereal rx" using assms by (cases x) auto
  1950   fix S assume "open S" "x : S"
  1951   then have "open (ereal -` S)" unfolding open_ereal_def by auto
  1952   with `x \<in> S` obtain r where "0 < r" and dist: "!!y. dist y rx < r ==> ereal y \<in> S"
  1953     unfolding open_real_def rx_def by auto
  1954   then obtain n where
  1955     upper: "!!N. n <= N ==> u N < x + ereal r" and
  1956     lower: "!!N. n <= N ==> x < u N + ereal r" using assms(2)[of "ereal r"] by auto
  1957   show "EX N. ALL n>=N. u n : S"
  1958   proof (safe intro!: exI[of _ n])
  1959     fix N assume "n <= N"
  1960     from upper[OF this] lower[OF this] assms `0 < r`
  1961     have "u N ~: {\<infinity>,(-\<infinity>)}" by auto
  1962     then obtain ra where ra_def: "(u N) = ereal ra" by (cases "u N") auto
  1963     hence "rx < ra + r" and "ra < rx + r"
  1964        using rx_def assms `0 < r` lower[OF `n <= N`] upper[OF `n <= N`] by auto
  1965     hence "dist (real (u N)) rx < r"
  1966       using rx_def ra_def
  1967       by (auto simp: dist_real_def abs_diff_less_iff field_simps)
  1968     from dist[OF this] show "u N : S" using `u N  ~: {\<infinity>, -\<infinity>}`
  1969       by (auto simp: ereal_real split: split_if_asm)
  1970   qed
  1971 qed
  1972 
  1973 lemma tendsto_obtains_N:
  1974   assumes "f ----> f0"
  1975   assumes "open S" "f0 : S"
  1976   obtains N where "ALL n>=N. f n : S"
  1977   using assms using tendsto_def
  1978   using tendsto_explicit[of f f0] assms by auto
  1979 
  1980 lemma ereal_LimI_finite_iff:
  1981   fixes x :: ereal
  1982   assumes "\<bar>x\<bar> \<noteq> \<infinity>"
  1983   shows "u ----> x <-> (ALL r. 0 < r --> (EX N. ALL n>=N. u n < x + r & x < u n + r))"
  1984   (is "?lhs <-> ?rhs")
  1985 proof
  1986   assume lim: "u ----> x"
  1987   { fix r assume "(r::ereal)>0"
  1988     then obtain N where N_def: "ALL n>=N. u n : {x - r <..< x + r}"
  1989        apply (subst tendsto_obtains_N[of u x "{x - r <..< x + r}"])
  1990        using lim ereal_between[of x r] assms `r>0` by auto
  1991     hence "EX N. ALL n>=N. u n < x + r & x < u n + r"
  1992       using ereal_minus_less[of r x] by (cases r) auto
  1993   } then show "?rhs" by auto
  1994 next
  1995   assume ?rhs then show "u ----> x"
  1996     using ereal_LimI_finite[of x] assms by auto
  1997 qed
  1998 
  1999 lemma ereal_Limsup_uminus:
  2000   fixes f :: "'a => ereal"
  2001   shows "Limsup net (\<lambda>x. - (f x)) = -(Liminf net f)"
  2002   unfolding Limsup_def Liminf_def ereal_SUPR_uminus ereal_INFI_uminus ..
  2003 
  2004 lemma liminf_bounded_iff:
  2005   fixes x :: "nat \<Rightarrow> ereal"
  2006   shows "C \<le> liminf x \<longleftrightarrow> (\<forall>B<C. \<exists>N. \<forall>n\<ge>N. B < x n)" (is "?lhs <-> ?rhs")
  2007   unfolding le_Liminf_iff eventually_sequentially ..
  2008 
  2009 lemma
  2010   fixes X :: "nat \<Rightarrow> ereal"
  2011   shows ereal_incseq_uminus[simp]: "incseq (\<lambda>i. - X i) = decseq X"
  2012     and ereal_decseq_uminus[simp]: "decseq (\<lambda>i. - X i) = incseq X"
  2013   unfolding incseq_def decseq_def by auto
  2014 
  2015 subsubsection {* Tests for code generator *}
  2016 
  2017 (* A small list of simple arithmetic expressions *)
  2018 
  2019 value [code] "- \<infinity> :: ereal"
  2020 value [code] "\<bar>-\<infinity>\<bar> :: ereal"
  2021 value [code] "4 + 5 / 4 - ereal 2 :: ereal"
  2022 value [code] "ereal 3 < \<infinity>"
  2023 value [code] "real (\<infinity>::ereal) = 0"
  2024 
  2025 end