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