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