src/HOL/Probability/Positive_Extended_Real.thy
author wenzelm
Wed Dec 29 17:34:41 2010 +0100 (2010-12-29)
changeset 41413 64cd30d6b0b8
parent 41096 843c40bbc379
child 41544 c3b977fee8a3
permissions -rw-r--r--
explicit file specifications -- avoid secondary load path;
hoelzl@38656
     1
(* Author: Johannes Hoelzl, TU Muenchen *)
hoelzl@38656
     2
hoelzl@38656
     3
header {* A type for positive real numbers with infinity *}
hoelzl@38656
     4
hoelzl@41023
     5
theory Positive_Extended_Real
wenzelm@41413
     6
  imports Complex_Main "~~/src/HOL/Library/Nat_Bijection" Multivariate_Analysis
hoelzl@38656
     7
begin
hoelzl@38656
     8
hoelzl@38705
     9
lemma (in complete_lattice) Sup_start:
hoelzl@38705
    10
  assumes *: "\<And>x. f x \<le> f 0"
hoelzl@38705
    11
  shows "(SUP n. f n) = f 0"
hoelzl@38705
    12
proof (rule antisym)
hoelzl@38705
    13
  show "f 0 \<le> (SUP n. f n)" by (rule le_SUPI) auto
hoelzl@38705
    14
  show "(SUP n. f n) \<le> f 0" by (rule SUP_leI[OF *])
hoelzl@38656
    15
qed
hoelzl@38656
    16
hoelzl@38705
    17
lemma (in complete_lattice) Inf_start:
hoelzl@38705
    18
  assumes *: "\<And>x. f 0 \<le> f x"
hoelzl@38705
    19
  shows "(INF n. f n) = f 0"
hoelzl@38705
    20
proof (rule antisym)
hoelzl@38705
    21
  show "(INF n. f n) \<le> f 0" by (rule INF_leI) simp
hoelzl@38705
    22
  show "f 0 \<le> (INF n. f n)" by (rule le_INFI[OF *])
hoelzl@38656
    23
qed
hoelzl@38656
    24
hoelzl@38656
    25
lemma (in complete_lattice) Sup_mono_offset:
hoelzl@38656
    26
  fixes f :: "'b :: {ordered_ab_semigroup_add,monoid_add} \<Rightarrow> 'a"
hoelzl@38656
    27
  assumes *: "\<And>x y. x \<le> y \<Longrightarrow> f x \<le> f y" and "0 \<le> k"
hoelzl@38656
    28
  shows "(SUP n . f (k + n)) = (SUP n. f n)"
hoelzl@38656
    29
proof (rule antisym)
hoelzl@38656
    30
  show "(SUP n. f (k + n)) \<le> (SUP n. f n)"
hoelzl@38656
    31
    by (auto intro!: Sup_mono simp: SUPR_def)
hoelzl@38656
    32
  { fix n :: 'b
hoelzl@38656
    33
    have "0 + n \<le> k + n" using `0 \<le> k` by (rule add_right_mono)
hoelzl@38656
    34
    with * have "f n \<le> f (k + n)" by simp }
hoelzl@38656
    35
  thus "(SUP n. f n) \<le> (SUP n. f (k + n))"
hoelzl@38656
    36
    by (auto intro!: Sup_mono exI simp: SUPR_def)
hoelzl@38656
    37
qed
hoelzl@38656
    38
hoelzl@38656
    39
lemma (in complete_lattice) Sup_mono_offset_Suc:
hoelzl@38656
    40
  assumes *: "\<And>x. f x \<le> f (Suc x)"
hoelzl@38656
    41
  shows "(SUP n . f (Suc n)) = (SUP n. f n)"
hoelzl@38656
    42
  unfolding Suc_eq_plus1
hoelzl@38656
    43
  apply (subst add_commute)
hoelzl@38656
    44
  apply (rule Sup_mono_offset)
hoelzl@38656
    45
  by (auto intro!: order.lift_Suc_mono_le[of "op \<le>" "op <" f, OF _ *]) default
hoelzl@38656
    46
hoelzl@38705
    47
lemma (in complete_lattice) Inf_mono_offset:
hoelzl@38705
    48
  fixes f :: "'b :: {ordered_ab_semigroup_add,monoid_add} \<Rightarrow> 'a"
hoelzl@38705
    49
  assumes *: "\<And>x y. x \<le> y \<Longrightarrow> f y \<le> f x" and "0 \<le> k"
hoelzl@38705
    50
  shows "(INF n . f (k + n)) = (INF n. f n)"
hoelzl@38656
    51
proof (rule antisym)
hoelzl@38705
    52
  show "(INF n. f n) \<le> (INF n. f (k + n))"
hoelzl@38705
    53
    by (auto intro!: Inf_mono simp: INFI_def)
hoelzl@38705
    54
  { fix n :: 'b
hoelzl@38705
    55
    have "0 + n \<le> k + n" using `0 \<le> k` by (rule add_right_mono)
hoelzl@38705
    56
    with * have "f (k + n) \<le> f n" by simp }
hoelzl@38705
    57
  thus "(INF n. f (k + n)) \<le> (INF n. f n)"
hoelzl@38705
    58
    by (auto intro!: Inf_mono exI simp: INFI_def)
hoelzl@38656
    59
qed
hoelzl@38656
    60
hoelzl@38656
    61
lemma (in complete_lattice) isotone_converge:
hoelzl@38656
    62
  fixes f :: "nat \<Rightarrow> 'a" assumes "\<And>x y. x \<le> y \<Longrightarrow> f x \<le> f y "
hoelzl@38656
    63
  shows "(INF n. SUP m. f (n + m)) = (SUP n. INF m. f (n + m))"
hoelzl@38656
    64
proof -
hoelzl@38656
    65
  have "\<And>n. (SUP m. f (n + m)) = (SUP n. f n)"
hoelzl@38656
    66
    apply (rule Sup_mono_offset)
hoelzl@38656
    67
    apply (rule assms)
hoelzl@38656
    68
    by simp_all
hoelzl@38656
    69
  moreover
hoelzl@38656
    70
  { fix n have "(INF m. f (n + m)) = f n"
hoelzl@38656
    71
      using Inf_start[of "\<lambda>m. f (n + m)"] assms by simp }
hoelzl@38656
    72
  ultimately show ?thesis by simp
hoelzl@38656
    73
qed
hoelzl@38656
    74
hoelzl@38656
    75
lemma (in complete_lattice) antitone_converges:
hoelzl@38656
    76
  fixes f :: "nat \<Rightarrow> 'a" assumes "\<And>x y. x \<le> y \<Longrightarrow> f y \<le> f x"
hoelzl@38656
    77
  shows "(INF n. SUP m. f (n + m)) = (SUP n. INF m. f (n + m))"
hoelzl@38656
    78
proof -
hoelzl@38656
    79
  have "\<And>n. (INF m. f (n + m)) = (INF n. f n)"
hoelzl@38656
    80
    apply (rule Inf_mono_offset)
hoelzl@38656
    81
    apply (rule assms)
hoelzl@38656
    82
    by simp_all
hoelzl@38656
    83
  moreover
hoelzl@38656
    84
  { fix n have "(SUP m. f (n + m)) = f n"
hoelzl@38656
    85
      using Sup_start[of "\<lambda>m. f (n + m)"] assms by simp }
hoelzl@38656
    86
  ultimately show ?thesis by simp
hoelzl@38656
    87
qed
hoelzl@38656
    88
hoelzl@40872
    89
lemma (in complete_lattice) lim_INF_le_lim_SUP:
hoelzl@40872
    90
  fixes f :: "nat \<Rightarrow> 'a"
hoelzl@40872
    91
  shows "(SUP n. INF m. f (n + m)) \<le> (INF n. SUP m. f (n + m))"
hoelzl@40872
    92
proof (rule SUP_leI, rule le_INFI)
hoelzl@40872
    93
  fix i j show "(INF m. f (i + m)) \<le> (SUP m. f (j + m))"
hoelzl@40872
    94
  proof (cases rule: le_cases)
hoelzl@40872
    95
    assume "i \<le> j"
hoelzl@40872
    96
    have "(INF m. f (i + m)) \<le> f (i + (j - i))" by (rule INF_leI) simp
hoelzl@40872
    97
    also have "\<dots> = f (j + 0)" using `i \<le> j` by auto
hoelzl@40872
    98
    also have "\<dots> \<le> (SUP m. f (j + m))" by (rule le_SUPI) simp
hoelzl@40872
    99
    finally show ?thesis .
hoelzl@40872
   100
  next
hoelzl@40872
   101
    assume "j \<le> i"
hoelzl@40872
   102
    have "(INF m. f (i + m)) \<le> f (i + 0)" by (rule INF_leI) simp
hoelzl@40872
   103
    also have "\<dots> = f (j + (i - j))" using `j \<le> i` by auto
hoelzl@40872
   104
    also have "\<dots> \<le> (SUP m. f (j + m))" by (rule le_SUPI) simp
hoelzl@40872
   105
    finally show ?thesis .
hoelzl@40872
   106
  qed
hoelzl@40872
   107
qed
hoelzl@40872
   108
hoelzl@38656
   109
text {*
hoelzl@38656
   110
hoelzl@38656
   111
We introduce the the positive real numbers as needed for measure theory.
hoelzl@38656
   112
hoelzl@38656
   113
*}
hoelzl@38656
   114
hoelzl@41023
   115
typedef pextreal = "(Some ` {0::real..}) \<union> {None}"
hoelzl@38656
   116
  by (rule exI[of _ None]) simp
hoelzl@38656
   117
hoelzl@41023
   118
subsection "Introduce @{typ pextreal} similar to a datatype"
hoelzl@38656
   119
hoelzl@41023
   120
definition "Real x = Abs_pextreal (Some (sup 0 x))"
hoelzl@41023
   121
definition "\<omega> = Abs_pextreal None"
hoelzl@38656
   122
hoelzl@41023
   123
definition "pextreal_case f i x = (if x = \<omega> then i else f (THE r. 0 \<le> r \<and> x = Real r))"
hoelzl@38656
   124
hoelzl@41023
   125
definition "of_pextreal = pextreal_case (\<lambda>x. x) 0"
hoelzl@38656
   126
hoelzl@38656
   127
defs (overloaded)
hoelzl@41023
   128
  real_of_pextreal_def [code_unfold]: "real == of_pextreal"
hoelzl@38656
   129
hoelzl@41023
   130
lemma pextreal_Some[simp]: "0 \<le> x \<Longrightarrow> Some x \<in> pextreal"
hoelzl@41023
   131
  unfolding pextreal_def by simp
hoelzl@38656
   132
hoelzl@41023
   133
lemma pextreal_Some_sup[simp]: "Some (sup 0 x) \<in> pextreal"
hoelzl@38656
   134
  by (simp add: sup_ge1)
hoelzl@38656
   135
hoelzl@41023
   136
lemma pextreal_None[simp]: "None \<in> pextreal"
hoelzl@41023
   137
  unfolding pextreal_def by simp
hoelzl@38656
   138
hoelzl@38656
   139
lemma Real_inj[simp]:
hoelzl@38656
   140
  assumes  "0 \<le> x" and "0 \<le> y"
hoelzl@38656
   141
  shows "Real x = Real y \<longleftrightarrow> x = y"
hoelzl@38656
   142
  unfolding Real_def assms[THEN sup_absorb2]
hoelzl@41023
   143
  using assms by (simp add: Abs_pextreal_inject)
hoelzl@38656
   144
hoelzl@38656
   145
lemma Real_neq_\<omega>[simp]:
hoelzl@38656
   146
  "Real x = \<omega> \<longleftrightarrow> False"
hoelzl@38656
   147
  "\<omega> = Real x \<longleftrightarrow> False"
hoelzl@41023
   148
  by (simp_all add: Abs_pextreal_inject \<omega>_def Real_def)
hoelzl@38656
   149
hoelzl@38656
   150
lemma Real_neg: "x < 0 \<Longrightarrow> Real x = Real 0"
hoelzl@41023
   151
  unfolding Real_def by (auto simp add: Abs_pextreal_inject intro!: sup_absorb1)
hoelzl@38656
   152
hoelzl@41023
   153
lemma pextreal_cases[case_names preal infinite, cases type: pextreal]:
hoelzl@38656
   154
  assumes preal: "\<And>r. x = Real r \<Longrightarrow> 0 \<le> r \<Longrightarrow> P" and inf: "x = \<omega> \<Longrightarrow> P"
hoelzl@38656
   155
  shows P
hoelzl@41023
   156
proof (cases x rule: pextreal.Abs_pextreal_cases)
hoelzl@41023
   157
  case (Abs_pextreal y)
hoelzl@38656
   158
  hence "y = None \<or> (\<exists>x \<ge> 0. y = Some x)"
hoelzl@41023
   159
    unfolding pextreal_def by auto
hoelzl@38656
   160
  thus P
hoelzl@38656
   161
  proof (rule disjE)
hoelzl@38656
   162
    assume "\<exists>x\<ge>0. y = Some x" then guess x ..
hoelzl@41023
   163
    thus P by (simp add: preal[of x] Real_def Abs_pextreal(1) sup_absorb2)
hoelzl@41023
   164
  qed (simp add: \<omega>_def Abs_pextreal(1) inf)
hoelzl@38656
   165
qed
hoelzl@38656
   166
hoelzl@41023
   167
lemma pextreal_case_\<omega>[simp]: "pextreal_case f i \<omega> = i"
hoelzl@41023
   168
  unfolding pextreal_case_def by simp
hoelzl@38656
   169
hoelzl@41023
   170
lemma pextreal_case_Real[simp]: "pextreal_case f i (Real x) = (if 0 \<le> x then f x else f 0)"
hoelzl@38656
   171
proof (cases "0 \<le> x")
hoelzl@41023
   172
  case True thus ?thesis unfolding pextreal_case_def by (auto intro: theI2)
hoelzl@38656
   173
next
hoelzl@38656
   174
  case False
hoelzl@38656
   175
  moreover have "(THE r. 0 \<le> r \<and> Real 0 = Real r) = 0"
hoelzl@38656
   176
    by (auto intro!: the_equality)
hoelzl@41023
   177
  ultimately show ?thesis unfolding pextreal_case_def by (simp add: Real_neg)
hoelzl@38656
   178
qed
hoelzl@38656
   179
hoelzl@41023
   180
lemma pextreal_case_cancel[simp]: "pextreal_case (\<lambda>c. i) i x = i"
hoelzl@38656
   181
  by (cases x) simp_all
hoelzl@38656
   182
hoelzl@41023
   183
lemma pextreal_case_split:
hoelzl@41023
   184
  "P (pextreal_case f i x) = ((x = \<omega> \<longrightarrow> P i) \<and> (\<forall>r\<ge>0. x = Real r \<longrightarrow> P (f r)))"
hoelzl@38656
   185
  by (cases x) simp_all
hoelzl@38656
   186
hoelzl@41023
   187
lemma pextreal_case_split_asm:
hoelzl@41023
   188
  "P (pextreal_case f i x) = (\<not> (x = \<omega> \<and> \<not> P i \<or> (\<exists>r. r \<ge> 0 \<and> x = Real r \<and> \<not> P (f r))))"
hoelzl@38656
   189
  by (cases x) auto
hoelzl@38656
   190
hoelzl@41023
   191
lemma pextreal_case_cong[cong]:
hoelzl@38656
   192
  assumes eq: "x = x'" "i = i'" and cong: "\<And>r. 0 \<le> r \<Longrightarrow> f r = f' r"
hoelzl@41023
   193
  shows "pextreal_case f i x = pextreal_case f' i' x'"
