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