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