hoelzl@38656
   194
  unfolding eq using cong by (cases x') simp_all
hoelzl@38656
   195
hoelzl@38656
   196
lemma real_Real[simp]: "real (Real x) = (if 0 \<le> x then x else 0)"
hoelzl@41023
   197
  unfolding real_of_pextreal_def of_pextreal_def by simp
hoelzl@38656
   198
hoelzl@38656
   199
lemma Real_real_image:
hoelzl@38656
   200
  assumes "\<omega> \<notin> A" shows "Real ` real ` A = A"
hoelzl@38656
   201
proof safe
hoelzl@38656
   202
  fix x assume "x \<in> A"
hoelzl@38656
   203
  hence *: "x = Real (real x)"
hoelzl@38656
   204
    using `\<omega> \<notin> A` by (cases x) auto
hoelzl@38656
   205
  show "x \<in> Real ` real ` A"
hoelzl@38656
   206
    using `x \<in> A` by (subst *) (auto intro!: imageI)
hoelzl@38656
   207
next
hoelzl@38656
   208
  fix x assume "x \<in> A"
hoelzl@38656
   209
  thus "Real (real x) \<in> A"
hoelzl@38656
   210
    using `\<omega> \<notin> A` by (cases x) auto
hoelzl@38656
   211
qed
hoelzl@38656
   212
hoelzl@41023
   213
lemma real_pextreal_nonneg[simp, intro]: "0 \<le> real (x :: pextreal)"
hoelzl@41023
   214
  unfolding real_of_pextreal_def of_pextreal_def
hoelzl@38656
   215
  by (cases x) auto
hoelzl@38656
   216
hoelzl@38656
   217
lemma real_\<omega>[simp]: "real \<omega> = 0"
hoelzl@41023
   218
  unfolding real_of_pextreal_def of_pextreal_def by simp
hoelzl@38656
   219
hoelzl@41023
   220
lemma pextreal_noteq_omega_Ex: "X \<noteq> \<omega> \<longleftrightarrow> (\<exists>r\<ge>0. X = Real r)" by (cases X) auto
hoelzl@38656
   221
hoelzl@41023
   222
subsection "@{typ pextreal} is a monoid for addition"
hoelzl@38656
   223
hoelzl@41023
   224
instantiation pextreal :: comm_monoid_add
hoelzl@38656
   225
begin
hoelzl@38656
   226
hoelzl@38656
   227
definition "0 = Real 0"
hoelzl@41023
   228
definition "x + y = pextreal_case (\<lambda>r. pextreal_case (\<lambda>p. Real (r + p)) \<omega> y) \<omega> x"
hoelzl@38656
   229
hoelzl@41023
   230
lemma pextreal_plus[simp]:
hoelzl@38656
   231
  "Real r + Real p = (if 0 \<le> r then if 0 \<le> p then Real (r + p) else Real r else Real p)"
hoelzl@38656
   232
  "x + 0 = x"
hoelzl@38656
   233
  "0 + x = x"
hoelzl@38656
   234
  "x + \<omega> = \<omega>"
hoelzl@38656
   235
  "\<omega> + x = \<omega>"
hoelzl@41023
   236
  by (simp_all add: plus_pextreal_def Real_neg zero_pextreal_def split: pextreal_case_split)
hoelzl@38656
   237
hoelzl@38656
   238
lemma \<omega>_neq_0[simp]:
hoelzl@38656
   239
  "\<omega> = 0 \<longleftrightarrow> False"
hoelzl@38656
   240
  "0 = \<omega> \<longleftrightarrow> False"
hoelzl@41023
   241
  by (simp_all add: zero_pextreal_def)
hoelzl@38656
   242
hoelzl@38656
   243
lemma Real_eq_0[simp]:
hoelzl@38656
   244
  "Real r = 0 \<longleftrightarrow> r \<le> 0"
hoelzl@38656
   245
  "0 = Real r \<longleftrightarrow> r \<le> 0"
hoelzl@41023
   246
  by (auto simp add: Abs_pextreal_inject zero_pextreal_def Real_def sup_real_def)
hoelzl@38656
   247
hoelzl@41023
   248
lemma Real_0[simp]: "Real 0 = 0" by (simp add: zero_pextreal_def)
hoelzl@38656
   249
hoelzl@38656
   250
instance
hoelzl@38656
   251
proof
hoelzl@41023
   252
  fix a :: pextreal
hoelzl@38656
   253
  show "0 + a = a" by (cases a) simp_all
hoelzl@38656
   254
hoelzl@38656
   255
  fix b show "a + b = b + a"
hoelzl@38656
   256
    by (cases a, cases b) simp_all
hoelzl@38656
   257
hoelzl@38656
   258
  fix c show "a + b + c = a + (b + c)"
hoelzl@38656
   259
    by (cases a, cases b, cases c) simp_all
hoelzl@38656
   260
qed
hoelzl@38656
   261
end
hoelzl@38656
   262
hoelzl@41096
   263
lemma Real_minus_abs[simp]: "Real (- \<bar>x\<bar>) = 0"
hoelzl@41096
   264
  by simp
hoelzl@41096
   265
hoelzl@41023
   266
lemma pextreal_plus_eq_\<omega>[simp]: "(a :: pextreal) + b = \<omega> \<longleftrightarrow> a = \<omega> \<or> b = \<omega>"
hoelzl@38656
   267
  by (cases a, cases b) auto
hoelzl@38656
   268
hoelzl@41023
   269
lemma pextreal_add_cancel_left:
hoelzl@38656
   270
  "a + b = a + c \<longleftrightarrow> (a = \<omega> \<or> b = c)"
hoelzl@38656
   271
  by (cases a, cases b, cases c, simp_all, cases c, simp_all)
hoelzl@38656
   272
hoelzl@41023
   273
lemma pextreal_add_cancel_right:
hoelzl@38656
   274
  "b + a = c + a \<longleftrightarrow> (a = \<omega> \<or> b = c)"
hoelzl@38656
   275
  by (cases a, cases b, cases c, simp_all, cases c, simp_all)
hoelzl@38656
   276
hoelzl@38656
   277
lemma Real_eq_Real:
hoelzl@38656
   278
  "Real a = Real b \<longleftrightarrow> (a = b \<or> (a \<le> 0 \<and> b \<le> 0))"
hoelzl@38656
   279
proof (cases "a \<le> 0 \<or> b \<le> 0")
hoelzl@38656
   280
  case False with Real_inj[of a b] show ?thesis by auto
hoelzl@38656
   281
next
hoelzl@38656
   282
  case True
hoelzl@38656
   283
  thus ?thesis
hoelzl@38656
   284
  proof
hoelzl@38656
   285
    assume "a \<le> 0"
hoelzl@38656
   286
    hence *: "Real a = 0" by simp
hoelzl@38656
   287
    show ?thesis using `a \<le> 0` unfolding * by auto
hoelzl@38656
   288
  next
hoelzl@38656
   289
    assume "b \<le> 0"
hoelzl@38656
   290
    hence *: "Real b = 0" by simp
hoelzl@38656
   291
    show ?thesis using `b \<le> 0` unfolding * by auto
hoelzl@38656
   292
  qed
hoelzl@38656
   293
qed
hoelzl@38656
   294
hoelzl@41023
   295
lemma real_pextreal_0[simp]: "real (0 :: pextreal) = 0"
hoelzl@41023
   296
  unfolding zero_pextreal_def real_Real by simp
hoelzl@38656
   297
hoelzl@41023
   298
lemma real_of_pextreal_eq_0: "real X = 0 \<longleftrightarrow> (X = 0 \<or> X = \<omega>)"
hoelzl@38656
   299
  by (cases X) auto
hoelzl@38656
   300
hoelzl@41023
   301
lemma real_of_pextreal_eq: "real X = real Y \<longleftrightarrow>
hoelzl@38656
   302
    (X = Y \<or> (X = 0 \<and> Y = \<omega>) \<or> (Y = 0 \<and> X = \<omega>))"
hoelzl@41023
   303
  by (cases X, cases Y) (auto simp add: real_of_pextreal_eq_0)
hoelzl@38656
   304
hoelzl@41023
   305
lemma real_of_pextreal_add: "real X + real Y =
hoelzl@38656
   306
    (if X = \<omega> then real Y else if Y = \<omega> then real X else real (X + Y))"
hoelzl@41023
   307
  by (auto simp: pextreal_noteq_omega_Ex)
hoelzl@38656
   308
hoelzl@41023
   309
subsection "@{typ pextreal} is a monoid for multiplication"
hoelzl@38656
   310
hoelzl@41023
   311
instantiation pextreal :: comm_monoid_mult
hoelzl@38656
   312
begin
hoelzl@38656
   313
hoelzl@38656
   314
definition "1 = Real 1"
hoelzl@38656
   315
definition "x * y = (if x = 0 \<or> y = 0 then 0 else
hoelzl@41023
   316
  pextreal_case (\<lambda>r. pextreal_case (\<lambda>p. Real (r * p)) \<omega> y) \<omega> x)"
hoelzl@38656
   317
hoelzl@41023
   318
lemma pextreal_times[simp]:
hoelzl@38656
   319
  "Real r * Real p = (if 0 \<le> r \<and> 0 \<le> p then Real (r * p) else 0)"
hoelzl@38656
   320
  "\<omega> * x = (if x = 0 then 0 else \<omega>)"
hoelzl@38656
   321
  "x * \<omega> = (if x = 0 then 0 else \<omega>)"
hoelzl@38656
   322
  "0 * x = 0"
hoelzl@38656
   323
  "x * 0 = 0"
hoelzl@38656
   324
  "1 = \<omega> \<longleftrightarrow> False"
hoelzl@38656
   325
  "\<omega> = 1 \<longleftrightarrow> False"
hoelzl@41023
   326
  by (auto simp add: times_pextreal_def one_pextreal_def)
hoelzl@38656
   327
hoelzl@41023
   328
lemma pextreal_one_mult[simp]:
hoelzl@38656
   329
  "Real x + 1 = (if 0 \<le> x then Real (x + 1) else 1)"
hoelzl@38656
   330
  "1 + Real x = (if 0 \<le> x then Real (1 + x) else 1)"
hoelzl@41023
   331
  unfolding one_pextreal_def by simp_all
hoelzl@38656
   332
hoelzl@38656
   333
instance
hoelzl@38656
   334
proof
hoelzl@41023
   335
  fix a :: pextreal show "1 * a = a"
hoelzl@41023
   336
    by (cases a) (simp_all add: one_pextreal_def)
hoelzl@38656
   337
hoelzl@38656
   338
  fix b show "a * b = b * a"
hoelzl@38656
   339
    by (cases a, cases b) (simp_all add: mult_nonneg_nonneg)
hoelzl@38656
   340
hoelzl@38656
   341
  fix c show "a * b * c = a * (b * c)"
hoelzl@38656
   342
    apply (cases a, cases b, cases c)
hoelzl@38656
   343
    apply (simp_all add: mult_nonneg_nonneg not_le mult_pos_pos)
hoelzl@38656
   344
    apply (cases b, cases c)
hoelzl@38656
   345
    apply (simp_all add: mult_nonneg_nonneg not_le mult_pos_pos)
hoelzl@38656
   346
    done
hoelzl@38656
   347
qed
hoelzl@38656
   348
end
hoelzl@38656
   349
hoelzl@41023
   350
lemma pextreal_mult_cancel_left:
hoelzl@38656
   351
  "a * b = a * c \<longleftrightarrow> (a = 0 \<or> b = c \<or> (a = \<omega> \<and> b \<noteq> 0 \<and> c \<noteq> 0))"
hoelzl@38656
   352
  by (cases a, cases b, cases c, auto simp: Real_eq_Real mult_le_0_iff, cases c, auto)
hoelzl@38656
   353
hoelzl@41023
   354
lemma pextreal_mult_cancel_right:
hoelzl@38656
   355
  "b * a = c * a \<longleftrightarrow> (a = 0 \<or> b = c \<or> (a = \<omega> \<and> b \<noteq> 0 \<and> c \<noteq> 0))"
hoelzl@38656
   356
  by (cases a, cases b, cases c, auto simp: Real_eq_Real mult_le_0_iff, cases c, auto)
hoelzl@38656
   357
hoelzl@41023
   358
lemma Real_1[simp]: "Real 1 = 1" by (simp add: one_pextreal_def)
hoelzl@38656
   359
hoelzl@41023
   360
lemma real_pextreal_1[simp]: "real (1 :: pextreal) = 1"
hoelzl@41023
   361
  unfolding one_pextreal_def real_Real by simp
hoelzl@38656
   362
hoelzl@41023
   363
lemma real_of_pextreal_mult: "real X * real Y = real (X * Y :: pextreal)"
hoelzl@38656
   364
  by (cases X, cases Y) (auto simp: zero_le_mult_iff)
hoelzl@38656
   365
hoelzl@40874
   366
lemma Real_mult_nonneg: assumes "x \<ge> 0" "y \<ge> 0"
hoelzl@40874
   367
  shows "Real (x * y) = Real x * Real y" using assms by auto
hoelzl@40874
   368
hoelzl@40874
   369
lemma Real_setprod: assumes "\<forall>x\<in>A. f x \<ge> 0" shows "Real (setprod f A) = setprod (\<lambda>x. Real (f x)) A"
hoelzl@40874
   370
proof(cases "finite A")
hoelzl@40874
   371
  case True thus ?thesis using assms
hoelzl@40874
   372
  proof(induct A) case (insert x A)
hoelzl@40874
   373
    have "0 \<le> setprod f A" apply(rule setprod_nonneg) using insert by auto
hoelzl@40874
   374
    thus ?case unfolding setprod_insert[OF insert(1-2)] apply-
hoelzl@40874
   375
      apply(subst Real_mult_nonneg) prefer 3 apply(subst insert(3)[THEN sym])
hoelzl@40874
   376
      using insert by auto
hoelzl@40874
   377
  qed auto
hoelzl@40874
   378
qed auto
hoelzl@40874
   379
hoelzl@41023
   380
subsection "@{typ pextreal} is a linear order"
hoelzl@38656
   381
hoelzl@41023
   382
instantiation pextreal :: linorder
hoelzl@38656
   383
begin
hoelzl@38656
   384
hoelzl@41023
   385
definition "x < y \<longleftrightarrow> pextreal_case (\<lambda>i. pextreal_case (\<lambda>j. i < j) True y) False x"
hoelzl@41023
   386
definition "x \<le> y \<longleftrightarrow> pextreal_case (\<lambda>j. pextreal_case (\<lambda>i. i \<le> j) False x) True y"
hoelzl@38656
   387
hoelzl@41023
   388
lemma pextreal_less[simp]:
hoelzl@38656
   389
  "Real r < \<omega>"
hoelzl@38656
   390
  "Real r < Real p \<longleftrightarrow> (if 0 \<le> r \<and> 0 \<le> p then r < p else 0 < p)"
hoelzl@38656
   391
  "\<omega> < x \<longleftrightarrow> False"
hoelzl@38656
   392
  "0 < \<omega>"
hoelzl@38656
   393
  "0 < Real r \<longleftrightarrow> 0 < r"
hoelzl@38656
   394
  "x < 0 \<longleftrightarrow> False"
hoelzl@41023
   395
  "0 < (1::pextreal)"
hoelzl@41023
   396
  by (simp_all add: less_pextreal_def zero_pextreal_def one_pextreal_def del: Real_0 Real_1)
hoelzl@38656
   397
hoelzl@41023
   398
lemma pextreal_less_eq[simp]:
hoelzl@38656
   399
  "x \<le> \<omega>"
hoelzl@38656
   400
  "Real r \<le> Real p \<longleftrightarrow> (if 0 \<le> r \<and> 0 \<le> p then r \<le> p else r \<le> 0)"
hoelzl@38656
   401
  "0 \<le> x"
hoelzl@41023
   402
  by (simp_all add: less_eq_pextreal_def zero_pextreal_def del: Real_0)
hoelzl@38656
   403
hoelzl@41023
   404
lemma pextreal_\<omega>_less_eq[simp]:
hoelzl@38656
   405
  "\<omega> \<le> x \<longleftrightarrow> x = \<omega>"
hoelzl@41023
   406
  by (cases x) (simp_all add: not_le less_eq_pextreal_def)
hoelzl@38656
   407
hoelzl@41023
   408
lemma pextreal_less_eq_zero[simp]:
hoelzl@41023
   409
  "(x::pextreal) \<le> 0 \<longleftrightarrow> x = 0"
hoelzl@41023
   410
  by (cases x) (simp_all add: zero_pextreal_def del: Real_0)
hoelzl@38656
   411
hoelzl@38656
   412
instance
hoelzl@38656
   413
proof
hoelzl@41023
   414
  fix x :: pextreal
hoelzl@38656
   415
  show "x \<le> x" by (cases x) simp_all
hoelzl@38656
   416
  fix y
hoelzl@38656
   417
  show "(x < y) = (x \<le> y \<and> \<not> y \<le> x)"
hoelzl@38656
   418
    by (cases x, cases y) auto
hoelzl@38656
   419
  show "x \<le> y \<or> y \<le> x "
hoelzl@38656
   420
    by (cases x, cases y) auto
hoelzl@38656
   421
  { assume "x \<le> y" "y \<le> x" thus "x = y"
hoelzl@38656
   422
      by (cases x, cases y) auto }
hoelzl@38656
   423
  { fix z assume "x \<le> y" "y \<le> z"
hoelzl@38656
   424
    thus "x \<le> z" by (cases x, cases y, cases z) auto }
hoelzl@38656
   425
qed
hoelzl@38656
   426
end
hoelzl@38656
   427
hoelzl@41023
   428
lemma pextreal_zero_lessI[intro]:
hoelzl@41023
   429
  "(a :: pextreal) \<noteq> 0 \<Longrightarrow> 0 < a"
hoelzl@38656
   430
  by (cases a) auto
hoelzl@38656
   431
hoelzl@41023
   432
lemma pextreal_less_omegaI[intro, simp]:
hoelzl@38656
   433
  "a \<noteq> \<omega> \<Longrightarrow> a < \<omega>"
hoelzl@38656
   434
  by (cases a) auto
hoelzl@38656
   435
hoelzl@41023
   436
