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