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