lemma pextreal_plus_eq_0[simp]: "(a :: pextreal) + b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
hoelzl@38656
   437
  by (cases a, cases b) auto
hoelzl@38656
   438
hoelzl@41023
   439
lemma pextreal_le_add1[simp, intro]: "n \<le> n + (m::pextreal)"
hoelzl@38656
   440
  by (cases n, cases m) simp_all
hoelzl@38656
   441
hoelzl@41023
   442
lemma pextreal_le_add2: "(n::pextreal) + m \<le> k \<Longrightarrow> m \<le> k"
hoelzl@38656
   443
  by (cases n, cases m, cases k) simp_all
hoelzl@38656
   444
hoelzl@41023
   445
lemma pextreal_le_add3: "(n::pextreal) + m \<le> k \<Longrightarrow> n \<le> k"
hoelzl@38656
   446
  by (cases n, cases m, cases k) simp_all
hoelzl@38656
   447
hoelzl@41023
   448
lemma pextreal_less_\<omega>: "x < \<omega> \<longleftrightarrow> x \<noteq> \<omega>"
hoelzl@38656
   449
  by (cases x) auto
hoelzl@38656
   450
hoelzl@41023
   451
lemma pextreal_0_less_mult_iff[simp]:
hoelzl@41023
   452
  fixes x y :: pextreal shows "0 < x * y \<longleftrightarrow> 0 < x \<and> 0 < y"
hoelzl@39092
   453
  by (cases x, cases y) (auto simp: zero_less_mult_iff)
hoelzl@39092
   454
hoelzl@41023
   455
lemma pextreal_ord_one[simp]:
hoelzl@40859
   456
  "Real p < 1 \<longleftrightarrow> p < 1"
hoelzl@40859
   457
  "Real p \<le> 1 \<longleftrightarrow> p \<le> 1"
hoelzl@40859
   458
  "1 < Real p \<longleftrightarrow> 1 < p"
hoelzl@40859
   459
  "1 \<le> Real p \<longleftrightarrow> 1 \<le> p"
hoelzl@41023
   460
  by (simp_all add: one_pextreal_def del: Real_1)
hoelzl@40859
   461
hoelzl@41023
   462
subsection {* @{text "x - y"} on @{typ pextreal} *}
hoelzl@38656
   463
hoelzl@41023
   464
instantiation pextreal :: minus
hoelzl@38656
   465
begin
hoelzl@41023
   466
definition "x - y = (if y < x then THE d. x = y + d else 0 :: pextreal)"
hoelzl@38656
   467
hoelzl@41023
   468
lemma minus_pextreal_eq:
hoelzl@41023
   469
  "(x - y = (z :: pextreal)) \<longleftrightarrow> (if y < x then x = y + z else z = 0)"
hoelzl@38656
   470
  (is "?diff \<longleftrightarrow> ?if")
hoelzl@38656
   471
proof
hoelzl@38656
   472
  assume ?diff
hoelzl@38656
   473
  thus ?if
hoelzl@38656
   474
  proof (cases "y < x")
hoelzl@38656
   475
    case True
hoelzl@38656
   476
    then obtain p where p: "y = Real p" "0 \<le> p" by (cases y) auto
hoelzl@38656
   477
hoelzl@41023
   478
    show ?thesis unfolding `?diff`[symmetric] if_P[OF True] minus_pextreal_def
hoelzl@38656
   479
    proof (rule theI2[where Q="\<lambda>d. x = y + d"])
hoelzl@41023
   480
      show "x = y + pextreal_case (\<lambda>r. Real (r - real y)) \<omega> x" (is "x = y + ?d")
hoelzl@38656
   481
        using `y < x` p by (cases x) simp_all
hoelzl@38656
   482
hoelzl@38656
   483
      fix d assume "x = y + d"
hoelzl@38656
   484
      thus "d = ?d" using `y < x` p by (cases d, cases x) simp_all
hoelzl@38656
   485
    qed simp
hoelzl@41023
   486
  qed (simp add: minus_pextreal_def)
hoelzl@38656
   487
next
hoelzl@38656
   488
  assume ?if
hoelzl@38656
   489
  thus ?diff
hoelzl@38656
   490
  proof (cases "y < x")
hoelzl@38656
   491
    case True
hoelzl@38656
   492
    then obtain p where p: "y = Real p" "0 \<le> p" by (cases y) auto
hoelzl@38656
   493
hoelzl@38656
   494
    from True `?if` have "x = y + z" by simp
hoelzl@38656
   495
hoelzl@41023
   496
    show ?thesis unfolding minus_pextreal_def if_P[OF True] unfolding `x = y + z`
hoelzl@38656
   497
    proof (rule the_equality)
hoelzl@41023
   498
      fix d :: pextreal assume "y + z = y + d"
hoelzl@38656
   499
      thus "d = z" using `y < x` p
hoelzl@38656
   500
        by (cases d, cases z) simp_all
hoelzl@38656
   501
    qed simp
hoelzl@41023
   502
  qed (simp add: minus_pextreal_def)
hoelzl@38656
   503
qed
hoelzl@38656
   504
hoelzl@38656
   505
instance ..
hoelzl@38656
   506
end
hoelzl@38656
   507
hoelzl@41023
   508
lemma pextreal_minus[simp]:
hoelzl@38656
   509
  "Real r - Real p = (if 0 \<le> r \<and> p < r then if 0 \<le> p then Real (r - p) else Real r else 0)"
hoelzl@41023
   510
  "(A::pextreal) - A = 0"
hoelzl@38656
   511
  "\<omega> - Real r = \<omega>"
hoelzl@38656
   512
  "Real r - \<omega> = 0"
hoelzl@38656
   513
  "A - 0 = A"
hoelzl@38656
   514
  "0 - A = 0"
hoelzl@41023
   515
  by (auto simp: minus_pextreal_eq not_less)
hoelzl@38656
   516
hoelzl@41023
   517
lemma pextreal_le_epsilon:
hoelzl@41023
   518
  fixes x y :: pextreal
hoelzl@38656
   519
  assumes "\<And>e. 0 < e \<Longrightarrow> x \<le> y + e"
hoelzl@38656
   520
  shows "x \<le> y"
hoelzl@38656
   521
proof (cases y)
hoelzl@38656
   522
  case (preal r)
hoelzl@38656
   523
  then obtain p where x: "x = Real p" "0 \<le> p"
hoelzl@38656
   524
    using assms[of 1] by (cases x) auto
hoelzl@38656
   525
  { fix e have "0 < e \<Longrightarrow> p \<le> r + e"
hoelzl@38656
   526
      using assms[of "Real e"] preal x by auto }
hoelzl@38656
   527
  hence "p \<le> r" by (rule field_le_epsilon)
hoelzl@38656
   528
  thus ?thesis using preal x by auto
hoelzl@38656
   529
qed simp
hoelzl@38656
   530
hoelzl@41023
   531
instance pextreal :: "{ordered_comm_semiring, comm_semiring_1}"
hoelzl@38656
   532
proof
hoelzl@41023
   533
  show "0 \<noteq> (1::pextreal)" unfolding zero_pextreal_def one_pextreal_def
hoelzl@38656
   534
    by (simp del: Real_1 Real_0)
hoelzl@38656
   535
hoelzl@41023
   536
  fix a :: pextreal
hoelzl@38656
   537
  show "0 * a = 0" "a * 0 = 0" by simp_all
hoelzl@38656
   538
hoelzl@41023
   539
  fix b c :: pextreal
hoelzl@38656
   540
  show "(a + b) * c = a * c + b * c"
hoelzl@38656
   541
    by (cases c, cases a, cases b)
hoelzl@38656
   542
       (auto intro!: arg_cong[where f=Real] simp: field_simps not_le mult_le_0_iff mult_less_0_iff)
hoelzl@38656
   543
hoelzl@38656
   544
  { assume "a \<le> b" thus "c + a \<le> c + b"
hoelzl@38656
   545
     by (cases c, cases a, cases b) auto }
hoelzl@38656
   546
hoelzl@38656
   547
  assume "a \<le> b" "0 \<le> c"
hoelzl@38656
   548
  thus "c * a \<le> c * b"
hoelzl@38656
   549
    apply (cases c, cases a, cases b)
hoelzl@38656
   550
    by (auto simp: mult_left_mono mult_le_0_iff mult_less_0_iff not_le)
hoelzl@38656
   551
qed
hoelzl@38656
   552
hoelzl@38656
   553
lemma mult_\<omega>[simp]: "x * y = \<omega> \<longleftrightarrow> (x = \<omega> \<or> y = \<omega>) \<and> x \<noteq> 0 \<and> y \<noteq> 0"
hoelzl@38656
   554
  by (cases x, cases y) auto
hoelzl@38656
   555
hoelzl@38656
   556
lemma \<omega>_mult[simp]: "(\<omega> = x * y) = ((x = \<omega> \<or> y = \<omega>) \<and> x \<noteq> 0 \<and> y \<noteq> 0)"
hoelzl@38656
   557
  by (cases x, cases y) auto
hoelzl@38656
   558
hoelzl@41023
   559
lemma pextreal_mult_0[simp]: "x * y = 0 \<longleftrightarrow> x = 0 \<or> (y::pextreal) = 0"
hoelzl@38656
   560
  by (cases x, cases y) (auto simp: mult_le_0_iff)
hoelzl@38656
   561
hoelzl@41023
   562
lemma pextreal_mult_cancel:
hoelzl@41023
   563
  fixes x y z :: pextreal
hoelzl@38656
   564
  assumes "y \<le> z"
hoelzl@38656
   565
  shows "x * y \<le> x * z"
hoelzl@38656
   566
  using assms
hoelzl@38656
   567
  by (cases x, cases y, cases z)
hoelzl@38656
   568
     (auto simp: mult_le_cancel_left mult_le_0_iff mult_less_0_iff not_le)
hoelzl@38656
   569
hoelzl@38656
   570
lemma Real_power[simp]:
hoelzl@38656
   571
  "Real x ^ n = (if x \<le> 0 then (if n = 0 then 1 else 0) else Real (x ^ n))"
hoelzl@38656
   572
  by (induct n) auto
hoelzl@38656
   573
hoelzl@38656
   574
lemma Real_power_\<omega>[simp]:
hoelzl@38656
   575
  "\<omega> ^ n = (if n = 0 then 1 else \<omega>)"
hoelzl@38656
   576
  by (induct n) auto
hoelzl@38656
   577
hoelzl@41023
   578
lemma pextreal_of_nat[simp]: "of_nat m = Real (real m)"
hoelzl@41023
   579
  by (induct m) (auto simp: real_of_nat_Suc one_pextreal_def simp del: Real_1)
hoelzl@38656
   580
hoelzl@40871
   581
lemma less_\<omega>_Ex_of_nat: "x < \<omega> \<longleftrightarrow> (\<exists>n. x < of_nat n)"
hoelzl@40871
   582
proof safe
hoelzl@40871
   583
  assume "x < \<omega>"
hoelzl@40871
   584
  then obtain r where "0 \<le> r" "x = Real r" by (cases x) auto
hoelzl@40871
   585
  moreover obtain n where "r < of_nat n" using ex_less_of_nat by auto
hoelzl@40871
   586
  ultimately show "\<exists>n. x < of_nat n" by (auto simp: real_eq_of_nat)
hoelzl@40871
   587
qed auto
hoelzl@40871
   588
hoelzl@41023
   589
lemma real_of_pextreal_mono:
hoelzl@41023
   590
  fixes a b :: pextreal
hoelzl@38656
   591
  assumes "b \<noteq> \<omega>" "a \<le> b"
hoelzl@38656
   592
  shows "real a \<le> real b"
hoelzl@38656
   593
using assms by (cases b, cases a) auto
hoelzl@38656
   594
hoelzl@41023
   595
lemma setprod_pextreal_0:
hoelzl@41023
   596
  "(\<Prod>i\<in>I. f i) = (0::pextreal) \<longleftrightarrow> finite I \<and> (\<exists>i\<in>I. f i = 0)"
hoelzl@40859
   597
proof cases
hoelzl@40859
   598
  assume "finite I" then show ?thesis
hoelzl@40859
   599
  proof (induct I)
hoelzl@40859
   600
    case (insert i I)
hoelzl@40859
   601
    then show ?case by simp
hoelzl@40859
   602
  qed simp
hoelzl@40859
   603
qed simp
hoelzl@40859
   604
hoelzl@40859
   605
lemma setprod_\<omega>:
hoelzl@40859
   606
  "(\<Prod>i\<in>I. f i) = \<omega> \<longleftrightarrow> finite I \<and> (\<exists>i\<in>I. f i = \<omega>) \<and> (\<forall>i\<in>I. f i \<noteq> 0)"
hoelzl@40859
   607
proof cases
hoelzl@40859
   608
  assume "finite I" then show ?thesis
hoelzl@40859
   609
  proof (induct I)
hoelzl@40859
   610
    case (insert i I) then show ?case
hoelzl@41023
   611
      by (auto simp: setprod_pextreal_0)
hoelzl@40859
   612
  qed simp
hoelzl@40859
   613
qed simp
hoelzl@40859
   614
hoelzl@41023
   615
instance pextreal :: "semiring_char_0"
hoelzl@38656
   616
proof
hoelzl@38656
   617
  fix m n
hoelzl@41023
   618
  show "inj (of_nat::nat\<Rightarrow>pextreal)" by (auto intro!: inj_onI)
hoelzl@38656
   619
qed
hoelzl@38656
   620
hoelzl@41023
   621
subsection "@{typ pextreal} is a complete lattice"
hoelzl@38656
   622
hoelzl@41023
   623
instantiation pextreal :: lattice
hoelzl@38656
   624
begin
hoelzl@41023
   625
definition [simp]: "sup x y = (max x y :: pextreal)"
hoelzl@41023
   626
definition [simp]: "inf x y = (min x y :: pextreal)"
hoelzl@38656
   627
instance proof qed simp_all
hoelzl@38656
   628
end
hoelzl@38656
   629
hoelzl@41023
   630
instantiation pextreal :: complete_lattice
hoelzl@38656
   631
begin
hoelzl@38656
   632
hoelzl@38656
   633
definition "bot = Real 0"
hoelzl@38656
   634
definition "top = \<omega>"
hoelzl@38656
   635
hoelzl@41023
   636
definition "Sup S = (LEAST z. \<forall>x\<in>S. x \<le> z :: pextreal)"
hoelzl@41023
   637
definition "Inf S = (GREATEST z. \<forall>x\<in>S. z \<le> x :: pextreal)"
hoelzl@38656
   638
hoelzl@41023
   639
lemma pextreal_complete_Sup:
hoelzl@41023
   640
  fixes S :: "pextreal set" assumes "S \<noteq> {}"
hoelzl@38656
   641
  shows "\<exists>x. (\<forall>y\<in>S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>S. y \<le> z) \<longrightarrow> x \<le> z)"
hoelzl@38656
   642
proof (cases "\<exists>x\<ge>0. \<forall>a\<in>S. a \<le> Real x")
hoelzl@38656
   643
  case False
hoelzl@38656
   644
  hence *: "\<And>x. x\<ge>0 \<Longrightarrow> \<exists>a\<in>S. \<not>a \<le> Real x" by simp
hoelzl@38656
   645
  show ?thesis
hoelzl@38656
   646
  proof (safe intro!: exI[of _ \<omega>])
hoelzl@38656
   647
    fix y assume **: "\<forall>z\<in>S. z \<le> y"
hoelzl@41023
   648
    show "\<omega> \<le> y" unfolding pextreal_\<omega>_less_eq
hoelzl@38656
   649
    proof (rule ccontr)
hoelzl@38656
   650
      assume "y \<noteq> \<omega>"
hoelzl@38656
   651
      then obtain x where [simp]: "y = Real x" and "0 \<le> x" by (cases y) auto
hoelzl@38656
   652
      from *[OF `0 \<le> x`] show False using ** by auto
hoelzl@38656
   653
    qed
hoelzl@38656
   654
  qed simp
hoelzl@38656
   655
next
hoelzl@38656
   656
  case True then obtain y where y: "\<And>a. a\<in>S \<Longrightarrow> a \<le> Real y" and "0 \<le> y" by auto
hoelzl@38656
   657
  from y[of \<omega>] have "\<omega> \<notin> S" by auto
hoelzl@38656
   658
hoelzl@38656
   659
  with `S \<noteq> {}` obtain x where "x \<in> S" and "x \<noteq> \<omega>" by auto
hoelzl@38656
   660
hoelzl@38656
   661
  have bound: "\<forall>x\<in>real ` S. x \<le> y"
hoelzl@38656
   662
  proof
hoelzl@38656
   663
    fix z assume "z \<in> real ` S" then guess a ..
hoelzl@38656
   664
    with y[of a] `\<omega> \<notin> S` `0 \<le> y` show "z \<le> y" by (cases a) auto
hoelzl@38656
   665
  qed
