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