src/HOL/Library/Extended_Nonnegative_Real.thy
author haftmann
Fri Mar 22 19:18:08 2019 +0000 (3 months ago)
changeset 69946 494934c30f38
parent 69861 62e47f06d22c
child 70271 f7630118814c
permissions -rw-r--r--
improved code equations taken over from AFP
hoelzl@62375
     1
(*  Title:      HOL/Library/Extended_Nonnegative_Real.thy
hoelzl@62375
     2
    Author:     Johannes Hölzl
hoelzl@62375
     3
*)
hoelzl@62375
     4
hoelzl@62375
     5
subsection \<open>The type of non-negative extended real numbers\<close>
hoelzl@62375
     6
hoelzl@62375
     7
theory Extended_Nonnegative_Real
hoelzl@62648
     8
  imports Extended_Real Indicator_Function
hoelzl@62375
     9
begin
hoelzl@62375
    10
hoelzl@62975
    11
lemma ereal_ineq_diff_add:
hoelzl@62975
    12
  assumes "b \<noteq> (-\<infinity>::ereal)" "a \<ge> b"
hoelzl@62975
    13
  shows "a = b + (a-b)"
lp15@67691
    14
by (metis add.commute assms ereal_eq_minus_iff ereal_minus_le_iff ereal_plus_eq_PInfty)
hoelzl@62975
    15
hoelzl@62975
    16
lemma Limsup_const_add:
hoelzl@62975
    17
  fixes c :: "'a::{complete_linorder, linorder_topology, topological_monoid_add, ordered_ab_semigroup_add}"
hoelzl@62975
    18
  shows "F \<noteq> bot \<Longrightarrow> Limsup F (\<lambda>x. c + f x) = c + Limsup F f"
hoelzl@62975
    19
  by (rule Limsup_compose_continuous_mono)
hoelzl@62975
    20
     (auto intro!: monoI add_mono continuous_on_add continuous_on_id continuous_on_const)
hoelzl@62975
    21
hoelzl@62975
    22
lemma Liminf_const_add:
hoelzl@62975
    23
  fixes c :: "'a::{complete_linorder, linorder_topology, topological_monoid_add, ordered_ab_semigroup_add}"
hoelzl@62975
    24
  shows "F \<noteq> bot \<Longrightarrow> Liminf F (\<lambda>x. c + f x) = c + Liminf F f"
hoelzl@62975
    25
  by (rule Liminf_compose_continuous_mono)
hoelzl@62975
    26
     (auto intro!: monoI add_mono continuous_on_add continuous_on_id continuous_on_const)
hoelzl@62975
    27
hoelzl@62975
    28
lemma Liminf_add_const:
hoelzl@62975
    29
  fixes c :: "'a::{complete_linorder, linorder_topology, topological_monoid_add, ordered_ab_semigroup_add}"
hoelzl@62975
    30
  shows "F \<noteq> bot \<Longrightarrow> Liminf F (\<lambda>x. f x + c) = Liminf F f + c"
hoelzl@62975
    31
  by (rule Liminf_compose_continuous_mono)
hoelzl@62975
    32
     (auto intro!: monoI add_mono continuous_on_add continuous_on_id continuous_on_const)
hoelzl@62975
    33
hoelzl@62975
    34
lemma sums_offset:
hoelzl@62975
    35
  fixes f g :: "nat \<Rightarrow> 'a :: {t2_space, topological_comm_monoid_add}"
hoelzl@62975
    36
  assumes "(\<lambda>n. f (n + i)) sums l" shows "f sums (l + (\<Sum>j<i. f j))"
hoelzl@62975
    37
proof  -
hoelzl@62975
    38
  have "(\<lambda>k. (\<Sum>n<k. f (n + i)) + (\<Sum>j<i. f j)) \<longlonglongrightarrow> l + (\<Sum>j<i. f j)"
hoelzl@62975
    39
    using assms by (auto intro!: tendsto_add simp: sums_def)
hoelzl@62975
    40
  moreover
hoelzl@62975
    41
  { fix k :: nat
hoelzl@62975
    42
    have "(\<Sum>j<k + i. f j) = (\<Sum>j=i..<k + i. f j) + (\<Sum>j=0..<i. f j)"
nipkow@64267
    43
      by (subst sum.union_disjoint[symmetric]) (auto intro!: sum.cong)
hoelzl@62975
    44
    also have "(\<Sum>j=i..<k + i. f j) = (\<Sum>j\<in>(\<lambda>n. n + i)`{0..<k}. f j)"
haftmann@67411
    45
      unfolding image_add_atLeastLessThan by simp
hoelzl@62975
    46
    finally have "(\<Sum>j<k + i. f j) = (\<Sum>n<k. f (n + i)) + (\<Sum>j<i. f j)"
nipkow@64267
    47
      by (auto simp: inj_on_def atLeast0LessThan sum.reindex) }
hoelzl@62975
    48
  ultimately have "(\<lambda>k. (\<Sum>n<k + i. f n)) \<longlonglongrightarrow> l + (\<Sum>j<i. f j)"
hoelzl@62975
    49
    by simp
hoelzl@62975
    50
  then show ?thesis
hoelzl@62975
    51
    unfolding sums_def by (rule LIMSEQ_offset)
hoelzl@62975
    52
qed
hoelzl@62975
    53
hoelzl@62975
    54
lemma suminf_offset:
hoelzl@62975
    55
  fixes f g :: "nat \<Rightarrow> 'a :: {t2_space, topological_comm_monoid_add}"
hoelzl@62975
    56
  shows "summable (\<lambda>j. f (j + i)) \<Longrightarrow> suminf f = (\<Sum>j. f (j + i)) + (\<Sum>j<i. f j)"
hoelzl@62975
    57
  by (intro sums_unique[symmetric] sums_offset summable_sums)
hoelzl@62975
    58
hoelzl@62975
    59
lemma eventually_at_left_1: "(\<And>z::real. 0 < z \<Longrightarrow> z < 1 \<Longrightarrow> P z) \<Longrightarrow> eventually P (at_left 1)"
hoelzl@62975
    60
  by (subst eventually_at_left[of 0]) (auto intro: exI[of _ 0])
hoelzl@62975
    61
hoelzl@62975
    62
lemma mult_eq_1:
hoelzl@62975
    63
  fixes a b :: "'a :: {ordered_semiring, comm_monoid_mult}"
hoelzl@62975
    64
  shows "0 \<le> a \<Longrightarrow> a \<le> 1 \<Longrightarrow> b \<le> 1 \<Longrightarrow> a * b = 1 \<longleftrightarrow> (a = 1 \<and> b = 1)"
hoelzl@62975
    65
  by (metis mult.left_neutral eq_iff mult.commute mult_right_mono)
hoelzl@62975
    66
hoelzl@62975
    67
lemma ereal_add_diff_cancel:
hoelzl@62975
    68
  fixes a b :: ereal
hoelzl@62975
    69
  shows "\<bar>b\<bar> \<noteq> \<infinity> \<Longrightarrow> (a + b) - b = a"
hoelzl@62975
    70
  by (cases a b rule: ereal2_cases) auto
hoelzl@62975
    71
hoelzl@62975
    72
lemma add_top:
hoelzl@62975
    73
  fixes x :: "'a::{order_top, ordered_comm_monoid_add}"
hoelzl@62975
    74
  shows "0 \<le> x \<Longrightarrow> x + top = top"
hoelzl@62975
    75
  by (intro top_le add_increasing order_refl)
hoelzl@62975
    76
hoelzl@62975
    77
lemma top_add:
hoelzl@62975
    78
  fixes x :: "'a::{order_top, ordered_comm_monoid_add}"
hoelzl@62975
    79
  shows "0 \<le> x \<Longrightarrow> top + x = top"
hoelzl@62975
    80
  by (intro top_le add_increasing2 order_refl)
hoelzl@62975
    81
hoelzl@62975
    82
lemma le_lfp: "mono f \<Longrightarrow> x \<le> lfp f \<Longrightarrow> f x \<le> lfp f"
hoelzl@62975
    83
  by (subst lfp_unfold) (auto dest: monoD)
hoelzl@62975
    84
hoelzl@62975
    85
lemma lfp_transfer:
hoelzl@62975
    86
  assumes \<alpha>: "sup_continuous \<alpha>" and f: "sup_continuous f" and mg: "mono g"
hoelzl@62975
    87
  assumes bot: "\<alpha> bot \<le> lfp g" and eq: "\<And>x. x \<le> lfp f \<Longrightarrow> \<alpha> (f x) = g (\<alpha> x)"
hoelzl@62975
    88
  shows "\<alpha> (lfp f) = lfp g"
hoelzl@62975
    89
proof (rule antisym)
hoelzl@62975
    90
  note mf = sup_continuous_mono[OF f]
hoelzl@62975
    91
  have f_le_lfp: "(f ^^ i) bot \<le> lfp f" for i
hoelzl@62975
    92
    by (induction i) (auto intro: le_lfp mf)
hoelzl@62975
    93
hoelzl@62975
    94
  have "\<alpha> ((f ^^ i) bot) \<le> lfp g" for i
hoelzl@62975
    95
    by (induction i) (auto simp: bot eq f_le_lfp intro!: le_lfp mg)
hoelzl@62975
    96
  then show "\<alpha> (lfp f) \<le> lfp g"
hoelzl@62975
    97
    unfolding sup_continuous_lfp[OF f]
hoelzl@62975
    98
    by (subst \<alpha>[THEN sup_continuousD])
hoelzl@62975
    99
       (auto intro!: mono_funpow sup_continuous_mono[OF f] SUP_least)
hoelzl@62975
   100
hoelzl@62975
   101
  show "lfp g \<le> \<alpha> (lfp f)"
wenzelm@63979
   102
    by (rule lfp_lowerbound) (simp add: eq[symmetric] lfp_fixpoint[OF mf])
hoelzl@62975
   103
qed
hoelzl@62975
   104
hoelzl@62975
   105
lemma sup_continuous_applyD: "sup_continuous f \<Longrightarrow> sup_continuous (\<lambda>x. f x h)"
hoelzl@62975
   106
  using sup_continuous_apply[THEN sup_continuous_compose] .
hoelzl@62975
   107
hoelzl@62975
   108
lemma sup_continuous_SUP[order_continuous_intros]:
hoelzl@62975
   109
  fixes M :: "_ \<Rightarrow> _ \<Rightarrow> 'a::complete_lattice"
hoelzl@62975
   110
  assumes M: "\<And>i. i \<in> I \<Longrightarrow> sup_continuous (M i)"
haftmann@69260
   111
  shows  "sup_continuous (SUP i\<in>I. M i)"
haftmann@69861
   112
  unfolding sup_continuous_def by (auto simp add: sup_continuousD [OF M] image_comp intro: SUP_commute)
hoelzl@62975
   113
hoelzl@62975
   114
lemma sup_continuous_apply_SUP[order_continuous_intros]:
hoelzl@62975
   115
  fixes M :: "_ \<Rightarrow> _ \<Rightarrow> 'a::complete_lattice"
haftmann@69260
   116
  shows "(\<And>i. i \<in> I \<Longrightarrow> sup_continuous (M i)) \<Longrightarrow> sup_continuous (\<lambda>x. SUP i\<in>I. M i x)"
hoelzl@62975
   117
  unfolding SUP_apply[symmetric] by (rule sup_continuous_SUP)
hoelzl@62975
   118
hoelzl@62975
   119
lemma sup_continuous_lfp'[order_continuous_intros]:
hoelzl@62975
   120
  assumes 1: "sup_continuous f"
hoelzl@62975
   121
  assumes 2: "\<And>g. sup_continuous g \<Longrightarrow> sup_continuous (f g)"
hoelzl@62975
   122
  shows "sup_continuous (lfp f)"
hoelzl@62975
   123
proof -
hoelzl@62975
   124
  have "sup_continuous ((f ^^ i) bot)" for i
hoelzl@62975
   125
  proof (induction i)
hoelzl@62975
   126
    case (Suc i) then show ?case
hoelzl@62975
   127
      by (auto intro!: 2)
hoelzl@62975
   128
  qed (simp add: bot_fun_def sup_continuous_const)
hoelzl@62975
   129
  then show ?thesis
hoelzl@62975
   130
    unfolding sup_continuous_lfp[OF 1] by (intro order_continuous_intros)
hoelzl@62975
   131
qed
hoelzl@62975
   132
hoelzl@62975
   133
lemma sup_continuous_lfp''[order_continuous_intros]:
hoelzl@62975
   134
  assumes 1: "\<And>s. sup_continuous (f s)"
hoelzl@62975
   135
  assumes 2: "\<And>g. sup_continuous g \<Longrightarrow> sup_continuous (\<lambda>s. f s (g s))"
hoelzl@62975
   136
  shows "sup_continuous (\<lambda>x. lfp (f x))"
hoelzl@62975
   137
proof -
hoelzl@62975
   138
  have "sup_continuous (\<lambda>x. (f x ^^ i) bot)" for i
hoelzl@62975
   139
  proof (induction i)
hoelzl@62975
   140
    case (Suc i) then show ?case
hoelzl@62975
   141
      by (auto intro!: 2)
hoelzl@62975
   142
  qed (simp add: bot_fun_def sup_continuous_const)
hoelzl@62975
   143
  then show ?thesis
hoelzl@62975
   144
    unfolding sup_continuous_lfp[OF 1] by (intro order_continuous_intros)
hoelzl@62975
   145
qed
hoelzl@62975
   146
hoelzl@62975
   147
lemma mono_INF_fun:
haftmann@69260
   148
    "(\<And>x y. mono (F x y)) \<Longrightarrow> mono (\<lambda>z x. INF y \<in> X x. F x y z :: 'a :: complete_lattice)"
hoelzl@62975
   149
  by (auto intro!: INF_mono[OF bexI] simp: le_fun_def mono_def)
hoelzl@62975
   150
hoelzl@62975
   151
lemma continuous_on_max:
hoelzl@62975
   152
  fixes f g :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"
hoelzl@62975
   153
  shows "continuous_on A f \<Longrightarrow> continuous_on A g \<Longrightarrow> continuous_on A (\<lambda>x. max (f x) (g x))"
hoelzl@62975
   154
  by (auto simp: continuous_on_def intro!: tendsto_max)
hoelzl@62975
   155
hoelzl@62975
   156
lemma continuous_on_cmult_ereal:
hoelzl@62975
   157
  "\<bar>c::ereal\<bar> \<noteq> \<infinity> \<Longrightarrow> continuous_on A f \<Longrightarrow> continuous_on A (\<lambda>x. c * f x)"
hoelzl@62975
   158
  using tendsto_cmult_ereal[of c f "f x" "at x within A" for x]
hoelzl@62975
   159
  by (auto simp: continuous_on_def simp del: tendsto_cmult_ereal)
hoelzl@62975
   160
hoelzl@62975
   161
lemma real_of_nat_Sup:
hoelzl@62975
   162
  assumes "A \<noteq> {}" "bdd_above A"
haftmann@69260
   163
  shows "of_nat (Sup A) = (SUP a\<in>A. of_nat a :: real)"
hoelzl@62975
   164
proof (intro antisym)
haftmann@69260
   165
  show "(SUP a\<in>A. of_nat a::real) \<le> of_nat (Sup A)"
hoelzl@62975
   166
    using assms by (intro cSUP_least of_nat_mono) (auto intro: cSup_upper)
hoelzl@62975
   167
  have "Sup A \<in> A"
hoelzl@62975
   168
    unfolding Sup_nat_def using assms by (intro Max_in) (auto simp: bdd_above_nat)
haftmann@69260
   169
  then show "of_nat (Sup A) \<le> (SUP a\<in>A. of_nat a::real)"
hoelzl@62975
   170
    by (intro cSUP_upper bdd_above_image_mono assms) (auto simp: mono_def)
hoelzl@62975
   171
qed
hoelzl@62975
   172
hoelzl@62378
   173
lemma (in complete_lattice) SUP_sup_const1:
haftmann@69260
   174
  "I \<noteq> {} \<Longrightarrow> (SUP i\<in>I. sup c (f i)) = sup c (SUP i\<in>I. f i)"
hoelzl@62378
   175
  using SUP_sup_distrib[of "\<lambda>_. c" I f] by simp
hoelzl@62378
   176
hoelzl@62378
   177
lemma (in complete_lattice) SUP_sup_const2:
haftmann@69260
   178
  "I \<noteq> {} \<Longrightarrow> (SUP i\<in>I. sup (f i) c) = sup (SUP i\<in>I. f i) c"
hoelzl@62378
   179
  using SUP_sup_distrib[of f I "\<lambda>_. c"] by simp
hoelzl@62378
   180
hoelzl@62378
   181
lemma one_less_of_natD:
hoelzl@62378
   182
  "(1::'a::linordered_semidom) < of_nat n \<Longrightarrow> 1 < n"
hoelzl@62378
   183
  using zero_le_one[where 'a='a]
hoelzl@62378
   184
  apply (cases n)
hoelzl@62378
   185
  apply simp
hoelzl@62378
   186
  subgoal for n'