hoelzl@38656
   666
  with reals_complete2[of "real ` S"] `x \<in> S`
hoelzl@38656
   667
  obtain s where s: "\<forall>y\<in>S. real y \<le> s" "\<forall>z. ((\<forall>y\<in>S. real y \<le> z) \<longrightarrow> s \<le> z)"
hoelzl@38656
   668
    by auto
hoelzl@38656
   669
hoelzl@38656
   670
  show ?thesis
hoelzl@38656
   671
  proof (safe intro!: exI[of _ "Real s"])
hoelzl@38656
   672
    fix z assume "z \<in> S" thus "z \<le> Real s"
hoelzl@38656
   673
      using s `\<omega> \<notin> S` by (cases z) auto
hoelzl@38656
   674
  next
hoelzl@38656
   675
    fix z assume *: "\<forall>y\<in>S. y \<le> z"
hoelzl@38656
   676
    show "Real s \<le> z"
hoelzl@38656
   677
    proof (cases z)
hoelzl@38656
   678
      case (preal u)
hoelzl@38656
   679
      { fix v assume "v \<in> S"
hoelzl@38656
   680
        hence "v \<le> Real u" using * preal by auto
hoelzl@38656
   681
        hence "real v \<le> u" using `\<omega> \<notin> S` `0 \<le> u` by (cases v) auto }
hoelzl@38656
   682
      hence "s \<le> u" using s(2) by auto
hoelzl@38656
   683
      thus "Real s \<le> z" using preal by simp
hoelzl@38656
   684
    qed simp
hoelzl@38656
   685
  qed
hoelzl@38656
   686
qed
hoelzl@38656
   687
hoelzl@41023
   688
lemma pextreal_complete_Inf:
hoelzl@41023
   689
  fixes S :: "pextreal set" assumes "S \<noteq> {}"
hoelzl@38656
   690
  shows "\<exists>x. (\<forall>y\<in>S. x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> x)"
hoelzl@38656
   691
proof (cases "S = {\<omega>}")
hoelzl@38656
   692
  case True thus ?thesis by (auto intro!: exI[of _ \<omega>])
hoelzl@38656
   693
next
hoelzl@38656
   694
  case False with `S \<noteq> {}` have "S - {\<omega>} \<noteq> {}" by auto
hoelzl@38656
   695
  hence not_empty: "\<exists>x. x \<in> uminus ` real ` (S - {\<omega>})" by auto
hoelzl@38656
   696
  have bounds: "\<exists>x. \<forall>y\<in>uminus ` real ` (S - {\<omega>}). y \<le> x" by (auto intro!: exI[of _ 0])
hoelzl@38656
   697
  from reals_complete2[OF not_empty bounds]
hoelzl@38656
   698
  obtain s where s: "\<And>y. y\<in>S - {\<omega>} \<Longrightarrow> - real y \<le> s" "\<forall>z. ((\<forall>y\<in>S - {\<omega>}. - real y \<le> z) \<longrightarrow> s \<le> z)"
hoelzl@38656
   699
    by auto
hoelzl@38656
   700
hoelzl@38656
   701
  show ?thesis
hoelzl@38656
   702
  proof (safe intro!: exI[of _ "Real (-s)"])
hoelzl@38656
   703
    fix z assume "z \<in> S"
hoelzl@38656
   704
    show "Real (-s) \<le> z"
hoelzl@38656
   705
    proof (cases z)
hoelzl@38656
   706
      case (preal r)
hoelzl@38656
   707
      with s `z \<in> S` have "z \<in> S - {\<omega>}" by simp
hoelzl@38656
   708
      hence "- r \<le> s" using preal s(1)[of z] by auto
hoelzl@38656
   709
      hence "- s \<le> r" by (subst neg_le_iff_le[symmetric]) simp
hoelzl@38656
   710
      thus ?thesis using preal by simp
hoelzl@38656
   711
    qed simp
hoelzl@38656
   712
  next
hoelzl@38656
   713
    fix z assume *: "\<forall>y\<in>S. z \<le> y"
hoelzl@38656
   714
    show "z \<le> Real (-s)"
hoelzl@38656
   715
    proof (cases z)
hoelzl@38656
   716
      case (preal u)
hoelzl@38656
   717
      { fix v assume "v \<in> S-{\<omega>}"
hoelzl@38656
   718
        hence "Real u \<le> v" using * preal by auto
hoelzl@38656
   719
        hence "- real v \<le> - u" using `0 \<le> u` `v \<in> S - {\<omega>}` by (cases v) auto }
hoelzl@38656
   720
      hence "u \<le> - s" using s(2) by (subst neg_le_iff_le[symmetric]) auto
hoelzl@38656
   721
      thus "z \<le> Real (-s)" using preal by simp
hoelzl@38656
   722
    next
hoelzl@38656
   723
      case infinite
hoelzl@38656
   724
      with * have "S = {\<omega>}" using `S \<noteq> {}` by auto
hoelzl@38656
   725
      with `S - {\<omega>} \<noteq> {}` show ?thesis by simp
hoelzl@38656
   726
    qed
hoelzl@38656
   727
  qed
hoelzl@38656
   728
qed
hoelzl@38656
   729
hoelzl@38656
   730
instance
hoelzl@38656
   731
proof
hoelzl@41023
   732
  fix x :: pextreal and A
hoelzl@38656
   733
hoelzl@41023
   734
  show "bot \<le> x" by (cases x) (simp_all add: bot_pextreal_def)
hoelzl@41023
   735
  show "x \<le> top" by (simp add: top_pextreal_def)
hoelzl@38656
   736
hoelzl@38656
   737
  { assume "x \<in> A"
hoelzl@41023
   738
    with pextreal_complete_Sup[of A]
hoelzl@38656
   739
    obtain s where s: "\<forall>y\<in>A. y \<le> s" "\<forall>z. (\<forall>y\<in>A. y \<le> z) \<longrightarrow> s \<le> z" by auto
hoelzl@38656
   740
    hence "x \<le> s" using `x \<in> A` by auto
hoelzl@41023
   741
    also have "... = Sup A" using s unfolding Sup_pextreal_def
hoelzl@38656
   742
      by (auto intro!: Least_equality[symmetric])
hoelzl@38656
   743
    finally show "x \<le> Sup A" . }
hoelzl@38656
   744
hoelzl@38656
   745
  { assume "x \<in> A"
hoelzl@41023
   746
    with pextreal_complete_Inf[of A]
hoelzl@38656
   747
    obtain i where i: "\<forall>y\<in>A. i \<le> y" "\<forall>z. (\<forall>y\<in>A. z \<le> y) \<longrightarrow> z \<le> i" by auto
hoelzl@41023
   748
    hence "Inf A = i" unfolding Inf_pextreal_def
hoelzl@38656
   749
      by (auto intro!: Greatest_equality)
hoelzl@38656
   750
    also have "i \<le> x" using i `x \<in> A` by auto
hoelzl@38656
   751
    finally show "Inf A \<le> x" . }
hoelzl@38656
   752
hoelzl@38656
   753
  { assume *: "\<And>z. z \<in> A \<Longrightarrow> z \<le> x"
hoelzl@38656
   754
    show "Sup A \<le> x"
hoelzl@38656
   755
    proof (cases "A = {}")
hoelzl@38656
   756
      case True
hoelzl@41023
   757
      hence "Sup A = 0" unfolding Sup_pextreal_def
hoelzl@38656
   758
        by (auto intro!: Least_equality)
hoelzl@38656
   759
      thus "Sup A \<le> x" by simp
hoelzl@38656
   760
    next
hoelzl@38656
   761
      case False
hoelzl@41023
   762
      with pextreal_complete_Sup[of A]
hoelzl@38656
   763
      obtain s where s: "\<forall>y\<in>A. y \<le> s" "\<forall>z. (\<forall>y\<in>A. y \<le> z) \<longrightarrow> s \<le> z" by auto
hoelzl@38656
   764
      hence "Sup A = s"
hoelzl@41023
   765
        unfolding Sup_pextreal_def by (auto intro!: Least_equality)
hoelzl@38656
   766
      also have "s \<le> x" using * s by auto
hoelzl@38656
   767
      finally show "Sup A \<le> x" .
hoelzl@38656
   768
    qed }
hoelzl@38656
   769
hoelzl@38656
   770
  { assume *: "\<And>z. z \<in> A \<Longrightarrow> x \<le> z"
hoelzl@38656
   771
    show "x \<le> Inf A"
hoelzl@38656
   772
    proof (cases "A = {}")
hoelzl@38656
   773
      case True
hoelzl@41023
   774
      hence "Inf A = \<omega>" unfolding Inf_pextreal_def
hoelzl@38656
   775
        by (auto intro!: Greatest_equality)
hoelzl@38656
   776
      thus "x \<le> Inf A" by simp
hoelzl@38656
   777
    next
hoelzl@38656
   778
      case False
hoelzl@41023
   779
      with pextreal_complete_Inf[of A]
hoelzl@38656
   780
      obtain i where i: "\<forall>y\<in>A. i \<le> y" "\<forall>z. (\<forall>y\<in>A. z \<le> y) \<longrightarrow> z \<le> i" by auto
hoelzl@38656
   781
      have "x \<le> i" using * i by auto
hoelzl@38656
   782
      also have "i = Inf A" using i
hoelzl@41023
   783
        unfolding Inf_pextreal_def by (auto intro!: Greatest_equality[symmetric])
hoelzl@38656
   784
      finally show "x \<le> Inf A" .
hoelzl@38656
   785
    qed }
hoelzl@38656
   786
qed
hoelzl@38656
   787
end
hoelzl@38656
   788
hoelzl@41023
   789
lemma Inf_pextreal_iff:
hoelzl@41023
   790
  fixes z :: pextreal
hoelzl@38656
   791
  shows "(\<And>x. x \<in> X \<Longrightarrow> z \<le> x) \<Longrightarrow> (\<exists>x\<in>X. x<y) \<longleftrightarrow> Inf X < y"
hoelzl@38656
   792
  by (metis complete_lattice_class.Inf_greatest complete_lattice_class.Inf_lower less_le_not_le linear
hoelzl@38656
   793
            order_less_le_trans)
hoelzl@38656
   794
hoelzl@38656
   795
lemma Inf_greater:
hoelzl@41023
   796
  fixes z :: pextreal assumes "Inf X < z"
hoelzl@38656
   797
  shows "\<exists>x \<in> X. x < z"
hoelzl@38656
   798
proof -
hoelzl@41023
   799
  have "X \<noteq> {}" using assms by (auto simp: Inf_empty top_pextreal_def)
hoelzl@38656
   800
  with assms show ?thesis
hoelzl@41023
   801
    by (metis Inf_pextreal_iff mem_def not_leE)
hoelzl@38656
   802
qed
hoelzl@38656
   803
hoelzl@38656
   804
lemma Inf_close:
hoelzl@41023
   805
  fixes e :: pextreal assumes "Inf X \<noteq> \<omega>" "0 < e"
hoelzl@38656
   806
  shows "\<exists>x \<in> X. x < Inf X + e"
hoelzl@38656
   807
proof (rule Inf_greater)
hoelzl@38656
   808
  show "Inf X < Inf X + e" using assms
hoelzl@38656
   809
    by (cases "Inf X", cases e) auto
hoelzl@38656
   810
qed
hoelzl@38656
   811
hoelzl@41023
   812
lemma pextreal_SUPI:
hoelzl@41023
   813
  fixes x :: pextreal
hoelzl@38656
   814
  assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<le> x"
hoelzl@38656
   815
  assumes "\<And>y. (\<And>i. i \<in> A \<Longrightarrow> f i \<le> y) \<Longrightarrow> x \<le> y"
hoelzl@38656
   816
  shows "(SUP i:A. f i) = x"
hoelzl@41023
   817
  unfolding SUPR_def Sup_pextreal_def
hoelzl@38656
   818
  using assms by (auto intro!: Least_equality)
hoelzl@38656
   819
hoelzl@41023
   820
lemma Sup_pextreal_iff:
hoelzl@41023
   821
  fixes z :: pextreal
hoelzl@38656
   822
  shows "(\<And>x. x \<in> X \<Longrightarrow> x \<le> z) \<Longrightarrow> (\<exists>x\<in>X. y<x) \<longleftrightarrow> y < Sup X"
hoelzl@38656
   823
  by (metis complete_lattice_class.Sup_least complete_lattice_class.Sup_upper less_le_not_le linear
hoelzl@38656
   824
            order_less_le_trans)
hoelzl@38656
   825
hoelzl@38656
   826
lemma Sup_lesser:
hoelzl@41023
   827
  fixes z :: pextreal assumes "z < Sup X"
hoelzl@38656
   828
  shows "\<exists>x \<in> X. z < x"
hoelzl@38656
   829
proof -
hoelzl@41023
   830
  have "X \<noteq> {}" using assms by (auto simp: Sup_empty bot_pextreal_def)
hoelzl@38656
   831
  with assms show ?thesis
hoelzl@41023
   832
    by (metis Sup_pextreal_iff mem_def not_leE)
hoelzl@38656
   833
qed
hoelzl@38656
   834
hoelzl@38656
   835
lemma Sup_eq_\<omega>: "\<omega> \<in> S \<Longrightarrow> Sup S = \<omega>"
hoelzl@41023
   836
  unfolding Sup_pextreal_def
hoelzl@38656
   837
  by (auto intro!: Least_equality)
hoelzl@38656
   838
hoelzl@38656
   839
lemma Sup_close:
hoelzl@38656
   840
  assumes "0 < e" and S: "Sup S \<noteq> \<omega>" "S \<noteq> {}"
hoelzl@38656
   841
  shows "\<exists>X\<in>S. Sup S < X + e"
hoelzl@38656
   842
proof cases
hoelzl@38656
   843
  assume "Sup S = 0"
hoelzl@38656
   844
  moreover obtain X where "X \<in> S" using `S \<noteq> {}` by auto
hoelzl@38656
   845
  ultimately show ?thesis using `0 < e` by (auto intro!: bexI[OF _ `X\<in>S`])
hoelzl@38656
   846
next
hoelzl@38656
   847
  assume "Sup S \<noteq> 0"
hoelzl@38656
   848
  have "\<exists>X\<in>S. Sup S - e < X"
hoelzl@38656
   849
  proof (rule Sup_lesser)
hoelzl@38656
   850
    show "Sup S - e < Sup S" using `0 < e` `Sup S \<noteq> 0` `Sup S \<noteq> \<omega>`
hoelzl@41023
   851
      by (cases e) (auto simp: pextreal_noteq_omega_Ex)
hoelzl@38656
   852
  qed
hoelzl@38656
   853
  then guess X .. note X = this
hoelzl@38656
   854
  with `Sup S \<noteq> \<omega>` Sup_eq_\<omega> have "X \<noteq> \<omega>" by auto
hoelzl@41023
   855
  thus ?thesis using `Sup S \<noteq> \<omega>` X unfolding pextreal_noteq_omega_Ex
hoelzl@38656
   856
    by (cases e) (auto intro!: bexI[OF _ `X\<in>S`] simp: split: split_if_asm)
hoelzl@38656
   857
qed
hoelzl@38656
   858
hoelzl@38656
   859
lemma Sup_\<omega>: "(SUP i::nat. Real (real i)) = \<omega>"
hoelzl@41023
   860
proof (rule pextreal_SUPI)
hoelzl@38656
   861
  fix y assume *: "\<And>i::nat. i \<in> UNIV \<Longrightarrow> Real (real i) \<le> y"
hoelzl@38656
   862
  thus "\<omega> \<le> y"
hoelzl@38656
   863
  proof (cases y)
hoelzl@38656
   864
    case (preal r)
hoelzl@38656
   865
    then obtain k :: nat where "r < real k"
hoelzl@38656
   866
      using ex_less_of_nat by (auto simp: real_eq_of_nat)
hoelzl@38656
   867
    with *[of k] preal show ?thesis by auto
hoelzl@38656
   868
  qed simp
hoelzl@38656
   869
qed simp
hoelzl@38656
   870
hoelzl@40871
   871
lemma SUP_\<omega>: "(SUP i:A. f i) = \<omega> \<longleftrightarrow> (\<forall>x<\<omega>. \<exists>i\<in>A. x < f i)"
hoelzl@40871
   872
proof
hoelzl@40871
   873
  assume *: "(SUP i:A. f i) = \<omega>"
hoelzl@40871
   874
  show "(\<forall>x<\<omega>. \<exists>i\<in>A. x < f i)" unfolding *[symmetric]
hoelzl@40871
   875
  proof (intro allI impI)
hoelzl@40871
   876
    fix x assume "x < SUPR A f" then show "\<exists>i\<in>A. x < f i"
hoelzl@40871
   877
      unfolding less_SUP_iff by auto
hoelzl@40871
   878
  qed
hoelzl@40871
   879
next
hoelzl@40871
   880
  assume *: "\<forall>x<\<omega>. \<exists>i\<in>A. x < f i"
