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