hoelzl@62378
   187
    apply (cases n')
hoelzl@62378
   188
    apply simp
hoelzl@62378
   189
    apply simp
hoelzl@62378
   190
    done
hoelzl@62378
   191
  done
hoelzl@62378
   192
hoelzl@62975
   193
subsection \<open>Defining the extended non-negative reals\<close>
hoelzl@62975
   194
hoelzl@62975
   195
text \<open>Basic definitions and type class setup\<close>
hoelzl@62975
   196
hoelzl@62375
   197
typedef ennreal = "{x :: ereal. 0 \<le> x}"
hoelzl@62378
   198
  morphisms enn2ereal e2ennreal'
hoelzl@62375
   199
  by auto
hoelzl@62375
   200
hoelzl@62378
   201
definition "e2ennreal x = e2ennreal' (max 0 x)"
hoelzl@62375
   202
hoelzl@62975
   203
lemma enn2ereal_range: "e2ennreal ` {0..} = UNIV"
hoelzl@62975
   204
proof -
hoelzl@62975
   205
  have "\<exists>y\<ge>0. x = e2ennreal y" for x
hoelzl@62975
   206
    by (cases x) (auto simp: e2ennreal_def max_absorb2)
hoelzl@62975
   207
  then show ?thesis
hoelzl@62975
   208
    by (auto simp: image_iff Bex_def)
hoelzl@62975
   209
qed
hoelzl@62975
   210
hoelzl@62378
   211
lemma type_definition_ennreal': "type_definition enn2ereal e2ennreal {x. 0 \<le> x}"
hoelzl@62378
   212
  using type_definition_ennreal
hoelzl@62378
   213
  by (auto simp: type_definition_def e2ennreal_def max_absorb2)
hoelzl@62375
   214
hoelzl@62378
   215
setup_lifting type_definition_ennreal'
hoelzl@62378
   216
hoelzl@62375
   217
declare [[coercion e2ennreal]]
hoelzl@62375
   218
hoelzl@62378
   219
instantiation ennreal :: complete_linorder
hoelzl@62378
   220
begin
hoelzl@62378
   221
hoelzl@62378
   222
lift_definition top_ennreal :: ennreal is top by (rule top_greatest)
hoelzl@62378
   223
lift_definition bot_ennreal :: ennreal is 0 by (rule order_refl)
hoelzl@62378
   224
lift_definition sup_ennreal :: "ennreal \<Rightarrow> ennreal \<Rightarrow> ennreal" is sup by (rule le_supI1)
hoelzl@62378
   225
lift_definition inf_ennreal :: "ennreal \<Rightarrow> ennreal \<Rightarrow> ennreal" is inf by (rule le_infI)
hoelzl@62378
   226
hoelzl@62378
   227
lift_definition Inf_ennreal :: "ennreal set \<Rightarrow> ennreal" is "Inf"
hoelzl@62378
   228
  by (rule Inf_greatest)
hoelzl@62378
   229
hoelzl@62378
   230
lift_definition Sup_ennreal :: "ennreal set \<Rightarrow> ennreal" is "sup 0 \<circ> Sup"
hoelzl@62378
   231
  by auto
hoelzl@62378
   232
nipkow@67399
   233
lift_definition less_eq_ennreal :: "ennreal \<Rightarrow> ennreal \<Rightarrow> bool" is "(\<le>)" .
nipkow@67399
   234
lift_definition less_ennreal :: "ennreal \<Rightarrow> ennreal \<Rightarrow> bool" is "(<)" .
hoelzl@62378
   235
hoelzl@62378
   236
instance
hoelzl@62378
   237
  by standard
hoelzl@62378
   238
     (transfer ; auto simp: Inf_lower Inf_greatest Sup_upper Sup_least le_max_iff_disj max.absorb1)+
hoelzl@62378
   239
hoelzl@62378
   240
end
hoelzl@62378
   241
hoelzl@62975
   242
lemma pcr_ennreal_enn2ereal[simp]: "pcr_ennreal (enn2ereal x) x"
hoelzl@62975
   243
  by (simp add: ennreal.pcr_cr_eq cr_ennreal_def)
hoelzl@62975
   244
nipkow@67399
   245
lemma rel_fun_eq_pcr_ennreal: "rel_fun (=) pcr_ennreal f g \<longleftrightarrow> f = enn2ereal \<circ> g"
hoelzl@62975
   246
  by (auto simp: rel_fun_def ennreal.pcr_cr_eq cr_ennreal_def)
hoelzl@62378
   247
hoelzl@62378
   248
instantiation ennreal :: infinity
hoelzl@62378
   249
begin
hoelzl@62378
   250
hoelzl@62378
   251
definition infinity_ennreal :: ennreal
hoelzl@62378
   252
where
hoelzl@62378
   253
  [simp]: "\<infinity> = (top::ennreal)"
hoelzl@62378
   254
hoelzl@62378
   255
instance ..
hoelzl@62378
   256
hoelzl@62378
   257
end
hoelzl@62378
   258
hoelzl@62378
   259
instantiation ennreal :: "{semiring_1_no_zero_divisors, comm_semiring_1}"
hoelzl@62375
   260
begin
hoelzl@62375
   261
hoelzl@62375
   262
lift_definition one_ennreal :: ennreal is 1 by simp
hoelzl@62375
   263
lift_definition zero_ennreal :: ennreal is 0 by simp
nipkow@67399
   264
lift_definition plus_ennreal :: "ennreal \<Rightarrow> ennreal \<Rightarrow> ennreal" is "(+)" by simp
nipkow@69064
   265
lift_definition times_ennreal :: "ennreal \<Rightarrow> ennreal \<Rightarrow> ennreal" is "(*)" by simp
hoelzl@62375
   266
hoelzl@62375
   267
instance
hoelzl@62375
   268
  by standard (transfer; auto simp: field_simps ereal_right_distrib)+
hoelzl@62375
   269
hoelzl@62375
   270
end
hoelzl@62375
   271
hoelzl@62378
   272
instantiation ennreal :: minus
hoelzl@62378
   273
begin
hoelzl@62378
   274
hoelzl@62378
   275
lift_definition minus_ennreal :: "ennreal \<Rightarrow> ennreal \<Rightarrow> ennreal" is "\<lambda>a b. max 0 (a - b)"
hoelzl@62378
   276
  by simp
hoelzl@62378
   277
hoelzl@62378
   278
instance ..
hoelzl@62378
   279
hoelzl@62378
   280
end
hoelzl@62378
   281
hoelzl@62375
   282
instance ennreal :: numeral ..
hoelzl@62375
   283
hoelzl@62375
   284
instantiation ennreal :: inverse
hoelzl@62375
   285
begin
hoelzl@62375
   286
hoelzl@62375
   287
lift_definition inverse_ennreal :: "ennreal \<Rightarrow> ennreal" is inverse
hoelzl@62375
   288
  by (rule inverse_ereal_ge0I)
hoelzl@62375
   289
hoelzl@62375
   290
definition divide_ennreal :: "ennreal \<Rightarrow> ennreal \<Rightarrow> ennreal"
hoelzl@62375
   291
  where "x div y = x * inverse (y :: ennreal)"
hoelzl@62375
   292
hoelzl@62375
   293
instance ..
hoelzl@62375
   294
hoelzl@62375
   295
end
hoelzl@62375
   296
wenzelm@67443
   297
lemma ennreal_zero_less_one: "0 < (1::ennreal)" \<comment> \<open>TODO: remove\<close>
hoelzl@62375
   298
  by transfer auto
hoelzl@62375
   299
hoelzl@62376
   300
instance ennreal :: dioid
hoelzl@62376
   301
proof (standard; transfer)
nipkow@67399
   302
  fix a b :: ereal assume "0 \<le> a" "0 \<le> b" then show "(a \<le> b) = (\<exists>c\<in>Collect ((\<le>) 0). b = a + c)"
hoelzl@62376
   303
    unfolding ereal_ex_split Bex_def
hoelzl@62376
   304
    by (cases a b rule: ereal2_cases) (auto intro!: exI[of _ "real_of_ereal (b - a)"])
hoelzl@62376
   305
qed
hoelzl@62376
   306
hoelzl@62375
   307
instance ennreal :: ordered_comm_semiring
hoelzl@62375
   308
  by standard
hoelzl@62375
   309
     (transfer ; auto intro: add_mono mult_mono mult_ac ereal_left_distrib ereal_mult_left_mono)+
hoelzl@62375
   310
hoelzl@62378
   311
instance ennreal :: linordered_nonzero_semiring
lp15@67691
   312
proof
lp15@67691
   313
  fix a b::ennreal
lp15@67691
   314
  show "a < b \<Longrightarrow> a + 1 < b + 1"
lp15@67691
   315
    by transfer (simp add: add_right_mono ereal_add_cancel_right less_le)
lp15@67691
   316
qed (transfer; simp)
hoelzl@62378
   317
hoelzl@62975
   318
instance ennreal :: strict_ordered_ab_semigroup_add
hoelzl@62975
   319
proof
hoelzl@62975
   320
  fix a b c d :: ennreal show "a < b \<Longrightarrow> c < d \<Longrightarrow> a + c < b + d"
hoelzl@62975
   321
    by transfer (auto intro!: ereal_add_strict_mono)
hoelzl@62975
   322
qed
hoelzl@62975
   323
hoelzl@62378
   324
declare [[coercion "of_nat :: nat \<Rightarrow> ennreal"]]
hoelzl@62378
   325
hoelzl@62378
   326
lemma e2ennreal_neg: "x \<le> 0 \<Longrightarrow> e2ennreal x = 0"
hoelzl@62378
   327
  unfolding zero_ennreal_def e2ennreal_def by (simp add: max_absorb1)
hoelzl@62378
   328
hoelzl@62378
   329
lemma e2ennreal_mono: "x \<le> y \<Longrightarrow> e2ennreal x \<le> e2ennreal y"
hoelzl@62378
   330
  by (cases "0 \<le> x" "0 \<le> y" rule: bool.exhaust[case_product bool.exhaust])
hoelzl@62378
   331
     (auto simp: e2ennreal_neg less_eq_ennreal.abs_eq eq_onp_def)
hoelzl@62378
   332
hoelzl@62975
   333
lemma enn2ereal_nonneg[simp]: "0 \<le> enn2ereal x"
hoelzl@62975
   334
  using ennreal.enn2ereal[of x] by simp
hoelzl@62975
   335
hoelzl@62975
   336
lemma ereal_ennreal_cases:
hoelzl@62975
   337
  obtains b where "0 \<le> a" "a = enn2ereal b" | "a < 0"
hoelzl@62975
   338
  using e2ennreal'_inverse[of a, symmetric] by (cases "0 \<le> a") (auto intro: enn2ereal_nonneg)
hoelzl@62975
   339
nipkow@67399
   340
lemma rel_fun_liminf[transfer_rule]: "rel_fun (rel_fun (=) pcr_ennreal) pcr_ennreal liminf liminf"
hoelzl@62975
   341
proof -
nipkow@67399
   342
  have "rel_fun (rel_fun (=) pcr_ennreal) pcr_ennreal (\<lambda>x. sup 0 (liminf x)) liminf"
hoelzl@62975
   343
    unfolding liminf_SUP_INF[abs_def] by (transfer_prover_start, transfer_step+; simp)
hoelzl@62975
   344
  then show ?thesis
hoelzl@62975
   345
    apply (subst (asm) (2) rel_fun_def)
hoelzl@62975
   346
    apply (subst (2) rel_fun_def)
hoelzl@62975
   347
    apply (auto simp: comp_def max.absorb2 Liminf_bounded rel_fun_eq_pcr_ennreal)
hoelzl@62975
   348
    done
hoelzl@62975
   349
qed
hoelzl@62975
   350
nipkow@67399
   351
lemma rel_fun_limsup[transfer_rule]: "rel_fun (rel_fun (=) pcr_ennreal) pcr_ennreal limsup limsup"
hoelzl@62975
   352
proof -
haftmann@69260
   353
  have "rel_fun (rel_fun (=) pcr_ennreal) pcr_ennreal (\<lambda>x. INF n. sup 0 (SUP i\<in>{n..}. x i)) limsup"
hoelzl@62975
   354
    unfolding limsup_INF_SUP[abs_def] by (transfer_prover_start, transfer_step+; simp)
hoelzl@62975
   355
  then show ?thesis
hoelzl@62975
   356
    unfolding limsup_INF_SUP[abs_def]
hoelzl@62975
   357
    apply (subst (asm) (2) rel_fun_def)
hoelzl@62975
   358
    apply (subst (2) rel_fun_def)
hoelzl@62975
   359
    apply (auto simp: comp_def max.absorb2 Sup_upper2 rel_fun_eq_pcr_ennreal)
hoelzl@62975
   360
    apply (subst (asm) max.absorb2)
hoelzl@62975
   361
    apply (rule SUP_upper2)
hoelzl@62975
   362
    apply auto
hoelzl@62975
   363
    done
hoelzl@62975
   364
qed
hoelzl@62975
   365
nipkow@64267
   366
lemma sum_enn2ereal[simp]: "(\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i) \<Longrightarrow> (\<Sum>i\<in>I. enn2ereal (f i)) = enn2ereal (sum f I)"
nipkow@64267
   367
  by (induction I rule: infinite_finite_induct) (auto simp: sum_nonneg zero_ennreal.rep_eq plus_ennreal.rep_eq)
hoelzl@62975
   368
nipkow@64267
   369
lemma transfer_e2ennreal_sum [transfer_rule]:
nipkow@67399
   370
  "rel_fun (rel_fun (=) pcr_ennreal) (rel_fun (=) pcr_ennreal) sum sum"
hoelzl@62975
   371
  by (auto intro!: rel_funI simp: rel_fun_eq_pcr_ennreal comp_def)
hoelzl@62975
   372
hoelzl@62975
   373
lemma enn2ereal_of_nat[simp]: "enn2ereal (of_nat n) = ereal n"
hoelzl@62975
   374
  by (induction n) (auto simp: zero_ennreal.rep_eq one_ennreal.rep_eq plus_ennreal.rep_eq)
hoelzl@62975
   375
hoelzl@62975
   376
lemma enn2ereal_numeral[simp]: "enn2ereal (numeral a) = numeral a"
hoelzl@62975
   377
  apply (subst of_nat_numeral[of a, symmetric])
hoelzl@62975
   378
  apply (subst enn2ereal_of_nat)
hoelzl@62975
   379
  apply simp
hoelzl@62975
   380
  done
hoelzl@62975
   381
hoelzl@62975
   382
lemma transfer_numeral[transfer_rule]: "pcr_ennreal (numeral a) (numeral a)"
hoelzl@62975
   383
  unfolding cr_ennreal_def pcr_ennreal_def by auto
hoelzl@62975
   384
hoelzl@62378
   385
subsection \<open>Cancellation simprocs\<close>
hoelzl@62378
   386
hoelzl@62378
   387
lemma ennreal_add_left_cancel: "a + b = a + c \<longleftrightarrow> a = (\<infinity>::ennreal) \<or> b = c"
hoelzl@62378
   388
  unfolding infinity_ennreal_def by transfer (simp add: top_ereal_def ereal_add_cancel_left)
hoelzl@62378
   389
hoelzl@62378
   390
lemma ennreal_add_left_cancel_le: "a + b \<le> a + c \<longleftrightarrow> a = (\<infinity>::ennreal) \<or> b \<le> c"
hoelzl@62378
   391
  unfolding infinity_ennreal_def by transfer (simp add: ereal_add_le_add_iff top_ereal_def disj_commute)
hoelzl@62378
   392
hoelzl@62378
   393
lemma ereal_add_left_cancel_less:
hoelzl@62378
   394
  fixes a b c :: ereal
hoelzl@62378
   395
  shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> a + b < a + c \<longleftrightarrow> a \<noteq> \<infinity> \<and> b < c"
hoelzl@62378
   396
  by (cases a b c rule: ereal3_cases) auto
hoelzl@62378
   397
hoelzl@62378
   398
lemma ennreal_add_left_cancel_less: "a + b < a + c \<longleftrightarrow> a \<noteq> (\<infinity>::ennreal) \<and> b < c"
hoelzl@62378
   399
  unfolding infinity_ennreal_def
hoelzl@62378
   400
  by transfer (simp add: top_ereal_def ereal_add_left_cancel_less)
hoelzl@62378
   401
hoelzl@62378
   402
ML \<open>
hoelzl@62378
   403
structure Cancel_Ennreal_Common =
hoelzl@62378
   404
struct
hoelzl@62378
   405
  (* copied from src/HOL/Tools/nat_numeral_simprocs.ML *)
hoelzl@62378
   406
  fun find_first_t _    _ []         = raise TERM("find_first_t", [])
hoelzl@62378
   407
    | find_first_t past u (t::terms) =
hoelzl@62378
   408
          if u aconv t then (rev past @ terms)
hoelzl@62378
   409
          else find_first_t (t::past) u terms
hoelzl@62378
   410
wenzelm@69593
   411
  fun dest_summing (Const (\<^const_name>\<open>Groups.plus\<close>, _) $ t $ u, ts) =
hoelzl@62378
   412
        dest_summing (t, dest_summing (u, ts))
hoelzl@62378
   413
    | dest_summing (t, ts) = t :: ts
hoelzl@62378
   414
hoelzl@62378
   415
  val mk_sum = Arith_Data.long_mk_sum
hoelzl@62378
   416
  fun dest_sum t = dest_summing (t, [])
hoelzl@62378
   417
  val find_first = find_first_t []
hoelzl@62378
   418
  val trans_tac = Numeral_Simprocs.trans_tac
hoelzl@62378
   419
  val norm_ss =
wenzelm@69593
   420
    simpset_of (put_simpset HOL_basic_ss \<^context>
hoelzl@62378
   421
      addsimps @{thms ac_simps add_0_left add_0_right})
hoelzl@62378
   422
  fun norm_tac ctxt = ALLGOALS (simp_tac (put_simpset norm_ss ctxt))
hoelzl@62378
   423
  fun simplify_meta_eq ctxt cancel_th th =
hoelzl@62378
   424
    Arith_Data.simplify_meta_eq [] ctxt
hoelzl@62378
   425
      ([th, cancel_th] MRS trans)
hoelzl@62378
   426
  fun mk_eq (a, b) = HOLogic.mk_Trueprop (HOLogic.mk_eq (a, b))
hoelzl@62378
   427
end
hoelzl@62378
   428
hoelzl@62378
   429
structure Eq_Ennreal_Cancel = ExtractCommonTermFun
hoelzl@62378
   430
(open Cancel_Ennreal_Common
hoelzl@62378
   431
  val mk_bal = HOLogic.mk_eq
wenzelm@69593
   432
  val dest_bal = HOLogic.dest_bin \<^const_name>\<open>HOL.eq\<close> \<^typ>\<open>ennreal\<close>
hoelzl@62378
   433
  fun simp_conv _ _ = SOME @{thm ennreal_add_left_cancel}
hoelzl@62378
   434
)
hoelzl@62378
   435
hoelzl@62378
   436
structure Le_Ennreal_Cancel = ExtractCommonTermFun
hoelzl@62378
   437
(open Cancel_Ennreal_Common
wenzelm@69593
   438
  val mk_bal = HOLogic.mk_binrel \<^const_name>\<open>Orderings.less_eq\<close>
wenzelm@69593
   439
  val dest_bal = HOLogic.dest_bin \<^const_name>\<open>Orderings.less_eq\<close> \<^typ>\<open>ennreal\<close>
hoelzl@62378
   440
  fun simp_conv _ _ = SOME @{thm ennreal_add_left_cancel_le}
hoelzl@62378
   441
)
hoelzl@62378
   442
hoelzl@62378
   443
structure Less_Ennreal_Cancel = ExtractCommonTermFun
hoelzl@62378
   444
(open Cancel_Ennreal_Common
wenzelm@69593
   445
  val mk_bal = HOLogic.mk_binrel \<^const_name>\<open>Orderings.less\<close>
wenzelm@69593
   446
  val dest_bal = HOLogic.dest_bin \<^const_name>\<open>Orderings.less\<close> \<^typ>\<open>ennreal\<close>
hoelzl@62378
   447
  fun simp_conv _ _ = SOME @{thm ennreal_add_left_cancel_less}
hoelzl@62378
   448
)
hoelzl@62378
   449
\<close>
hoelzl@62378
   450
hoelzl@62378
   451
simproc_setup ennreal_eq_cancel
hoelzl@62378
   452
  ("(l::ennreal) + m = n" | "(l::ennreal) = m + n") =
hoelzl@62378
   453
  \<open>fn phi => fn ctxt => fn ct => Eq_Ennreal_Cancel.proc ctxt (Thm.term_of ct)\<close>
hoelzl@62378
   454
hoelzl@62378
   455
simproc_setup ennreal_le_cancel
hoelzl@62378
   456
  ("(l::ennreal) + m \<le> n" | "(l::ennreal) \<le> m + n") =
hoelzl@62378
   457
  \<open>fn phi => fn ctxt => fn ct => Le_Ennreal_Cancel.proc ctxt (Thm.term_of ct)\<close>
hoelzl@62378
   458
hoelzl@62378
   459
simproc_setup ennreal_less_cancel
hoelzl@62378
   460
  ("(l::ennreal) + m < n" | "(l::ennreal) < m + n") =
hoelzl@62378
   461
  \<open>fn phi => fn ctxt => fn ct => Less_Ennreal_Cancel.proc ctxt (Thm.term_of ct)\<close>
hoelzl@62378
   462
hoelzl@62375
   463
hoelzl@62975
   464
subsection \<open>Order with top\<close>
hoelzl@62378
   465
hoelzl@62378
   466
lemma ennreal_zero_less_top[simp]: "0 < (top::ennreal)"
hoelzl@62378
   467
  by transfer (simp add: top_ereal_def)
hoelzl@62378
   468
hoelzl@62378
   469
lemma ennreal_one_less_top[simp]: "1 < (top::ennreal)"
hoelzl@62378
   470
  by transfer (simp add: top_ereal_def)
hoelzl@62378
   471
hoelzl@62378
   472
lemma ennreal_zero_neq_top[simp]: "0 \<noteq> (top::ennreal)"
hoelzl@62378
   473
  by transfer (simp add: top_ereal_def)
hoelzl@62378
   474
hoelzl@62378
   475
lemma ennreal_top_neq_zero[simp]: "(top::ennreal) \<noteq> 0"
hoelzl@62378
   476
  by transfer (simp add: top_ereal_def)
hoelzl@62378
   477
hoelzl@62378
   478
lemma ennreal_top_neq_one[simp]: "top \<noteq> (1::ennreal)"
nipkow@68406
   479
  by transfer (simp add: top_ereal_def one_ereal_def flip: ereal_max)
hoelzl@62378
   480
hoelzl@62378
   481
lemma ennreal_one_neq_top[simp]: "1 \<noteq> (top::ennreal)"
nipkow@68406
   482
  by transfer (simp add: top_ereal_def one_ereal_def flip: ereal_max)
hoelzl@62378
   483
hoelzl@62378
   484
lemma ennreal_add_less_top[simp]:
hoelzl@62378
   485
  fixes a b :: ennreal
hoelzl@62378
   486
  shows "a + b < top \<longleftrightarrow> a < top \<and> b < top"
hoelzl@62378
   487
  by transfer (auto simp: top_ereal_def)
hoelzl@62378
   488
hoelzl@62378
   489
lemma ennreal_add_eq_top[simp]:
hoelzl@62378
   490
  fixes a b :: ennreal
hoelzl@62378
   491
  shows "a + b = top \<longleftrightarrow> a = top \<or> b = top"
hoelzl@62378
   492
  by transfer (auto simp: top_ereal_def)
hoelzl@62378
   493
nipkow@64267
   494
lemma ennreal_sum_less_top[simp]:
hoelzl@62378
   495
  fixes f :: "'a \<Rightarrow> ennreal"
hoelzl@62378
   496
  shows "finite I \<Longrightarrow> (\<Sum>i\<in>I. f i) < top \<longleftrightarrow> (\<forall>i\<in>I. f i < top)"
hoelzl@62378
   497
  by (induction I rule: finite_induct) auto
hoelzl@62378
   498
nipkow@64267
   499
lemma ennreal_sum_eq_top[simp]:
hoelzl@62378
   500
  fixes f :: "'a \<Rightarrow> ennreal"
hoelzl@62378
   501
  shows "finite I \<Longrightarrow> (\<Sum>i\<in>I. f i) = top \<longleftrightarrow> (\<exists>i\<in>I. f i = top)"
hoelzl@62378
   502
  by (induction I rule: finite_induct) auto
hoelzl@62378
   503
hoelzl@62975
   504
lemma ennreal_mult_eq_top_iff:
hoelzl@62975
   505
  fixes a b :: ennreal
hoelzl@62975
   506
  shows "a * b = top \<longleftrightarrow> (a = top \<and> b \<noteq> 0) \<or> (b = top \<and> a \<noteq> 0)"
hoelzl@62975
   507
  by transfer (auto simp: top_ereal_def)
hoelzl@62975
   508
hoelzl@62975
   509
lemma ennreal_top_eq_mult_iff:
hoelzl@62975
   510
  fixes a b :: ennreal
hoelzl@62975
   511
  shows "top = a * b \<longleftrightarrow> (a = top \<and> b \<noteq> 0) \<or> (b = top \<and> a \<noteq> 0)"
hoelzl@62975
   512
  using ennreal_mult_eq_top_iff[of a b] by auto
hoelzl@62975
   513
hoelzl@62975
   514
lemma ennreal_mult_less_top:
hoelzl@62975
   515
  fixes a b :: ennreal
hoelzl@62975
   516
  shows "a * b < top \<longleftrightarrow> (a = 0 \<or> b = 0 \<or> (a < top \<and> b < top))"
hoelzl@62975
   517
  by transfer (auto simp add: top_ereal_def)
hoelzl@62975
   518
hoelzl@62975
   519
lemma top_power_ennreal: "top ^ n = (if n = 0 then 1 else top :: ennreal)"
hoelzl@62975
   520
  by (induction n) (simp_all add: ennreal_mult_eq_top_iff)
hoelzl@62975
   521
nipkow@64272
   522
lemma ennreal_prod_eq_0[simp]:
hoelzl@62975
   523
  fixes f :: "'a \<Rightarrow> ennreal"
nipkow@64272
   524
  shows "(prod f A = 0) = (finite A \<and> (\<exists>i\<in>A. f i = 0))"
hoelzl@62975
   525
  by (induction A rule: infinite_finite_induct) auto
hoelzl@62975
   526
nipkow@64272
   527
lemma ennreal_prod_eq_top:
hoelzl@62975
   528
  fixes f :: "'a \<Rightarrow> ennreal"
hoelzl@62975
   529
  shows "(\<Prod>i\<in>I. f i) = top \<longleftrightarrow> (finite I \<and> ((\<forall>i\<in>I. f i \<noteq> 0) \<and> (\<exists>i\<in>I. f i = top)))"
hoelzl@62975
   530
  by (induction I rule: infinite_finite_induct) (auto simp: ennreal_mult_eq_top_iff)
hoelzl@62975
   531
hoelzl@62975
   532
lemma ennreal_top_mult: "top * a = (if a = 0 then 0 else top :: ennreal)"
hoelzl@62975
   533
  by (simp add: ennreal_mult_eq_top_iff)
hoelzl@62975
   534
hoelzl@62975
   535
lemma ennreal_mult_top: "a * top = (if a = 0 then 0 else top :: ennreal)"
hoelzl@62975
   536
  by (simp add: ennreal_mult_eq_top_iff)
hoelzl@62975
   537
hoelzl@62378
   538
lemma enn2ereal_eq_top_iff[simp]: "enn2ereal x = \<infinity> \<longleftrightarrow> x = top"
hoelzl@62378
   539
  by transfer (simp add: top_ereal_def)
hoelzl@62378
   540
nipkow@67451
   541
lemma enn2ereal_top[simp]: "enn2ereal top = \<infinity>"
hoelzl@62975
   542
  by transfer (simp add: top_ereal_def)
hoelzl@62975
   543
nipkow@67451
   544
lemma e2ennreal_infty[simp]: "e2ennreal \<infinity> = top"
hoelzl@62975
   545
  by (simp add: top_ennreal.abs_eq top_ereal_def)
hoelzl@62975
   546
hoelzl@62975
   547
lemma ennreal_top_minus[simp]: "top - x = (top::ennreal)"
hoelzl@62378
   548
  by transfer (auto simp: top_ereal_def max_def)
hoelzl@62378
   549
hoelzl@62975
   550
lemma minus_top_ennreal: "x - top = (if x = top then top else 0:: ennreal)"
hoelzl@62975
   551
  apply transfer
hoelzl@62975
   552
  subgoal for x
hoelzl@62975
   553
    by (cases x) (auto simp: top_ereal_def max_def)
hoelzl@62975
   554
  done
hoelzl@62975
   555
hoelzl@62975
   556
lemma bot_ennreal: "bot = (0::ennreal)"
hoelzl@62975
   557
  by transfer rule
hoelzl@62975
   558
hoelzl@62975
   559
lemma ennreal_of_nat_neq_top[simp]: "of_nat i \<noteq> (top::ennreal)"
hoelzl@62975
   560
  by (induction i) auto
hoelzl@62975
   561
hoelzl@62975
   562
lemma numeral_eq_of_nat: "(numeral a::ennreal) = of_nat (numeral a)"
hoelzl@62975
   563
  by simp
hoelzl@62975
   564
hoelzl@62975
   565
lemma of_nat_less_top: "of_nat i < (top::ennreal)"
hoelzl@62975
   566
  using less_le_trans[of "of_nat i" "of_nat (Suc i)" "top::ennreal"]
hoelzl@62975
   567
  by simp
hoelzl@62975
   568
hoelzl@62975
   569
lemma top_neq_numeral[simp]: "top \<noteq> (numeral i::ennreal)"
hoelzl@62975
   570
  using of_nat_less_top[of "numeral i"] by simp
hoelzl@62975
   571
hoelzl@62975
   572
lemma ennreal_numeral_less_top[simp]: "numeral i < (top::ennreal)"
hoelzl@62975
   573
  using of_nat_less_top[of "numeral i"] by simp
hoelzl@62975
   574
hoelzl@62975
   575
lemma ennreal_add_bot[simp]: "bot + x = (x::ennreal)"
hoelzl@62975
   576
  by transfer simp
hoelzl@62975
   577
hoelzl@62975
   578
instance ennreal :: semiring_char_0
hoelzl@62975
   579
proof (standard, safe intro!: linorder_injI)
hoelzl@62975
   580
  have *: "1 + of_nat k \<noteq> (0::ennreal)" for k
hoelzl@62975
   581
    using add_pos_nonneg[OF zero_less_one, of "of_nat k :: ennreal"] by auto
hoelzl@62975
   582
  fix x y :: nat assume "x < y" "of_nat x = (of_nat y::ennreal)" then show False
hoelzl@62975
   583
    by (auto simp add: less_iff_Suc_add *)
hoelzl@62975
   584
qed
hoelzl@62975
   585
hoelzl@62975
   586
subsection \<open>Arithmetic\<close>
hoelzl@62975
   587
hoelzl@62378
   588
lemma ennreal_minus_zero[simp]: "a - (0::ennreal) = a"
hoelzl@62378
   589
  by transfer (auto simp: max_def)
hoelzl@62378
   590
hoelzl@62378
   591
lemma ennreal_add_diff_cancel_right[simp]:
hoelzl@62378
   592
  fixes x y z :: ennreal shows "y \<noteq> top \<Longrightarrow> (x + y) - y = x"
hoelzl@62378
   593
  apply transfer
hoelzl@62378
   594
  subgoal for x y
hoelzl@62378
   595
    apply (cases x y rule: ereal2_cases)
hoelzl@62378
   596
    apply (auto split: split_max simp: top_ereal_def)
hoelzl@62378
   597
    done
hoelzl@62378
   598
  done
hoelzl@62378
   599
hoelzl@62378
   600
lemma ennreal_add_diff_cancel_left[simp]:
hoelzl@62378
   601
  fixes x y z :: ennreal shows "y \<noteq> top \<Longrightarrow> (y + x) - y = x"
hoelzl@62378
   602
  by (simp add: add.commute)
hoelzl@62378
   603
hoelzl@62378
   604
lemma
hoelzl@62378
   605
  fixes a b :: ennreal
hoelzl@62378
   606
  shows "a - b = 0 \<Longrightarrow> a \<le> b"
hoelzl@62378
   607
  apply transfer
hoelzl@62378
   608
  subgoal for a b
hoelzl@62378
   609
    apply (cases a b rule: ereal2_cases)
hoelzl@62378
   610
    apply (auto simp: not_le max_def split: if_splits)
hoelzl@62378
   611
    done
hoelzl@62378
   612
  done
hoelzl@62378
   613
hoelzl@62378
   614
lemma ennreal_minus_cancel:
hoelzl@62378
   615
  fixes a b c :: ennreal
hoelzl@62378
   616
  shows "c \<noteq> top \<Longrightarrow> a \<le> c \<Longrightarrow> b \<le> c \<Longrightarrow> c - a = c - b \<Longrightarrow> a = b"
hoelzl@62378
   617
  apply transfer
hoelzl@62378
   618
  subgoal for a b c
hoelzl@62378
   619
    by (cases a b c rule: ereal3_cases)
hoelzl@62378
   620
       (auto simp: top_ereal_def max_def split: if_splits)
hoelzl@62378
   621
  done
hoelzl@62378
   622
hoelzl@62378
   623
lemma sup_const_add_ennreal:
hoelzl@62378
   624
  fixes a b c :: "ennreal"
hoelzl@62378
   625
  shows "sup (c + a) (c + b) = c + sup a b"
hoelzl@62378
   626
  apply transfer
hoelzl@62378
   627
  subgoal for a b c
hoelzl@62378
   628
    apply (cases a b c rule: ereal3_cases)
nipkow@68406
   629
    apply (auto simp flip: ereal_max)
hoelzl@62378
   630
    done
hoelzl@62378
   631
  done
hoelzl@62378
   632
hoelzl@62378
   633
lemma ennreal_diff_add_assoc:
hoelzl@62378
   634
  fixes a b c :: ennreal
hoelzl@62378
   635
  shows "a \<le> b \<Longrightarrow> c + b - a = c + (b - a)"
hoelzl@62378
   636
  apply transfer
hoelzl@62378
   637
  subgoal for a b c
hoelzl@62975
   638
    by (cases a b c rule: ereal3_cases) (auto simp: field_simps max_absorb2)
hoelzl@62378
   639
  done
hoelzl@62378
   640
hoelzl@62648
   641
lemma mult_divide_eq_ennreal:
hoelzl@62648
   642
  fixes a b :: ennreal
hoelzl@62648
   643
  shows "b \<noteq> 0 \<Longrightarrow> b \<noteq> top \<Longrightarrow> (a * b) / b = a"
hoelzl@62648
   644
  unfolding divide_ennreal_def
hoelzl@62648
   645
  apply transfer
hoelzl@62648
   646
  apply (subst mult.assoc)
nipkow@68406
   647
  apply (simp add: top_ereal_def flip: divide_ereal_def)
hoelzl@62648
   648
  done
hoelzl@62648
   649
hoelzl@62648
   650
lemma divide_mult_eq: "a \<noteq> 0 \<Longrightarrow> a \<noteq> \<infinity> \<Longrightarrow> x * a / (b * a) = x / (b::ennreal)"
hoelzl@62648
   651
  unfolding divide_ennreal_def infinity_ennreal_def
hoelzl@62648
   652
  apply transfer
hoelzl@62648
   653
  subgoal for a b c
hoelzl@62648
   654
    apply (cases a b c rule: ereal3_cases)
hoelzl@62648
   655
    apply (auto simp: top_ereal_def)
hoelzl@62648
   656
    done
hoelzl@62648
   657
  done
hoelzl@62648
   658
hoelzl@62648
   659
lemma ennreal_mult_divide_eq:
hoelzl@62648
   660
  fixes a b :: ennreal
hoelzl@62648
   661
  shows "b \<noteq> 0 \<Longrightarrow> b \<noteq> top \<Longrightarrow> (a * b) / b = a"
hoelzl@62648
   662
  unfolding divide_ennreal_def
hoelzl@62648
   663
  apply transfer
hoelzl@62648
   664
  apply (subst mult.assoc)
nipkow@68406
   665
  apply (simp add: top_ereal_def flip: divide_ereal_def)
hoelzl@62648
   666
  done
hoelzl@62648
   667
hoelzl@62975
   668
lemma ennreal_add_diff_cancel:
hoelzl@62975
   669
  fixes a b :: ennreal
hoelzl@62975
   670
  shows "b \<noteq> \<infinity> \<Longrightarrow> (a + b) - b = a"
hoelzl@62975
   671
  unfolding infinity_ennreal_def
hoelzl@62975
   672
  by transfer (simp add: max_absorb2 top_ereal_def ereal_add_diff_cancel)
hoelzl@62648
   673
hoelzl@62648
   674
lemma ennreal_minus_eq_0:
hoelzl@62648
   675
  "a - b = 0 \<Longrightarrow> a \<le> (b::ennreal)"
hoelzl@62648
   676
  apply transfer
hoelzl@62648
   677
  subgoal for a b
hoelzl@62648
   678
    apply (cases a b rule: ereal2_cases)
nipkow@68406
   679
    apply (auto simp: zero_ereal_def max.absorb2 simp flip: ereal_max)
hoelzl@62648
   680
    done
hoelzl@62648
   681
  done
hoelzl@62648
   682
hoelzl@62648
   683
lemma ennreal_mono_minus_cancel:
hoelzl@62648
   684
  fixes a b c :: ennreal
hoelzl@62648
   685
  shows "a - b \<le> a - c \<Longrightarrow> a < top \<Longrightarrow> b \<le> a \<Longrightarrow> c \<le> a \<Longrightarrow> c \<le> b"
hoelzl@62648
   686
  by transfer
hoelzl@62648
   687
     (auto simp add: max.absorb2 ereal_diff_positive top_ereal_def dest: ereal_mono_minus_cancel)
hoelzl@62648
   688
hoelzl@62648
   689
lemma ennreal_mono_minus:
hoelzl@62648
   690
  fixes a b c :: ennreal
hoelzl@62648
   691
  shows "c \<le> b \<Longrightarrow> a - b \<le> a - c"
hoelzl@62648
   692
  apply transfer
hoelzl@62648
   693
  apply (rule max.mono)
hoelzl@62648
   694
  apply simp
hoelzl@62648
   695
  subgoal for a b c
hoelzl@62648
   696
    by (cases a b c rule: ereal3_cases) auto
hoelzl@62648
   697
  done
hoelzl@62648
   698
hoelzl@62648
   699
lemma ennreal_minus_pos_iff:
hoelzl@62648
   700
  fixes a b :: ennreal
hoelzl@62648
   701
  shows "a < top \<or> b < top \<Longrightarrow> 0 < a - b \<Longrightarrow> b < a"
hoelzl@62648
   702
  apply transfer
hoelzl@62648
   703
  subgoal for a b
hoelzl@62648
   704
    by (cases a b rule: ereal2_cases) (auto simp: less_max_iff_disj)
hoelzl@62648
   705
  done
hoelzl@62648
   706
hoelzl@62648
   707
lemma ennreal_inverse_top[simp]: "inverse top = (0::ennreal)"
hoelzl@62648
   708
  by transfer (simp add: top_ereal_def ereal_inverse_eq_0)
hoelzl@62648
   709
hoelzl@62648
   710
lemma ennreal_inverse_zero[simp]: "inverse 0 = (top::ennreal)"
hoelzl@62648
   711
  by transfer (simp add: top_ereal_def ereal_inverse_eq_0)
hoelzl@62648
   712
hoelzl@62648
   713
lemma ennreal_top_divide: "top / (x::ennreal) = (if x = top then 0 else top)"
hoelzl@62648
   714
  unfolding divide_ennreal_def
hoelzl@62648
   715
  by transfer (simp add: top_ereal_def ereal_inverse_eq_0 ereal_0_gt_inverse)
hoelzl@62648
   716
hoelzl@62648
   717
lemma ennreal_zero_divide[simp]: "0 / (x::ennreal) = 0"
hoelzl@62648
   718
  by (simp add: divide_ennreal_def)
hoelzl@62648
   719
hoelzl@62648
   720
lemma ennreal_divide_zero[simp]: "x / (0::ennreal) = (if x = 0 then 0 else top)"
hoelzl@62648
   721
  by (simp add: divide_ennreal_def ennreal_mult_top)
hoelzl@62648
   722
hoelzl@62648
   723
lemma ennreal_divide_top[simp]: "x / (top::ennreal) = 0"
hoelzl@62648
   724
  by (simp add: divide_ennreal_def ennreal_top_mult)
hoelzl@62648
   725
hoelzl@62648
   726
lemma ennreal_times_divide: "a * (b / c) = a * b / (c::ennreal)"
hoelzl@62648
   727
  unfolding divide_ennreal_def
hoelzl@62648
   728
  by transfer (simp add: divide_ereal_def[symmetric] ereal_times_divide_eq)
hoelzl@62648
   729
hoelzl@62648
   730
lemma ennreal_zero_less_divide: "0 < a / b \<longleftrightarrow> (0 < a \<and> b < (top::ennreal))"
hoelzl@62648
   731
  unfolding divide_ennreal_def
hoelzl@62648
   732
  by transfer (auto simp: ereal_zero_less_0_iff top_ereal_def ereal_0_gt_inverse)
hoelzl@62648
   733
hoelzl@62975
   734
lemma divide_right_mono_ennreal:
hoelzl@62975
   735
  fixes a b c :: ennreal
hoelzl@62975
   736
  shows "a \<le> b \<Longrightarrow> a / c \<le> b / c"
hoelzl@62975
   737
  unfolding divide_ennreal_def by (intro mult_mono) auto
hoelzl@62975
   738
hoelzl@62975
   739
lemma ennreal_mult_strict_right_mono: "(a::ennreal) < c \<Longrightarrow> 0 < b \<Longrightarrow> b < top \<Longrightarrow> a * b < c * b"
hoelzl@62975
   740
  by transfer (auto intro!: ereal_mult_strict_right_mono)
hoelzl@62975
   741
hoelzl@62975
   742
lemma ennreal_indicator_less[simp]:
hoelzl@62975
   743
  "indicator A x \<le> (indicator B x::ennreal) \<longleftrightarrow> (x \<in> A \<longrightarrow> x \<in> B)"
hoelzl@62975
   744
  by (simp add: indicator_def not_le)
hoelzl@62975
   745
hoelzl@62975
   746
lemma ennreal_inverse_positive: "0 < inverse x \<longleftrightarrow> (x::ennreal) \<noteq> top"
hoelzl@62975
   747
  by transfer (simp add: ereal_0_gt_inverse top_ereal_def)
hoelzl@62975
   748
hoelzl@62975
   749
lemma ennreal_inverse_mult': "((0 < b \<or> a < top) \<and> (0 < a \<or> b < top)) \<Longrightarrow> inverse (a * b::ennreal) = inverse a * inverse b"
hoelzl@62975
   750
  apply transfer
hoelzl@62975
   751
  subgoal for a b
hoelzl@62975
   752
    by (cases a b rule: ereal2_cases) (auto simp: top_ereal_def)
hoelzl@62975
   753
  done
hoelzl@62975
   754
hoelzl@62975
   755
lemma ennreal_inverse_mult: "a < top \<Longrightarrow> b < top \<Longrightarrow> inverse (a * b::ennreal) = inverse a * inverse b"
hoelzl@62975
   756
  apply transfer
hoelzl@62975
   757
  subgoal for a b
hoelzl@62975
   758
    by (cases a b rule: ereal2_cases) (auto simp: top_ereal_def)
hoelzl@62975
   759
  done
hoelzl@62975
   760
hoelzl@62975
   761
lemma ennreal_inverse_1[simp]: "inverse (1::ennreal) = 1"
hoelzl@62975
   762
  by transfer simp
hoelzl@62975
   763
hoelzl@62975
   764
lemma ennreal_inverse_eq_0_iff[simp]: "inverse (a::ennreal) = 0 \<longleftrightarrow> a = top"
hoelzl@62975
   765
  by transfer (simp add: ereal_inverse_eq_0 top_ereal_def)
hoelzl@62975
   766
hoelzl@62975
   767
lemma ennreal_inverse_eq_top_iff[simp]: "inverse (a::ennreal) = top \<longleftrightarrow> a = 0"
hoelzl@62975
   768
  by transfer (simp add: top_ereal_def)
hoelzl@62975
   769
hoelzl@62975
   770
lemma ennreal_divide_eq_0_iff[simp]: "(a::ennreal) / b = 0 \<longleftrightarrow> (a = 0 \<or> b = top)"
hoelzl@62975
   771
  by (simp add: divide_ennreal_def)
hoelzl@62975
   772
hoelzl@62975
   773
lemma ennreal_divide_eq_top_iff: "(a::ennreal) / b = top \<longleftrightarrow> ((a \<noteq> 0 \<and> b = 0) \<or> (a = top \<and> b \<noteq> top))"
hoelzl@62975
   774
  by (auto simp add: divide_ennreal_def ennreal_mult_eq_top_iff)
hoelzl@62975
   775
hoelzl@62975
   776
lemma one_divide_one_divide_ennreal[simp]: "1 / (1 / c) = (c::ennreal)"
hoelzl@62975
   777
  including ennreal.lifting
hoelzl@62975
   778
  unfolding divide_ennreal_def
hoelzl@62975
   779
  by transfer auto
hoelzl@62975
   780
hoelzl@62975
   781
lemma ennreal_mult_left_cong:
hoelzl@62975
   782
  "((a::ennreal) \<noteq> 0 \<Longrightarrow> b = c) \<Longrightarrow> a * b = a * c"
hoelzl@62975
   783
  by (cases "a = 0") simp_all
hoelzl@62975
   784
hoelzl@62975
   785
lemma ennreal_mult_right_cong:
hoelzl@62975
   786
  "((a::ennreal) \<noteq> 0 \<Longrightarrow> b = c) \<Longrightarrow> b * a = c * a"
hoelzl@62975
   787
  by (cases "a = 0") simp_all
hoelzl@62975
   788
hoelzl@62975
   789
lemma ennreal_zero_less_mult_iff: "0 < a * b \<longleftrightarrow> 0 < a \<and> 0 < (b::ennreal)"
hoelzl@62975
   790
  by transfer (auto simp add: ereal_zero_less_0_iff le_less)
hoelzl@62975
   791
hoelzl@62975
   792
lemma less_diff_eq_ennreal:
hoelzl@62975
   793
  fixes a b c :: ennreal
hoelzl@62975
   794
  shows "b < top \<or> c < top \<Longrightarrow> a < b - c \<longleftrightarrow> a + c < b"
hoelzl@62975
   795
  apply transfer
hoelzl@62975
   796
  subgoal for a b c
hoelzl@62975
   797
    by (cases a b c rule: ereal3_cases) (auto split: split_max)
hoelzl@62975
   798
  done
hoelzl@62975
   799
hoelzl@62975
   800
lemma diff_add_cancel_ennreal:
hoelzl@62975
   801
  fixes a b :: ennreal shows "a \<le> b \<Longrightarrow> b - a + a = b"
hoelzl@62975
   802
  unfolding infinity_ennreal_def
hoelzl@62975
   803
  apply transfer
hoelzl@62975
   804
  subgoal for a b
hoelzl@62975
   805
    by (cases a b rule: ereal2_cases) (auto simp: max_absorb2)
hoelzl@62975
   806
  done
hoelzl@62975
   807
hoelzl@62975
   808
lemma ennreal_diff_self[simp]: "a \<noteq> top \<Longrightarrow> a - a = (0::ennreal)"
hoelzl@62975
   809
  by transfer (simp add: top_ereal_def)
hoelzl@62975
   810
hoelzl@62975
   811
lemma ennreal_minus_mono:
hoelzl@62975
   812
  fixes a b c :: ennreal
hoelzl@62975
   813
  shows "a \<le> c \<Longrightarrow> d \<le> b \<Longrightarrow> a - b \<le> c - d"
hoelzl@62975
   814
  apply transfer
hoelzl@62975
   815
  apply (rule max.mono)
hoelzl@62975
   816
  apply simp
hoelzl@62975
   817
  subgoal for a b c d
hoelzl@62975
   818
    by (cases a b c d rule: ereal3_cases[case_product ereal_cases]) auto
hoelzl@62975
   819
  done
hoelzl@62975
   820
hoelzl@62975
   821
lemma ennreal_minus_eq_top[simp]: "a - (b::ennreal) = top \<longleftrightarrow> a = top"
hoelzl@62975
   822
  by transfer (auto simp: top_ereal_def max.absorb2 ereal_minus_eq_PInfty_iff split: split_max)
hoelzl@62975
   823
hoelzl@62975
   824
lemma ennreal_divide_self[simp]: "a \<noteq> 0 \<Longrightarrow> a < top \<Longrightarrow> a / a = (1::ennreal)"
hoelzl@62975
   825
  unfolding divide_ennreal_def
hoelzl@62975
   826
  apply transfer
hoelzl@62975
   827
  subgoal for a
hoelzl@62975
   828
    by (cases a) (auto simp: top_ereal_def)
hoelzl@62975
   829
  done
hoelzl@62975
   830
wenzelm@69593
   831
subsection \<open>Coercion from \<^typ>\<open>real\<close> to \<^typ>\<open>ennreal\<close>\<close>
hoelzl@62975
   832
hoelzl@62975
   833
lift_definition ennreal :: "real \<Rightarrow> ennreal" is "sup 0 \<circ> ereal"
hoelzl@62975
   834
  by simp
hoelzl@62975
   835
hoelzl@62975
   836
declare [[coercion ennreal]]
hoelzl@62975
   837
eberlm@63099
   838
lemma ennreal_cong: "x = y \<Longrightarrow> ennreal x = ennreal y" by simp
eberlm@63099
   839
hoelzl@62975
   840
lemma ennreal_cases[cases type: ennreal]:
hoelzl@62975
   841
  fixes x :: ennreal
hoelzl@62975
   842
  obtains (real) r :: real where "0 \<le> r" "x = ennreal r" | (top) "x = top"
hoelzl@62975
   843
  apply transfer
hoelzl@62975
   844
  subgoal for x thesis
hoelzl@62975
   845
    by (cases x) (auto simp: max.absorb2 top_ereal_def)
hoelzl@62975
   846
  done
hoelzl@62975
   847
hoelzl@62975
   848
lemmas ennreal2_cases = ennreal_cases[case_product ennreal_cases]
hoelzl@62975
   849
lemmas ennreal3_cases = ennreal_cases[case_product ennreal2_cases]
hoelzl@62975
   850
hoelzl@62975
   851
lemma ennreal_neq_top[simp]: "ennreal r \<noteq> top"
nipkow@68406
   852
  by transfer (simp add: top_ereal_def zero_ereal_def flip: ereal_max)
hoelzl@62975
   853
hoelzl@62975
   854
lemma top_neq_ennreal[simp]: "top \<noteq> ennreal r"
hoelzl@62975
   855
  using ennreal_neq_top[of r] by (auto simp del: ennreal_neq_top)
hoelzl@62975
   856
hoelzl@62975
   857
lemma ennreal_less_top[simp]: "ennreal x < top"
hoelzl@62975
   858
  by transfer (simp add: top_ereal_def max_def)
hoelzl@62975
   859
hoelzl@62975
   860
lemma ennreal_neg: "x \<le> 0 \<Longrightarrow> ennreal x = 0"
hoelzl@62975
   861
  by transfer (simp add: max.absorb1)
hoelzl@62975
   862
hoelzl@62975
   863
lemma ennreal_inj[simp]:
hoelzl@62975
   864
  "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> ennreal a = ennreal b \<longleftrightarrow> a = b"
hoelzl@62975
   865
  by (transfer fixing: a b) (auto simp: max_absorb2)
hoelzl@62975
   866
hoelzl@62975
   867
lemma ennreal_le_iff[simp]: "0 \<le> y \<Longrightarrow> ennreal x \<le> ennreal y \<longleftrightarrow> x \<le> y"
hoelzl@62975
   868
  by (auto simp: ennreal_def zero_ereal_def less_eq_ennreal.abs_eq eq_onp_def split: split_max)
hoelzl@62975
   869
hoelzl@62975
   870
lemma le_ennreal_iff: "0 \<le> r \<Longrightarrow> x \<le> ennreal r \<longleftrightarrow> (\<exists>q\<ge>0. x = ennreal q \<and> q \<le> r)"
hoelzl@62975
   871
  by (cases x) (auto simp: top_unique)
hoelzl@62975
   872
hoelzl@62975
   873
lemma ennreal_less_iff: "0 \<le> r \<Longrightarrow> ennreal r < ennreal q \<longleftrightarrow> r < q"
hoelzl@62975
   874
  unfolding not_le[symmetric] by auto
hoelzl@62975
   875
hoelzl@62975
   876
lemma ennreal_eq_zero_iff[simp]: "0 \<le> x \<Longrightarrow> ennreal x = 0 \<longleftrightarrow> x = 0"
hoelzl@62975
   877
  by transfer (auto simp: max_absorb2)
hoelzl@62975
   878
hoelzl@62975
   879
lemma ennreal_less_zero_iff[simp]: "0 < ennreal x \<longleftrightarrow> 0 < x"
hoelzl@62975
   880
  by transfer (auto simp: max_def)
hoelzl@62975
   881
hoelzl@62975
   882
lemma ennreal_lessI: "0 < q \<Longrightarrow> r < q \<Longrightarrow> ennreal r < ennreal q"
hoelzl@62975
   883
  by (cases "0 \<le> r") (auto simp: ennreal_less_iff ennreal_neg)
hoelzl@62975
   884
hoelzl@62975
   885
lemma ennreal_leI: "x \<le> y \<Longrightarrow> ennreal x \<le> ennreal y"
hoelzl@62975
   886
  by (cases "0 \<le> y") (auto simp: ennreal_neg)
hoelzl@62975
   887
hoelzl@62975
   888
lemma enn2ereal_ennreal[simp]: "0 \<le> x \<Longrightarrow> enn2ereal (ennreal x) = x"
hoelzl@62975
   889
  by transfer (simp add: max_absorb2)
hoelzl@62975
   890
hoelzl@62975
   891
lemma e2ennreal_enn2ereal[simp]: "e2ennreal (enn2ereal x) = x"
hoelzl@62975
   892
  by (simp add: e2ennreal_def max_absorb2 ennreal.enn2ereal_inverse)
hoelzl@62975
   893
nipkow@67456
   894
lemma enn2ereal_e2ennreal: "x \<ge> 0 \<Longrightarrow> enn2ereal (e2ennreal x) = x"
nipkow@67456
   895
by (metis e2ennreal_enn2ereal ereal_ennreal_cases not_le)
nipkow@67456
   896
nipkow@67451
   897
lemma e2ennreal_ereal [simp]: "e2ennreal (ereal x) = ennreal x"
nipkow@67451
   898
by (metis e2ennreal_def enn2ereal_inverse ennreal.rep_eq sup_ereal_def)
nipkow@67451
   899
hoelzl@62975
   900
lemma ennreal_0[simp]: "ennreal 0 = 0"
hoelzl@62975
   901
  by (simp add: ennreal_def max.absorb1 zero_ennreal.abs_eq)
hoelzl@62975
   902
hoelzl@62975
   903
lemma ennreal_1[simp]: "ennreal 1 = 1"
hoelzl@62975
   904
  by transfer (simp add: max_absorb2)
hoelzl@62975
   905
hoelzl@62975
   906
lemma ennreal_eq_0_iff: "ennreal x = 0 \<longleftrightarrow> x \<le> 0"
hoelzl@62975
   907
  by (cases "0 \<le> x") (auto simp: ennreal_neg)
hoelzl@62975
   908
hoelzl@62975
   909
lemma ennreal_le_iff2: "ennreal x \<le> ennreal y \<longleftrightarrow> ((0 \<le> y \<and> x \<le> y) \<or> (x \<le> 0 \<and> y \<le> 0))"
hoelzl@62975
   910
  by (cases "0 \<le> y") (auto simp: ennreal_eq_0_iff ennreal_neg)
hoelzl@62975
   911
hoelzl@62975
   912
lemma ennreal_eq_1[simp]: "ennreal x = 1 \<longleftrightarrow> x = 1"
nipkow@68406
   913
  by (cases "0 \<le> x") (auto simp: ennreal_neg simp flip: ennreal_1)
hoelzl@62975
   914
hoelzl@62975
   915
lemma ennreal_le_1[simp]: "ennreal x \<le> 1 \<longleftrightarrow> x \<le> 1"
nipkow@68406
   916
  by (cases "0 \<le> x") (auto simp: ennreal_neg simp flip: ennreal_1)
hoelzl@62975
   917
hoelzl@62975
   918
lemma ennreal_ge_1[simp]: "ennreal x \<ge> 1 \<longleftrightarrow> x \<ge> 1"
nipkow@68406
   919
  by (cases "0 \<le> x") (auto simp: ennreal_neg simp flip: ennreal_1)
hoelzl@62975
   920
hoelzl@64284
   921
lemma one_less_ennreal[simp]: "1 < ennreal x \<longleftrightarrow> 1 < x"
hoelzl@64284
   922
  by transfer (auto simp: max.absorb2 less_max_iff_disj)
hoelzl@64284
   923
hoelzl@62975
   924
lemma ennreal_plus[simp]:
hoelzl@62975
   925
  "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> ennreal (a + b) = ennreal a + ennreal b"
hoelzl@62975
   926
  by (transfer fixing: a b) (auto simp: max_absorb2)
hoelzl@62975
   927
nipkow@64267
   928
lemma sum_ennreal[simp]: "(\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i) \<Longrightarrow> (\<Sum>i\<in>I. ennreal (f i)) = ennreal (sum f I)"
nipkow@64267
   929
  by (induction I rule: infinite_finite_induct) (auto simp: sum_nonneg)
hoelzl@62975
   930
nipkow@63882
   931
lemma sum_list_ennreal[simp]:
hoelzl@63225
   932
  assumes "\<And>x. x \<in> set xs \<Longrightarrow> f x \<ge> 0"
nipkow@63882
   933
  shows   "sum_list (map (\<lambda>x. ennreal (f x)) xs) = ennreal (sum_list (map f xs))"
eberlm@63099
   934
using assms
eberlm@63099
   935
proof (induction xs)
eberlm@63099
   936
  case (Cons x xs)
nipkow@63882
   937
  from Cons have "(\<Sum>x\<leftarrow>x # xs. ennreal (f x)) = ennreal (f x) + ennreal (sum_list (map f xs))"
eberlm@63099
   938
    by simp
nipkow@63882
   939
  also from Cons.prems have "\<dots> = ennreal (f x + sum_list (map f xs))"
nipkow@63882
   940
    by (intro ennreal_plus [symmetric] sum_list_nonneg) auto
eberlm@63099
   941
  finally show ?case by simp
eberlm@63099
   942
qed simp_all
eberlm@63099
   943
hoelzl@62975
   944
lemma ennreal_of_nat_eq_real_of_nat: "of_nat i = ennreal (of_nat i)"
hoelzl@62975
   945
  by (induction i) simp_all
hoelzl@62975
   946
hoelzl@62975
   947
lemma of_nat_le_ennreal_iff[simp]: "0 \<le> r \<Longrightarrow> of_nat i \<le> ennreal r \<longleftrightarrow> of_nat i \<le> r"
hoelzl@62975
   948
  by (simp add: ennreal_of_nat_eq_real_of_nat)
hoelzl@62975
   949
hoelzl@62975
   950
lemma ennreal_le_of_nat_iff[simp]: "ennreal r \<le> of_nat i \<longleftrightarrow> r \<le> of_nat i"
hoelzl@62975
   951
  by (simp add: ennreal_of_nat_eq_real_of_nat)
hoelzl@62975
   952
hoelzl@62975
   953
lemma ennreal_indicator: "ennreal (indicator A x) = indicator A x"
hoelzl@62975
   954
  by (auto split: split_indicator)
hoelzl@62975
   955
hoelzl@62975
   956
lemma ennreal_numeral[simp]: "ennreal (numeral n) = numeral n"
hoelzl@62975
   957
  using ennreal_of_nat_eq_real_of_nat[of "numeral n"] by simp
hoelzl@62975
   958
hoelzl@62975
   959
lemma min_ennreal: "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> min (ennreal x) (ennreal y) = ennreal (min x y)"
hoelzl@62975
   960
  by (auto split: split_min)
hoelzl@62975
   961
hoelzl@62975
   962
lemma ennreal_half[simp]: "ennreal (1/2) = inverse 2"
hoelzl@62975
   963
  by transfer (simp add: max.absorb2)
hoelzl@62975
   964
hoelzl@62975
   965
lemma ennreal_minus: "0 \<le> q \<Longrightarrow> ennreal r - ennreal q = ennreal (r - q)"
hoelzl@62975
   966
  by transfer
nipkow@68406
   967
     (simp add: max.absorb2 zero_ereal_def flip: ereal_max)
hoelzl@62975
   968
hoelzl@62975
   969
lemma ennreal_minus_top[simp]: "ennreal a - top = 0"
hoelzl@62975
   970
  by (simp add: minus_top_ennreal)
hoelzl@62975
   971
nipkow@67451
   972
lemma e2eenreal_enn2ereal_diff [simp]:
nipkow@67451
   973
  "e2ennreal(enn2ereal x - enn2ereal y) = x - y" for x y
nipkow@67451
   974
by (cases x, cases y, auto simp add: ennreal_minus e2ennreal_neg)
nipkow@67451
   975
hoelzl@62975
   976
lemma ennreal_mult: "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> ennreal (a * b) = ennreal a * ennreal b"
hoelzl@62975
   977
  by transfer (simp add: max_absorb2)
hoelzl@62975
   978
hoelzl@62975
   979
lemma ennreal_mult': "0 \<le> a \<Longrightarrow> ennreal (a * b) = ennreal a * ennreal b"
hoelzl@62975
   980
  by (cases "0 \<le> b") (auto simp: ennreal_mult ennreal_neg mult_nonneg_nonpos)
hoelzl@62975
   981
hoelzl@62975
   982
lemma indicator_mult_ennreal: "indicator A x * ennreal r = ennreal (indicator A x * r)"
hoelzl@62975
   983
  by (simp split: split_indicator)
hoelzl@62975
   984
hoelzl@62975
   985
lemma ennreal_mult'': "0 \<le> b \<Longrightarrow> ennreal (a * b) = ennreal a * ennreal b"
hoelzl@62975
   986
  by (cases "0 \<le> a") (auto simp: ennreal_mult ennreal_neg mult_nonpos_nonneg)
hoelzl@62975
   987
hoelzl@62975
   988
lemma numeral_mult_ennreal: "0 \<le> x \<Longrightarrow> numeral b * ennreal x = ennreal (numeral b * x)"
hoelzl@62975
   989
  by (simp add: ennreal_mult)
hoelzl@62975
   990
hoelzl@62975
   991
lemma ennreal_power: "0 \<le> r \<Longrightarrow> ennreal r ^ n = ennreal (r ^ n)"
hoelzl@62975
   992
  by (induction n) (auto simp: ennreal_mult)
hoelzl@62975
   993
hoelzl@62975
   994
lemma power_eq_top_ennreal: "x ^ n = top \<longleftrightarrow> (n \<noteq> 0 \<and> (x::ennreal) = top)"
hoelzl@62975
   995
  by (cases x rule: ennreal_cases)
hoelzl@62975
   996
     (auto simp: ennreal_power top_power_ennreal)
hoelzl@62975
   997
hoelzl@62975
   998
lemma inverse_ennreal: "0 < r \<Longrightarrow> inverse (ennreal r) = ennreal (inverse r)"
hoelzl@62975
   999
  by transfer (simp add: max.absorb2)
hoelzl@62975
  1000
hoelzl@62975
  1001
lemma divide_ennreal: "0 \<le> r \<Longrightarrow> 0 < q \<Longrightarrow> ennreal r / ennreal q = ennreal (r / q)"
hoelzl@62975
  1002
  by (simp add: divide_ennreal_def inverse_ennreal ennreal_mult[symmetric] inverse_eq_divide)
hoelzl@62975
  1003
hoelzl@62975
  1004
lemma ennreal_inverse_power: "inverse (x ^ n :: ennreal) = inverse x ^ n"
hoelzl@62975
  1005
proof (cases x rule: ennreal_cases)
hoelzl@62975
  1006
  case top with power_eq_top_ennreal[of x n] show ?thesis
hoelzl@62975
  1007
    by (cases "n = 0") auto
hoelzl@62975
  1008
next
hoelzl@62975
  1009
  case (real r) then show ?thesis
hoelzl@62975
  1010
  proof cases
hoelzl@62975
  1011
    assume "x = 0" then show ?thesis
hoelzl@62975
  1012
      using power_eq_top_ennreal[of top "n - 1"]
hoelzl@62975
  1013
      by (cases n) (auto simp: ennreal_top_mult)
hoelzl@62975
  1014
  next
hoelzl@62975
  1015
    assume "x \<noteq> 0"
hoelzl@62975
  1016
    with real have "0 < r" by auto
hoelzl@62975
  1017
    with real show ?thesis
hoelzl@62975
  1018
      by (induction n)
hoelzl@62975
  1019
         (auto simp add: ennreal_power ennreal_mult[symmetric] inverse_ennreal)
hoelzl@62975
  1020
  qed
hoelzl@62975
  1021
qed
hoelzl@62975
  1022
hoelzl@62975
  1023
lemma ennreal_divide_numeral: "0 \<le> x \<Longrightarrow> ennreal x / numeral b = ennreal (x / numeral b)"
hoelzl@62975
  1024
  by (subst divide_ennreal[symmetric]) auto
hoelzl@62975
  1025
nipkow@64272
  1026
lemma prod_ennreal: "(\<And>i. i \<in> A \<Longrightarrow> 0 \<le> f i) \<Longrightarrow> (\<Prod>i\<in>A. ennreal (f i)) = ennreal (prod f A)"
hoelzl@62975
  1027
  by (induction A rule: infinite_finite_induct)
nipkow@64272
  1028
     (auto simp: ennreal_mult prod_nonneg)
hoelzl@62975
  1029
hoelzl@62975
  1030
lemma mult_right_ennreal_cancel: "a * ennreal c = b * ennreal c \<longleftrightarrow> (a = b \<or> c \<le> 0)"
hoelzl@62975
  1031
  apply (cases "0 \<le> c")
hoelzl@62975
  1032
  apply (cases a b rule: ennreal2_cases)
hoelzl@62975
  1033
  apply (auto simp: ennreal_mult[symmetric] ennreal_neg ennreal_top_mult)
hoelzl@62975
  1034
  done
hoelzl@62975
  1035
hoelzl@62975
  1036
lemma ennreal_le_epsilon:
hoelzl@62975
  1037
  "(\<And>e::real. y < top \<Longrightarrow> 0 < e \<Longrightarrow> x \<le> y + ennreal e) \<Longrightarrow> x \<le> y"
hoelzl@62975
  1038
  apply (cases y rule: ennreal_cases)
hoelzl@62975
  1039
  apply (cases x rule: ennreal_cases)
nipkow@68406
  1040
  apply (auto simp flip: ennreal_plus simp add: top_unique intro: zero_less_one field_le_epsilon)
hoelzl@62975
  1041
  done
hoelzl@62975
  1042
hoelzl@62975
  1043
lemma ennreal_rat_dense:
hoelzl@62975
  1044
  fixes x y :: ennreal
hoelzl@62975
  1045
  shows "x < y \<Longrightarrow> \<exists>r::rat. x < real_of_rat r \<and> real_of_rat r < y"
hoelzl@62975
  1046
proof transfer
hoelzl@62975
  1047
  fix x y :: ereal assume xy: "0 \<le> x" "0 \<le> y" "x < y"
hoelzl@62975
  1048
  moreover
hoelzl@62975
  1049
  from ereal_dense3[OF \<open>x < y\<close>]
wenzelm@63540
  1050
  obtain r where r: "x < ereal (real_of_rat r)" "ereal (real_of_rat r) < y"
hoelzl@62975
  1051
    by auto
wenzelm@63540
  1052
  then have "0 \<le> r"
hoelzl@62975
  1053
    using le_less_trans[OF \<open>0 \<le> x\<close> \<open>x < ereal (real_of_rat r)\<close>] by auto
wenzelm@63540
  1054
  with r show "\<exists>r. x < (sup 0 \<circ> ereal) (real_of_rat r) \<and> (sup 0 \<circ> ereal) (real_of_rat r) < y"
hoelzl@62975
  1055
    by (intro exI[of _ r]) (auto simp: max_absorb2)
hoelzl@62975
  1056
qed
hoelzl@62975
  1057
hoelzl@62975
  1058
lemma ennreal_Ex_less_of_nat: "(x::ennreal) < top \<Longrightarrow> \<exists>n. x < of_nat n"
hoelzl@62975
  1059
  by (cases x rule: ennreal_cases)
hoelzl@62975
  1060
     (auto simp: ennreal_of_nat_eq_real_of_nat ennreal_less_iff reals_Archimedean2)
hoelzl@62975
  1061
wenzelm@69593
  1062
subsection \<open>Coercion from \<^typ>\<open>ennreal\<close> to \<^typ>\<open>real\<close>\<close>
hoelzl@62975
  1063
hoelzl@62975
  1064
definition "enn2real x = real_of_ereal (enn2ereal x)"
hoelzl@62975
  1065
hoelzl@62975
  1066
lemma enn2real_nonneg[simp]: "0 \<le> enn2real x"
hoelzl@62975
  1067
  by (auto simp: enn2real_def intro!: real_of_ereal_pos enn2ereal_nonneg)
hoelzl@62975
  1068
hoelzl@62975
  1069
lemma enn2real_mono: "a \<le> b \<Longrightarrow> b < top \<Longrightarrow> enn2real a \<le> enn2real b"
hoelzl@62975
  1070
  by (auto simp add: enn2real_def less_eq_ennreal.rep_eq intro!: real_of_ereal_positive_mono enn2ereal_nonneg)
hoelzl@62975
  1071
hoelzl@62975
  1072
lemma enn2real_of_nat[simp]: "enn2real (of_nat n) = n"
hoelzl@62975
  1073
  by (auto simp: enn2real_def)
hoelzl@62975
  1074
hoelzl@62975
  1075
lemma enn2real_ennreal[simp]: "0 \<le> r \<Longrightarrow> enn2real (ennreal r) = r"
hoelzl@62975
  1076
  by (simp add: enn2real_def)
hoelzl@62975
  1077
hoelzl@62975
  1078
lemma ennreal_enn2real[simp]: "r < top \<Longrightarrow> ennreal (enn2real r) = r"
hoelzl@62975
  1079
  by (cases r rule: ennreal_cases) auto
hoelzl@62975
  1080
hoelzl@62975
  1081
lemma real_of_ereal_enn2ereal[simp]: "real_of_ereal (enn2ereal x) = enn2real x"
hoelzl@62975
  1082
  by (simp add: enn2real_def)
hoelzl@62975
  1083
hoelzl@62975
  1084
lemma enn2real_top[simp]: "enn2real top = 0"
hoelzl@62975
  1085
  unfolding enn2real_def top_ennreal.rep_eq top_ereal_def by simp
hoelzl@62975
  1086
hoelzl@62975
  1087
lemma enn2real_0[simp]: "enn2real 0 = 0"
hoelzl@62975
  1088
  unfolding enn2real_def zero_ennreal.rep_eq by simp
hoelzl@62975
  1089
hoelzl@62975
  1090
lemma enn2real_1[simp]: "enn2real 1 = 1"
hoelzl@62975
  1091
  unfolding enn2real_def one_ennreal.rep_eq by simp
hoelzl@62975
  1092
hoelzl@62975
  1093
lemma enn2real_numeral[simp]: "enn2real (numeral n) = (numeral n)"
hoelzl@62975
  1094
  unfolding enn2real_def by simp
hoelzl@62975
  1095
hoelzl@62975
  1096
lemma enn2real_mult: "enn2real (a * b) = enn2real a * enn2real b"
hoelzl@62975
  1097
  unfolding enn2real_def
hoelzl@62975
  1098
  by (simp del: real_of_ereal_enn2ereal add: times_ennreal.rep_eq)
hoelzl@62975
  1099
hoelzl@62975
  1100
lemma enn2real_leI: "0 \<le> B \<Longrightarrow> x \<le> ennreal B \<Longrightarrow> enn2real x \<le> B"
hoelzl@62975
  1101
  by (cases x rule: ennreal_cases) (auto simp: top_unique)
hoelzl@62975
  1102
hoelzl@62975
  1103
lemma enn2real_positive_iff: "0 < enn2real x \<longleftrightarrow> (0 < x \<and> x < top)"
hoelzl@62975
  1104
  by (cases x rule: ennreal_cases) auto
hoelzl@62975
  1105
hoelzl@64320
  1106
lemma enn2real_eq_1_iff[simp]: "enn2real x = 1 \<longleftrightarrow> x = 1"
hoelzl@64320
  1107
  by (cases x) auto
hoelzl@64320
  1108
wenzelm@69593
  1109
subsection \<open>Coercion from \<^typ>\<open>enat\<close> to \<^typ>\<open>ennreal\<close>\<close>
hoelzl@62975
  1110
hoelzl@62975
  1111
hoelzl@62975
  1112
definition ennreal_of_enat :: "enat \<Rightarrow> ennreal"
hoelzl@62975
  1113
where
hoelzl@62975
  1114
  "ennreal_of_enat n = (case n of \<infinity> \<Rightarrow> top | enat n \<Rightarrow> of_nat n)"
hoelzl@62975
  1115
hoelzl@62975
  1116
declare [[coercion ennreal_of_enat]]
hoelzl@62975
  1117
declare [[coercion "of_nat :: nat \<Rightarrow> ennreal"]]
hoelzl@62975
  1118
hoelzl@62975
  1119
lemma ennreal_of_enat_infty[simp]: "ennreal_of_enat \<infinity> = \<infinity>"
hoelzl@62975
  1120
  by (simp add: ennreal_of_enat_def)
hoelzl@62975
  1121
hoelzl@62975
  1122
lemma ennreal_of_enat_enat[simp]: "ennreal_of_enat (enat n) = of_nat n"
hoelzl@62975
  1123
  by (simp add: ennreal_of_enat_def)
hoelzl@62975
  1124
hoelzl@62975
  1125
lemma ennreal_of_enat_0[simp]: "ennreal_of_enat 0 = 0"
hoelzl@62975
  1126
  using ennreal_of_enat_enat[of 0] unfolding enat_0 by simp
hoelzl@62975
  1127
hoelzl@62975
  1128
lemma ennreal_of_enat_1[simp]: "ennreal_of_enat 1 = 1"
hoelzl@62975
  1129
  using ennreal_of_enat_enat[of 1] unfolding enat_1 by simp
hoelzl@62975
  1130
hoelzl@62975
  1131
lemma ennreal_top_neq_of_nat[simp]: "(top::ennreal) \<noteq> of_nat i"
hoelzl@62975
  1132
  using ennreal_of_nat_neq_top[of i] by metis
hoelzl@62975
  1133
hoelzl@62975
  1134
lemma ennreal_of_enat_inj[simp]: "ennreal_of_enat i = ennreal_of_enat j \<longleftrightarrow> i = j"
hoelzl@62975
  1135
  by (cases i j rule: enat.exhaust[case_product enat.exhaust]) auto
hoelzl@62975
  1136
hoelzl@62975
  1137
lemma ennreal_of_enat_le_iff[simp]: "ennreal_of_enat m \<le> ennreal_of_enat n \<longleftrightarrow> m \<le> n"
hoelzl@62975
  1138
  by (auto simp: ennreal_of_enat_def top_unique split: enat.split)
hoelzl@62975
  1139
hoelzl@62975
  1140
lemma of_nat_less_ennreal_of_nat[simp]: "of_nat n \<le> ennreal_of_enat x \<longleftrightarrow> of_nat n \<le> x"
hoelzl@62975
  1141
  by (cases x) (auto simp: of_nat_eq_enat)
hoelzl@62975
  1142
haftmann@69260
  1143
lemma ennreal_of_enat_Sup: "ennreal_of_enat (Sup X) = (SUP x\<in>X. ennreal_of_enat x)"
hoelzl@62975
  1144
proof -
haftmann@69260
  1145
  have "ennreal_of_enat (Sup X) \<le> (SUP x \<in> X. ennreal_of_enat x)"
hoelzl@62975
  1146
    unfolding Sup_enat_def
hoelzl@62975
  1147
  proof (clarsimp, intro conjI impI)
hoelzl@62975
  1148
    fix x assume "finite X" "X \<noteq> {}"
haftmann@69260
  1149
    then show "ennreal_of_enat (Max X) \<le> (SUP x \<in> X. ennreal_of_enat x)"
hoelzl@62975
  1150
      by (intro SUP_upper Max_in)
hoelzl@62975
  1151
  next
hoelzl@62975
  1152
    assume "infinite X" "X \<noteq> {}"
hoelzl@62975
  1153
    have "\<exists>y\<in>X. r < ennreal_of_enat y" if r: "r < top" for r
hoelzl@62975
  1154
    proof -
hoelzl@62975
  1155
      from ennreal_Ex_less_of_nat[OF r] guess n .. note n = this
hoelzl@62975
  1156
      have "\<not> (X \<subseteq> enat ` {.. n})"
hoelzl@62975
  1157
        using \<open>infinite X\<close> by (auto dest: finite_subset)
wenzelm@63540
  1158
      then obtain x where x: "x \<in> X" "x \<notin> enat ` {..n}"
hoelzl@62975
  1159
        by blast
wenzelm@63540
  1160
      then have "of_nat n \<le> x"
hoelzl@62975
  1161
        by (cases x) (auto simp: of_nat_eq_enat)
wenzelm@63540
  1162
      with x show ?thesis
hoelzl@62975
  1163
        by (auto intro!: bexI[of _ x] less_le_trans[OF n])
hoelzl@62975
  1164
    qed
haftmann@69260
  1165
    then have "(SUP x \<in> X. ennreal_of_enat x) = top"
hoelzl@62975
  1166
      by simp
haftmann@69260
  1167
    then show "top \<le> (SUP x \<in> X. ennreal_of_enat x)"
hoelzl@62975
  1168
      unfolding top_unique by simp
hoelzl@62975
  1169
  qed
hoelzl@62975
  1170
  then show ?thesis
hoelzl@62975
  1171
    by (auto intro!: antisym Sup_least intro: Sup_upper)
hoelzl@62975
  1172
qed
hoelzl@62975
  1173
hoelzl@62975
  1174
lemma ennreal_of_enat_eSuc[simp]: "ennreal_of_enat (eSuc x) = 1 + ennreal_of_enat x"
hoelzl@62975
  1175
  by (cases x) (auto simp: eSuc_enat)
hoelzl@62975
  1176
wenzelm@69593
  1177
subsection \<open>Topology on \<^typ>\<open>ennreal\<close>\<close>
hoelzl@62975
  1178
hoelzl@62975
  1179
lemma enn2ereal_Iio: "enn2ereal -` {..<a} = (if 0 \<le> a then {..< e2ennreal a} else {})"
hoelzl@62975
  1180
  using enn2ereal_nonneg
hoelzl@62975
  1181
  by (cases a rule: ereal_ennreal_cases)
hoelzl@62975
  1182
     (auto simp add: vimage_def set_eq_iff ennreal.enn2ereal_inverse less_ennreal.rep_eq e2ennreal_def max_absorb2
hoelzl@62975
  1183
           simp del: enn2ereal_nonneg
hoelzl@62975
  1184
           intro: le_less_trans less_imp_le)
hoelzl@62975
  1185
hoelzl@62975
  1186
lemma enn2ereal_Ioi: "enn2ereal -` {a <..} = (if 0 \<le> a then {e2ennreal a <..} else UNIV)"
hoelzl@62975
  1187
  by (cases a rule: ereal_ennreal_cases)
hoelzl@62975
  1188
     (auto simp add: vimage_def set_eq_iff ennreal.enn2ereal_inverse less_ennreal.rep_eq e2ennreal_def max_absorb2
hoelzl@62975
  1189
           intro: less_le_trans)
hoelzl@62975
  1190
hoelzl@62975
  1191
instantiation ennreal :: linear_continuum_topology
hoelzl@62975
  1192
begin
hoelzl@62975
  1193
hoelzl@62975
  1194
definition open_ennreal :: "ennreal set \<Rightarrow> bool"
hoelzl@62975
  1195
  where "(open :: ennreal set \<Rightarrow> bool) = generate_topology (range lessThan \<union> range greaterThan)"
hoelzl@62975
  1196
hoelzl@62975
  1197
instance
hoelzl@62975
  1198
proof
hoelzl@62975
  1199
  show "\<exists>a b::ennreal. a \<noteq> b"
hoelzl@62975
  1200
    using zero_neq_one by (intro exI)
hoelzl@62975
  1201
  show "\<And>x y::ennreal. x < y \<Longrightarrow> \<exists>z>x. z < y"
hoelzl@62975
  1202
  proof transfer
wenzelm@63539
  1203
    fix x y :: ereal assume "0 \<le> x" and *: "x < y"
wenzelm@63539
  1204
    moreover from dense[OF *] guess z ..
nipkow@67399
  1205
    ultimately show "\<exists>z\<in>Collect ((\<le>) 0). x < z \<and> z < y"
hoelzl@62975
  1206
      by (intro bexI[of _ z]) auto
hoelzl@62975
  1207
  qed
hoelzl@62975
  1208
qed (rule open_ennreal_def)
hoelzl@62975
  1209
hoelzl@62375
  1210
end
hoelzl@62975
  1211
hoelzl@62975
  1212
lemma continuous_on_e2ennreal: "continuous_on A e2ennreal"
hoelzl@62975
  1213
proof (rule continuous_on_subset)
hoelzl@62975
  1214
  show "continuous_on ({0..} \<union> {..0}) e2ennreal"
hoelzl@62975
  1215
  proof (rule continuous_on_closed_Un)
hoelzl@62975
  1216
    show "continuous_on {0 ..} e2ennreal"
hoelzl@62975
  1217
      by (rule continuous_onI_mono)
hoelzl@62975
  1218
         (auto simp add: less_eq_ennreal.abs_eq eq_onp_def enn2ereal_range)
hoelzl@62975
  1219
    show "continuous_on {.. 0} e2ennreal"
hoelzl@62975
  1220
      by (subst continuous_on_cong[OF refl, of _ _ "\<lambda>_. 0"])
hoelzl@62975
  1221
         (auto simp add: e2ennreal_neg continuous_on_const)
hoelzl@62975
  1222
  qed auto
hoelzl@62975
  1223
  show "A \<subseteq> {0..} \<union> {..0::ereal}"
hoelzl@62975
  1224
    by auto
hoelzl@62975
  1225
qed
hoelzl@62975
  1226
hoelzl@62975
  1227
lemma continuous_at_e2ennreal: "continuous (at x within A) e2ennreal"
hoelzl@62975
  1228
  by (rule continuous_on_imp_continuous_within[OF continuous_on_e2ennreal, of _ UNIV]) auto
hoelzl@62975
  1229
hoelzl@62975
  1230
lemma continuous_on_enn2ereal: "continuous_on UNIV enn2ereal"
hoelzl@62975
  1231
  by (rule continuous_on_generate_topology[OF open_generated_order])
hoelzl@62975
  1232
     (auto simp add: enn2ereal_Iio enn2ereal_Ioi)
hoelzl@62975
  1233
hoelzl@62975
  1234
lemma continuous_at_enn2ereal: "continuous (at x within A) enn2ereal"
hoelzl@62975
  1235
  by (rule continuous_on_imp_continuous_within[OF continuous_on_enn2ereal]) auto
hoelzl@62975
  1236
hoelzl@62975
  1237
lemma sup_continuous_e2ennreal[order_continuous_intros]:
hoelzl@62975
  1238
  assumes f: "sup_continuous f" shows "sup_continuous (\<lambda>x. e2ennreal (f x))"
hoelzl@62975
  1239
  apply (rule sup_continuous_compose[OF _ f])
hoelzl@62975
  1240
  apply (rule continuous_at_left_imp_sup_continuous)
hoelzl@62975
  1241
  apply (auto simp: mono_def e2ennreal_mono continuous_at_e2ennreal)
hoelzl@62975
  1242
  done
hoelzl@62975
  1243
hoelzl@62975
  1244
lemma sup_continuous_enn2ereal[order_continuous_intros]:
hoelzl@62975
  1245
  assumes f: "sup_continuous f" shows "sup_continuous (\<lambda>x. enn2ereal (f x))"
hoelzl@62975
  1246
  apply (rule sup_continuous_compose[OF _ f])
hoelzl@62975
  1247
  apply (rule continuous_at_left_imp_sup_continuous)
hoelzl@62975
  1248
  apply (simp_all add: mono_def less_eq_ennreal.rep_eq continuous_at_enn2ereal)
hoelzl@62975
  1249
  done
hoelzl@62975
  1250
hoelzl@62975
  1251
lemma sup_continuous_mult_left_ennreal':
hoelzl@62975
  1252
  fixes c :: "ennreal"
hoelzl@62975
  1253
  shows "sup_continuous (\<lambda>x. c * x)"
hoelzl@62975
  1254
  unfolding sup_continuous_def
hoelzl@62975
  1255
  by transfer (auto simp: SUP_ereal_mult_left max.absorb2 SUP_upper2)
hoelzl@62975
  1256
hoelzl@62975
  1257
lemma sup_continuous_mult_left_ennreal[order_continuous_intros]:
hoelzl@62975
  1258
  "sup_continuous f \<Longrightarrow> sup_continuous (\<lambda>x. c * f x :: ennreal)"
hoelzl@62975
  1259
  by (rule sup_continuous_compose[OF sup_continuous_mult_left_ennreal'])
hoelzl@62975
  1260
hoelzl@62975
  1261
lemma sup_continuous_mult_right_ennreal[order_continuous_intros]:
hoelzl@62975
  1262
  "sup_continuous f \<Longrightarrow> sup_continuous (\<lambda>x. f x * c :: ennreal)"
hoelzl@62975
  1263
  using sup_continuous_mult_left_ennreal[of f c] by (simp add: mult.commute)
hoelzl@62975
  1264
hoelzl@62975
  1265
lemma sup_continuous_divide_ennreal[order_continuous_intros]:
hoelzl@62975
  1266
  fixes f g :: "'a::complete_lattice \<Rightarrow> ennreal"
hoelzl@62975
  1267
  shows "sup_continuous f \<Longrightarrow> sup_continuous (\<lambda>x. f x / c)"
hoelzl@62975
  1268
  unfolding divide_ennreal_def by (rule sup_continuous_mult_right_ennreal)
hoelzl@62975
  1269
hoelzl@62975
  1270
lemma transfer_enn2ereal_continuous_on [transfer_rule]:
nipkow@67399
  1271
  "rel_fun (=) (rel_fun (rel_fun (=) pcr_ennreal) (=)) continuous_on continuous_on"
hoelzl@62975
  1272
proof -
hoelzl@62975
  1273
  have "continuous_on A f" if "continuous_on A (\<lambda>x. enn2ereal (f x))" for A and f :: "'a \<Rightarrow> ennreal"
hoelzl@62975
  1274
    using continuous_on_compose2[OF continuous_on_e2ennreal[of "{0..}"] that]
hoelzl@62975
  1275
    by (auto simp: ennreal.enn2ereal_inverse subset_eq e2ennreal_def max_absorb2)
hoelzl@62975
  1276
  moreover
hoelzl@62975
  1277
  have "continuous_on A (\<lambda>x. enn2ereal (f x))" if "continuous_on A f" for A and f :: "'a \<Rightarrow> ennreal"
hoelzl@62975
  1278
    using continuous_on_compose2[OF continuous_on_enn2ereal that] by auto
hoelzl@62975
  1279
  ultimately
hoelzl@62975
  1280
  show ?thesis
hoelzl@62975
  1281
    by (auto simp add: rel_fun_def ennreal.pcr_cr_eq cr_ennreal_def)
hoelzl@62975
  1282
qed
hoelzl@62975
  1283
hoelzl@62975
  1284
lemma transfer_sup_continuous[transfer_rule]:
nipkow@67399
  1285
  "(rel_fun (rel_fun (=) pcr_ennreal) (=)) sup_continuous sup_continuous"
hoelzl@62975
  1286
proof (safe intro!: rel_funI dest!: rel_fun_eq_pcr_ennreal[THEN iffD1])
hoelzl@62975
  1287
  show "sup_continuous (enn2ereal \<circ> f) \<Longrightarrow> sup_continuous f" for f :: "'a \<Rightarrow> _"
hoelzl@62975
  1288
    using sup_continuous_e2ennreal[of "enn2ereal \<circ> f"] by simp
hoelzl@62975
  1289
  show "sup_continuous f \<Longrightarrow> sup_continuous (enn2ereal \<circ> f)" for f :: "'a \<Rightarrow> _"
hoelzl@62975
  1290
    using sup_continuous_enn2ereal[of f] by (simp add: comp_def)
hoelzl@62975
  1291
qed
hoelzl@62975
  1292
hoelzl@62975
  1293
lemma continuous_on_ennreal[tendsto_intros]:
hoelzl@62975
  1294
  "continuous_on A f \<Longrightarrow> continuous_on A (\<lambda>x. ennreal (f x))"
hoelzl@62975
  1295
  by transfer (auto intro!: continuous_on_max continuous_on_const continuous_on_ereal)
hoelzl@62975
  1296
hoelzl@62975
  1297
lemma tendsto_ennrealD:
hoelzl@62975
  1298
  assumes lim: "((\<lambda>x. ennreal (f x)) \<longlongrightarrow> ennreal x) F"
hoelzl@62975
  1299
  assumes *: "\<forall>\<^sub>F x in F. 0 \<le> f x" and x: "0 \<le> x"
hoelzl@62975
  1300
  shows "(f \<longlongrightarrow> x) F"
hoelzl@62975
  1301
  using continuous_on_tendsto_compose[OF continuous_on_enn2ereal lim]
hoelzl@62975
  1302
  apply simp
hoelzl@62975
  1303
  apply (subst (asm) tendsto_cong)
hoelzl@62975
  1304
  using *
hoelzl@62975
  1305
  apply eventually_elim
hoelzl@62975
  1306
  apply (auto simp: max_absorb2 \<open>0 \<le> x\<close>)
hoelzl@62975
  1307
  done
hoelzl@62975
  1308
hoelzl@62975
  1309
lemma tendsto_ennreal_iff[simp]:
hoelzl@62975
  1310
  "\<forall>\<^sub>F x in F. 0 \<le> f x \<Longrightarrow> 0 \<le> x \<Longrightarrow> ((\<lambda>x. ennreal (f x)) \<longlongrightarrow> ennreal x) F \<longleftrightarrow> (f \<longlongrightarrow> x) F"
hoelzl@62975
  1311
  by (auto dest: tendsto_ennrealD)
hoelzl@62975
  1312
     (auto simp: ennreal_def
hoelzl@62975
  1313
           intro!: continuous_on_tendsto_compose[OF continuous_on_e2ennreal[of UNIV]] tendsto_max)
hoelzl@62975
  1314
hoelzl@62975
  1315
lemma tendsto_enn2ereal_iff[simp]: "((\<lambda>i. enn2ereal (f i)) \<longlongrightarrow> enn2ereal x) F \<longleftrightarrow> (f \<longlongrightarrow> x) F"
hoelzl@62975
  1316
  using continuous_on_enn2ereal[THEN continuous_on_tendsto_compose, of f x F]
hoelzl@62975
  1317
    continuous_on_e2ennreal[THEN continuous_on_tendsto_compose, of "\<lambda>x. enn2ereal (f x)" "enn2ereal x" F UNIV]
hoelzl@62975
  1318
  by auto
hoelzl@62975
  1319
hoelzl@62975
  1320
lemma continuous_on_add_ennreal:
hoelzl@62975
  1321
  fixes f g :: "'a::topological_space \<Rightarrow> ennreal"
hoelzl@62975
  1322
  shows "continuous_on A f \<Longrightarrow> continuous_on A g \<Longrightarrow> continuous_on A (\<lambda>x. f x + g x)"
hoelzl@62975
  1323
  by (transfer fixing: A) (auto intro!: tendsto_add_ereal_nonneg simp: continuous_on_def)
hoelzl@62975
  1324
hoelzl@62975
  1325
lemma continuous_on_inverse_ennreal[continuous_intros]:
hoelzl@62975
  1326
  fixes f :: "'a::topological_space \<Rightarrow> ennreal"
hoelzl@62975
  1327
  shows "continuous_on A f \<Longrightarrow> continuous_on A (\<lambda>x. inverse (f x))"
hoelzl@62975
  1328
proof (transfer fixing: A)
nipkow@67399
  1329
  show "pred_fun top  ((\<le>) 0) f \<Longrightarrow> continuous_on A (\<lambda>x. inverse (f x))" if "continuous_on A f"
hoelzl@62975
  1330
    for f :: "'a \<Rightarrow> ereal"
hoelzl@62975
  1331
    using continuous_on_compose2[OF continuous_on_inverse_ereal that] by (auto simp: subset_eq)
hoelzl@62975
  1332
qed
hoelzl@62975
  1333
hoelzl@62975
  1334
instance ennreal :: topological_comm_monoid_add
hoelzl@62975
  1335
proof
hoelzl@62975
  1336
  show "((\<lambda>x. fst x + snd x) \<longlongrightarrow> a + b) (nhds a \<times>\<^sub>F nhds b)" for a b :: ennreal
hoelzl@62975
  1337
    using continuous_on_add_ennreal[of UNIV fst snd]
hoelzl@62975
  1338
    using tendsto_at_iff_tendsto_nhds[symmetric, of "\<lambda>x::(ennreal \<times> ennreal). fst x + snd x"]
hoelzl@62975
  1339
    by (auto simp: continuous_on_eq_continuous_at)
hoelzl@62975
  1340
       (simp add: isCont_def nhds_prod[symmetric])
hoelzl@62975
  1341
qed
hoelzl@62975
  1342
hoelzl@62975
  1343
lemma sup_continuous_add_ennreal[order_continuous_intros]:
hoelzl@62975
  1344
  fixes f g :: "'a::complete_lattice \<Rightarrow> ennreal"
hoelzl@62975
  1345
  shows "sup_continuous f \<Longrightarrow> sup_continuous g \<Longrightarrow> sup_continuous (\<lambda>x. f x + g x)"
hoelzl@62975
  1346
  by transfer (auto intro!: sup_continuous_add)
hoelzl@62975
  1347
hoelzl@62975
  1348
lemma ennreal_suminf_lessD: "(\<Sum>i. f i :: ennreal) < x \<Longrightarrow> f i < x"
nipkow@64267
  1349
  using le_less_trans[OF sum_le_suminf[OF summableI, of "{i}" f]] by simp
hoelzl@62975
  1350
hoelzl@62975
  1351
lemma sums_ennreal[simp]: "(\<And>i. 0 \<le> f i) \<Longrightarrow> 0 \<le> x \<Longrightarrow> (\<lambda>i. ennreal (f i)) sums ennreal x \<longleftrightarrow> f sums x"
nipkow@64267
  1352
  unfolding sums_def by (simp add: always_eventually sum_nonneg)
hoelzl@62975
  1353
hoelzl@62975
  1354
lemma summable_suminf_not_top: "(\<And>i. 0 \<le> f i) \<Longrightarrow> (\<Sum>i. ennreal (f i)) \<noteq> top \<Longrightarrow> summable f"
hoelzl@62975
  1355
  using summable_sums[OF summableI, of "\<lambda>i. ennreal (f i)"]
hoelzl@62975
  1356
  by (cases "\<Sum>i. ennreal (f i)" rule: ennreal_cases)
hoelzl@62975
  1357
     (auto simp: summable_def)
hoelzl@62975
  1358
hoelzl@62975
  1359
lemma suminf_ennreal[simp]:
hoelzl@62975
  1360
  "(\<And>i. 0 \<le> f i) \<Longrightarrow> (\<Sum>i. ennreal (f i)) \<noteq> top \<Longrightarrow> (\<Sum>i. ennreal (f i)) = ennreal (\<Sum>i. f i)"
hoelzl@62975
  1361
  by (rule sums_unique[symmetric]) (simp add: summable_suminf_not_top suminf_nonneg summable_sums)
hoelzl@62975
  1362
hoelzl@62975
  1363
lemma sums_enn2ereal[simp]: "(\<lambda>i. enn2ereal (f i)) sums enn2ereal x \<longleftrightarrow> f sums x"
nipkow@64267
  1364
  unfolding sums_def by (simp add: always_eventually sum_nonneg)
hoelzl@62975
  1365
hoelzl@62975
  1366
lemma suminf_enn2ereal[simp]: "(\<Sum>i. enn2ereal (f i)) = enn2ereal (suminf f)"
hoelzl@62975
  1367
  by (rule sums_unique[symmetric]) (simp add: summable_sums)
hoelzl@62975
  1368
nipkow@67399
  1369
lemma transfer_e2ennreal_suminf [transfer_rule]: "rel_fun (rel_fun (=) pcr_ennreal) pcr_ennreal suminf suminf"
hoelzl@62975
  1370
  by (auto simp: rel_funI rel_fun_eq_pcr_ennreal comp_def)
hoelzl@62975
  1371
hoelzl@62975
  1372
lemma ennreal_suminf_cmult[simp]: "(\<Sum>i. r * f i) = r * (\<Sum>i. f i::ennreal)"
hoelzl@62975
  1373
  by transfer (auto intro!: suminf_cmult_ereal)
hoelzl@62975
  1374
hoelzl@62975
  1375
lemma ennreal_suminf_multc[simp]: "(\<Sum>i. f i * r) = (\<Sum>i. f i::ennreal) * r"
hoelzl@62975
  1376
  using ennreal_suminf_cmult[of r f] by (simp add: ac_simps)
hoelzl@62975
  1377
hoelzl@62975
  1378
lemma ennreal_suminf_divide[simp]: "(\<Sum>i. f i / r) = (\<Sum>i. f i::ennreal) / r"
hoelzl@62975
  1379
  by (simp add: divide_ennreal_def)
hoelzl@62975
  1380
hoelzl@62975
  1381
lemma ennreal_suminf_neq_top: "summable f \<Longrightarrow> (\<And>i. 0 \<le> f i) \<Longrightarrow> (\<Sum>i. ennreal (f i)) \<noteq> top"
hoelzl@62975
  1382
  using sums_ennreal[of f "suminf f"]
nipkow@68406
  1383
  by (simp add: suminf_nonneg flip: sums_unique summable_sums_iff del: sums_ennreal)
hoelzl@62975
  1384
hoelzl@62975
  1385
lemma suminf_ennreal_eq:
hoelzl@62975
  1386
  "(\<And>i. 0 \<le> f i) \<Longrightarrow> f sums x \<Longrightarrow> (\<Sum>i. ennreal (f i)) = ennreal x"
hoelzl@62975
  1387
  using suminf_nonneg[of f] sums_unique[of f x]
hoelzl@62975
  1388
  by (intro sums_unique[symmetric]) (auto simp: summable_sums_iff)
hoelzl@62975
  1389
hoelzl@62975
  1390
lemma ennreal_suminf_bound_add:
hoelzl@62975
  1391
  fixes f :: "nat \<Rightarrow> ennreal"
hoelzl@62975
  1392
  shows "(\<And>N. (\<Sum>n<N. f n) + y \<le> x) \<Longrightarrow> suminf f + y \<le> x"
hoelzl@62975
  1393
  by transfer (auto intro!: suminf_bound_add)
hoelzl@62975
  1394
hoelzl@62975
  1395
lemma ennreal_suminf_SUP_eq_directed:
hoelzl@62975
  1396
  fixes f :: "'a \<Rightarrow> nat \<Rightarrow> ennreal"
hoelzl@62975
  1397
  assumes *: "\<And>N i j. i \<in> I \<Longrightarrow> j \<in> I \<Longrightarrow> finite N \<Longrightarrow> \<exists>k\<in>I. \<forall>n\<in>N. f i n \<le> f k n \<and> f j n \<le> f k n"
haftmann@69260
  1398
  shows "(\<Sum>n. SUP i\<in>I. f i n) = (SUP i\<in>I. \<Sum>n. f i n)"
hoelzl@62975
  1399
proof cases
hoelzl@62975
  1400
  assume "I \<noteq> {}"
hoelzl@62975
  1401
  then obtain i where "i \<in> I" by auto
hoelzl@62975
  1402
  from * show ?thesis
hoelzl@62975
  1403
    by (transfer fixing: I)
hoelzl@62975
  1404
       (auto simp: max_absorb2 SUP_upper2[OF \<open>i \<in> I\<close>] suminf_nonneg summable_ereal_pos \<open>I \<noteq> {}\<close>
hoelzl@62975
  1405
             intro!: suminf_SUP_eq_directed)
hoelzl@62975
  1406
qed (simp add: bot_ennreal)
hoelzl@62975
  1407
hoelzl@62975
  1408
lemma INF_ennreal_add_const:
hoelzl@62975
  1409
  fixes f g :: "nat \<Rightarrow> ennreal"
hoelzl@62975
  1410
  shows "(INF i. f i + c) = (INF i. f i) + c"
hoelzl@62975
  1411
  using continuous_at_Inf_mono[of "\<lambda>x. x + c" "f`UNIV"]
hoelzl@62975
  1412
  using continuous_add[of "at_right (Inf (range f))", of "\<lambda>x. x" "\<lambda>x. c"]
haftmann@69861
  1413
  by (auto simp: mono_def image_comp)
hoelzl@62975
  1414
hoelzl@62975
  1415
lemma INF_ennreal_const_add:
hoelzl@62975
  1416
  fixes f g :: "nat \<Rightarrow> ennreal"
hoelzl@62975
  1417
  shows "(INF i. c + f i) = c + (INF i. f i)"
hoelzl@62975
  1418
  using INF_ennreal_add_const[of f c] by (simp add: ac_simps)
hoelzl@62975
  1419
haftmann@69260
  1420
lemma SUP_mult_left_ennreal: "c * (SUP i\<in>I. f i) = (SUP i\<in>I. c * f i ::ennreal)"
hoelzl@62975
  1421
proof cases
hoelzl@62975
  1422
  assume "I \<noteq> {}" then show ?thesis
hoelzl@62975
  1423
    by transfer (auto simp add: SUP_ereal_mult_left max_absorb2 SUP_upper2)
hoelzl@62975
  1424
qed (simp add: bot_ennreal)
hoelzl@62975
  1425
haftmann@69260
  1426
lemma SUP_mult_right_ennreal: "(SUP i\<in>I. f i) * c = (SUP i\<in>I. f i * c ::ennreal)"
hoelzl@62975
  1427
  using SUP_mult_left_ennreal by (simp add: mult.commute)
hoelzl@62975
  1428
haftmann@69260
  1429
lemma SUP_divide_ennreal: "(SUP i\<in>I. f i) / c = (SUP i\<in>I. f i / c ::ennreal)"
hoelzl@62975
  1430
  using SUP_mult_right_ennreal by (simp add: divide_ennreal_def)
hoelzl@62975
  1431
hoelzl@62975
  1432
lemma ennreal_SUP_of_nat_eq_top: "(SUP x. of_nat x :: ennreal) = top"
hoelzl@62975
  1433
proof (intro antisym top_greatest le_SUP_iff[THEN iffD2] allI impI)
hoelzl@62975
  1434
  fix y :: ennreal assume "y < top"
hoelzl@62975
  1435
  then obtain r where "y = ennreal r"
hoelzl@62975
  1436
    by (cases y rule: ennreal_cases) auto
hoelzl@62975
  1437
  then show "\<exists>i\<in>UNIV. y < of_nat i"
hoelzl@62975
  1438
    using reals_Archimedean2[of "max 1 r"] zero_less_one
nipkow@67451
  1439
    by (simp add: ennreal_Ex_less_of_nat)
hoelzl@62975
  1440
qed
hoelzl@62975
  1441
hoelzl@62975
  1442
lemma ennreal_SUP_eq_top:
hoelzl@62975
  1443
  fixes f :: "'a \<Rightarrow> ennreal"
hoelzl@62975
  1444
  assumes "\<And>n. \<exists>i\<in>I. of_nat n \<le> f i"
haftmann@69260
  1445
  shows "(SUP i \<in> I. f i) = top"
hoelzl@62975
  1446
proof -
haftmann@69260
  1447
  have "(SUP x. of_nat x :: ennreal) \<le> (SUP i \<in> I. f i)"
hoelzl@62975
  1448
    using assms by (auto intro!: SUP_least intro: SUP_upper2)
hoelzl@62975
  1449
  then show ?thesis
hoelzl@62975
  1450
    by (auto simp: ennreal_SUP_of_nat_eq_top top_unique)
hoelzl@62975
  1451
qed
hoelzl@62975
  1452
hoelzl@62975
  1453
lemma ennreal_INF_const_minus:
hoelzl@62975
  1454
  fixes f :: "'a \<Rightarrow> ennreal"
haftmann@69260
  1455
  shows "I \<noteq> {} \<Longrightarrow> (SUP x\<in>I. c - f x) = c - (INF x\<in>I. f x)"
hoelzl@62975
  1456
  by (transfer fixing: I)
hoelzl@62975
  1457
     (simp add: sup_max[symmetric] SUP_sup_const1 SUP_ereal_minus_right del: sup_ereal_def)
hoelzl@62975
  1458
hoelzl@62975
  1459
lemma of_nat_Sup_ennreal:
hoelzl@62975
  1460
  assumes "A \<noteq> {}" "bdd_above A"
haftmann@69260
  1461
  shows "of_nat (Sup A) = (SUP a\<in>A. of_nat a :: ennreal)"
hoelzl@62975
  1462
proof (intro antisym)
haftmann@69260
  1463
  show "(SUP a\<in>A. of_nat a::ennreal) \<le> of_nat (Sup A)"
hoelzl@62975
  1464
    by (intro SUP_least of_nat_mono) (auto intro: cSup_upper assms)
hoelzl@62975
  1465
  have "Sup A \<in> A"
hoelzl@62975
  1466
    unfolding Sup_nat_def using assms by (intro Max_in) (auto simp: bdd_above_nat)
haftmann@69260
  1467
  then show "of_nat (Sup A) \<le> (SUP a\<in>A. of_nat a::ennreal)"
hoelzl@62975
  1468
    by (intro SUP_upper)
hoelzl@62975
  1469
qed
hoelzl@62975
  1470
hoelzl@62975
  1471
lemma ennreal_tendsto_const_minus:
hoelzl@62975
  1472
  fixes g :: "'a \<Rightarrow> ennreal"
hoelzl@62975
  1473
  assumes ae: "\<forall>\<^sub>F x in F. g x \<le> c"
hoelzl@62975
  1474
  assumes g: "((\<lambda>x. c - g x) \<longlongrightarrow> 0) F"
hoelzl@62975
  1475
  shows "(g \<longlongrightarrow> c) F"
hoelzl@62975
  1476
proof (cases c rule: ennreal_cases)
hoelzl@62975
  1477
  case top with tendsto_unique[OF _ g, of "top"] show ?thesis
hoelzl@62975
  1478
    by (cases "F = bot") auto
hoelzl@62975
  1479
next
hoelzl@62975
  1480
  case (real r)
hoelzl@62975
  1481
  then have "\<forall>x. \<exists>q\<ge>0. g x \<le> c \<longrightarrow> (g x = ennreal q \<and> q \<le> r)"
hoelzl@62975
  1482
    by (auto simp: le_ennreal_iff)
wenzelm@63060
  1483
  then obtain f where *: "0 \<le> f x" "g x = ennreal (f x)" "f x \<le> r" if "g x \<le> c" for x
hoelzl@62975
  1484
    by metis
hoelzl@62975
  1485
  from ae have ae2: "\<forall>\<^sub>F x in F. c - g x = ennreal (r - f x) \<and> f x \<le> r \<and> g x = ennreal (f x) \<and> 0 \<le> f x"
hoelzl@62975
  1486
  proof eventually_elim
hoelzl@62975
  1487
    fix x assume "g x \<le> c" with *[of x] \<open>0 \<le> r\<close> show "c - g x = ennreal (r - f x) \<and> f x \<le> r \<and> g x = ennreal (f x) \<and> 0 \<le> f x"
hoelzl@62975
  1488
      by (auto simp: real ennreal_minus)
hoelzl@62975
  1489
  qed
hoelzl@62975
  1490
  with g have "((\<lambda>x. ennreal (r - f x)) \<longlongrightarrow> ennreal 0) F"
hoelzl@62975
  1491
    by (auto simp add: tendsto_cong eventually_conj_iff)
hoelzl@62975
  1492
  with ae2 have "((\<lambda>x. r - f x) \<longlongrightarrow> 0) F"
hoelzl@62975
  1493
    by (subst (asm) tendsto_ennreal_iff) (auto elim: eventually_mono)
hoelzl@62975
  1494
  then have "(f \<longlongrightarrow> r) F"
hoelzl@62975
  1495
    by (rule Lim_transform2[OF tendsto_const])
hoelzl@62975
  1496
  with ae2 have "((\<lambda>x. ennreal (f x)) \<longlongrightarrow> ennreal r) F"
hoelzl@62975
  1497
    by (subst tendsto_ennreal_iff) (auto elim: eventually_mono simp: real)
hoelzl@62975
  1498
  with ae2 show ?thesis
hoelzl@62975
  1499
    by (auto simp: real tendsto_cong eventually_conj_iff)
hoelzl@62975
  1500
qed
hoelzl@62975
  1501
hoelzl@62975
  1502
lemma ennreal_SUP_add:
hoelzl@62975
  1503
  fixes f g :: "nat \<Rightarrow> ennreal"
haftmann@69313
  1504
  shows "incseq f \<Longrightarrow> incseq g \<Longrightarrow> (SUP i. f i + g i) = Sup (f ` UNIV) + Sup (g ` UNIV)"
hoelzl@62975
  1505
  unfolding incseq_def le_fun_def
hoelzl@62975
  1506
  by transfer
hoelzl@62975
  1507
     (simp add: SUP_ereal_add incseq_def le_fun_def max_absorb2 SUP_upper2)
hoelzl@62975
  1508
nipkow@64267
  1509
lemma ennreal_SUP_sum:
hoelzl@62975
  1510
  fixes f :: "'a \<Rightarrow> nat \<Rightarrow> ennreal"
hoelzl@62975
  1511
  shows "(\<And>i. i \<in> I \<Longrightarrow> incseq (f i)) \<Longrightarrow> (SUP n. \<Sum>i\<in>I. f i n) = (\<Sum>i\<in>I. SUP n. f i n)"
hoelzl@62975
  1512
  unfolding incseq_def
hoelzl@62975
  1513
  by transfer
nipkow@64267
  1514
     (simp add: SUP_ereal_sum incseq_def SUP_upper2 max_absorb2 sum_nonneg)
hoelzl@62975
  1515
hoelzl@62975
  1516
lemma ennreal_liminf_minus:
hoelzl@62975
  1517
  fixes f :: "nat \<Rightarrow> ennreal"
hoelzl@62975
  1518
  shows "(\<And>n. f n \<le> c) \<Longrightarrow> liminf (\<lambda>n. c - f n) = c - limsup f"
hoelzl@62975
  1519
  apply transfer
hoelzl@62975
  1520
  apply (simp add: ereal_diff_positive max.absorb2 liminf_ereal_cminus)
hoelzl@62975
  1521
  apply (subst max.absorb2)
hoelzl@62975
  1522
  apply (rule ereal_diff_positive)
hoelzl@62975
  1523
  apply (rule Limsup_bounded)
hoelzl@62975
  1524
  apply auto
hoelzl@62975
  1525
  done
hoelzl@62975
  1526
hoelzl@62975
  1527
lemma ennreal_continuous_on_cmult:
hoelzl@62975
  1528
  "(c::ennreal) < top \<Longrightarrow> continuous_on A f \<Longrightarrow> continuous_on A (\<lambda>x. c * f x)"
hoelzl@62975
  1529
  by (transfer fixing: A) (auto intro: continuous_on_cmult_ereal)
hoelzl@62975
  1530
hoelzl@62975
  1531
lemma ennreal_tendsto_cmult:
hoelzl@62975
  1532
  "(c::ennreal) < top \<Longrightarrow> (f \<longlongrightarrow> x) F \<Longrightarrow> ((\<lambda>x. c * f x) \<longlongrightarrow> c * x) F"
hoelzl@62975
  1533
  by (rule continuous_on_tendsto_compose[where g=f, OF ennreal_continuous_on_cmult, where s=UNIV])
hoelzl@62975
  1534
     (auto simp: continuous_on_id)
hoelzl@62975
  1535
immler@67727
  1536
lemma tendsto_ennrealI[intro, simp, tendsto_intros]:
hoelzl@62975
  1537
  "(f \<longlongrightarrow> x) F \<Longrightarrow> ((\<lambda>x. ennreal (f x)) \<longlongrightarrow> ennreal x) F"
hoelzl@62975
  1538
  by (auto simp: ennreal_def
hoelzl@62975
  1539
           intro!: continuous_on_tendsto_compose[OF continuous_on_e2ennreal[of UNIV]] tendsto_max)
hoelzl@62975
  1540
immler@67727
  1541
lemma tendsto_enn2erealI [tendsto_intros]:
immler@67727
  1542
  assumes "(f \<longlongrightarrow> l) F"
immler@67727
  1543
  shows "((\<lambda>i. enn2ereal(f i)) \<longlongrightarrow> enn2ereal l) F"
immler@67727
  1544
using tendsto_enn2ereal_iff assms by auto
immler@67727
  1545
immler@67727
  1546
lemma tendsto_e2ennrealI [tendsto_intros]:
immler@67727
  1547
  assumes "(f \<longlongrightarrow> l) F"
immler@67727
  1548
  shows "((\<lambda>i. e2ennreal(f i)) \<longlongrightarrow> e2ennreal l) F"
immler@67727
  1549
proof -
immler@67727
  1550
  have *: "e2ennreal (max x 0) = e2ennreal x" for x
immler@67727
  1551
    by (simp add: e2ennreal_def max.commute)
immler@67727
  1552
  have "((\<lambda>i. max (f i) 0) \<longlongrightarrow> max l 0) F"
immler@67727
  1553
    apply (intro tendsto_intros) using assms by auto
immler@67727
  1554
  then have "((\<lambda>i. enn2ereal(e2ennreal (max (f i) 0))) \<longlongrightarrow> enn2ereal (e2ennreal (max l 0))) F"
immler@67727
  1555
    by (subst enn2ereal_e2ennreal, auto)+
immler@67727
  1556
  then have "((\<lambda>i. e2ennreal (max (f i) 0)) \<longlongrightarrow> e2ennreal (max l 0)) F"
immler@67727
  1557
    using tendsto_enn2ereal_iff by auto
immler@67727
  1558
  then show ?thesis
immler@67727
  1559
    unfolding * by auto
immler@67727
  1560
qed
immler@67727
  1561
hoelzl@62975
  1562
lemma ennreal_suminf_minus:
hoelzl@62975
  1563
  fixes f g :: "nat \<Rightarrow> ennreal"
hoelzl@62975
  1564
  shows "(\<And>i. g i \<le> f i) \<Longrightarrow> suminf f \<noteq> top \<Longrightarrow> suminf g \<noteq> top \<Longrightarrow> (\<Sum>i. f i - g i) = suminf f - suminf g"
hoelzl@62975
  1565
  by transfer
hoelzl@62975
  1566
     (auto simp add: max.absorb2 ereal_diff_positive suminf_le_pos top_ereal_def intro!: suminf_ereal_minus)
hoelzl@62975
  1567
hoelzl@62975
  1568
lemma ennreal_Sup_countable_SUP:
hoelzl@62975
  1569
  "A \<noteq> {} \<Longrightarrow> \<exists>f::nat \<Rightarrow> ennreal. incseq f \<and> range f \<subseteq> A \<and> Sup A = (SUP i. f i)"
hoelzl@62975
  1570
  unfolding incseq_def
hoelzl@62975
  1571
  apply transfer
hoelzl@62975
  1572
  subgoal for A
hoelzl@62975
  1573
    using Sup_countable_SUP[of A]
hoelzl@62975
  1574
    apply (clarsimp simp add: incseq_def[symmetric] SUP_upper2 max.absorb2 image_subset_iff Sup_upper2 cong: conj_cong)
hoelzl@62975
  1575
    subgoal for f
hoelzl@62975
  1576
      by (intro exI[of _ f]) auto
hoelzl@62975
  1577
    done
hoelzl@62975
  1578
  done
hoelzl@62975
  1579
hoelzl@63940
  1580
lemma ennreal_Inf_countable_INF:
hoelzl@63940
  1581
  "A \<noteq> {} \<Longrightarrow> \<exists>f::nat \<Rightarrow> ennreal. decseq f \<and> range f \<subseteq> A \<and> Inf A = (INF i. f i)"
hoelzl@63940
  1582
  including ennreal.lifting
hoelzl@63940
  1583
  unfolding decseq_def
hoelzl@63940
  1584
  apply transfer
hoelzl@63940
  1585
  subgoal for A
hoelzl@63940
  1586
    using Inf_countable_INF[of A]
nipkow@68406
  1587
    apply (clarsimp simp flip: decseq_def)
hoelzl@63940
  1588
    subgoal for f
hoelzl@63940
  1589
      by (intro exI[of _ f]) auto
hoelzl@63940
  1590
    done
hoelzl@63940
  1591
  done
hoelzl@63940
  1592
hoelzl@62975
  1593
lemma ennreal_SUP_countable_SUP:
haftmann@69313
  1594
  "A \<noteq> {} \<Longrightarrow> \<exists>f::nat \<Rightarrow> ennreal. range f \<subseteq> g`A \<and> Sup (g ` A) = Sup (f ` UNIV)"
hoelzl@62975
  1595
  using ennreal_Sup_countable_SUP [of "g`A"] by auto
hoelzl@62975
  1596
hoelzl@62975
  1597
lemma of_nat_tendsto_top_ennreal: "(\<lambda>n::nat. of_nat n :: ennreal) \<longlonglongrightarrow> top"
hoelzl@62975
  1598
  using LIMSEQ_SUP[of "of_nat :: nat \<Rightarrow> ennreal"]
hoelzl@62975
  1599
  by (simp add: ennreal_SUP_of_nat_eq_top incseq_def)
hoelzl@62975
  1600
hoelzl@62975
  1601
lemma SUP_sup_continuous_ennreal:
hoelzl@62975
  1602
  fixes f :: "ennreal \<Rightarrow> 'a::complete_lattice"
hoelzl@62975
  1603
  assumes f: "sup_continuous f" and "I \<noteq> {}"
haftmann@69260
  1604
  shows "(SUP i\<in>I. f (g i)) = f (SUP i\<in>I. g i)"
hoelzl@62975
  1605
proof (rule antisym)
haftmann@69260
  1606
  show "(SUP i\<in>I. f (g i)) \<le> f (SUP i\<in>I. g i)"
hoelzl@62975
  1607
    by (rule mono_SUP[OF sup_continuous_mono[OF f]])
hoelzl@62975
  1608
  from ennreal_Sup_countable_SUP[of "g`I"] \<open>I \<noteq> {}\<close>
haftmann@69260
  1609
  obtain M :: "nat \<Rightarrow> ennreal" where "incseq M" and M: "range M \<subseteq> g ` I" and eq: "(SUP i \<in> I. g i) = (SUP i. M i)"
hoelzl@62975
  1610
    by auto
haftmann@69260
  1611
  have "f (SUP i \<in> I. g i) = (SUP i \<in> range M. f i)"
haftmann@69861
  1612
    unfolding eq sup_continuousD[OF f \<open>mono M\<close>] by (simp add: image_comp)
haftmann@69260
  1613
  also have "\<dots> \<le> (SUP i \<in> I. f (g i))"
haftmann@69861
  1614
    by (insert M, drule SUP_subset_mono) (auto simp add: image_comp)
haftmann@69260
  1615
  finally show "f (SUP i \<in> I. g i) \<le> (SUP i \<in> I. f (g i))" .
hoelzl@62975
  1616
qed
hoelzl@62975
  1617
hoelzl@62975
  1618
lemma ennreal_suminf_SUP_eq:
hoelzl@62975
  1619
  fixes f :: "nat \<Rightarrow> nat \<Rightarrow> ennreal"
hoelzl@62975
  1620
  shows "(\<And>i. incseq (\<lambda>n. f n i)) \<Longrightarrow> (\<Sum>i. SUP n. f n i) = (SUP n. \<Sum>i. f n i)"
hoelzl@62975
  1621
  apply (rule ennreal_suminf_SUP_eq_directed)
hoelzl@62975
  1622
  subgoal for N n j
hoelzl@62975
  1623
    by (auto simp: incseq_def intro!:exI[of _ "max n j"])
hoelzl@62975
  1624
  done
hoelzl@62975
  1625
hoelzl@62975
  1626
lemma ennreal_SUP_add_left:
hoelzl@62975
  1627
  fixes c :: ennreal
haftmann@69260
  1628
  shows "I \<noteq> {} \<Longrightarrow> (SUP i\<in>I. f i + c) = (SUP i\<in>I. f i) + c"
hoelzl@62975
  1629
  apply transfer
hoelzl@62975
  1630
  apply (simp add: SUP_ereal_add_left)
hoelzl@62975
  1631
  apply (subst (1 2) max.absorb2)
nipkow@68752
  1632
  apply (auto intro: SUP_upper2 add_nonneg_nonneg)
hoelzl@62975
  1633
  done
hoelzl@62975
  1634
hoelzl@63225
  1635
lemma ennreal_SUP_const_minus: (* TODO: rename: ennreal_SUP_const_minus *)
hoelzl@62975
  1636
  fixes f :: "'a \<Rightarrow> ennreal"
haftmann@69260
  1637
  shows "I \<noteq> {} \<Longrightarrow> c < top \<Longrightarrow> (INF x\<in>I. c - f x) = c - (SUP x\<in>I. f x)"
hoelzl@62975
  1638
  apply (transfer fixing: I)
hoelzl@62975
  1639
  unfolding ex_in_conv[symmetric]
hoelzl@62975
  1640
  apply (auto simp add: sup_max[symmetric] SUP_upper2 sup_absorb2
hoelzl@62975
  1641
              simp del: sup_ereal_def)
hoelzl@62975
  1642
  apply (subst INF_ereal_minus_right[symmetric])
hoelzl@62975
  1643
  apply (auto simp del: sup_ereal_def simp add: sup_INF)
hoelzl@62975
  1644
  done
hoelzl@62975
  1645
hoelzl@62975
  1646
subsection \<open>Approximation lemmas\<close>
hoelzl@62975
  1647
hoelzl@62975
  1648
lemma INF_approx_ennreal:
hoelzl@62975
  1649
  fixes x::ennreal and e::real
hoelzl@62975
  1650
  assumes "e > 0"
haftmann@69260
  1651
  assumes INF: "x = (INF i \<in> A. f i)"
hoelzl@62975
  1652
  assumes "x \<noteq> \<infinity>"
hoelzl@62975
  1653
  shows "\<exists>i \<in> A. f i < x + e"
hoelzl@62975
  1654
proof -
haftmann@69260
  1655
  have "(INF i \<in> A. f i) < x + e"
hoelzl@62975
  1656
    unfolding INF[symmetric] using \<open>0<e\<close> \<open>x \<noteq> \<infinity>\<close> by (cases x) auto
hoelzl@62975
  1657
  then show ?thesis
hoelzl@62975
  1658
    unfolding INF_less_iff .
hoelzl@62975
  1659
qed
hoelzl@62975
  1660
hoelzl@62975
  1661
lemma SUP_approx_ennreal:
hoelzl@62975
  1662
  fixes x::ennreal and e::real
hoelzl@62975
  1663
  assumes "e > 0" "A \<noteq> {}"
haftmann@69260
  1664
  assumes SUP: "x = (SUP i \<in> A. f i)"
hoelzl@62975
  1665
  assumes "x \<noteq> \<infinity>"
hoelzl@62975
  1666
  shows "\<exists>i \<in> A. x < f i + e"
hoelzl@62975
  1667
proof -
hoelzl@62975
  1668
  have "x < x + e"
hoelzl@62975
  1669
    using \<open>0<e\<close> \<open>x \<noteq> \<infinity>\<close> by (cases x) auto
haftmann@69260
  1670
  also have "x + e = (SUP i \<in> A. f i + e)"
hoelzl@62975
  1671
    unfolding SUP ennreal_SUP_add_left[OF \<open>A \<noteq> {}\<close>] ..
hoelzl@62975
  1672
  finally show ?thesis
hoelzl@62975
  1673
    unfolding less_SUP_iff .
hoelzl@62975
  1674
qed
hoelzl@62975
  1675
hoelzl@62975
  1676
lemma ennreal_approx_SUP:
hoelzl@62975
  1677
  fixes x::ennreal
hoelzl@62975
  1678
  assumes f_bound: "\<And>i. i \<in> A \<Longrightarrow> f i \<le> x"
hoelzl@62975
  1679
  assumes approx: "\<And>e. (e::real) > 0 \<Longrightarrow> \<exists>i \<in> A. x \<le> f i + e"
haftmann@69260
  1680
  shows "x = (SUP i \<in> A. f i)"
hoelzl@62975
  1681
proof (rule antisym)
haftmann@69260
  1682
  show "x \<le> (SUP i\<in>A. f i)"
hoelzl@62975
  1683
  proof (rule ennreal_le_epsilon)
hoelzl@62975
  1684
    fix e :: real assume "0 < e"
hoelzl@62975
  1685
    from approx[OF this] guess i ..
hoelzl@62975
  1686
    then have "x \<le> f i + e"
hoelzl@62975
  1687
      by simp
haftmann@69260
  1688
    also have "\<dots> \<le> (SUP i\<in>A. f i) + e"
hoelzl@62975
  1689
      by (intro add_mono \<open>i \<in> A\<close> SUP_upper order_refl)
haftmann@69260
  1690
    finally show "x \<le> (SUP i\<in>A. f i) + e" .
hoelzl@62975
  1691
  qed
hoelzl@62975
  1692
qed (intro SUP_least f_bound)
hoelzl@62975
  1693
hoelzl@62975
  1694
lemma ennreal_approx_INF:
hoelzl@62975
  1695
  fixes x::ennreal
hoelzl@62975
  1696
  assumes f_bound: "\<And>i. i \<in> A \<Longrightarrow> x \<le> f i"
hoelzl@62975
  1697
  assumes approx: "\<And>e. (e::real) > 0 \<Longrightarrow> \<exists>i \<in> A. f i \<le> x + e"
haftmann@69260
  1698
  shows "x = (INF i \<in> A. f i)"
hoelzl@62975
  1699
proof (rule antisym)
haftmann@69260
  1700
  show "(INF i\<in>A. f i) \<le> x"
hoelzl@62975
  1701
  proof (rule ennreal_le_epsilon)
hoelzl@62975
  1702
    fix e :: real assume "0 < e"
hoelzl@62975
  1703
    from approx[OF this] guess i .. note i = this
haftmann@69260
  1704
    then have "(INF i\<in>A. f i) \<le> f i"
hoelzl@62975
  1705
      by (intro INF_lower)
hoelzl@62975
  1706
    also have "\<dots> \<le> x + e"
hoelzl@62975
  1707
      by fact
haftmann@69260
  1708
    finally show "(INF i\<in>A. f i) \<le> x + e" .
hoelzl@62975
  1709
  qed
hoelzl@62975
  1710
qed (intro INF_greatest f_bound)
hoelzl@62975
  1711
hoelzl@62975
  1712
lemma ennreal_approx_unit:
hoelzl@62975
  1713
  "(\<And>a::ennreal. 0 < a \<Longrightarrow> a < 1 \<Longrightarrow> a * z \<le> y) \<Longrightarrow> z \<le> y"
hoelzl@62975
  1714
  apply (subst SUP_mult_right_ennreal[of "\<lambda>x. x" "{0 <..< 1}" z, simplified])
hoelzl@62975
  1715
  apply (rule SUP_least)
hoelzl@62975
  1716
  apply auto
hoelzl@62975
  1717
  done
hoelzl@62975
  1718
hoelzl@62975
  1719
lemma suminf_ennreal2:
hoelzl@62975
  1720
  "(\<And>i. 0 \<le> f i) \<Longrightarrow> summable f \<Longrightarrow> (\<Sum>i. ennreal (f i)) = ennreal (\<Sum>i. f i)"
hoelzl@62975
  1721
  using suminf_ennreal_eq by blast
hoelzl@62975
  1722
hoelzl@62975
  1723
lemma less_top_ennreal: "x < top \<longleftrightarrow> (\<exists>r\<ge>0. x = ennreal r)"
hoelzl@62975
  1724
  by (cases x) auto
hoelzl@62975
  1725
hoelzl@62975
  1726
lemma tendsto_top_iff_ennreal:
hoelzl@62975
  1727
  fixes f :: "'a \<Rightarrow> ennreal"
hoelzl@62975
  1728
  shows "(f \<longlongrightarrow> top) F \<longleftrightarrow> (\<forall>l\<ge>0. eventually (\<lambda>x. ennreal l < f x) F)"
hoelzl@62975
  1729
  by (auto simp: less_top_ennreal order_tendsto_iff )
hoelzl@62975
  1730
hoelzl@62975
  1731
lemma ennreal_tendsto_top_eq_at_top:
hoelzl@62975
  1732
  "((\<lambda>z. ennreal (f z)) \<longlongrightarrow> top) F \<longleftrightarrow> (LIM z F. f z :> at_top)"
hoelzl@62975
  1733
  unfolding filterlim_at_top_dense tendsto_top_iff_ennreal
hoelzl@62975
  1734
  apply (auto simp: ennreal_less_iff)
hoelzl@62975
  1735
  subgoal for y
hoelzl@62975
  1736
    by (auto elim!: eventually_mono allE[of _ "max 0 y"])
hoelzl@62975
  1737
  done
hoelzl@62975
  1738
hoelzl@62975
  1739
lemma tendsto_0_if_Limsup_eq_0_ennreal:
hoelzl@62975
  1740
  fixes f :: "_ \<Rightarrow> ennreal"
hoelzl@62975
  1741
  shows "Limsup F f = 0 \<Longrightarrow> (f \<longlongrightarrow> 0) F"
hoelzl@62975
  1742
  using Liminf_le_Limsup[of F f] tendsto_iff_Liminf_eq_Limsup[of F f 0]
hoelzl@62975
  1743
  by (cases "F = bot") auto
hoelzl@62975
  1744
hoelzl@62975
  1745
lemma diff_le_self_ennreal[simp]: "a - b \<le> (a::ennreal)"
hoelzl@62975
  1746
  by (cases a b rule: ennreal2_cases) (auto simp: ennreal_minus)
hoelzl@62975
  1747
hoelzl@62975
  1748
lemma ennreal_ineq_diff_add: "b \<le> a \<Longrightarrow> a = b + (a - b::ennreal)"
hoelzl@62975
  1749
  by transfer (auto simp: ereal_diff_positive max.absorb2 ereal_ineq_diff_add)
hoelzl@62975
  1750
hoelzl@62975
  1751
lemma ennreal_mult_strict_left_mono: "(a::ennreal) < c \<Longrightarrow> 0 < b \<Longrightarrow> b < top \<Longrightarrow> b * a < b * c"
hoelzl@62975
  1752
  by transfer (auto intro!: ereal_mult_strict_left_mono)
hoelzl@62975
  1753
hoelzl@62975
  1754
lemma ennreal_between: "0 < e \<Longrightarrow> 0 < x \<Longrightarrow> x < top \<Longrightarrow> x - e < (x::ennreal)"
hoelzl@62975
  1755
  by transfer (auto intro!: ereal_between)
hoelzl@62975
  1756
hoelzl@62975
  1757
lemma minus_less_iff_ennreal: "b < top \<Longrightarrow> b \<le> a \<Longrightarrow> a - b < c \<longleftrightarrow> a < c + (b::ennreal)"
hoelzl@62975
  1758
  by transfer
hoelzl@62975
  1759
     (auto simp: top_ereal_def ereal_minus_less le_less)
hoelzl@62975
  1760
hoelzl@62975
  1761
lemma tendsto_zero_ennreal:
hoelzl@62975
  1762
  assumes ev: "\<And>r. 0 < r \<Longrightarrow> \<forall>\<^sub>F x in F. f x < ennreal r"
hoelzl@62975
  1763
  shows "(f \<longlongrightarrow> 0) F"
hoelzl@62975
  1764
proof (rule order_tendstoI)
hoelzl@62975
  1765
  fix e::ennreal assume "e > 0"
hoelzl@62975
  1766
  obtain e'::real where "e' > 0" "ennreal e' < e"
wenzelm@63145
  1767
    using \<open>0 < e\<close> dense[of 0 "if e = top then 1 else (enn2real e)"]
hoelzl@62975
  1768
    by (cases e) (auto simp: ennreal_less_iff)
hoelzl@62975
  1769
  from ev[OF \<open>e' > 0\<close>] show "\<forall>\<^sub>F x in F. f x < e"
hoelzl@62975
  1770
    by eventually_elim (insert \<open>ennreal e' < e\<close>, auto)
hoelzl@62975
  1771
qed simp
hoelzl@62975
  1772
hoelzl@62975
  1773
lifting_update ennreal.lifting
hoelzl@62975
  1774
lifting_forget ennreal.lifting
hoelzl@62975
  1775
hoelzl@63225
  1776
wenzelm@69593
  1777
subsection \<open>\<^typ>\<open>ennreal\<close> theorems\<close>
hoelzl@63225
  1778
hoelzl@63225
  1779
lemma neq_top_trans: fixes x y :: ennreal shows "\<lbrakk> y \<noteq> top; x \<le> y \<rbrakk> \<Longrightarrow> x \<noteq> top"
hoelzl@63225
  1780
by (auto simp: top_unique)
hoelzl@63225
  1781
hoelzl@63225
  1782
lemma diff_diff_ennreal: fixes a b :: ennreal shows "a \<le> b \<Longrightarrow> b \<noteq> \<infinity> \<Longrightarrow> b - (b - a) = a"
nipkow@68406
  1783
  by (cases a b rule: ennreal2_cases) (auto simp: ennreal_minus top_unique)
hoelzl@63225
  1784
hoelzl@63225
  1785
lemma ennreal_less_one_iff[simp]: "ennreal x < 1 \<longleftrightarrow> x < 1"
nipkow@68406
  1786
  by (cases "0 \<le> x") (auto simp: ennreal_neg ennreal_less_iff simp flip: ennreal_1)
hoelzl@63225
  1787
hoelzl@63225
  1788
lemma SUP_const_minus_ennreal:
haftmann@69260
  1789
  fixes f :: "'a \<Rightarrow> ennreal" shows "I \<noteq> {} \<Longrightarrow> (SUP x\<in>I. c - f x) = c - (INF x\<in>I. f x)"
hoelzl@63225
  1790
  including ennreal.lifting
hoelzl@63225
  1791
  by (transfer fixing: I)
nipkow@68406
  1792
     (simp add: SUP_sup_distrib[symmetric] SUP_ereal_minus_right
nipkow@68406
  1793
           flip: sup_ereal_def)
hoelzl@63225
  1794
hoelzl@63225
  1795
lemma zero_minus_ennreal[simp]: "0 - (a::ennreal) = 0"
hoelzl@63225
  1796
  including ennreal.lifting
hoelzl@63225
  1797
  by transfer (simp split: split_max)
hoelzl@63225
  1798
hoelzl@63225
  1799
lemma diff_diff_commute_ennreal:
hoelzl@63225
  1800
  fixes a b c :: ennreal shows "a - b - c = a - c - b"
hoelzl@63225
  1801
  by (cases a b c rule: ennreal3_cases) (simp_all add: ennreal_minus field_simps)
hoelzl@63225
  1802
hoelzl@63225
  1803
lemma diff_gr0_ennreal: "b < (a::ennreal) \<Longrightarrow> 0 < a - b"
hoelzl@63225
  1804
  including ennreal.lifting by transfer (auto simp: ereal_diff_gr0 ereal_diff_positive split: split_max)
hoelzl@63225
  1805
hoelzl@63225
  1806
lemma divide_le_posI_ennreal:
hoelzl@63225
  1807
  fixes x y z :: ennreal
hoelzl@63225
  1808
  shows "x > 0 \<Longrightarrow> z \<le> x * y \<Longrightarrow> z / x \<le> y"
hoelzl@63225
  1809
  by (cases x y z rule: ennreal3_cases)
hoelzl@63225
  1810
     (auto simp: divide_ennreal ennreal_mult[symmetric] field_simps top_unique)
hoelzl@63225
  1811
hoelzl@63225
  1812
lemma add_diff_eq_ennreal:
hoelzl@63225
  1813
  fixes x y z :: ennreal
hoelzl@63225
  1814
  shows "z \<le> y \<Longrightarrow> x + (y - z) = x + y - z"
hoelzl@63225
  1815
  including ennreal.lifting
hoelzl@63225
  1816
  by transfer
nipkow@68752
  1817
     (insert add_mono[of "0::ereal"], auto simp add: ereal_diff_positive max.absorb2 add_diff_eq_ereal)
hoelzl@63225
  1818
hoelzl@63225
  1819
lemma add_diff_inverse_ennreal:
hoelzl@63225
  1820
  fixes x y :: ennreal shows "x \<le> y \<Longrightarrow> x + (y - x) = y"
hoelzl@63225
  1821
  by (cases x) (simp_all add: top_unique add_diff_eq_ennreal)
hoelzl@63225
  1822
hoelzl@63225
  1823
lemma add_diff_eq_iff_ennreal[simp]:
hoelzl@63225
  1824
  fixes x y :: ennreal shows "x + (y - x) = y \<longleftrightarrow> x \<le> y"
hoelzl@63225
  1825
proof
hoelzl@63225
  1826
  assume *: "x + (y - x) = y" show "x \<le> y"
hoelzl@63225
  1827
    by (subst *[symmetric]) simp
hoelzl@63225
  1828
qed (simp add: add_diff_inverse_ennreal)
hoelzl@63225
  1829
hoelzl@63225
  1830
lemma add_diff_le_ennreal: "a + b - c \<le> a + (b - c::ennreal)"
hoelzl@63225
  1831
  apply (cases a b c rule: ennreal3_cases)
hoelzl@63225
  1832
  subgoal for a' b' c'
nipkow@68406
  1833
    by (cases "0 \<le> b' - c'") (simp_all add: ennreal_minus top_add ennreal_neg flip: ennreal_plus)
nipkow@68406
  1834
  apply (simp_all add: top_add flip: ennreal_plus)
hoelzl@63225
  1835
  done
hoelzl@63225
  1836
hoelzl@63225
  1837
lemma diff_eq_0_ennreal: "a < top \<Longrightarrow> a \<le> b \<Longrightarrow> a - b = (0::ennreal)"
hoelzl@63225
  1838
  using ennreal_minus_pos_iff gr_zeroI not_less by blast
hoelzl@63225
  1839
hoelzl@63225
  1840
lemma diff_diff_ennreal': fixes x y z :: ennreal shows "z \<le> y \<Longrightarrow> y - z \<le> x \<Longrightarrow> x - (y - z) = x + z - y"
hoelzl@63225
  1841
  by (cases x; cases y; cases z)
nipkow@68406
  1842
     (auto simp add: top_add add_top minus_top_ennreal ennreal_minus top_unique
nipkow@68406
  1843
           simp flip: ennreal_plus)
hoelzl@63225
  1844
hoelzl@63225
  1845
lemma diff_diff_ennreal'': fixes x y z :: ennreal
hoelzl@63225
  1846
  shows "z \<le> y \<Longrightarrow> x - (y - z) = (if y - z \<le> x then x + z - y else 0)"
hoelzl@63225
  1847
  by (cases x; cases y; cases z)
nipkow@68406
  1848
     (auto simp add: top_add add_top minus_top_ennreal ennreal_minus top_unique ennreal_neg
nipkow@68406
  1849
           simp flip: ennreal_plus)
hoelzl@63225
  1850
hoelzl@63225
  1851
lemma power_less_top_ennreal: fixes x :: ennreal shows "x ^ n < top \<longleftrightarrow> x < top \<or> n = 0"
hoelzl@63225
  1852
  using power_eq_top_ennreal[of x n] by (auto simp: less_top)
hoelzl@63225
  1853
hoelzl@63225
  1854
lemma ennreal_divide_times: "(a / b) * c = a * (c / b :: ennreal)"
hoelzl@63225
  1855
  by (simp add: mult.commute ennreal_times_divide)
hoelzl@63225
  1856
hoelzl@63225
  1857
lemma diff_less_top_ennreal: "a - b < top \<longleftrightarrow>  a < (top :: ennreal)"
hoelzl@63225
  1858
  by (cases a; cases b) (auto simp: ennreal_minus)
hoelzl@63225
  1859
hoelzl@63225
  1860
lemma divide_less_ennreal: "b \<noteq> 0 \<Longrightarrow> b < top \<Longrightarrow> a / b < c \<longleftrightarrow> a < (c * b :: ennreal)"
hoelzl@63225
  1861
  by (cases a; cases b; cases c)
hoelzl@63225
  1862
     (auto simp: divide_ennreal ennreal_mult[symmetric] ennreal_less_iff field_simps ennreal_top_mult ennreal_top_divide)
hoelzl@63225
  1863
hoelzl@63225
  1864
lemma one_less_numeral[simp]: "1 < (numeral n::ennreal) \<longleftrightarrow> (num.One < n)"
nipkow@68406
  1865
  by (simp flip: ennreal_1 ennreal_numeral add: ennreal_less_iff)
hoelzl@63225
  1866
hoelzl@63225
  1867
lemma divide_eq_1_ennreal: "a / b = (1::ennreal) \<longleftrightarrow> (b \<noteq> top \<and> b \<noteq> 0 \<and> b = a)"
hoelzl@63225
  1868
  by (cases a ; cases b; cases "b = 0") (auto simp: ennreal_top_divide divide_ennreal split: if_split_asm)
hoelzl@63225
  1869
hoelzl@63225
  1870
lemma ennreal_mult_cancel_left: "(a * b = a * c) = (a = top \<and> b \<noteq> 0 \<and> c \<noteq> 0 \<or> a = 0 \<or> b = (c::ennreal))"
hoelzl@63225
  1871
  by (cases a; cases b; cases c) (auto simp: ennreal_mult[symmetric] ennreal_mult_top ennreal_top_mult)
hoelzl@63225
  1872
hoelzl@63225
  1873
lemma ennreal_minus_if: "ennreal a - ennreal b = ennreal (if 0 \<le> b then (if b \<le> a then a - b else 0) else a)"
hoelzl@63225
  1874
  by (auto simp: ennreal_minus ennreal_neg)
hoelzl@63225
  1875
hoelzl@63225
  1876
lemma ennreal_plus_if: "ennreal a + ennreal b = ennreal (if 0 \<le> a then (if 0 \<le> b then a + b else a) else b)"
hoelzl@63225
  1877
  by (auto simp: ennreal_neg)
hoelzl@63225
  1878
hoelzl@63225
  1879
lemma power_le_one_iff: "0 \<le> (a::real) \<Longrightarrow> a ^ n \<le> 1 \<longleftrightarrow> (n = 0 \<or> a \<le> 1)"
hoelzl@63225
  1880
  by (metis (mono_tags, hide_lams) le_less neq0_conv not_le one_le_power power_0 power_eq_imp_eq_base power_le_one zero_le_one)
hoelzl@63225
  1881
hoelzl@63225
  1882
lemma ennreal_diff_le_mono_left: "a \<le> b \<Longrightarrow> a - c \<le> (b::ennreal)"
hoelzl@63225
  1883
  using ennreal_mono_minus[of 0 c a, THEN order_trans, of b] by simp
hoelzl@63225
  1884
hoelzl@63225
  1885
lemma ennreal_minus_le_iff: "a - b \<le> c \<longleftrightarrow> (a \<le> b + (c::ennreal) \<and> (a = top \<and> b = top \<longrightarrow> c = top))"
hoelzl@63225
  1886
  by (cases a; cases b; cases c)
nipkow@68406
  1887
     (auto simp: top_unique top_add add_top ennreal_minus simp flip: ennreal_plus)
hoelzl@63225
  1888
hoelzl@63225
  1889
lemma ennreal_le_minus_iff: "a \<le> b - c \<longleftrightarrow> (a + c \<le> (b::ennreal) \<or> (a = 0 \<and> b \<le> c))"
hoelzl@63225
  1890
  by (cases a; cases b; cases c)
nipkow@68406
  1891
     (auto simp: top_unique top_add add_top ennreal_minus ennreal_le_iff2
nipkow@68406
  1892
           simp flip: ennreal_plus)
hoelzl@63225
  1893
hoelzl@63225
  1894
lemma diff_add_eq_diff_diff_swap_ennreal: "x - (y + z :: ennreal) = x - y - z"
hoelzl@63225
  1895
  by (cases x; cases y; cases z)
nipkow@68406
  1896
     (auto simp: ennreal_minus_if add_top top_add simp flip: ennreal_plus)
hoelzl@63225
  1897
hoelzl@63225
  1898
lemma diff_add_assoc2_ennreal: "b \<le> a \<Longrightarrow> (a - b + c::ennreal) = a + c - b"
hoelzl@63225
  1899
  by (cases a; cases b; cases c)
hoelzl@63225
  1900
     (auto simp add: ennreal_minus_if ennreal_plus_if add_top top_add top_unique simp del: ennreal_plus)
hoelzl@63225
  1901
hoelzl@63225
  1902
lemma diff_gt_0_iff_gt_ennreal: "0 < a - b \<longleftrightarrow> (a = top \<and> b = top \<or> b < (a::ennreal))"
hoelzl@63225
  1903
  by (cases a; cases b) (auto simp: ennreal_minus_if ennreal_less_iff)
hoelzl@63225
  1904
hoelzl@63225
  1905
lemma diff_eq_0_iff_ennreal: "(a - b::ennreal) = 0 \<longleftrightarrow> (a < top \<and> a \<le> b)"
hoelzl@63225
  1906
  by (cases a) (auto simp: ennreal_minus_eq_0 diff_eq_0_ennreal)
hoelzl@63225
  1907
hoelzl@63225
  1908
lemma add_diff_self_ennreal: "a + (b - a::ennreal) = (if a \<le> b then b else a)"
hoelzl@63225
  1909
  by (auto simp: diff_eq_0_iff_ennreal less_top)
hoelzl@63225
  1910
hoelzl@63225
  1911
lemma diff_add_self_ennreal: "(b - a + a::ennreal) = (if a \<le> b then b else a)"
hoelzl@63225
  1912
  by (auto simp: diff_add_cancel_ennreal diff_eq_0_iff_ennreal less_top)
hoelzl@63225
  1913
hoelzl@63225
  1914
lemma ennreal_minus_cancel_iff:
hoelzl@63225
  1915
  fixes a b c :: ennreal
hoelzl@63225
  1916
  shows "a - b = a - c \<longleftrightarrow> (b = c \<or> (a \<le> b \<and> a \<le> c) \<or> a = top)"
hoelzl@63225
  1917
  by (cases a; cases b; cases c) (auto simp: ennreal_minus_if)
hoelzl@63225
  1918
immler@67727
  1919
text \<open>The next lemma is wrong for $a = top$, for $b = c = 1$ for instance.\<close>
immler@67727
  1920
immler@67727
  1921
lemma ennreal_right_diff_distrib:
immler@67727
  1922
  fixes a b c::ennreal
immler@67727
  1923
  assumes "a \<noteq> top"
immler@67727
  1924
  shows "a * (b - c) = a * b - a * c"
immler@67727
  1925
  apply (cases a, cases b, cases c, auto simp add: assms)
immler@67727
  1926
  apply (metis (mono_tags, lifting) ennreal_minus ennreal_mult' linordered_field_class.sign_simps(38) split_mult_pos_le)
immler@67727
  1927
  apply (metis ennreal_minus_zero ennreal_mult_cancel_left ennreal_top_eq_mult_iff minus_top_ennreal mult_eq_0_iff top_neq_ennreal)
immler@67727
  1928
  apply (metis ennreal_minus_eq_top ennreal_minus_zero ennreal_mult_eq_top_iff mult_eq_0_iff)
immler@67727
  1929
  done
immler@67727
  1930
hoelzl@63225
  1931
lemma SUP_diff_ennreal:
haftmann@69260
  1932
  "c < top \<Longrightarrow> (SUP i\<in>I. f i - c :: ennreal) = (SUP i\<in>I. f i) - c"
hoelzl@63225
  1933
  by (auto intro!: SUP_eqI ennreal_minus_mono SUP_least intro: SUP_upper
hoelzl@63225
  1934
           simp: ennreal_minus_cancel_iff ennreal_minus_le_iff less_top[symmetric])
hoelzl@63225
  1935
hoelzl@63225
  1936
lemma ennreal_SUP_add_right:
haftmann@69260
  1937
  fixes c :: ennreal shows "I \<noteq> {} \<Longrightarrow> c + (SUP i\<in>I. f i) = (SUP i\<in>I. c + f i)"
hoelzl@63225
  1938
  using ennreal_SUP_add_left[of I f c] by (simp add: add.commute)
hoelzl@63225
  1939
hoelzl@63225
  1940
lemma SUP_add_directed_ennreal:
hoelzl@63225
  1941
  fixes f g :: "_ \<Rightarrow> ennreal"
hoelzl@63225
  1942
  assumes directed: "\<And>i j. i \<in> I \<Longrightarrow> j \<in> I \<Longrightarrow> \<exists>k\<in>I. f i + g j \<le> f k + g k"
haftmann@69260
  1943
  shows "(SUP i\<in>I. f i + g i) = (SUP i\<in>I. f i) + (SUP i\<in>I. g i)"
hoelzl@63225
  1944
proof cases
hoelzl@63225
  1945
  assume "I = {}" then show ?thesis
hoelzl@63225
  1946
    by (simp add: bot_ereal_def)
hoelzl@63225
  1947
next
hoelzl@63225
  1948
  assume "I \<noteq> {}"
hoelzl@63225
  1949
  show ?thesis
hoelzl@63225
  1950
  proof (rule antisym)
haftmann@69260
  1951
    show "(SUP i\<in>I. f i + g i) \<le> (SUP i\<in>I. f i) + (SUP i\<in>I. g i)"
hoelzl@63225
  1952
      by (rule SUP_least; intro add_mono SUP_upper)
hoelzl@63225
  1953
  next
haftmann@69260
  1954
    have "(SUP i\<in>I. f i) + (SUP i\<in>I. g i) = (SUP i\<in>I. f i + (SUP i\<in>I. g i))"
hoelzl@63225
  1955
      by (intro ennreal_SUP_add_left[symmetric] \<open>I \<noteq> {}\<close>)
haftmann@69260
  1956
    also have "\<dots> = (SUP i\<in>I. (SUP j\<in>I. f i + g j))"
haftmann@69661
  1957
      using \<open>I \<noteq> {}\<close> by (simp add: ennreal_SUP_add_right)
haftmann@69260
  1958
    also have "\<dots> \<le> (SUP i\<in>I. f i + g i)"
hoelzl@63225
  1959
      using directed by (intro SUP_least) (blast intro: SUP_upper2)
haftmann@69260
  1960
    finally show "(SUP i\<in>I. f i) + (SUP i\<in>I. g i) \<le> (SUP i\<in>I. f i + g i)" .
hoelzl@63225
  1961
  qed
hoelzl@63225
  1962
qed
hoelzl@63225
  1963
hoelzl@63225
  1964
lemma enn2real_eq_0_iff: "enn2real x = 0 \<longleftrightarrow> x = 0 \<or> x = top"
hoelzl@63225
  1965
  by (cases x) auto
hoelzl@63225
  1966
immler@67727
  1967
lemma continuous_on_diff_ennreal:
hoelzl@63225
  1968
  "continuous_on A f \<Longrightarrow> continuous_on A g \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> f x \<noteq> top) \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> g x \<noteq> top) \<Longrightarrow> continuous_on A (\<lambda>z. f z - g z::ennreal)"
hoelzl@63225
  1969
  including ennreal.lifting
hoelzl@63225
  1970
proof (transfer fixing: A, simp add: top_ereal_def)
hoelzl@63225
  1971
  fix f g :: "'a \<Rightarrow> ereal" assume "\<forall>x. 0 \<le> f x" "\<forall>x. 0 \<le> g x" "continuous_on A f" "continuous_on A g"
hoelzl@63225
  1972
  moreover assume "f x \<noteq> \<infinity>" "g x \<noteq> \<infinity>" if "x \<in> A" for x
hoelzl@63225
  1973
  ultimately show "continuous_on A (\<lambda>z. max 0 (f z - g z))"
hoelzl@63225
  1974
    by (intro continuous_on_max continuous_on_const continuous_on_diff_ereal) auto
hoelzl@63225
  1975
qed
hoelzl@63225
  1976
immler@67727
  1977
lemma tendsto_diff_ennreal:
hoelzl@63225
  1978
  "(f \<longlongrightarrow> x) F \<Longrightarrow> (g \<longlongrightarrow> y) F \<Longrightarrow> x \<noteq> top \<Longrightarrow> y \<noteq> top \<Longrightarrow> ((\<lambda>z. f z - g z::ennreal) \<longlongrightarrow> x - y) F"
hoelzl@63225
  1979
  using continuous_on_tendsto_compose[where f="\<lambda>x. fst x - snd x::ennreal" and s="{(x, y). x \<noteq> top \<and> y \<noteq> top}" and g="\<lambda>x. (f x, g x)" and l="(x, y)" and F="F",
hoelzl@63225
  1980
    OF continuous_on_diff_ennreal]
hoelzl@63225
  1981
  by (auto simp: tendsto_Pair eventually_conj_iff less_top order_tendstoD continuous_on_fst continuous_on_snd continuous_on_id)
hoelzl@63225
  1982
immler@67727
  1983
declare lim_real_of_ereal [tendsto_intros]
immler@67727
  1984
immler@67727
  1985
lemma tendsto_enn2real [tendsto_intros]:
immler@67727
  1986
  assumes "(u \<longlongrightarrow> ennreal l) F" "l \<ge> 0"
immler@67727
  1987
  shows "((\<lambda>n. enn2real (u n)) \<longlongrightarrow> l) F"
immler@67727
  1988
  unfolding enn2real_def
immler@67727
  1989
  apply (intro tendsto_intros)
immler@67727
  1990
  apply (subst enn2ereal_ennreal[symmetric])
immler@67727
  1991
  by (intro tendsto_intros assms)+
immler@67727
  1992
hoelzl@62975
  1993
end