hoelzl@40871
   881
  show "(SUP i:A. f i) = \<omega>"
hoelzl@41023
   882
  proof (rule pextreal_SUPI)
hoelzl@40871
   883
    fix y assume **: "\<And>i. i \<in> A \<Longrightarrow> f i \<le> y"
hoelzl@40871
   884
    show "\<omega> \<le> y"
hoelzl@40871
   885
    proof cases
hoelzl@40871
   886
      assume "y < \<omega>"
hoelzl@40871
   887
      from *[THEN spec, THEN mp, OF this]
hoelzl@40871
   888
      obtain i where "i \<in> A" "\<not> (f i \<le> y)" by auto
hoelzl@40871
   889
      with ** show ?thesis by auto
hoelzl@40871
   890
    qed auto
hoelzl@40871
   891
  qed auto
hoelzl@40871
   892
qed
hoelzl@40871
   893
hoelzl@41023
   894
subsubsection {* Equivalence between @{text "f ----> x"} and @{text SUP} on @{typ pextreal} *}
hoelzl@38656
   895
hoelzl@38656
   896
lemma monoseq_monoI: "mono f \<Longrightarrow> monoseq f"
hoelzl@38656
   897
  unfolding mono_def monoseq_def by auto
hoelzl@38656
   898
hoelzl@38656
   899
lemma incseq_mono: "mono f \<longleftrightarrow> incseq f"
hoelzl@38656
   900
  unfolding mono_def incseq_def by auto
hoelzl@38656
   901
hoelzl@38656
   902
lemma SUP_eq_LIMSEQ:
hoelzl@38656
   903
  assumes "mono f" and "\<And>n. 0 \<le> f n" and "0 \<le> x"
hoelzl@38656
   904
  shows "(SUP n. Real (f n)) = Real x \<longleftrightarrow> f ----> x"
hoelzl@38656
   905
proof
hoelzl@38656
   906
  assume x: "(SUP n. Real (f n)) = Real x"
hoelzl@38656
   907
  { fix n
hoelzl@38656
   908
    have "Real (f n) \<le> Real x" using x[symmetric] by (auto intro: le_SUPI)
hoelzl@38656
   909
    hence "f n \<le> x" using assms by simp }
hoelzl@38656
   910
  show "f ----> x"
hoelzl@38656
   911
  proof (rule LIMSEQ_I)
hoelzl@38656
   912
    fix r :: real assume "0 < r"
hoelzl@38656
   913
    show "\<exists>no. \<forall>n\<ge>no. norm (f n - x) < r"
hoelzl@38656
   914
    proof (rule ccontr)
hoelzl@38656
   915
      assume *: "\<not> ?thesis"
hoelzl@38656
   916
      { fix N
hoelzl@38656
   917
        from * obtain n where "N \<le> n" "r \<le> x - f n"
hoelzl@38656
   918
          using `\<And>n. f n \<le> x` by (auto simp: not_less)
hoelzl@38656
   919
        hence "f N \<le> f n" using `mono f` by (auto dest: monoD)
hoelzl@38656
   920
        hence "f N \<le> x - r" using `r \<le> x - f n` by auto
hoelzl@38656
   921
        hence "Real (f N) \<le> Real (x - r)" and "r \<le> x" using `0 \<le> f N` by auto }
hoelzl@38656
   922
      hence "(SUP n. Real (f n)) \<le> Real (x - r)"
hoelzl@38656
   923
        and "Real (x - r) < Real x" using `0 < r` by (auto intro: SUP_leI)
hoelzl@38656
   924
      hence "(SUP n. Real (f n)) < Real x" by (rule le_less_trans)
hoelzl@38656
   925
      thus False using x by auto
hoelzl@38656
   926
    qed
hoelzl@38656
   927
  qed
hoelzl@38656
   928
next
hoelzl@38656
   929
  assume "f ----> x"
hoelzl@38656
   930
  show "(SUP n. Real (f n)) = Real x"
hoelzl@41023
   931
  proof (rule pextreal_SUPI)
hoelzl@38656
   932
    fix n
hoelzl@38656
   933
    from incseq_le[of f x] `mono f` `f ----> x`
hoelzl@38656
   934
    show "Real (f n) \<le> Real x" using assms incseq_mono by auto
hoelzl@38656
   935
  next
hoelzl@38656
   936
    fix y assume *: "\<And>n. n\<in>UNIV \<Longrightarrow> Real (f n) \<le> y"
hoelzl@38656
   937
    show "Real x \<le> y"
hoelzl@38656
   938
    proof (cases y)
hoelzl@38656
   939
      case (preal r)
hoelzl@38656
   940
      with * have "\<exists>N. \<forall>n\<ge>N. f n \<le> r" using assms by fastsimp
hoelzl@38656
   941
      from LIMSEQ_le_const2[OF `f ----> x` this]
hoelzl@38656
   942
      show "Real x \<le> y" using `0 \<le> x` preal by auto
hoelzl@38656
   943
    qed simp
hoelzl@38656
   944
  qed
hoelzl@38656
   945
qed
hoelzl@38656
   946
hoelzl@38656
   947
lemma SUPR_bound:
hoelzl@38656
   948
  assumes "\<forall>N. f N \<le> x"
hoelzl@38656
   949
  shows "(SUP n. f n) \<le> x"
hoelzl@38656
   950
  using assms by (simp add: SUPR_def Sup_le_iff)
hoelzl@38656
   951
hoelzl@41023
   952
lemma pextreal_less_eq_diff_eq_sum:
hoelzl@41023
   953
  fixes x y z :: pextreal
hoelzl@38656
   954
  assumes "y \<le> x" and "x \<noteq> \<omega>"
hoelzl@38656
   955
  shows "z \<le> x - y \<longleftrightarrow> z + y \<le> x"
hoelzl@38656
   956
  using assms
hoelzl@38656
   957
  apply (cases z, cases y, cases x)
hoelzl@41023
   958
  by (simp_all add: field_simps minus_pextreal_eq)
hoelzl@38656
   959
hoelzl@38656
   960
lemma Real_diff_less_omega: "Real r - x < \<omega>" by (cases x) auto
hoelzl@38656
   961
hoelzl@41023
   962
subsubsection {* Numbers on @{typ pextreal} *}
hoelzl@38656
   963
hoelzl@41023
   964
instantiation pextreal :: number
hoelzl@38656
   965
begin
hoelzl@38656
   966
definition [simp]: "number_of x = Real (number_of x)"
hoelzl@38656
   967
instance proof qed
hoelzl@38656
   968
end
hoelzl@38656
   969
hoelzl@41023
   970
subsubsection {* Division on @{typ pextreal} *}
hoelzl@38656
   971
hoelzl@41023
   972
instantiation pextreal :: inverse
hoelzl@38656
   973
begin
hoelzl@38656
   974
hoelzl@41023
   975
definition "inverse x = pextreal_case (\<lambda>x. if x = 0 then \<omega> else Real (inverse x)) 0 x"
hoelzl@41023
   976
definition [simp]: "x / y = x * inverse (y :: pextreal)"
hoelzl@38656
   977
hoelzl@38656
   978
instance proof qed
hoelzl@38656
   979
end
hoelzl@38656
   980
hoelzl@41023
   981
lemma pextreal_inverse[simp]:
hoelzl@38656
   982
  "inverse 0 = \<omega>"
hoelzl@38656
   983
  "inverse (Real x) = (if x \<le> 0 then \<omega> else Real (inverse x))"
hoelzl@38656
   984
  "inverse \<omega> = 0"
hoelzl@41023
   985
  "inverse (1::pextreal) = 1"
hoelzl@38656
   986
  "inverse (inverse x) = x"
hoelzl@41023
   987
  by (simp_all add: inverse_pextreal_def one_pextreal_def split: pextreal_case_split del: Real_1)
hoelzl@38656
   988
hoelzl@41023
   989
lemma pextreal_inverse_le_eq:
hoelzl@38656
   990
  assumes "x \<noteq> 0" "x \<noteq> \<omega>"
hoelzl@41023
   991
  shows "y \<le> z / x \<longleftrightarrow> x * y \<le> (z :: pextreal)"
hoelzl@38656
   992
proof -
hoelzl@38656
   993
  from assms obtain r where r: "x = Real r" "0 < r" by (cases x) auto
hoelzl@38656
   994
  { fix p q :: real assume "0 \<le> p" "0 \<le> q"
hoelzl@38656
   995
    have "p \<le> q * inverse r \<longleftrightarrow> p \<le> q / r" by (simp add: divide_inverse)
hoelzl@38656
   996
    also have "... \<longleftrightarrow> p * r \<le> q" using `0 < r` by (auto simp: field_simps)
hoelzl@38656
   997
    finally have "p \<le> q * inverse r \<longleftrightarrow> p * r \<le> q" . }
hoelzl@38656
   998
  with r show ?thesis
hoelzl@38656
   999
    by (cases y, cases z, auto simp: zero_le_mult_iff field_simps)
hoelzl@38656
  1000
qed
hoelzl@38656
  1001
hoelzl@38656
  1002
lemma inverse_antimono_strict:
hoelzl@41023
  1003
  fixes x y :: pextreal
hoelzl@38656
  1004
  assumes "x < y" shows "inverse y < inverse x"
hoelzl@38656
  1005
  using assms by (cases x, cases y) auto
hoelzl@38656
  1006
hoelzl@38656
  1007
lemma inverse_antimono:
hoelzl@41023
  1008
  fixes x y :: pextreal
hoelzl@38656
  1009
  assumes "x \<le> y" shows "inverse y \<le> inverse x"
hoelzl@38656
  1010
  using assms by (cases x, cases y) auto
hoelzl@38656
  1011
hoelzl@41023
  1012
lemma pextreal_inverse_\<omega>_iff[simp]: "inverse x = \<omega> \<longleftrightarrow> x = 0"
hoelzl@38656
  1013
  by (cases x) auto
hoelzl@38656
  1014
hoelzl@41023
  1015
subsection "Infinite sum over @{typ pextreal}"
hoelzl@38656
  1016
hoelzl@38656
  1017
text {*
hoelzl@38656
  1018
hoelzl@41023
  1019
The infinite sum over @{typ pextreal} has the nice property that it is always
hoelzl@38656
  1020
defined.
hoelzl@38656
  1021
hoelzl@38656
  1022
*}
hoelzl@38656
  1023
hoelzl@41023
  1024
definition psuminf :: "(nat \<Rightarrow> pextreal) \<Rightarrow> pextreal" (binder "\<Sum>\<^isub>\<infinity>" 10) where
hoelzl@38656
  1025
  "(\<Sum>\<^isub>\<infinity> x. f x) = (SUP n. \<Sum>i<n. f i)"
hoelzl@38656
  1026
hoelzl@38656
  1027
subsubsection {* Equivalence between @{text "\<Sum> n. f n"} and @{text "\<Sum>\<^isub>\<infinity> n. f n"} *}
hoelzl@38656
  1028
hoelzl@38656
  1029
lemma setsum_Real:
hoelzl@38656
  1030
  assumes "\<forall>x\<in>A. 0 \<le> f x"
hoelzl@38656
  1031
  shows "(\<Sum>x\<in>A. Real (f x)) = Real (\<Sum>x\<in>A. f x)"
hoelzl@38656
  1032
proof (cases "finite A")
hoelzl@38656
  1033
  case True
hoelzl@38656
  1034
  thus ?thesis using assms
hoelzl@38656
  1035
  proof induct case (insert x s)
hoelzl@38656
  1036
    hence "0 \<le> setsum f s" apply-apply(rule setsum_nonneg) by auto
hoelzl@38656
  1037
    thus ?case using insert by auto
hoelzl@38656
  1038
  qed auto
hoelzl@38656
  1039
qed simp
hoelzl@38656
  1040
hoelzl@38656
  1041
lemma setsum_Real':
hoelzl@38656
  1042
  assumes "\<forall>x. 0 \<le> f x"
hoelzl@38656
  1043
  shows "(\<Sum>x\<in>A. Real (f x)) = Real (\<Sum>x\<in>A. f x)"
hoelzl@38656
  1044
  apply(rule setsum_Real) using assms by auto
hoelzl@38656
  1045
hoelzl@38656
  1046
lemma setsum_\<omega>:
hoelzl@38656
  1047
  "(\<Sum>x\<in>P. f x) = \<omega> \<longleftrightarrow> (finite P \<and> (\<exists>i\<in>P. f i = \<omega>))"
hoelzl@38656
  1048
proof safe
hoelzl@38656
  1049
  assume *: "setsum f P = \<omega>"
hoelzl@38656
  1050
  show "finite P"
hoelzl@38656
  1051
  proof (rule ccontr) assume "infinite P" with * show False by auto qed
hoelzl@38656
  1052
  show "\<exists>i\<in>P. f i = \<omega>"
hoelzl@38656
  1053
  proof (rule ccontr)
hoelzl@38656
  1054
    assume "\<not> ?thesis" hence "\<And>i. i\<in>P \<Longrightarrow> f i \<noteq> \<omega>" by auto
hoelzl@38656
  1055
    from `finite P` this have "setsum f P \<noteq> \<omega>"
hoelzl@38656
  1056
      by induct auto
hoelzl@38656
  1057
    with * show False by auto
hoelzl@38656
  1058
  qed
hoelzl@38656
  1059
next
hoelzl@38656
  1060
  fix i assume "finite P" "i \<in> P" "f i = \<omega>"
hoelzl@38656
  1061
  thus "setsum f P = \<omega>"
hoelzl@38656
  1062
  proof induct
hoelzl@38656
  1063
    case (insert x A)
hoelzl@38656
  1064
    show ?case using insert by (cases "x = i") auto
hoelzl@38656
  1065
  qed simp
hoelzl@38656
  1066
qed
hoelzl@38656
  1067
hoelzl@41023
  1068
lemma real_of_pextreal_setsum:
hoelzl@38656
  1069
  assumes "\<And>x. x \<in> S \<Longrightarrow> f x \<noteq> \<omega>"
hoelzl@38656
  1070
  shows "(\<Sum>x\<in>S. real (f x)) = real (setsum f S)"
hoelzl@38656
  1071
proof cases
hoelzl@38656
  1072
  assume "finite S"
hoelzl@38656
  1073
  from this assms show ?thesis
hoelzl@41023
  1074
    by induct (simp_all add: real_of_pextreal_add setsum_\<omega>)
hoelzl@38656
  1075
qed simp
hoelzl@38656
  1076
hoelzl@38656
  1077
lemma setsum_0:
hoelzl@41023
  1078
  fixes f :: "'a \<Rightarrow> pextreal" assumes "finite A"
hoelzl@38656
  1079
  shows "(\<Sum>x\<in>A. f x) = 0 \<longleftrightarrow> (\<forall>i\<in>A. f i = 0)"
hoelzl@38656
  1080
  using assms by induct auto
hoelzl@38656
  1081
hoelzl@38656
  1082
lemma suminf_imp_psuminf:
hoelzl@38656
  1083
  assumes "f sums x" and "\<forall>n. 0 \<le> f n"
hoelzl@38656
  1084
  shows "(\<Sum>\<^isub>\<infinity> x. Real (f x)) = Real x"
hoelzl@38656
  1085
  unfolding psuminf_def setsum_Real'[OF assms(2)]
hoelzl@38656
  1086
proof (rule SUP_eq_LIMSEQ[THEN iffD2])
hoelzl@38656
  1087
  show "mono (\<lambda>n. setsum f {..<n})" (is "mono ?S")
hoelzl@38656
  1088
    unfolding mono_iff_le_Suc using assms by simp
hoelzl@38656
  1089
hoelzl@38656
  1090
  { fix n show "0 \<le> ?S n"
hoelzl@38656
  1091
      using setsum_nonneg[of "{..<n}" f] assms by auto }
hoelzl@38656
  1092
hoelzl@38656
  1093
  thus "0 \<le> x" "?S ----> x"
hoelzl@38656
  1094
    using `f sums x` LIMSEQ_le_const
hoelzl@38656
  1095
    by (auto simp: atLeast0LessThan sums_def)
hoelzl@38656
  1096
qed
hoelzl@38656
  1097
hoelzl@38656
  1098
lemma psuminf_equality:
hoelzl@38656
  1099
  assumes "\<And>n. setsum f {..<n} \<le> x"
hoelzl@38656
  1100
  and "\<And>y. y \<noteq> \<omega> \<Longrightarrow> (\<And>n. setsum f {..<n} \<le> y) \<Longrightarrow> x \<le> y"
hoelzl@38656
  1101
  shows "psuminf f = x"
hoelzl@38656
  1102
  unfolding psuminf_def
hoelzl@41023
  1103
proof (safe intro!: pextreal_SUPI)
hoelzl@38656
  1104
  fix n show "setsum f {..<n} \<le> x" using assms(1) .
hoelzl@38656
  1105
next
hoelzl@38656
  1106
  fix y assume *: "\<forall>n. n \<in> UNIV \<longrightarrow> setsum f {..<n} \<le> y"
hoelzl@38656
  1107
  show "x \<le> y"
hoelzl@38656
  1108
  proof (cases "y = \<omega>")
hoelzl@38656
  1109
    assume "y \<noteq> \<omega>" from assms(2)[OF this] *
hoelzl@38656
  1110
    show "x \<le> y" by auto
hoelzl@38656
  1111
  qed simp
hoelzl@38656
  1112
qed
hoelzl@38656
  1113
hoelzl@38656
  1114
lemma psuminf_\<omega>:
hoelzl@38656
  1115
  assumes "f i = \<omega>"
hoelzl@38656
  1116
  shows "(\<Sum>\<^isub>\<infinity> x. f x) = \<omega>"
hoelzl@38656
  1117
proof (rule psuminf_equality)
hoelzl@38656
  1118
  fix y assume *: "\<And>n. setsum f {..<n} \<le> y"
hoelzl@38656
  1119
  have "setsum f {..<Suc i} = \<omega>" 
hoelzl@38656
  1120
    using assms by (simp add: setsum_\<omega>)
hoelzl@38656
  1121
  thus "\<omega> \<le> y" using *[of "Suc i"] by auto
hoelzl@38656
  1122
qed simp
hoelzl@38656
  1123
hoelzl@38656
  1124
lemma psuminf_imp_suminf:
hoelzl@38656
  1125
  assumes "(\<Sum>\<^isub>\<infinity> x. f x) \<noteq> \<omega>"
hoelzl@38656
  1126
  shows "(\<lambda>x. real (f x)) sums real (\<Sum>\<^isub>\<infinity> x. f x)"
hoelzl@38656
  1127
proof -
hoelzl@38656
  1128
  have "\<forall>i. \<exists>r. f i = Real r \<and> 0 \<le> r"
hoelzl@38656
  1129
  proof
hoelzl@38656
  1130
    fix i show "\<exists>r. f i = Real r \<and> 0 \<le> r" using psuminf_\<omega> assms by (cases "f i") auto
hoelzl@38656
  1131
  qed
hoelzl@38656
  1132
  from choice[OF this] obtain r where f: "f = (\<lambda>i. Real (r i))"
hoelzl@38656
  1133
    and pos: "\<forall>i. 0 \<le> r i"
nipkow@39302
  1134
    by (auto simp: fun_eq_iff)
hoelzl@38656
  1135
  hence [simp]: "\<And>i. real (f i) = r i" by auto
hoelzl@38656
  1136
hoelzl@38656
  1137
  have "mono (\<lambda>n. setsum r {..<n})" (is "mono ?S")
hoelzl@38656
  1138
    unfolding mono_iff_le_Suc using pos by simp
hoelzl@38656
  1139
hoelzl@38656
  1140
  { fix n have "0 \<le> ?S n"
hoelzl@38656
  1141
      using setsum_nonneg[of "{..<n}" r] pos by auto }
hoelzl@38656
  1142
hoelzl@38656
  1143
  from assms obtain p where *: "(\<Sum>\<^isub>\<infinity> x. f x) = Real p" and "0 \<le> p"
hoelzl@38656
  1144
    by (cases "(\<Sum>\<^isub>\<infinity> x. f x)") auto
hoelzl@38656
  1145
  show ?thesis unfolding * using * pos `0 \<le> p` SUP_eq_LIMSEQ[OF `mono ?S` `\<And>n. 0 \<le> ?S n` `0 \<le> p`]
hoelzl@38656
  1146
    by (simp add: f atLeast0LessThan sums_def psuminf_def setsum_Real'[OF pos] f)
hoelzl@38656
  1147
qed
hoelzl@38656
  1148
hoelzl@38656
  1149
lemma psuminf_bound:
hoelzl@38656
  1150
  assumes "\<forall>N. (\<Sum>n<N. f n) \<le> x"
hoelzl@38656
  1151
  shows "(\<Sum>\<^isub>\<infinity> n. f n) \<le> x"
hoelzl@38656
  1152
  using assms by (simp add: psuminf_def SUPR_def Sup_le_iff)
hoelzl@38656
  1153
hoelzl@38656
  1154
lemma psuminf_bound_add:
hoelzl@38656
  1155
  assumes "\<forall>N. (\<Sum>n<N. f n) + y \<le> x"
hoelzl@38656
  1156
  shows "(\<Sum>\<^isub>\<infinity> n. f n) + y \<le> x"
hoelzl@38656
  1157
proof (cases "x = \<omega>")
hoelzl@41023
  1158
  have "y \<le> x" using assms by (auto intro: pextreal_le_add2)
hoelzl@38656
  1159
  assume "x \<noteq> \<omega>"
hoelzl@41023
  1160
  note move_y = pextreal_less_eq_diff_eq_sum[OF `y \<le> x` this]
hoelzl@38656
  1161
hoelzl@38656
  1162
  have "\<forall>N. (\<Sum>n<N. f n) \<le> x - y" using assms by (simp add: move_y)
hoelzl@38656
  1163
  hence "(\<Sum>\<^isub>\<infinity> n. f n) \<le> x - y" by (rule psuminf_bound)
hoelzl@38656
  1164
  thus ?thesis by (simp add: move_y)
hoelzl@38656
  1165
qed simp
hoelzl@38656
  1166
hoelzl@38656
  1167
lemma psuminf_finite:
hoelzl@38656
  1168
  assumes "\<forall>N\<ge>n. f N = 0"
hoelzl@38656
  1169
  shows "(\<Sum>\<^isub>\<infinity> n. f n) = (\<Sum>N<n. f N)"
hoelzl@38656
  1170
proof (rule psuminf_equality)
hoelzl@38656
  1171
  fix N
hoelzl@38656
  1172
  show "setsum f {..<N} \<le> setsum f {..<n}"
hoelzl@38656
  1173
  proof (cases rule: linorder_cases)
hoelzl@38656
  1174
    assume "N < n" thus ?thesis by (auto intro!: setsum_mono3)
hoelzl@38656
  1175
  next
hoelzl@38656
  1176
    assume "n < N"
hoelzl@38656
  1177
    hence *: "{..<N} = {..<n} \<union> {n..<N}" by auto
hoelzl@38656
  1178
    moreover have "setsum f {n..<N} = 0"
hoelzl@38656
  1179
      using assms by (auto intro!: setsum_0')
hoelzl@38656
  1180
    ultimately show ?thesis unfolding *
hoelzl@38656
  1181
      by (subst setsum_Un_disjoint) auto
hoelzl@38656
  1182
  qed simp
hoelzl@38656
  1183
qed simp
hoelzl@38656
  1184
hoelzl@38656
  1185
lemma psuminf_upper:
hoelzl@38656
  1186
  shows "(\<Sum>n<N. f n) \<le> (\<Sum>\<^isub>\<infinity> n. f n)"
hoelzl@38656
  1187
  unfolding psuminf_def SUPR_def
hoelzl@38656
  1188
  by (auto intro: complete_lattice_class.Sup_upper image_eqI)
hoelzl@38656
  1189
hoelzl@38656
  1190
lemma psuminf_le:
hoelzl@38656
  1191
  assumes "\<And>N. f N \<le> g N"
hoelzl@38656
  1192
  shows "psuminf f \<le> psuminf g"
hoelzl@38656
  1193
proof (safe intro!: psuminf_bound)
hoelzl@38656
  1194
  fix n
hoelzl@38656
  1195
  have "setsum f {..<n} \<le> setsum g {..<n}" using assms by (auto intro: setsum_mono)
hoelzl@38656
  1196
  also have "... \<le> psuminf g" by (rule psuminf_upper)
hoelzl@38656
  1197
  finally show "setsum f {..<n} \<le> psuminf g" .
hoelzl@38656
  1198
qed
hoelzl@38656
  1199
hoelzl@38656
  1200
lemma psuminf_const[simp]: "psuminf (\<lambda>n. c) = (if c = 0 then 0 else \<omega>)" (is "_ = ?if")
hoelzl@38656
  1201
proof (rule psuminf_equality)
hoelzl@38656
  1202
  fix y assume *: "\<And>n :: nat. (\<Sum>n<n. c) \<le> y" and "y \<noteq> \<omega>"
hoelzl@38656
  1203
  then obtain r p where
hoelzl@38656
  1204
    y: "y = Real r" "0 \<le> r" and
hoelzl@38656
  1205
    c: "c = Real p" "0 \<le> p"
hoelzl@38656
  1206
      using *[of 1] by (cases c, cases y) auto
hoelzl@38656
  1207
  show "(if c = 0 then 0 else \<omega>) \<le> y"
hoelzl@38656
  1208
  proof (cases "p = 0")
hoelzl@38656
  1209
    assume "p = 0" with c show ?thesis by simp
hoelzl@38656
  1210
  next
hoelzl@38656
  1211
    assume "p \<noteq> 0"
hoelzl@38656
  1212
    with * c y have **: "\<And>n :: nat. real n \<le> r / p"
hoelzl@38656
  1213
      by (auto simp: zero_le_mult_iff field_simps)
hoelzl@38656
  1214
    from ex_less_of_nat[of "r / p"] guess n ..
hoelzl@38656
  1215
    with **[of n] show ?thesis by (simp add: real_eq_of_nat)
hoelzl@38656
  1216
  qed
hoelzl@38656
  1217
qed (cases "c = 0", simp_all)
hoelzl@38656
  1218
hoelzl@38656
  1219
lemma psuminf_add[simp]: "psuminf (\<lambda>n. f n + g n) = psuminf f + psuminf g"
hoelzl@38656
  1220
proof (rule psuminf_equality)
hoelzl@38656
  1221
  fix n
hoelzl@38656
  1222
  from psuminf_upper[of f n] psuminf_upper[of g n]
hoelzl@38656
  1223
  show "(\<Sum>n<n. f n + g n) \<le> psuminf f + psuminf g"
hoelzl@38656
  1224
    by (auto simp add: setsum_addf intro!: add_mono)
hoelzl@38656
  1225
next
hoelzl@38656
  1226
  fix y assume *: "\<And>n. (\<Sum>n<n. f n + g n) \<le> y" and "y \<noteq> \<omega>"
hoelzl@38656
  1227
  { fix n m
hoelzl@38656
  1228
    have **: "(\<Sum>n<n. f n) + (\<Sum>n<m. g n) \<le> y"
hoelzl@38656
  1229
    proof (cases rule: linorder_le_cases)
hoelzl@38656
  1230
      assume "n \<le> m"
hoelzl@38656
  1231
      hence "(\<Sum>n<n. f n) + (\<Sum>n<m. g n) \<le> (\<Sum>n<m. f n) + (\<Sum>n<m. g n)"
hoelzl@38656
  1232
        by (auto intro!: add_right_mono setsum_mono3)
hoelzl@38656
  1233
      also have "... \<le> y"
hoelzl@38656
  1234
        using * by (simp add: setsum_addf)
hoelzl@38656
  1235
      finally show ?thesis .
hoelzl@38656
  1236
    next
hoelzl@38656
  1237
      assume "m \<le> n"
hoelzl@38656
  1238
      hence "(\<Sum>n<n. f n) + (\<Sum>n<m. g n) \<le> (\<Sum>n<n. f n) + (\<Sum>n<n. g n)"
hoelzl@38656
  1239
        by (auto intro!: add_left_mono setsum_mono3)
hoelzl@38656
  1240
      also have "... \<le> y"
hoelzl@38656
  1241
        using * by (simp add: setsum_addf)
hoelzl@38656
  1242
      finally show ?thesis .
hoelzl@38656
  1243
    qed }
hoelzl@38656
  1244
  hence "\<And>m. \<forall>n. (\<Sum>n<n. f n) + (\<Sum>n<m. g n) \<le> y" by simp
hoelzl@38656
  1245
  from psuminf_bound_add[OF this]
hoelzl@38656
  1246
  have "\<forall>m. (\<Sum>n<m. g n) + psuminf f \<le> y" by (simp add: ac_simps)
hoelzl@38656
  1247
  from psuminf_bound_add[OF this]
hoelzl@38656
  1248
  show "psuminf f + psuminf g \<le> y" by (simp add: ac_simps)
hoelzl@38656
  1249
qed
hoelzl@38656
  1250
hoelzl@38656
  1251
lemma psuminf_0: "psuminf f = 0 \<longleftrightarrow> (\<forall>i. f i = 0)"
hoelzl@38656
  1252
proof safe
hoelzl@38656
  1253
  assume "\<forall>i. f i = 0"
nipkow@39302
  1254
  hence "f = (\<lambda>i. 0)" by (simp add: fun_eq_iff)
hoelzl@38656
  1255
  thus "psuminf f = 0" using psuminf_const by simp
hoelzl@38656
  1256
next
hoelzl@38656
  1257
  fix i assume "psuminf f = 0"
hoelzl@38656
  1258
  hence "(\<Sum>n<Suc i. f n) = 0" using psuminf_upper[of f "Suc i"] by simp
hoelzl@38656
  1259
  thus "f i = 0" by simp
hoelzl@38656
  1260
qed
hoelzl@38656
  1261
hoelzl@38656
  1262
lemma psuminf_cmult_right[simp]: "psuminf (\<lambda>n. c * f n) = c * psuminf f"
hoelzl@38656
  1263
proof (rule psuminf_equality)
hoelzl@38656
  1264
  fix n show "(\<Sum>n<n. c * f n) \<le> c * psuminf f"
hoelzl@38656
  1265
   by (auto simp: setsum_right_distrib[symmetric] intro: mult_left_mono psuminf_upper)
hoelzl@38656
  1266
next
hoelzl@38656
  1267
  fix y
hoelzl@38656
  1268
  assume "\<And>n. (\<Sum>n<n. c * f n) \<le> y"
hoelzl@38656
  1269
  hence *: "\<And>n. c * (\<Sum>n<n. f n) \<le> y" by (auto simp add: setsum_right_distrib)
hoelzl@38656
  1270
  thus "c * psuminf f \<le> y"
hoelzl@38656
  1271
  proof (cases "c = \<omega> \<or> c = 0")
hoelzl@38656
  1272
    assume "c = \<omega> \<or> c = 0"
hoelzl@38656
  1273
    thus ?thesis
hoelzl@38656
  1274
      using * by (fastsimp simp add: psuminf_0 setsum_0 split: split_if_asm)
hoelzl@38656
  1275
  next
hoelzl@38656
  1276
    assume "\<not> (c = \<omega> \<or> c = 0)"
hoelzl@38656
  1277
    hence "c \<noteq> 0" "c \<noteq> \<omega>" by auto
hoelzl@41023
  1278
    note rewrite_div = pextreal_inverse_le_eq[OF this, of _ y]
hoelzl@38656
  1279
    hence "\<forall>n. (\<Sum>n<n. f n) \<le> y / c" using * by simp
hoelzl@38656
  1280
    hence "psuminf f \<le> y / c" by (rule psuminf_bound)
hoelzl@38656
  1281
    thus ?thesis using rewrite_div by simp
hoelzl@38656
  1282
  qed
hoelzl@38656
  1283
qed
hoelzl@38656
  1284
hoelzl@38656
  1285
lemma psuminf_cmult_left[simp]: "psuminf (\<lambda>n. f n * c) = psuminf f * c"
hoelzl@38656
  1286
  using psuminf_cmult_right[of c f] by (simp add: ac_simps)
hoelzl@38656
  1287
hoelzl@38656
  1288
lemma psuminf_half_series: "psuminf (\<lambda>n. (1/2)^Suc n) = 1"
hoelzl@38656
  1289
  using suminf_imp_psuminf[OF power_half_series] by auto
hoelzl@38656
  1290
hoelzl@38656
  1291
lemma setsum_pinfsum: "(\<Sum>\<^isub>\<infinity> n. \<Sum>m\<in>A. f n m) = (\<Sum>m\<in>A. (\<Sum>\<^isub>\<infinity> n. f n m))"
hoelzl@38656
  1292
proof (cases "finite A")
hoelzl@38656
  1293
  assume "finite A"
hoelzl@38656
  1294
  thus ?thesis by induct simp_all
hoelzl@38656
  1295
qed simp
hoelzl@38656
  1296
hoelzl@38656
  1297
lemma psuminf_reindex:
hoelzl@38656
  1298
  fixes f:: "nat \<Rightarrow> nat" assumes "bij f"
hoelzl@38656
  1299
  shows "psuminf (g \<circ> f) = psuminf g"
hoelzl@38656
  1300
proof -
hoelzl@38656
  1301
  have [intro, simp]: "\<And>A. inj_on f A" using `bij f` unfolding bij_def by (auto intro: subset_inj_on)
hoelzl@38656
  1302
  have f[intro, simp]: "\<And>x. f (inv f x) = x"
hoelzl@38656
  1303
    using `bij f` unfolding bij_def by (auto intro: surj_f_inv_f)
hoelzl@38656
  1304
  show ?thesis
hoelzl@38656
  1305
  proof (rule psuminf_equality)
hoelzl@38656
  1306
    fix n
hoelzl@38656
  1307
    have "setsum (g \<circ> f) {..<n} = setsum g (f ` {..<n})"
hoelzl@38656
  1308
      by (simp add: setsum_reindex)
hoelzl@38656
  1309
    also have "\<dots> \<le> setsum g {..Max (f ` {..<n})}"
hoelzl@38656
  1310
      by (rule setsum_mono3) auto
hoelzl@38656
  1311
    also have "\<dots> \<le> psuminf g" unfolding lessThan_Suc_atMost[symmetric] by (rule psuminf_upper)
hoelzl@38656
  1312
    finally show "setsum (g \<circ> f) {..<n} \<le> psuminf g" .
hoelzl@38656
  1313
  next
hoelzl@38656
  1314
    fix y assume *: "\<And>n. setsum (g \<circ> f) {..<n} \<le> y"
hoelzl@38656
  1315
    show "psuminf g \<le> y"
hoelzl@38656
  1316
    proof (safe intro!: psuminf_bound)
hoelzl@38656
  1317
      fix N
hoelzl@38656
  1318
      have "setsum g {..<N} \<le> setsum g (f ` {..Max (inv f ` {..<N})})"
hoelzl@38656
  1319
        by (rule setsum_mono3) (auto intro!: image_eqI[where f="f", OF f[symmetric]])
hoelzl@38656
  1320
      also have "\<dots> = setsum (g \<circ> f) {..Max (inv f ` {..<N})}"
hoelzl@38656
  1321
        by (simp add: setsum_reindex)
hoelzl@38656
  1322
      also have "\<dots> \<le> y" unfolding lessThan_Suc_atMost[symmetric] by (rule *)
hoelzl@38656
  1323
      finally show "setsum g {..<N} \<le> y" .
hoelzl@38656
  1324
    qed
hoelzl@38656
  1325
  qed
hoelzl@38656
  1326
qed
hoelzl@38656
  1327
hoelzl@41023
  1328
lemma pextreal_mult_less_right:
hoelzl@38656
  1329
  assumes "b * a < c * a" "0 < a" "a < \<omega>"
hoelzl@38656
  1330
  shows "b < c"
hoelzl@38656
  1331
  using assms
hoelzl@38656
  1332
  by (cases a, cases b, cases c) (auto split: split_if_asm simp: zero_less_mult_iff zero_le_mult_iff)
hoelzl@38656
  1333
hoelzl@41023
  1334
lemma pextreal_\<omega>_eq_plus[simp]: "\<omega> = a + b \<longleftrightarrow> (a = \<omega> \<or> b = \<omega>)"
hoelzl@38656
  1335
  by (cases a, cases b) auto
hoelzl@38656
  1336
hoelzl@41023
  1337
lemma pextreal_of_nat_le_iff:
hoelzl@41023
  1338
  "(of_nat k :: pextreal) \<le> of_nat m \<longleftrightarrow> k \<le> m" by auto
hoelzl@38656
  1339
hoelzl@41023
  1340
lemma pextreal_of_nat_less_iff:
hoelzl@41023
  1341
  "(of_nat k :: pextreal) < of_nat m \<longleftrightarrow> k < m" by auto
hoelzl@38656
  1342
hoelzl@41023
  1343
lemma pextreal_bound_add:
hoelzl@41023
  1344
  assumes "\<forall>N. f N + y \<le> (x::pextreal)"
hoelzl@38656
  1345
  shows "(SUP n. f n) + y \<le> x"
hoelzl@38656
  1346
proof (cases "x = \<omega>")
hoelzl@41023
  1347
  have "y \<le> x" using assms by (auto intro: pextreal_le_add2)
hoelzl@38656
  1348
  assume "x \<noteq> \<omega>"
hoelzl@41023
  1349
  note move_y = pextreal_less_eq_diff_eq_sum[OF `y \<le> x` this]
hoelzl@38656
  1350
hoelzl@38656
  1351
  have "\<forall>N. f N \<le> x - y" using assms by (simp add: move_y)
hoelzl@38656
  1352
  hence "(SUP n. f n) \<le> x - y" by (rule SUPR_bound)
hoelzl@38656
  1353
  thus ?thesis by (simp add: move_y)
hoelzl@38656
  1354
qed simp
hoelzl@38656
  1355
hoelzl@41023
  1356
lemma SUPR_pextreal_add:
hoelzl@41023
  1357
  fixes f g :: "nat \<Rightarrow> pextreal"
hoelzl@38656
  1358
  assumes f: "\<forall>n. f n \<le> f (Suc n)" and g: "\<forall>n. g n \<le> g (Suc n)"
hoelzl@38656
  1359
  shows "(SUP n. f n + g n) = (SUP n. f n) + (SUP n. g n)"
hoelzl@41023
  1360
proof (rule pextreal_SUPI)
hoelzl@38656
  1361
  fix n :: nat from le_SUPI[of n UNIV f] le_SUPI[of n UNIV g]
hoelzl@38656
  1362
  show "f n + g n \<le> (SUP n. f n) + (SUP n. g n)"
hoelzl@38656
  1363
    by (auto intro!: add_mono)
hoelzl@38656
  1364
next
hoelzl@38656
  1365
  fix y assume *: "\<And>n. n \<in> UNIV \<Longrightarrow> f n + g n \<le> y"
hoelzl@38656
  1366
  { fix n m
hoelzl@38656
  1367
    have "f n + g m \<le> y"
hoelzl@38656
  1368
    proof (cases rule: linorder_le_cases)
hoelzl@38656
  1369
      assume "n \<le> m"
hoelzl@38656
  1370
      hence "f n + g m \<le> f m + g m"
hoelzl@38656
  1371
        using f lift_Suc_mono_le by (auto intro!: add_right_mono)
hoelzl@38656
  1372
      also have "\<dots> \<le> y" using * by simp
hoelzl@38656
  1373
      finally show ?thesis .
hoelzl@38656
  1374
    next
hoelzl@38656
  1375
      assume "m \<le> n"
hoelzl@38656
  1376
      hence "f n + g m \<le> f n + g n"
hoelzl@38656
  1377
        using g lift_Suc_mono_le by (auto intro!: add_left_mono)
hoelzl@38656
  1378
      also have "\<dots> \<le> y" using * by simp
hoelzl@38656
  1379
      finally show ?thesis .
hoelzl@38656
  1380
    qed }
hoelzl@38656
  1381
  hence "\<And>m. \<forall>n. f n + g m \<le> y" by simp
hoelzl@41023
  1382
  from pextreal_bound_add[OF this]
hoelzl@38656
  1383
  have "\<forall>m. (g m) + (SUP n. f n) \<le> y" by (simp add: ac_simps)
hoelzl@41023
  1384
  from pextreal_bound_add[OF this]
hoelzl@38656
  1385
  show "SUPR UNIV f + SUPR UNIV g \<le> y" by (simp add: ac_simps)
hoelzl@38656
  1386
qed
hoelzl@38656
  1387
hoelzl@41023
  1388
lemma SUPR_pextreal_setsum:
hoelzl@41023
  1389
  fixes f :: "'x \<Rightarrow> nat \<Rightarrow> pextreal"
hoelzl@38656
  1390
  assumes "\<And>i. i \<in> P \<Longrightarrow> \<forall>n. f i n \<le> f i (Suc n)"
hoelzl@38656
  1391
  shows "(SUP n. \<Sum>i\<in>P. f i n) = (\<Sum>i\<in>P. SUP n. f i n)"
hoelzl@38656
  1392
proof cases
hoelzl@38656
  1393
  assume "finite P" from this assms show ?thesis
hoelzl@38656
  1394
  proof induct
hoelzl@38656
  1395
    case (insert i P)
hoelzl@38656
  1396
    thus ?case
hoelzl@38656
  1397
      apply simp
hoelzl@41023
  1398
      apply (subst SUPR_pextreal_add)
hoelzl@38656
  1399
      by (auto intro!: setsum_mono)
hoelzl@38656
  1400
  qed simp
hoelzl@38656
  1401
qed simp
hoelzl@38656
  1402
hoelzl@40871
  1403
lemma psuminf_SUP_eq:
hoelzl@40871
  1404
  assumes "\<And>n i. f n i \<le> f (Suc n) i"
hoelzl@40871
  1405
  shows "(\<Sum>\<^isub>\<infinity> i. SUP n::nat. f n i) = (SUP n::nat. \<Sum>\<^isub>\<infinity> i. f n i)"
hoelzl@40871
  1406
proof -
hoelzl@40871
  1407
  { fix n :: nat
hoelzl@40871
  1408
    have "(\<Sum>i<n. SUP k. f k i) = (SUP k. \<Sum>i<n. f k i)"
hoelzl@41023
  1409
      using assms by (auto intro!: SUPR_pextreal_setsum[symmetric]) }
hoelzl@40871
  1410
  note * = this
hoelzl@40871
  1411
  show ?thesis
hoelzl@40871
  1412
    unfolding psuminf_def
hoelzl@40871
  1413
    unfolding *
hoelzl@40872
  1414
    apply (subst SUP_commute) ..
hoelzl@40871
  1415
qed
hoelzl@40871
  1416
hoelzl@40871
  1417
lemma psuminf_commute:
hoelzl@40871
  1418
  shows "(\<Sum>\<^isub>\<infinity> i j. f i j) = (\<Sum>\<^isub>\<infinity> j i. f i j)"
hoelzl@40871
  1419
proof -
hoelzl@40871
  1420
  have "(SUP n. \<Sum> i < n. SUP m. \<Sum> j < m. f i j) = (SUP n. SUP m. \<Sum> i < n. \<Sum> j < m. f i j)"
hoelzl@41023
  1421
    apply (subst SUPR_pextreal_setsum)
hoelzl@40871
  1422
    by auto
hoelzl@40871
  1423
  also have "\<dots> = (SUP m n. \<Sum> j < m. \<Sum> i < n. f i j)"
hoelzl@40872
  1424
    apply (subst SUP_commute)
hoelzl@40871
  1425
    apply (subst setsum_commute)
hoelzl@40871
  1426
    by auto
hoelzl@40871
  1427
  also have "\<dots> = (SUP m. \<Sum> j < m. SUP n. \<Sum> i < n. f i j)"
hoelzl@41023
  1428
    apply (subst SUPR_pextreal_setsum)
hoelzl@40871
  1429
    by auto
hoelzl@40871
  1430
  finally show ?thesis
hoelzl@40871
  1431
    unfolding psuminf_def by auto
hoelzl@40871
  1432
qed
hoelzl@40871
  1433
hoelzl@40872
  1434
lemma psuminf_2dimen:
hoelzl@41023
  1435
  fixes f:: "nat * nat \<Rightarrow> pextreal"
hoelzl@40872
  1436
  assumes fsums: "\<And>m. g m = (\<Sum>\<^isub>\<infinity> n. f (m,n))"
hoelzl@40872
  1437
  shows "psuminf (f \<circ> prod_decode) = psuminf g"
hoelzl@40872
  1438
proof (rule psuminf_equality)
hoelzl@40872
  1439
  fix n :: nat
hoelzl@40872
  1440
  let ?P = "prod_decode ` {..<n}"
hoelzl@40872
  1441
  have "setsum (f \<circ> prod_decode) {..<n} = setsum f ?P"
hoelzl@40872
  1442
    by (auto simp: setsum_reindex inj_prod_decode)
hoelzl@40872
  1443
  also have "\<dots> \<le> setsum f ({..Max (fst ` ?P)} \<times> {..Max (snd ` ?P)})"
hoelzl@40872
  1444
  proof (safe intro!: setsum_mono3 Max_ge image_eqI)
hoelzl@40872
  1445
    fix a b x assume "(a, b) = prod_decode x"
hoelzl@40872
  1446
    from this[symmetric] show "a = fst (prod_decode x)" "b = snd (prod_decode x)"
hoelzl@40872
  1447
      by simp_all
hoelzl@40872
  1448
  qed simp_all
hoelzl@40872
  1449
  also have "\<dots> = (\<Sum>m\<le>Max (fst ` ?P). (\<Sum>n\<le>Max (snd ` ?P). f (m,n)))"
hoelzl@40872
  1450
    unfolding setsum_cartesian_product by simp
hoelzl@40872
  1451
  also have "\<dots> \<le> (\<Sum>m\<le>Max (fst ` ?P). g m)"
hoelzl@40872
  1452
    by (auto intro!: setsum_mono psuminf_upper simp del: setsum_lessThan_Suc
hoelzl@40872
  1453
        simp: fsums lessThan_Suc_atMost[symmetric])
hoelzl@40872
  1454
  also have "\<dots> \<le> psuminf g"
hoelzl@40872
  1455
    by (auto intro!: psuminf_upper simp del: setsum_lessThan_Suc
hoelzl@40872
  1456
        simp: lessThan_Suc_atMost[symmetric])
hoelzl@40872
  1457
  finally show "setsum (f \<circ> prod_decode) {..<n} \<le> psuminf g" .
hoelzl@40872
  1458
next
hoelzl@40872
  1459
  fix y assume *: "\<And>n. setsum (f \<circ> prod_decode) {..<n} \<le> y"
hoelzl@40872
  1460
  have g: "g = (\<lambda>m. \<Sum>\<^isub>\<infinity> n. f (m,n))" unfolding fsums[symmetric] ..
hoelzl@40872
  1461
  show "psuminf g \<le> y" unfolding g
hoelzl@40872
  1462
  proof (rule psuminf_bound, unfold setsum_pinfsum[symmetric], safe intro!: psuminf_bound)
hoelzl@40872
  1463
    fix N M :: nat
hoelzl@40872
  1464
    let ?P = "{..<N} \<times> {..<M}"
hoelzl@40872
  1465
    let ?M = "Max (prod_encode ` ?P)"
hoelzl@40872
  1466
    have "(\<Sum>n<M. \<Sum>m<N. f (m, n)) \<le> (\<Sum>(m, n)\<in>?P. f (m, n))"
hoelzl@40872
  1467
      unfolding setsum_commute[of _ _ "{..<M}"] unfolding setsum_cartesian_product ..
hoelzl@40872
  1468
    also have "\<dots> \<le> (\<Sum>(m,n)\<in>(prod_decode ` {..?M}). f (m, n))"
hoelzl@40872
  1469
      by (auto intro!: setsum_mono3 image_eqI[where f=prod_decode, OF prod_encode_inverse[symmetric]])
hoelzl@40872
  1470
    also have "\<dots> \<le> y" using *[of "Suc ?M"]
hoelzl@40872
  1471
      by (simp add: lessThan_Suc_atMost[symmetric] setsum_reindex
hoelzl@40872
  1472
               inj_prod_decode del: setsum_lessThan_Suc)
hoelzl@40872
  1473
    finally show "(\<Sum>n<M. \<Sum>m<N. f (m, n)) \<le> y" .
hoelzl@40872
  1474
  qed
hoelzl@40872
  1475
qed
hoelzl@40872
  1476
hoelzl@38656
  1477
lemma Real_max:
hoelzl@38656
  1478
  assumes "x \<ge> 0" "y \<ge> 0"
hoelzl@38656
  1479
  shows "Real (max x y) = max (Real x) (Real y)"
hoelzl@38656
  1480
  using assms unfolding max_def by (auto simp add:not_le)
hoelzl@38656
  1481
hoelzl@38656
  1482
lemma Real_real: "Real (real x) = (if x = \<omega> then 0 else x)"
hoelzl@38656
  1483
  using assms by (cases x) auto
hoelzl@38656
  1484
hoelzl@38656
  1485
lemma inj_on_real: "inj_on real (UNIV - {\<omega>})"
hoelzl@38656
  1486
proof (rule inj_onI)
hoelzl@38656
  1487
  fix x y assume mem: "x \<in> UNIV - {\<omega>}" "y \<in> UNIV - {\<omega>}" and "real x = real y"
hoelzl@38656
  1488
  thus "x = y" by (cases x, cases y) auto
hoelzl@38656
  1489
qed
hoelzl@38656
  1490
hoelzl@38656
  1491
lemma inj_on_Real: "inj_on Real {0..}"
hoelzl@38656
  1492
  by (auto intro!: inj_onI)
hoelzl@38656
  1493
hoelzl@38656
  1494
lemma range_Real[simp]: "range Real = UNIV - {\<omega>}"
hoelzl@38656
  1495
proof safe
hoelzl@38656
  1496
  fix x assume "x \<notin> range Real"
hoelzl@38656
  1497
  thus "x = \<omega>" by (cases x) auto
hoelzl@38656
  1498
qed auto
hoelzl@38656
  1499
hoelzl@38656
  1500
lemma image_Real[simp]: "Real ` {0..} = UNIV - {\<omega>}"
hoelzl@38656
  1501
proof safe
hoelzl@38656
  1502
  fix x assume "x \<notin> Real ` {0..}"
hoelzl@38656
  1503
  thus "x = \<omega>" by (cases x) auto
hoelzl@38656
  1504
qed auto
hoelzl@38656
  1505
hoelzl@41023
  1506
lemma pextreal_SUP_cmult:
hoelzl@41023
  1507
  fixes f :: "'a \<Rightarrow> pextreal"
hoelzl@38656
  1508
  shows "(SUP i : R. z * f i) = z * (SUP i : R. f i)"
hoelzl@41023
  1509
proof (rule pextreal_SUPI)
hoelzl@38656
  1510
  fix i assume "i \<in> R"
hoelzl@38656
  1511
  from le_SUPI[OF this]
hoelzl@41023
  1512
  show "z * f i \<le> z * (SUP i:R. f i)" by (rule pextreal_mult_cancel)
hoelzl@38656
  1513
next
hoelzl@38656
  1514
  fix y assume "\<And>i. i\<in>R \<Longrightarrow> z * f i \<le> y"
hoelzl@38656
  1515
  hence *: "\<And>i. i\<in>R \<Longrightarrow> z * f i \<le> y" by auto
hoelzl@38656
  1516
  show "z * (SUP i:R. f i) \<le> y"
hoelzl@38656
  1517
  proof (cases "\<forall>i\<in>R. f i = 0")
hoelzl@38656
  1518
    case True
hoelzl@38656
  1519
    show ?thesis
hoelzl@38656
  1520
    proof cases
hoelzl@38656
  1521
      assume "R \<noteq> {}" hence "f ` R = {0}" using True by auto
hoelzl@38656
  1522
      thus ?thesis by (simp add: SUPR_def)
hoelzl@41023
  1523
    qed (simp add: SUPR_def Sup_empty bot_pextreal_def)
hoelzl@38656
  1524
  next
hoelzl@38656
  1525
    case False then obtain i where i: "i \<in> R" and f0: "f i \<noteq> 0" by auto
hoelzl@38656
  1526
    show ?thesis
hoelzl@38656
  1527
    proof (cases "z = 0 \<or> z = \<omega>")
hoelzl@38656
  1528
      case True with f0 *[OF i] show ?thesis by auto
hoelzl@38656
  1529
    next
hoelzl@38656
  1530
      case False hence z: "z \<noteq> 0" "z \<noteq> \<omega>" by auto
hoelzl@41023
  1531
      note div = pextreal_inverse_le_eq[OF this, symmetric]
hoelzl@38656
  1532
      hence "\<And>i. i\<in>R \<Longrightarrow> f i \<le> y / z" using * by auto
hoelzl@38656
  1533
      thus ?thesis unfolding div SUP_le_iff by simp
hoelzl@38656
  1534
    qed
hoelzl@38656
  1535
  qed
hoelzl@38656
  1536
qed
hoelzl@38656
  1537
hoelzl@41023
  1538
instantiation pextreal :: topological_space
hoelzl@38656
  1539
begin
hoelzl@38656
  1540
hoelzl@38656
  1541
definition "open A \<longleftrightarrow>
hoelzl@38656
  1542
  (\<exists>T. open T \<and> (Real ` (T\<inter>{0..}) = A - {\<omega>})) \<and> (\<omega> \<in> A \<longrightarrow> (\<exists>x\<ge>0. {Real x <..} \<subseteq> A))"
hoelzl@38656
  1543
hoelzl@38656
  1544
lemma open_omega: "open A \<Longrightarrow> \<omega> \<in> A \<Longrightarrow> (\<exists>x\<ge>0. {Real x<..} \<subseteq> A)"
hoelzl@41023
  1545
  unfolding open_pextreal_def by auto
hoelzl@38656
  1546
hoelzl@38656
  1547
lemma open_omegaD: assumes "open A" "\<omega> \<in> A" obtains x where "x\<ge>0" "{Real x<..} \<subseteq> A"
hoelzl@38656
  1548
  using open_omega[OF assms] by auto
hoelzl@38656
  1549
hoelzl@41023
  1550
lemma pextreal_openE: assumes "open A" obtains A' x where
hoelzl@38656
  1551
  "open A'" "Real ` (A' \<inter> {0..}) = A - {\<omega>}"
hoelzl@38656
  1552
  "x \<ge> 0" "\<omega> \<in> A \<Longrightarrow> {Real x<..} \<subseteq> A"
hoelzl@41023
  1553
  using assms open_pextreal_def by auto
hoelzl@38656
  1554
hoelzl@38656
  1555
instance
hoelzl@38656
  1556
proof
hoelzl@41023
  1557
  let ?U = "UNIV::pextreal set"
hoelzl@41023
  1558
  show "open ?U" unfolding open_pextreal_def
hoelzl@38656
  1559
    by (auto intro!: exI[of _ "UNIV"] exI[of _ 0])
hoelzl@38656
  1560
next
hoelzl@41023
  1561
  fix S T::"pextreal set" assume "open S" and "open T"
hoelzl@41023
  1562
  from `open S`[THEN pextreal_openE] guess S' xS . note S' = this
hoelzl@41023
  1563
  from `open T`[THEN pextreal_openE] guess T' xT . note T' = this
hoelzl@38656
  1564
hoelzl@38656
  1565
  from S'(1-3) T'(1-3)
hoelzl@41023
  1566
  show "open (S \<inter> T)" unfolding open_pextreal_def
hoelzl@38656
  1567
  proof (safe intro!: exI[of _ "S' \<inter> T'"] exI[of _ "max xS xT"])
hoelzl@38656
  1568
    fix x assume *: "Real (max xS xT) < x" and "\<omega> \<in> S" "\<omega> \<in> T"
hoelzl@38656
  1569
    from `\<omega> \<in> S`[THEN S'(4)] * show "x \<in> S"
hoelzl@38656
  1570
      by (cases x, auto simp: max_def split: split_if_asm)
hoelzl@38656
  1571
    from `\<omega> \<in> T`[THEN T'(4)] * show "x \<in> T"
hoelzl@38656
  1572
      by (cases x, auto simp: max_def split: split_if_asm)
hoelzl@38656
  1573
  next
hoelzl@38656
  1574
    fix x assume x: "x \<notin> Real ` (S' \<inter> T' \<inter> {0..})"
hoelzl@38656
  1575
    have *: "S' \<inter> T' \<inter> {0..} = (S' \<inter> {0..}) \<inter> (T' \<inter> {0..})" by auto
hoelzl@38656
  1576
    assume "x \<in> T" "x \<in> S"
hoelzl@38656
  1577
    with S'(2) T'(2) show "x = \<omega>"
hoelzl@38656
  1578
      using x[unfolded *] inj_on_image_Int[OF inj_on_Real] by auto
hoelzl@38656
  1579
  qed auto
hoelzl@38656
  1580
next
hoelzl@41023
  1581
  fix K assume openK: "\<forall>S \<in> K. open (S:: pextreal set)"
hoelzl@41023
  1582
  hence "\<forall>S\<in>K. \<exists>T. open T \<and> Real ` (T \<inter> {0..}) = S - {\<omega>}" by (auto simp: open_pextreal_def)
hoelzl@38656
  1583
  from bchoice[OF this] guess T .. note T = this[rule_format]
hoelzl@38656
  1584
hoelzl@41023
  1585
  show "open (\<Union>K)" unfolding open_pextreal_def
hoelzl@38656
  1586
  proof (safe intro!: exI[of _ "\<Union>(T ` K)"])
hoelzl@38656
  1587
    fix x S assume "0 \<le> x" "x \<in> T S" "S \<in> K"
hoelzl@38656
  1588
    with T[OF `S \<in> K`] show "Real x \<in> \<Union>K" by auto
hoelzl@38656
  1589
  next
hoelzl@38656
  1590
    fix x S assume x: "x \<notin> Real ` (\<Union>T ` K \<inter> {0..})" "S \<in> K" "x \<in> S"
hoelzl@38656
  1591
    hence "x \<notin> Real ` (T S \<inter> {0..})"
hoelzl@38656
  1592
      by (auto simp: image_UN UN_simps[symmetric] simp del: UN_simps)
hoelzl@38656
  1593
    thus "x = \<omega>" using T[OF `S \<in> K`] `x \<in> S` by auto
hoelzl@38656
  1594
  next
hoelzl@38656
  1595
    fix S assume "\<omega> \<in> S" "S \<in> K"
hoelzl@41023
  1596
    from openK[rule_format, OF `S \<in> K`, THEN pextreal_openE] guess S' x .
hoelzl@38656
  1597
    from this(3, 4) `\<omega> \<in> S`
hoelzl@38656
  1598
    show "\<exists>x\<ge>0. {Real x<..} \<subseteq> \<Union>K"
hoelzl@38656
  1599
      by (auto intro!: exI[of _ x] bexI[OF _ `S \<in> K`])
hoelzl@38656
  1600
  next
hoelzl@38656
  1601
    from T[THEN conjunct1] show "open (\<Union>T`K)" by auto
hoelzl@38656
  1602
  qed auto
hoelzl@38656
  1603
qed
hoelzl@38656
  1604
end
hoelzl@38656
  1605
hoelzl@41023
  1606
lemma open_pextreal_lessThan[simp]:
hoelzl@41023
  1607
  "open {..< a :: pextreal}"
hoelzl@38656
  1608
proof (cases a)
hoelzl@41023
  1609
  case (preal x) thus ?thesis unfolding open_pextreal_def
hoelzl@38656
  1610
  proof (safe intro!: exI[of _ "{..< x}"])
hoelzl@38656
  1611
    fix y assume "y < Real x"
hoelzl@38656
  1612
    moreover assume "y \<notin> Real ` ({..<x} \<inter> {0..})"
hoelzl@38656
  1613
    ultimately have "y \<noteq> Real (real y)" using preal by (cases y) auto
hoelzl@38656
  1614
    thus "y = \<omega>" by (auto simp: Real_real split: split_if_asm)
hoelzl@38656
  1615
  qed auto
hoelzl@38656
  1616
next
hoelzl@38656
  1617
  case infinite thus ?thesis
hoelzl@41023
  1618
    unfolding open_pextreal_def by (auto intro!: exI[of _ UNIV])
hoelzl@38656
  1619
qed
hoelzl@38656
  1620
hoelzl@41023
  1621
lemma open_pextreal_greaterThan[simp]:
hoelzl@41023
  1622
  "open {a :: pextreal <..}"
hoelzl@38656
  1623
proof (cases a)
hoelzl@41023
  1624
  case (preal x) thus ?thesis unfolding open_pextreal_def
hoelzl@38656
  1625
  proof (safe intro!: exI[of _ "{x <..}"])
hoelzl@38656
  1626
    fix y assume "Real x < y"
hoelzl@38656
  1627
    moreover assume "y \<notin> Real ` ({x<..} \<inter> {0..})"
hoelzl@38656
  1628
    ultimately have "y \<noteq> Real (real y)" using preal by (cases y) auto
hoelzl@38656
  1629
    thus "y = \<omega>" by (auto simp: Real_real split: split_if_asm)
hoelzl@38656
  1630
  qed auto
hoelzl@38656
  1631
next
hoelzl@38656
  1632
  case infinite thus ?thesis
hoelzl@41023
  1633
    unfolding open_pextreal_def by (auto intro!: exI[of _ "{}"])
hoelzl@38656
  1634
qed
hoelzl@38656
  1635
hoelzl@41023
  1636
lemma pextreal_open_greaterThanLessThan[simp]: "open {a::pextreal <..< b}"
hoelzl@38656
  1637
  unfolding greaterThanLessThan_def by auto
hoelzl@38656
  1638
hoelzl@41023
  1639
lemma closed_pextreal_atLeast[simp, intro]: "closed {a :: pextreal ..}"
hoelzl@38656
  1640
proof -
hoelzl@38656
  1641
  have "- {a ..} = {..< a}" by auto
hoelzl@38656
  1642
  then show "closed {a ..}"
hoelzl@41023
  1643
    unfolding closed_def using open_pextreal_lessThan by auto
hoelzl@38656
  1644
qed
hoelzl@38656
  1645
hoelzl@41023
  1646
lemma closed_pextreal_atMost[simp, intro]: "closed {.. b :: pextreal}"
hoelzl@38656
  1647
proof -
hoelzl@38656
  1648
  have "- {.. b} = {b <..}" by auto
hoelzl@38656
  1649
  then show "closed {.. b}" 
hoelzl@41023
  1650
    unfolding closed_def using open_pextreal_greaterThan by auto
hoelzl@38656
  1651
qed
hoelzl@38656
  1652
hoelzl@41023
  1653
lemma closed_pextreal_atLeastAtMost[simp, intro]:
hoelzl@41023
  1654
  shows "closed {a :: pextreal .. b}"
hoelzl@38656
  1655
  unfolding atLeastAtMost_def by auto
hoelzl@38656
  1656
hoelzl@41023
  1657
lemma pextreal_dense:
hoelzl@41023
  1658
  fixes x y :: pextreal assumes "x < y"
hoelzl@38656
  1659
  shows "\<exists>z. x < z \<and> z < y"
hoelzl@38656
  1660
proof -
hoelzl@38656
  1661
  from `x < y` obtain p where p: "x = Real p" "0 \<le> p" by (cases x) auto
hoelzl@38656
  1662
  show ?thesis
hoelzl@38656
  1663
  proof (cases y)
hoelzl@38656
  1664
    case (preal r) with p `x < y` have "p < r" by auto
hoelzl@38656
  1665
    with dense obtain z where "p < z" "z < r" by auto
hoelzl@38656
  1666
    thus ?thesis using preal p by (auto intro!: exI[of _ "Real z"])
hoelzl@38656
  1667
  next
hoelzl@38656
  1668
    case infinite thus ?thesis using `x < y` p
hoelzl@38656
  1669
      by (auto intro!: exI[of _ "Real p + 1"])
hoelzl@38656
  1670
  qed
hoelzl@38656
  1671
qed
hoelzl@38656
  1672
hoelzl@41023
  1673
instance pextreal :: t2_space
hoelzl@38656
  1674
proof
hoelzl@41023
  1675
  fix x y :: pextreal assume "x \<noteq> y"
hoelzl@41023
  1676
  let "?P x (y::pextreal)" = "\<exists> U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
hoelzl@38656
  1677
hoelzl@41023
  1678
  { fix x y :: pextreal assume "x < y"
hoelzl@41023
  1679
    from pextreal_dense[OF this] obtain z where z: "x < z" "z < y" by auto
hoelzl@38656
  1680
    have "?P x y"
hoelzl@38656
  1681
      apply (rule exI[of _ "{..<z}"])
hoelzl@38656
  1682
      apply (rule exI[of _ "{z<..}"])
hoelzl@38656
  1683
      using z by auto }
hoelzl@38656
  1684
  note * = this
hoelzl@38656
  1685
hoelzl@38656
  1686
  from `x \<noteq> y`
hoelzl@38656
  1687
  show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
hoelzl@38656
  1688
  proof (cases rule: linorder_cases)
hoelzl@38656
  1689
    assume "x = y" with `x \<noteq> y` show ?thesis by simp
hoelzl@38656
  1690
  next assume "x < y" from *[OF this] show ?thesis by auto
hoelzl@38656
  1691
  next assume "y < x" from *[OF this] show ?thesis by auto
hoelzl@38656
  1692
  qed
hoelzl@38656
  1693
qed
hoelzl@38656
  1694
hoelzl@38656
  1695
definition (in complete_lattice) isoton :: "(nat \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> bool" (infix "\<up>" 50) where
hoelzl@38656
  1696
  "A \<up> X \<longleftrightarrow> (\<forall>i. A i \<le> A (Suc i)) \<and> (SUP i. A i) = X"
hoelzl@38656
  1697
hoelzl@38656
  1698
definition (in complete_lattice) antiton (infix "\<down>" 50) where
hoelzl@38656
  1699
  "A \<down> X \<longleftrightarrow> (\<forall>i. A i \<ge> A (Suc i)) \<and> (INF i. A i) = X"
hoelzl@38656
  1700
hoelzl@40859
  1701
lemma isotoneI[intro?]: "\<lbrakk> \<And>i. f i \<le> f (Suc i) ; (SUP i. f i) = F \<rbrakk> \<Longrightarrow> f \<up> F"
hoelzl@40859
  1702
  unfolding isoton_def by auto
hoelzl@40859
  1703
hoelzl@40859
  1704
lemma (in complete_lattice) isotonD[dest]:
hoelzl@40859
  1705
  assumes "A \<up> X" shows "A i \<le> A (Suc i)" "(SUP i. A i) = X"
hoelzl@40859
  1706
  using assms unfolding isoton_def by auto
hoelzl@40859
  1707
hoelzl@40859
  1708
lemma isotonD'[dest]:
hoelzl@40859
  1709
  assumes "(A::_=>_) \<up> X" shows "A i x \<le> A (Suc i) x" "(SUP i. A i) = X"
hoelzl@40859
  1710
  using assms unfolding isoton_def le_fun_def by auto
hoelzl@40859
  1711