src/HOL/Real_Vector_Spaces.thy
author paulson
Mon Jan 11 22:14:15 2016 +0000 (2016-01-11)
changeset 62131 1baed43f453e
parent 62102 877463945ce9
child 62347 2230b7047376
permissions -rw-r--r--
nonneg_Reals, nonpos_Reals, Cauchy integral formula, etc.
hoelzl@51524
     1
(*  Title:      HOL/Real_Vector_Spaces.thy
haftmann@27552
     2
    Author:     Brian Huffman
hoelzl@51531
     3
    Author:     Johannes Hölzl
huffman@20504
     4
*)
huffman@20504
     5
wenzelm@60758
     6
section \<open>Vector Spaces and Algebras over the Reals\<close>
huffman@20504
     7
hoelzl@51524
     8
theory Real_Vector_Spaces
hoelzl@51531
     9
imports Real Topological_Spaces
huffman@20504
    10
begin
huffman@20504
    11
lp15@61762
    12
lemma (in ordered_ab_group_add) diff_ge_0_iff_ge [simp]: "a - b \<ge> 0 \<longleftrightarrow> a \<ge> b"
lp15@61762
    13
  by (simp add: le_diff_eq)
lp15@61762
    14
wenzelm@60758
    15
subsection \<open>Locale for additive functions\<close>
huffman@20504
    16
huffman@20504
    17
locale additive =
huffman@20504
    18
  fixes f :: "'a::ab_group_add \<Rightarrow> 'b::ab_group_add"
huffman@20504
    19
  assumes add: "f (x + y) = f x + f y"
huffman@27443
    20
begin
huffman@20504
    21
huffman@27443
    22
lemma zero: "f 0 = 0"
huffman@20504
    23
proof -
huffman@20504
    24
  have "f 0 = f (0 + 0)" by simp
huffman@20504
    25
  also have "\<dots> = f 0 + f 0" by (rule add)
huffman@20504
    26
  finally show "f 0 = 0" by simp
huffman@20504
    27
qed
huffman@20504
    28
huffman@27443
    29
lemma minus: "f (- x) = - f x"
huffman@20504
    30
proof -
huffman@20504
    31
  have "f (- x) + f x = f (- x + x)" by (rule add [symmetric])
huffman@20504
    32
  also have "\<dots> = - f x + f x" by (simp add: zero)
huffman@20504
    33
  finally show "f (- x) = - f x" by (rule add_right_imp_eq)
huffman@20504
    34
qed
huffman@20504
    35
huffman@27443
    36
lemma diff: "f (x - y) = f x - f y"
haftmann@54230
    37
  using add [of x "- y"] by (simp add: minus)
huffman@20504
    38
huffman@27443
    39
lemma setsum: "f (setsum g A) = (\<Sum>x\<in>A. f (g x))"
huffman@22942
    40
apply (cases "finite A")
huffman@22942
    41
apply (induct set: finite)
huffman@22942
    42
apply (simp add: zero)
huffman@22942
    43
apply (simp add: add)
huffman@22942
    44
apply (simp add: zero)
huffman@22942
    45
done
huffman@22942
    46
huffman@27443
    47
end
huffman@20504
    48
wenzelm@60758
    49
subsection \<open>Vector spaces\<close>
huffman@28029
    50
huffman@28029
    51
locale vector_space =
huffman@28029
    52
  fixes scale :: "'a::field \<Rightarrow> 'b::ab_group_add \<Rightarrow> 'b"
huffman@30070
    53
  assumes scale_right_distrib [algebra_simps]:
huffman@30070
    54
    "scale a (x + y) = scale a x + scale a y"
huffman@30070
    55
  and scale_left_distrib [algebra_simps]:
huffman@30070
    56
    "scale (a + b) x = scale a x + scale b x"
huffman@28029
    57
  and scale_scale [simp]: "scale a (scale b x) = scale (a * b) x"
huffman@28029
    58
  and scale_one [simp]: "scale 1 x = x"
huffman@28029
    59
begin
huffman@28029
    60
huffman@28029
    61
lemma scale_left_commute:
huffman@28029
    62
  "scale a (scale b x) = scale b (scale a x)"
haftmann@57512
    63
by (simp add: mult.commute)
huffman@28029
    64
huffman@28029
    65
lemma scale_zero_left [simp]: "scale 0 x = 0"
huffman@28029
    66
  and scale_minus_left [simp]: "scale (- a) x = - (scale a x)"
huffman@30070
    67
  and scale_left_diff_distrib [algebra_simps]:
huffman@30070
    68
        "scale (a - b) x = scale a x - scale b x"
huffman@44282
    69
  and scale_setsum_left: "scale (setsum f A) x = (\<Sum>a\<in>A. scale (f a) x)"
huffman@28029
    70
proof -
ballarin@29229
    71
  interpret s: additive "\<lambda>a. scale a x"
haftmann@28823
    72
    proof qed (rule scale_left_distrib)
huffman@28029
    73
  show "scale 0 x = 0" by (rule s.zero)
huffman@28029
    74
  show "scale (- a) x = - (scale a x)" by (rule s.minus)
huffman@28029
    75
  show "scale (a - b) x = scale a x - scale b x" by (rule s.diff)
huffman@44282
    76
  show "scale (setsum f A) x = (\<Sum>a\<in>A. scale (f a) x)" by (rule s.setsum)
huffman@28029
    77
qed
huffman@28029
    78
huffman@28029
    79
lemma scale_zero_right [simp]: "scale a 0 = 0"
huffman@28029
    80
  and scale_minus_right [simp]: "scale a (- x) = - (scale a x)"
huffman@30070
    81
  and scale_right_diff_distrib [algebra_simps]:
huffman@30070
    82
        "scale a (x - y) = scale a x - scale a y"
huffman@44282
    83
  and scale_setsum_right: "scale a (setsum f A) = (\<Sum>x\<in>A. scale a (f x))"
huffman@28029
    84
proof -
ballarin@29229
    85
  interpret s: additive "\<lambda>x. scale a x"
haftmann@28823
    86
    proof qed (rule scale_right_distrib)
huffman@28029
    87
  show "scale a 0 = 0" by (rule s.zero)
huffman@28029
    88
  show "scale a (- x) = - (scale a x)" by (rule s.minus)
huffman@28029
    89
  show "scale a (x - y) = scale a x - scale a y" by (rule s.diff)
huffman@44282
    90
  show "scale a (setsum f A) = (\<Sum>x\<in>A. scale a (f x))" by (rule s.setsum)
huffman@28029
    91
qed
huffman@28029
    92
huffman@28029
    93
lemma scale_eq_0_iff [simp]:
huffman@28029
    94
  "scale a x = 0 \<longleftrightarrow> a = 0 \<or> x = 0"
huffman@28029
    95
proof cases
huffman@28029
    96
  assume "a = 0" thus ?thesis by simp
huffman@28029
    97
next
huffman@28029
    98
  assume anz [simp]: "a \<noteq> 0"
huffman@28029
    99
  { assume "scale a x = 0"
huffman@28029
   100
    hence "scale (inverse a) (scale a x) = 0" by simp
huffman@28029
   101
    hence "x = 0" by simp }
huffman@28029
   102
  thus ?thesis by force
huffman@28029
   103
qed
huffman@28029
   104
huffman@28029
   105
lemma scale_left_imp_eq:
huffman@28029
   106
  "\<lbrakk>a \<noteq> 0; scale a x = scale a y\<rbrakk> \<Longrightarrow> x = y"
huffman@28029
   107
proof -
huffman@28029
   108
  assume nonzero: "a \<noteq> 0"
huffman@28029
   109
  assume "scale a x = scale a y"
huffman@28029
   110
  hence "scale a (x - y) = 0"
huffman@28029
   111
     by (simp add: scale_right_diff_distrib)
huffman@28029
   112
  hence "x - y = 0" by (simp add: nonzero)
huffman@28029
   113
  thus "x = y" by (simp only: right_minus_eq)
huffman@28029
   114
qed
huffman@28029
   115
huffman@28029
   116
lemma scale_right_imp_eq:
huffman@28029
   117
  "\<lbrakk>x \<noteq> 0; scale a x = scale b x\<rbrakk> \<Longrightarrow> a = b"
huffman@28029
   118
proof -
huffman@28029
   119
  assume nonzero: "x \<noteq> 0"
huffman@28029
   120
  assume "scale a x = scale b x"
huffman@28029
   121
  hence "scale (a - b) x = 0"
huffman@28029
   122
     by (simp add: scale_left_diff_distrib)
huffman@28029
   123
  hence "a - b = 0" by (simp add: nonzero)
huffman@28029
   124
  thus "a = b" by (simp only: right_minus_eq)
huffman@28029
   125
qed
huffman@28029
   126
huffman@31586
   127
lemma scale_cancel_left [simp]:
huffman@28029
   128
  "scale a x = scale a y \<longleftrightarrow> x = y \<or> a = 0"
huffman@28029
   129
by (auto intro: scale_left_imp_eq)
huffman@28029
   130
huffman@31586
   131
lemma scale_cancel_right [simp]:
huffman@28029
   132
  "scale a x = scale b x \<longleftrightarrow> a = b \<or> x = 0"
huffman@28029
   133
by (auto intro: scale_right_imp_eq)
huffman@28029
   134
huffman@28029
   135
end
huffman@28029
   136
wenzelm@60758
   137
subsection \<open>Real vector spaces\<close>
huffman@20504
   138
haftmann@29608
   139
class scaleR =
haftmann@25062
   140
  fixes scaleR :: "real \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "*\<^sub>R" 75)
haftmann@24748
   141
begin
huffman@20504
   142
huffman@20763
   143
abbreviation
haftmann@25062
   144
  divideR :: "'a \<Rightarrow> real \<Rightarrow> 'a" (infixl "'/\<^sub>R" 70)
haftmann@24748
   145
where
haftmann@25062
   146
  "x /\<^sub>R r == scaleR (inverse r) x"
haftmann@24748
   147
haftmann@24748
   148
end
haftmann@24748
   149
haftmann@24588
   150
class real_vector = scaleR + ab_group_add +
huffman@44282
   151
  assumes scaleR_add_right: "scaleR a (x + y) = scaleR a x + scaleR a y"
huffman@44282
   152
  and scaleR_add_left: "scaleR (a + b) x = scaleR a x + scaleR b x"
huffman@30070
   153
  and scaleR_scaleR: "scaleR a (scaleR b x) = scaleR (a * b) x"
huffman@30070
   154
  and scaleR_one: "scaleR 1 x = x"
huffman@20504
   155
wenzelm@30729
   156
interpretation real_vector:
ballarin@29229
   157
  vector_space "scaleR :: real \<Rightarrow> 'a \<Rightarrow> 'a::real_vector"
huffman@28009
   158
apply unfold_locales
huffman@44282
   159
apply (rule scaleR_add_right)
huffman@44282
   160
apply (rule scaleR_add_left)
huffman@28009
   161
apply (rule scaleR_scaleR)
huffman@28009
   162
apply (rule scaleR_one)
huffman@28009
   163
done
huffman@28009
   164
wenzelm@60758
   165
text \<open>Recover original theorem names\<close>
huffman@28009
   166
huffman@28009
   167
lemmas scaleR_left_commute = real_vector.scale_left_commute
huffman@28009
   168
lemmas scaleR_zero_left = real_vector.scale_zero_left
huffman@28009
   169
lemmas scaleR_minus_left = real_vector.scale_minus_left
huffman@44282
   170
lemmas scaleR_diff_left = real_vector.scale_left_diff_distrib
huffman@44282
   171
lemmas scaleR_setsum_left = real_vector.scale_setsum_left
huffman@28009
   172
lemmas scaleR_zero_right = real_vector.scale_zero_right
huffman@28009
   173
lemmas scaleR_minus_right = real_vector.scale_minus_right
huffman@44282
   174
lemmas scaleR_diff_right = real_vector.scale_right_diff_distrib
huffman@44282
   175
lemmas scaleR_setsum_right = real_vector.scale_setsum_right
huffman@28009
   176
lemmas scaleR_eq_0_iff = real_vector.scale_eq_0_iff
huffman@28009
   177
lemmas scaleR_left_imp_eq = real_vector.scale_left_imp_eq
huffman@28009
   178
lemmas scaleR_right_imp_eq = real_vector.scale_right_imp_eq
huffman@28009
   179
lemmas scaleR_cancel_left = real_vector.scale_cancel_left
huffman@28009
   180
lemmas scaleR_cancel_right = real_vector.scale_cancel_right
huffman@28009
   181
wenzelm@60758
   182
text \<open>Legacy names\<close>
huffman@44282
   183
huffman@44282
   184
lemmas scaleR_left_distrib = scaleR_add_left
huffman@44282
   185
lemmas scaleR_right_distrib = scaleR_add_right
huffman@44282
   186
lemmas scaleR_left_diff_distrib = scaleR_diff_left
huffman@44282
   187
lemmas scaleR_right_diff_distrib = scaleR_diff_right
huffman@44282
   188
huffman@31285
   189
lemma scaleR_minus1_left [simp]:
huffman@31285
   190
  fixes x :: "'a::real_vector"
huffman@31285
   191
  shows "scaleR (-1) x = - x"
huffman@31285
   192
  using scaleR_minus_left [of 1 x] by simp
hoelzl@62101
   193
haftmann@24588
   194
class real_algebra = real_vector + ring +
haftmann@25062
   195
  assumes mult_scaleR_left [simp]: "scaleR a x * y = scaleR a (x * y)"
haftmann@25062
   196
  and mult_scaleR_right [simp]: "x * scaleR a y = scaleR a (x * y)"
huffman@20504
   197
haftmann@24588
   198
class real_algebra_1 = real_algebra + ring_1
huffman@20554
   199
haftmann@24588
   200
class real_div_algebra = real_algebra_1 + division_ring
huffman@20584
   201
haftmann@24588
   202
class real_field = real_div_algebra + field
huffman@20584
   203
huffman@30069
   204
instantiation real :: real_field
huffman@30069
   205
begin
huffman@30069
   206
huffman@30069
   207
definition
huffman@30069
   208
  real_scaleR_def [simp]: "scaleR a x = a * x"
huffman@30069
   209
huffman@30070
   210
instance proof
huffman@30070
   211
qed (simp_all add: algebra_simps)
huffman@20554
   212
huffman@30069
   213
end
huffman@30069
   214
wenzelm@30729
   215
interpretation scaleR_left: additive "(\<lambda>a. scaleR a x::'a::real_vector)"
haftmann@28823
   216
proof qed (rule scaleR_left_distrib)
huffman@20504
   217
wenzelm@30729
   218
interpretation scaleR_right: additive "(\<lambda>x. scaleR a x::'a::real_vector)"
haftmann@28823
   219
proof qed (rule scaleR_right_distrib)
huffman@20504
   220
huffman@20584
   221
lemma nonzero_inverse_scaleR_distrib:
huffman@21809
   222
  fixes x :: "'a::real_div_algebra" shows
huffman@21809
   223
  "\<lbrakk>a \<noteq> 0; x \<noteq> 0\<rbrakk> \<Longrightarrow> inverse (scaleR a x) = scaleR (inverse a) (inverse x)"
huffman@20763
   224
by (rule inverse_unique, simp)
huffman@20584
   225
huffman@20584
   226
lemma inverse_scaleR_distrib:
haftmann@59867
   227
  fixes x :: "'a::{real_div_algebra, division_ring}"
huffman@21809
   228
  shows "inverse (scaleR a x) = scaleR (inverse a) (inverse x)"
huffman@20584
   229
apply (case_tac "a = 0", simp)
huffman@20584
   230
apply (case_tac "x = 0", simp)
huffman@20584
   231
apply (erule (1) nonzero_inverse_scaleR_distrib)
huffman@20584
   232
done
huffman@20584
   233
eberlm@61531
   234
lemma setsum_constant_scaleR:
eberlm@61531
   235
  fixes y :: "'a::real_vector"
eberlm@61531
   236
  shows "(\<Sum>x\<in>A. y) = of_nat (card A) *\<^sub>R y"
eberlm@61531
   237
  apply (cases "finite A")
eberlm@61531
   238
  apply (induct set: finite)
eberlm@61531
   239
  apply (simp_all add: algebra_simps)
eberlm@61531
   240
  done
eberlm@61531
   241
lp15@60800
   242
lemma real_vector_affinity_eq:
lp15@60800
   243
  fixes x :: "'a :: real_vector"
lp15@60800
   244
  assumes m0: "m \<noteq> 0"
lp15@60800
   245
  shows "m *\<^sub>R x + c = y \<longleftrightarrow> x = inverse m *\<^sub>R y - (inverse m *\<^sub>R c)"
lp15@60800
   246
proof
lp15@60800
   247
  assume h: "m *\<^sub>R x + c = y"
lp15@60800
   248
  hence "m *\<^sub>R x = y - c" by (simp add: field_simps)
lp15@60800
   249
  hence "inverse m *\<^sub>R (m *\<^sub>R x) = inverse m *\<^sub>R (y - c)" by simp
lp15@60800
   250
  then show "x = inverse m *\<^sub>R y - (inverse m *\<^sub>R c)"
lp15@60800
   251
    using m0
lp15@60800
   252
  by (simp add: real_vector.scale_right_diff_distrib)
lp15@60800
   253
next
lp15@60800
   254
  assume h: "x = inverse m *\<^sub>R y - (inverse m *\<^sub>R c)"
lp15@60800
   255
  show "m *\<^sub>R x + c = y" unfolding h
lp15@60800
   256
    using m0  by (simp add: real_vector.scale_right_diff_distrib)
lp15@60800
   257
qed
lp15@60800
   258
lp15@60800
   259
lemma real_vector_eq_affinity:
lp15@60800
   260
  fixes x :: "'a :: real_vector"
lp15@60800
   261
  shows "m \<noteq> 0 ==> (y = m *\<^sub>R x + c \<longleftrightarrow> inverse m *\<^sub>R y - (inverse m *\<^sub>R c) = x)"
lp15@60800
   262
  using real_vector_affinity_eq[where m=m and x=x and y=y and c=c]
lp15@60800
   263
  by metis
lp15@60800
   264
huffman@20554
   265
wenzelm@61799
   266
subsection \<open>Embedding of the Reals into any \<open>real_algebra_1\<close>:
wenzelm@60758
   267
@{term of_real}\<close>
huffman@20554
   268
huffman@20554
   269
definition
wenzelm@21404
   270
  of_real :: "real \<Rightarrow> 'a::real_algebra_1" where
huffman@21809
   271
  "of_real r = scaleR r 1"
huffman@20554
   272
huffman@21809
   273
lemma scaleR_conv_of_real: "scaleR r x = of_real r * x"
huffman@20763
   274
by (simp add: of_real_def)
huffman@20763
   275
huffman@20554
   276
lemma of_real_0 [simp]: "of_real 0 = 0"
huffman@20554
   277
by (simp add: of_real_def)
huffman@20554
   278
huffman@20554
   279
lemma of_real_1 [simp]: "of_real 1 = 1"
huffman@20554
   280
by (simp add: of_real_def)
huffman@20554
   281
huffman@20554
   282
lemma of_real_add [simp]: "of_real (x + y) = of_real x + of_real y"
huffman@20554
   283
by (simp add: of_real_def scaleR_left_distrib)
huffman@20554
   284
huffman@20554
   285
lemma of_real_minus [simp]: "of_real (- x) = - of_real x"
huffman@20554
   286
by (simp add: of_real_def)
huffman@20554
   287
huffman@20554
   288
lemma of_real_diff [simp]: "of_real (x - y) = of_real x - of_real y"
huffman@20554
   289
by (simp add: of_real_def scaleR_left_diff_distrib)
huffman@20554
   290
huffman@20554
   291
lemma of_real_mult [simp]: "of_real (x * y) = of_real x * of_real y"
haftmann@57512
   292
by (simp add: of_real_def mult.commute)
huffman@20554
   293
hoelzl@56889
   294
lemma of_real_setsum[simp]: "of_real (setsum f s) = (\<Sum>x\<in>s. of_real (f x))"
hoelzl@56889
   295
  by (induct s rule: infinite_finite_induct) auto
hoelzl@56889
   296
hoelzl@56889
   297
lemma of_real_setprod[simp]: "of_real (setprod f s) = (\<Prod>x\<in>s. of_real (f x))"
hoelzl@56889
   298
  by (induct s rule: infinite_finite_induct) auto
hoelzl@56889
   299
huffman@20584
   300
lemma nonzero_of_real_inverse:
huffman@20584
   301
  "x \<noteq> 0 \<Longrightarrow> of_real (inverse x) =
huffman@20584
   302
   inverse (of_real x :: 'a::real_div_algebra)"
huffman@20584
   303
by (simp add: of_real_def nonzero_inverse_scaleR_distrib)
huffman@20584
   304
huffman@20584
   305
lemma of_real_inverse [simp]:
huffman@20584
   306
  "of_real (inverse x) =
haftmann@59867
   307
   inverse (of_real x :: 'a::{real_div_algebra, division_ring})"
huffman@20584
   308
by (simp add: of_real_def inverse_scaleR_distrib)
huffman@20584
   309
huffman@20584
   310
lemma nonzero_of_real_divide:
huffman@20584
   311
  "y \<noteq> 0 \<Longrightarrow> of_real (x / y) =
huffman@20584
   312
   (of_real x / of_real y :: 'a::real_field)"
huffman@20584
   313
by (simp add: divide_inverse nonzero_of_real_inverse)
huffman@20722
   314
huffman@20722
   315
lemma of_real_divide [simp]:
paulson@62131
   316
  "of_real (x / y) = (of_real x / of_real y :: 'a::real_div_algebra)"
huffman@20584
   317
by (simp add: divide_inverse)
huffman@20584
   318
huffman@20722
   319
lemma of_real_power [simp]:
haftmann@31017
   320
  "of_real (x ^ n) = (of_real x :: 'a::{real_algebra_1}) ^ n"
huffman@30273
   321
by (induct n) simp_all
huffman@20722
   322
huffman@20554
   323
lemma of_real_eq_iff [simp]: "(of_real x = of_real y) = (x = y)"
huffman@35216
   324
by (simp add: of_real_def)
huffman@20554
   325
haftmann@38621
   326
lemma inj_of_real:
haftmann@38621
   327
  "inj of_real"
haftmann@38621
   328
  by (auto intro: injI)
haftmann@38621
   329
huffman@20584
   330
lemmas of_real_eq_0_iff [simp] = of_real_eq_iff [of _ 0, simplified]
huffman@20554
   331
huffman@20554
   332
lemma of_real_eq_id [simp]: "of_real = (id :: real \<Rightarrow> real)"
huffman@20554
   333
proof
huffman@20554
   334
  fix r
huffman@20554
   335
  show "of_real r = id r"
huffman@22973
   336
    by (simp add: of_real_def)
huffman@20554
   337
qed
huffman@20554
   338
wenzelm@60758
   339
text\<open>Collapse nested embeddings\<close>
huffman@20554
   340
lemma of_real_of_nat_eq [simp]: "of_real (of_nat n) = of_nat n"
wenzelm@20772
   341
by (induct n) auto
huffman@20554
   342
huffman@20554
   343
lemma of_real_of_int_eq [simp]: "of_real (of_int z) = of_int z"
huffman@20554
   344
by (cases z rule: int_diff_cases, simp)
huffman@20554
   345
lp15@60155
   346
lemma of_real_numeral [simp]: "of_real (numeral w) = numeral w"
huffman@47108
   347
using of_real_of_int_eq [of "numeral w"] by simp
huffman@47108
   348
lp15@60155
   349
lemma of_real_neg_numeral [simp]: "of_real (- numeral w) = - numeral w"
haftmann@54489
   350
using of_real_of_int_eq [of "- numeral w"] by simp
huffman@20554
   351
wenzelm@60758
   352
text\<open>Every real algebra has characteristic zero\<close>
haftmann@38621
   353
huffman@22912
   354
instance real_algebra_1 < ring_char_0
huffman@22912
   355
proof
haftmann@38621
   356
  from inj_of_real inj_of_nat have "inj (of_real \<circ> of_nat)" by (rule inj_comp)
haftmann@38621
   357
  then show "inj (of_nat :: nat \<Rightarrow> 'a)" by (simp add: comp_def)
huffman@22912
   358
qed
huffman@22912
   359
huffman@27553
   360
instance real_field < field_char_0 ..
huffman@27553
   361
huffman@20554
   362
wenzelm@60758
   363
subsection \<open>The Set of Real Numbers\<close>
huffman@20554
   364
wenzelm@61070
   365
definition Reals :: "'a::real_algebra_1 set"  ("\<real>")
wenzelm@61070
   366
  where "\<real> = range of_real"
huffman@20554
   367
wenzelm@61070
   368
lemma Reals_of_real [simp]: "of_real r \<in> \<real>"
huffman@20554
   369
by (simp add: Reals_def)
huffman@20554
   370
wenzelm@61070
   371
lemma Reals_of_int [simp]: "of_int z \<in> \<real>"
huffman@21809
   372
by (subst of_real_of_int_eq [symmetric], rule Reals_of_real)
huffman@20718
   373
wenzelm@61070
   374
lemma Reals_of_nat [simp]: "of_nat n \<in> \<real>"
huffman@21809
   375
by (subst of_real_of_nat_eq [symmetric], rule Reals_of_real)
huffman@21809
   376
wenzelm@61070
   377
lemma Reals_numeral [simp]: "numeral w \<in> \<real>"
huffman@47108
   378
by (subst of_real_numeral [symmetric], rule Reals_of_real)
huffman@47108
   379
wenzelm@61070
   380
lemma Reals_0 [simp]: "0 \<in> \<real>"
huffman@20554
   381
apply (unfold Reals_def)
huffman@20554
   382
apply (rule range_eqI)
huffman@20554
   383
apply (rule of_real_0 [symmetric])
huffman@20554
   384
done
huffman@20554
   385
wenzelm@61070
   386
lemma Reals_1 [simp]: "1 \<in> \<real>"
huffman@20554
   387
apply (unfold Reals_def)
huffman@20554
   388
apply (rule range_eqI)
huffman@20554
   389
apply (rule of_real_1 [symmetric])
huffman@20554
   390
done
huffman@20554
   391
wenzelm@61070
   392
lemma Reals_add [simp]: "\<lbrakk>a \<in> \<real>; b \<in> \<real>\<rbrakk> \<Longrightarrow> a + b \<in> \<real>"
huffman@20554
   393
apply (auto simp add: Reals_def)
huffman@20554
   394
apply (rule range_eqI)
huffman@20554
   395
apply (rule of_real_add [symmetric])
huffman@20554
   396
done
huffman@20554
   397
wenzelm@61070
   398
lemma Reals_minus [simp]: "a \<in> \<real> \<Longrightarrow> - a \<in> \<real>"
huffman@20584
   399
apply (auto simp add: Reals_def)
huffman@20584
   400
apply (rule range_eqI)
huffman@20584
   401
apply (rule of_real_minus [symmetric])
huffman@20584
   402
done
huffman@20584
   403
wenzelm@61070
   404
lemma Reals_diff [simp]: "\<lbrakk>a \<in> \<real>; b \<in> \<real>\<rbrakk> \<Longrightarrow> a - b \<in> \<real>"
huffman@20584
   405
apply (auto simp add: Reals_def)
huffman@20584
   406
apply (rule range_eqI)
huffman@20584
   407
apply (rule of_real_diff [symmetric])
huffman@20584
   408
done
huffman@20584
   409
wenzelm@61070
   410
lemma Reals_mult [simp]: "\<lbrakk>a \<in> \<real>; b \<in> \<real>\<rbrakk> \<Longrightarrow> a * b \<in> \<real>"
huffman@20554
   411
apply (auto simp add: Reals_def)
huffman@20554
   412
apply (rule range_eqI)
huffman@20554
   413
apply (rule of_real_mult [symmetric])
huffman@20554
   414
done
huffman@20554
   415
huffman@20584
   416
lemma nonzero_Reals_inverse:
huffman@20584
   417
  fixes a :: "'a::real_div_algebra"
wenzelm@61070
   418
  shows "\<lbrakk>a \<in> \<real>; a \<noteq> 0\<rbrakk> \<Longrightarrow> inverse a \<in> \<real>"
huffman@20584
   419
apply (auto simp add: Reals_def)
huffman@20584
   420
apply (rule range_eqI)
huffman@20584
   421
apply (erule nonzero_of_real_inverse [symmetric])
huffman@20584
   422
done
huffman@20584
   423
lp15@55719
   424
lemma Reals_inverse:
haftmann@59867
   425
  fixes a :: "'a::{real_div_algebra, division_ring}"
wenzelm@61070
   426
  shows "a \<in> \<real> \<Longrightarrow> inverse a \<in> \<real>"
huffman@20584
   427
apply (auto simp add: Reals_def)
huffman@20584
   428
apply (rule range_eqI)
huffman@20584
   429
apply (rule of_real_inverse [symmetric])
huffman@20584
   430
done
huffman@20584
   431
lp15@60026
   432
lemma Reals_inverse_iff [simp]:
haftmann@59867
   433
  fixes x:: "'a :: {real_div_algebra, division_ring}"
lp15@55719
   434
  shows "inverse x \<in> \<real> \<longleftrightarrow> x \<in> \<real>"
lp15@55719
   435
by (metis Reals_inverse inverse_inverse_eq)
lp15@55719
   436
huffman@20584
   437
lemma nonzero_Reals_divide:
huffman@20584
   438
  fixes a b :: "'a::real_field"
wenzelm@61070
   439
  shows "\<lbrakk>a \<in> \<real>; b \<in> \<real>; b \<noteq> 0\<rbrakk> \<Longrightarrow> a / b \<in> \<real>"
huffman@20584
   440
apply (auto simp add: Reals_def)
huffman@20584
   441
apply (rule range_eqI)
huffman@20584
   442
apply (erule nonzero_of_real_divide [symmetric])
huffman@20584
   443
done
huffman@20584
   444
huffman@20584
   445
lemma Reals_divide [simp]:
haftmann@59867
   446
  fixes a b :: "'a::{real_field, field}"
wenzelm@61070
   447
  shows "\<lbrakk>a \<in> \<real>; b \<in> \<real>\<rbrakk> \<Longrightarrow> a / b \<in> \<real>"
huffman@20584
   448
apply (auto simp add: Reals_def)
huffman@20584
   449
apply (rule range_eqI)
huffman@20584
   450
apply (rule of_real_divide [symmetric])
huffman@20584
   451
done
huffman@20584
   452
huffman@20722
   453
lemma Reals_power [simp]:
haftmann@31017
   454
  fixes a :: "'a::{real_algebra_1}"
wenzelm@61070
   455
  shows "a \<in> \<real> \<Longrightarrow> a ^ n \<in> \<real>"
huffman@20722
   456
apply (auto simp add: Reals_def)
huffman@20722
   457
apply (rule range_eqI)
huffman@20722
   458
apply (rule of_real_power [symmetric])
huffman@20722
   459
done
huffman@20722
   460
huffman@20554
   461
lemma Reals_cases [cases set: Reals]:
huffman@20554
   462
  assumes "q \<in> \<real>"
huffman@20554
   463
  obtains (of_real) r where "q = of_real r"
huffman@20554
   464
  unfolding Reals_def
huffman@20554
   465
proof -
wenzelm@60758
   466
  from \<open>q \<in> \<real>\<close> have "q \<in> range of_real" unfolding Reals_def .
huffman@20554
   467
  then obtain r where "q = of_real r" ..
huffman@20554
   468
  then show thesis ..
huffman@20554
   469
qed
huffman@20554
   470
lp15@59741
   471
lemma setsum_in_Reals [intro,simp]:
lp15@59741
   472
  assumes "\<And>i. i \<in> s \<Longrightarrow> f i \<in> \<real>" shows "setsum f s \<in> \<real>"
lp15@55719
   473
proof (cases "finite s")
lp15@55719
   474
  case True then show ?thesis using assms
lp15@55719
   475
    by (induct s rule: finite_induct) auto
lp15@55719
   476
next
lp15@55719
   477
  case False then show ?thesis using assms
haftmann@57418
   478
    by (metis Reals_0 setsum.infinite)
lp15@55719
   479
qed
lp15@55719
   480
lp15@60026
   481
lemma setprod_in_Reals [intro,simp]:
lp15@59741
   482
  assumes "\<And>i. i \<in> s \<Longrightarrow> f i \<in> \<real>" shows "setprod f s \<in> \<real>"
lp15@55719
   483
proof (cases "finite s")
lp15@55719
   484
  case True then show ?thesis using assms
lp15@55719
   485
    by (induct s rule: finite_induct) auto
lp15@55719
   486
next
lp15@55719
   487
  case False then show ?thesis using assms
haftmann@57418
   488
    by (metis Reals_1 setprod.infinite)
lp15@55719
   489
qed
lp15@55719
   490
huffman@20554
   491
lemma Reals_induct [case_names of_real, induct set: Reals]:
huffman@20554
   492
  "q \<in> \<real> \<Longrightarrow> (\<And>r. P (of_real r)) \<Longrightarrow> P q"
huffman@20554
   493
  by (rule Reals_cases) auto
huffman@20554
   494
wenzelm@60758
   495
subsection \<open>Ordered real vector spaces\<close>
immler@54778
   496
immler@54778
   497
class ordered_real_vector = real_vector + ordered_ab_group_add +
immler@54778
   498
  assumes scaleR_left_mono: "x \<le> y \<Longrightarrow> 0 \<le> a \<Longrightarrow> a *\<^sub>R x \<le> a *\<^sub>R y"
immler@54778
   499
  assumes scaleR_right_mono: "a \<le> b \<Longrightarrow> 0 \<le> x \<Longrightarrow> a *\<^sub>R x \<le> b *\<^sub>R x"
immler@54778
   500
begin
immler@54778
   501
immler@54778
   502
lemma scaleR_mono:
immler@54778
   503
  "a \<le> b \<Longrightarrow> x \<le> y \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> x \<Longrightarrow> a *\<^sub>R x \<le> b *\<^sub>R y"
immler@54778
   504
apply (erule scaleR_right_mono [THEN order_trans], assumption)
immler@54778
   505
apply (erule scaleR_left_mono, assumption)
immler@54778
   506
done
immler@54778
   507
immler@54778
   508
lemma scaleR_mono':
immler@54778
   509
  "a \<le> b \<Longrightarrow> c \<le> d \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> c \<Longrightarrow> a *\<^sub>R c \<le> b *\<^sub>R d"
immler@54778
   510
  by (rule scaleR_mono) (auto intro: order.trans)
immler@54778
   511
immler@54785
   512
lemma pos_le_divideRI:
immler@54785
   513
  assumes "0 < c"
immler@54785
   514
  assumes "c *\<^sub>R a \<le> b"
immler@54785
   515
  shows "a \<le> b /\<^sub>R c"
immler@54785
   516
proof -
immler@54785
   517
  from scaleR_left_mono[OF assms(2)] assms(1)
immler@54785
   518
  have "c *\<^sub>R a /\<^sub>R c \<le> b /\<^sub>R c"
immler@54785
   519
    by simp
immler@54785
   520
  with assms show ?thesis
immler@54785
   521
    by (simp add: scaleR_one scaleR_scaleR inverse_eq_divide)
immler@54785
   522
qed
immler@54785
   523
immler@54785
   524
lemma pos_le_divideR_eq:
immler@54785
   525
  assumes "0 < c"
immler@54785
   526
  shows "a \<le> b /\<^sub>R c \<longleftrightarrow> c *\<^sub>R a \<le> b"
immler@54785
   527
proof rule
immler@54785
   528
  assume "a \<le> b /\<^sub>R c"
immler@54785
   529
  from scaleR_left_mono[OF this] assms
immler@54785
   530
  have "c *\<^sub>R a \<le> c *\<^sub>R (b /\<^sub>R c)"
immler@54785
   531
    by simp
immler@54785
   532
  with assms show "c *\<^sub>R a \<le> b"
immler@54785
   533
    by (simp add: scaleR_one scaleR_scaleR inverse_eq_divide)
immler@54785
   534
qed (rule pos_le_divideRI[OF assms])
immler@54785
   535
immler@54785
   536
lemma scaleR_image_atLeastAtMost:
immler@54785
   537
  "c > 0 \<Longrightarrow> scaleR c ` {x..y} = {c *\<^sub>R x..c *\<^sub>R y}"
immler@54785
   538
  apply (auto intro!: scaleR_left_mono)
immler@54785
   539
  apply (rule_tac x = "inverse c *\<^sub>R xa" in image_eqI)
immler@54785
   540
  apply (simp_all add: pos_le_divideR_eq[symmetric] scaleR_scaleR scaleR_one)
immler@54785
   541
  done
immler@54785
   542
immler@54778
   543
end
immler@54778
   544
paulson@60303
   545
lemma neg_le_divideR_eq:
paulson@60303
   546
  fixes a :: "'a :: ordered_real_vector"
paulson@60303
   547
  assumes "c < 0"
paulson@60303
   548
  shows "a \<le> b /\<^sub>R c \<longleftrightarrow> b \<le> c *\<^sub>R a"
paulson@60303
   549
  using pos_le_divideR_eq [of "-c" a "-b"] assms
paulson@60303
   550
  by simp
paulson@60303
   551
immler@54778
   552
lemma scaleR_nonneg_nonneg: "0 \<le> a \<Longrightarrow> 0 \<le> (x::'a::ordered_real_vector) \<Longrightarrow> 0 \<le> a *\<^sub>R x"
immler@54778
   553
  using scaleR_left_mono [of 0 x a]
immler@54778
   554
  by simp
immler@54778
   555
immler@54778
   556
lemma scaleR_nonneg_nonpos: "0 \<le> a \<Longrightarrow> (x::'a::ordered_real_vector) \<le> 0 \<Longrightarrow> a *\<^sub>R x \<le> 0"
immler@54778
   557
  using scaleR_left_mono [of x 0 a] by simp
immler@54778
   558
immler@54778
   559
lemma scaleR_nonpos_nonneg: "a \<le> 0 \<Longrightarrow> 0 \<le> (x::'a::ordered_real_vector) \<Longrightarrow> a *\<^sub>R x \<le> 0"
immler@54778
   560
  using scaleR_right_mono [of a 0 x] by simp
immler@54778
   561
immler@54778
   562
lemma split_scaleR_neg_le: "(0 \<le> a & x \<le> 0) | (a \<le> 0 & 0 \<le> x) \<Longrightarrow>
immler@54778
   563
  a *\<^sub>R (x::'a::ordered_real_vector) \<le> 0"
immler@54778
   564
  by (auto simp add: scaleR_nonneg_nonpos scaleR_nonpos_nonneg)
immler@54778
   565
immler@54778
   566
lemma le_add_iff1:
immler@54778
   567
  fixes c d e::"'a::ordered_real_vector"
immler@54778
   568
  shows "a *\<^sub>R e + c \<le> b *\<^sub>R e + d \<longleftrightarrow> (a - b) *\<^sub>R e + c \<le> d"
immler@54778
   569
  by (simp add: algebra_simps)
immler@54778
   570
immler@54778
   571
lemma le_add_iff2:
immler@54778
   572
  fixes c d e::"'a::ordered_real_vector"
immler@54778
   573
  shows "a *\<^sub>R e + c \<le> b *\<^sub>R e + d \<longleftrightarrow> c \<le> (b - a) *\<^sub>R e + d"
immler@54778
   574
  by (simp add: algebra_simps)
immler@54778
   575
immler@54778
   576
lemma scaleR_left_mono_neg:
immler@54778
   577
  fixes a b::"'a::ordered_real_vector"
immler@54778
   578
  shows "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b"
immler@54778
   579
  apply (drule scaleR_left_mono [of _ _ "- c"])
immler@54778
   580
  apply simp_all
immler@54778
   581
  done
immler@54778
   582
immler@54778
   583
lemma scaleR_right_mono_neg:
immler@54778
   584
  fixes c::"'a::ordered_real_vector"
immler@54778
   585
  shows "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> a *\<^sub>R c \<le> b *\<^sub>R c"
immler@54778
   586
  apply (drule scaleR_right_mono [of _ _ "- c"])
immler@54778
   587
  apply simp_all
immler@54778
   588
  done
immler@54778
   589
immler@54778
   590
lemma scaleR_nonpos_nonpos: "a \<le> 0 \<Longrightarrow> (b::'a::ordered_real_vector) \<le> 0 \<Longrightarrow> 0 \<le> a *\<^sub>R b"
immler@54778
   591
using scaleR_right_mono_neg [of a 0 b] by simp
immler@54778
   592
immler@54778
   593
lemma split_scaleR_pos_le:
immler@54778
   594
  fixes b::"'a::ordered_real_vector"
immler@54778
   595
  shows "(0 \<le> a \<and> 0 \<le> b) \<or> (a \<le> 0 \<and> b \<le> 0) \<Longrightarrow> 0 \<le> a *\<^sub>R b"
immler@54778
   596
  by (auto simp add: scaleR_nonneg_nonneg scaleR_nonpos_nonpos)
immler@54778
   597
immler@54778
   598
lemma zero_le_scaleR_iff:
immler@54778
   599
  fixes b::"'a::ordered_real_vector"
immler@54778
   600
  shows "0 \<le> a *\<^sub>R b \<longleftrightarrow> 0 < a \<and> 0 \<le> b \<or> a < 0 \<and> b \<le> 0 \<or> a = 0" (is "?lhs = ?rhs")
immler@54778
   601
proof cases
immler@54778
   602
  assume "a \<noteq> 0"
immler@54778
   603
  show ?thesis
immler@54778
   604
  proof
immler@54778
   605
    assume lhs: ?lhs
immler@54778
   606
    {
immler@54778
   607
      assume "0 < a"
immler@54778
   608
      with lhs have "inverse a *\<^sub>R 0 \<le> inverse a *\<^sub>R (a *\<^sub>R b)"
immler@54778
   609
        by (intro scaleR_mono) auto
wenzelm@60758
   610
      hence ?rhs using \<open>0 < a\<close>
immler@54778
   611
        by simp
immler@54778
   612
    } moreover {
immler@54778
   613
      assume "0 > a"
immler@54778
   614
      with lhs have "- inverse a *\<^sub>R 0 \<le> - inverse a *\<^sub>R (a *\<^sub>R b)"
immler@54778
   615
        by (intro scaleR_mono) auto
wenzelm@60758
   616
      hence ?rhs using \<open>0 > a\<close>
immler@54778
   617
        by simp
wenzelm@60758
   618
    } ultimately show ?rhs using \<open>a \<noteq> 0\<close> by arith
wenzelm@60758
   619
  qed (auto simp: not_le \<open>a \<noteq> 0\<close> intro!: split_scaleR_pos_le)
immler@54778
   620
qed simp
immler@54778
   621
immler@54778
   622
lemma scaleR_le_0_iff:
immler@54778
   623
  fixes b::"'a::ordered_real_vector"
immler@54778
   624
  shows "a *\<^sub>R b \<le> 0 \<longleftrightarrow> 0 < a \<and> b \<le> 0 \<or> a < 0 \<and> 0 \<le> b \<or> a = 0"
immler@54778
   625
  by (insert zero_le_scaleR_iff [of "-a" b]) force
immler@54778
   626
immler@54778
   627
lemma scaleR_le_cancel_left:
immler@54778
   628
  fixes b::"'a::ordered_real_vector"
immler@54778
   629
  shows "c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
immler@54778
   630
  by (auto simp add: neq_iff scaleR_left_mono scaleR_left_mono_neg
immler@54778
   631
    dest: scaleR_left_mono[where a="inverse c"] scaleR_left_mono_neg[where c="inverse c"])
immler@54778
   632
immler@54778
   633
lemma scaleR_le_cancel_left_pos:
immler@54778
   634
  fixes b::"'a::ordered_real_vector"
immler@54778
   635
  shows "0 < c \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> a \<le> b"
immler@54778
   636
  by (auto simp: scaleR_le_cancel_left)
immler@54778
   637
immler@54778
   638
lemma scaleR_le_cancel_left_neg:
immler@54778
   639
  fixes b::"'a::ordered_real_vector"
immler@54778
   640
  shows "c < 0 \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> b \<le> a"
immler@54778
   641
  by (auto simp: scaleR_le_cancel_left)
immler@54778
   642
immler@54778
   643
lemma scaleR_left_le_one_le:
immler@54778
   644
  fixes x::"'a::ordered_real_vector" and a::real
immler@54778
   645
  shows "0 \<le> x \<Longrightarrow> a \<le> 1 \<Longrightarrow> a *\<^sub>R x \<le> x"
immler@54778
   646
  using scaleR_right_mono[of a 1 x] by simp
immler@54778
   647
huffman@20504
   648
wenzelm@60758
   649
subsection \<open>Real normed vector spaces\<close>
huffman@20504
   650
hoelzl@51531
   651
class dist =
hoelzl@51531
   652
  fixes dist :: "'a \<Rightarrow> 'a \<Rightarrow> real"
hoelzl@51531
   653
haftmann@29608
   654
class norm =
huffman@22636
   655
  fixes norm :: "'a \<Rightarrow> real"
huffman@20504
   656
huffman@24520
   657
class sgn_div_norm = scaleR + norm + sgn +
haftmann@25062
   658
  assumes sgn_div_norm: "sgn x = x /\<^sub>R norm x"
nipkow@24506
   659
huffman@31289
   660
class dist_norm = dist + norm + minus +
huffman@31289
   661
  assumes dist_norm: "dist x y = norm (x - y)"
huffman@31289
   662
hoelzl@62101
   663
class uniformity_dist = dist + uniformity +
hoelzl@62101
   664
  assumes uniformity_dist: "uniformity = (INF e:{0 <..}. principal {(x, y). dist x y < e})"
hoelzl@62101
   665
begin
hoelzl@51531
   666
hoelzl@62101
   667
lemma eventually_uniformity_metric:
hoelzl@62101
   668
  "eventually P uniformity \<longleftrightarrow> (\<exists>e>0. \<forall>x y. dist x y < e \<longrightarrow> P (x, y))"
hoelzl@62101
   669
  unfolding uniformity_dist
hoelzl@62101
   670
  by (subst eventually_INF_base)
hoelzl@62101
   671
     (auto simp: eventually_principal subset_eq intro: bexI[of _ "min _ _"])
hoelzl@62101
   672
hoelzl@62101
   673
end
hoelzl@62101
   674
hoelzl@62101
   675
class real_normed_vector = real_vector + sgn_div_norm + dist_norm + uniformity_dist + open_uniformity +
hoelzl@51002
   676
  assumes norm_eq_zero [simp]: "norm x = 0 \<longleftrightarrow> x = 0"
haftmann@25062
   677
  and norm_triangle_ineq: "norm (x + y) \<le> norm x + norm y"
huffman@31586
   678
  and norm_scaleR [simp]: "norm (scaleR a x) = \<bar>a\<bar> * norm x"
hoelzl@51002
   679
begin
hoelzl@51002
   680
hoelzl@51002
   681
lemma norm_ge_zero [simp]: "0 \<le> norm x"
hoelzl@51002
   682
proof -
lp15@60026
   683
  have "0 = norm (x + -1 *\<^sub>R x)"
hoelzl@51002
   684
    using scaleR_add_left[of 1 "-1" x] norm_scaleR[of 0 x] by (simp add: scaleR_one)
hoelzl@51002
   685
  also have "\<dots> \<le> norm x + norm (-1 *\<^sub>R x)" by (rule norm_triangle_ineq)
hoelzl@51002
   686
  finally show ?thesis by simp
hoelzl@51002
   687
qed
hoelzl@51002
   688
hoelzl@51002
   689
end
huffman@20504
   690
haftmann@24588
   691
class real_normed_algebra = real_algebra + real_normed_vector +
haftmann@25062
   692
  assumes norm_mult_ineq: "norm (x * y) \<le> norm x * norm y"
huffman@20504
   693
haftmann@24588
   694
class real_normed_algebra_1 = real_algebra_1 + real_normed_algebra +
haftmann@25062
   695
  assumes norm_one [simp]: "norm 1 = 1"
hoelzl@62101
   696
hoelzl@62101
   697
lemma (in real_normed_algebra_1) scaleR_power [simp]:
eberlm@62049
   698
  "(scaleR x y) ^ n = scaleR (x^n) (y^n)"
eberlm@62049
   699
  by (induction n) (simp_all add: scaleR_one scaleR_scaleR mult_ac)
huffman@22852
   700
haftmann@24588
   701
class real_normed_div_algebra = real_div_algebra + real_normed_vector +
haftmann@25062
   702
  assumes norm_mult: "norm (x * y) = norm x * norm y"
huffman@20504
   703
haftmann@24588
   704
class real_normed_field = real_field + real_normed_div_algebra
huffman@20584
   705
huffman@22852
   706
instance real_normed_div_algebra < real_normed_algebra_1
huffman@20554
   707
proof
huffman@20554
   708
  fix x y :: 'a
huffman@20554
   709
  show "norm (x * y) \<le> norm x * norm y"
huffman@20554
   710
    by (simp add: norm_mult)
huffman@22852
   711
next
huffman@22852
   712
  have "norm (1 * 1::'a) = norm (1::'a) * norm (1::'a)"
huffman@22852
   713
    by (rule norm_mult)
huffman@22852
   714
  thus "norm (1::'a) = 1" by simp
huffman@20554
   715
qed
huffman@20554
   716
huffman@22852
   717
lemma norm_zero [simp]: "norm (0::'a::real_normed_vector) = 0"
huffman@20504
   718
by simp
huffman@20504
   719
huffman@22852
   720
lemma zero_less_norm_iff [simp]:
huffman@22852
   721
  fixes x :: "'a::real_normed_vector"
huffman@22852
   722
  shows "(0 < norm x) = (x \<noteq> 0)"
huffman@20504
   723
by (simp add: order_less_le)
huffman@20504
   724
huffman@22852
   725
lemma norm_not_less_zero [simp]:
huffman@22852
   726
  fixes x :: "'a::real_normed_vector"
huffman@22852
   727
  shows "\<not> norm x < 0"
huffman@20828
   728
by (simp add: linorder_not_less)
huffman@20828
   729
huffman@22852
   730
lemma norm_le_zero_iff [simp]:
huffman@22852
   731
  fixes x :: "'a::real_normed_vector"
huffman@22852
   732
  shows "(norm x \<le> 0) = (x = 0)"
huffman@20828
   733
by (simp add: order_le_less)
huffman@20828
   734
huffman@20504
   735
lemma norm_minus_cancel [simp]:
huffman@20584
   736
  fixes x :: "'a::real_normed_vector"
huffman@20584
   737
  shows "norm (- x) = norm x"
huffman@20504
   738
proof -
huffman@21809
   739
  have "norm (- x) = norm (scaleR (- 1) x)"
huffman@20504
   740
    by (simp only: scaleR_minus_left scaleR_one)
huffman@20533
   741
  also have "\<dots> = \<bar>- 1\<bar> * norm x"
huffman@20504
   742
    by (rule norm_scaleR)
huffman@20504
   743
  finally show ?thesis by simp
huffman@20504
   744
qed
huffman@20504
   745
huffman@20504
   746
lemma norm_minus_commute:
huffman@20584
   747
  fixes a b :: "'a::real_normed_vector"
huffman@20584
   748
  shows "norm (a - b) = norm (b - a)"
huffman@20504
   749
proof -
huffman@22898
   750
  have "norm (- (b - a)) = norm (b - a)"
huffman@22898
   751
    by (rule norm_minus_cancel)
huffman@22898
   752
  thus ?thesis by simp
huffman@20504
   753
qed
huffman@20504
   754
eberlm@61524
   755
lemma norm_uminus_minus: "norm (-x - y :: 'a :: real_normed_vector) = norm (x + y)"
eberlm@61524
   756
  by (subst (2) norm_minus_cancel[symmetric], subst minus_add_distrib) simp
eberlm@61524
   757
huffman@20504
   758
lemma norm_triangle_ineq2:
huffman@20584
   759
  fixes a b :: "'a::real_normed_vector"
huffman@20533
   760
  shows "norm a - norm b \<le> norm (a - b)"
huffman@20504
   761
proof -
huffman@20533
   762
  have "norm (a - b + b) \<le> norm (a - b) + norm b"
huffman@20504
   763
    by (rule norm_triangle_ineq)
huffman@22898
   764
  thus ?thesis by simp
huffman@20504
   765
qed
huffman@20504
   766
huffman@20584
   767
lemma norm_triangle_ineq3:
huffman@20584
   768
  fixes a b :: "'a::real_normed_vector"
huffman@20584
   769
  shows "\<bar>norm a - norm b\<bar> \<le> norm (a - b)"
huffman@20584
   770
apply (subst abs_le_iff)
huffman@20584
   771
apply auto
huffman@20584
   772
apply (rule norm_triangle_ineq2)
huffman@20584
   773
apply (subst norm_minus_commute)
huffman@20584
   774
apply (rule norm_triangle_ineq2)
huffman@20584
   775
done
huffman@20584
   776
huffman@20504
   777
lemma norm_triangle_ineq4:
huffman@20584
   778
  fixes a b :: "'a::real_normed_vector"
huffman@20533
   779
  shows "norm (a - b) \<le> norm a + norm b"
huffman@20504
   780
proof -
huffman@22898
   781
  have "norm (a + - b) \<le> norm a + norm (- b)"
huffman@20504
   782
    by (rule norm_triangle_ineq)
haftmann@54230
   783
  then show ?thesis by simp
huffman@22898
   784
qed
huffman@22898
   785
huffman@22898
   786
lemma norm_diff_ineq:
huffman@22898
   787
  fixes a b :: "'a::real_normed_vector"
huffman@22898
   788
  shows "norm a - norm b \<le> norm (a + b)"
huffman@22898
   789
proof -
huffman@22898
   790
  have "norm a - norm (- b) \<le> norm (a - - b)"
huffman@22898
   791
    by (rule norm_triangle_ineq2)
huffman@22898
   792
  thus ?thesis by simp
huffman@20504
   793
qed
huffman@20504
   794
lp15@61762
   795
lemma norm_add_leD:
lp15@61762
   796
  fixes a b :: "'a::real_normed_vector"
lp15@61762
   797
  shows "norm (a + b) \<le> c \<Longrightarrow> norm b \<le> norm a + c"
lp15@61762
   798
    by (metis add.commute diff_le_eq norm_diff_ineq order.trans)
lp15@61762
   799
huffman@20551
   800
lemma norm_diff_triangle_ineq:
huffman@20551
   801
  fixes a b c d :: "'a::real_normed_vector"
huffman@20551
   802
  shows "norm ((a + b) - (c + d)) \<le> norm (a - c) + norm (b - d)"
huffman@20551
   803
proof -
huffman@20551
   804
  have "norm ((a + b) - (c + d)) = norm ((a - c) + (b - d))"
haftmann@54230
   805
    by (simp add: algebra_simps)
huffman@20551
   806
  also have "\<dots> \<le> norm (a - c) + norm (b - d)"
huffman@20551
   807
    by (rule norm_triangle_ineq)
huffman@20551
   808
  finally show ?thesis .
huffman@20551
   809
qed
huffman@20551
   810
lp15@60800
   811
lemma norm_diff_triangle_le:
lp15@60800
   812
  fixes x y z :: "'a::real_normed_vector"
lp15@60800
   813
  assumes "norm (x - y) \<le> e1"  "norm (y - z) \<le> e2"
lp15@60800
   814
    shows "norm (x - z) \<le> e1 + e2"
lp15@60800
   815
  using norm_diff_triangle_ineq [of x y y z] assms by simp
lp15@60800
   816
lp15@60800
   817
lemma norm_diff_triangle_less:
lp15@60800
   818
  fixes x y z :: "'a::real_normed_vector"
lp15@60800
   819
  assumes "norm (x - y) < e1"  "norm (y - z) < e2"
lp15@60800
   820
    shows "norm (x - z) < e1 + e2"
lp15@60800
   821
  using norm_diff_triangle_ineq [of x y y z] assms by simp
lp15@60800
   822
lp15@60026
   823
lemma norm_triangle_mono:
lp15@55719
   824
  fixes a b :: "'a::real_normed_vector"
lp15@55719
   825
  shows "\<lbrakk>norm a \<le> r; norm b \<le> s\<rbrakk> \<Longrightarrow> norm (a + b) \<le> r + s"
lp15@55719
   826
by (metis add_mono_thms_linordered_semiring(1) norm_triangle_ineq order.trans)
lp15@55719
   827
hoelzl@56194
   828
lemma norm_setsum:
hoelzl@56194
   829
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
hoelzl@56194
   830
  shows "norm (setsum f A) \<le> (\<Sum>i\<in>A. norm (f i))"
hoelzl@56194
   831
  by (induct A rule: infinite_finite_induct) (auto intro: norm_triangle_mono)
hoelzl@56194
   832
hoelzl@56369
   833
lemma setsum_norm_le:
hoelzl@56369
   834
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
hoelzl@56369
   835
  assumes fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
hoelzl@56369
   836
  shows "norm (setsum f S) \<le> setsum g S"
hoelzl@56369
   837
  by (rule order_trans [OF norm_setsum setsum_mono]) (simp add: fg)
hoelzl@56369
   838
huffman@22857
   839
lemma abs_norm_cancel [simp]:
huffman@22857
   840
  fixes a :: "'a::real_normed_vector"
huffman@22857
   841
  shows "\<bar>norm a\<bar> = norm a"
huffman@22857
   842
by (rule abs_of_nonneg [OF norm_ge_zero])
huffman@22857
   843
huffman@22880
   844
lemma norm_add_less:
huffman@22880
   845
  fixes x y :: "'a::real_normed_vector"
huffman@22880
   846
  shows "\<lbrakk>norm x < r; norm y < s\<rbrakk> \<Longrightarrow> norm (x + y) < r + s"
huffman@22880
   847
by (rule order_le_less_trans [OF norm_triangle_ineq add_strict_mono])
huffman@22880
   848
huffman@22880
   849
lemma norm_mult_less:
huffman@22880
   850
  fixes x y :: "'a::real_normed_algebra"
huffman@22880
   851
  shows "\<lbrakk>norm x < r; norm y < s\<rbrakk> \<Longrightarrow> norm (x * y) < r * s"
huffman@22880
   852
apply (rule order_le_less_trans [OF norm_mult_ineq])
huffman@22880
   853
apply (simp add: mult_strict_mono')
huffman@22880
   854
done
huffman@22880
   855
huffman@22857
   856
lemma norm_of_real [simp]:
huffman@22857
   857
  "norm (of_real r :: 'a::real_normed_algebra_1) = \<bar>r\<bar>"
huffman@31586
   858
unfolding of_real_def by simp
huffman@20560
   859
huffman@47108
   860
lemma norm_numeral [simp]:
huffman@47108
   861
  "norm (numeral w::'a::real_normed_algebra_1) = numeral w"
huffman@47108
   862
by (subst of_real_numeral [symmetric], subst norm_of_real, simp)
huffman@47108
   863
huffman@47108
   864
lemma norm_neg_numeral [simp]:
haftmann@54489
   865
  "norm (- numeral w::'a::real_normed_algebra_1) = numeral w"
huffman@47108
   866
by (subst of_real_neg_numeral [symmetric], subst norm_of_real, simp)
huffman@22876
   867
huffman@22876
   868
lemma norm_of_int [simp]:
huffman@22876
   869
  "norm (of_int z::'a::real_normed_algebra_1) = \<bar>of_int z\<bar>"
huffman@22876
   870
by (subst of_real_of_int_eq [symmetric], rule norm_of_real)
huffman@22876
   871
huffman@22876
   872
lemma norm_of_nat [simp]:
huffman@22876
   873
  "norm (of_nat n::'a::real_normed_algebra_1) = of_nat n"
huffman@22876
   874
apply (subst of_real_of_nat_eq [symmetric])
huffman@22876
   875
apply (subst norm_of_real, simp)
huffman@22876
   876
done
huffman@22876
   877
huffman@20504
   878
lemma nonzero_norm_inverse:
huffman@20504
   879
  fixes a :: "'a::real_normed_div_algebra"
huffman@20533
   880
  shows "a \<noteq> 0 \<Longrightarrow> norm (inverse a) = inverse (norm a)"
huffman@20504
   881
apply (rule inverse_unique [symmetric])
huffman@20504
   882
apply (simp add: norm_mult [symmetric])
huffman@20504
   883
done
huffman@20504
   884
huffman@20504
   885
lemma norm_inverse:
haftmann@59867
   886
  fixes a :: "'a::{real_normed_div_algebra, division_ring}"
huffman@20533
   887
  shows "norm (inverse a) = inverse (norm a)"
huffman@20504
   888
apply (case_tac "a = 0", simp)
huffman@20504
   889
apply (erule nonzero_norm_inverse)
huffman@20504
   890
done
huffman@20504
   891
huffman@20584
   892
lemma nonzero_norm_divide:
huffman@20584
   893
  fixes a b :: "'a::real_normed_field"
huffman@20584
   894
  shows "b \<noteq> 0 \<Longrightarrow> norm (a / b) = norm a / norm b"
huffman@20584
   895
by (simp add: divide_inverse norm_mult nonzero_norm_inverse)
huffman@20584
   896
huffman@20584
   897
lemma norm_divide:
haftmann@59867
   898
  fixes a b :: "'a::{real_normed_field, field}"
huffman@20584
   899
  shows "norm (a / b) = norm a / norm b"
huffman@20584
   900
by (simp add: divide_inverse norm_mult norm_inverse)
huffman@20584
   901
huffman@22852
   902
lemma norm_power_ineq:
haftmann@31017
   903
  fixes x :: "'a::{real_normed_algebra_1}"
huffman@22852
   904
  shows "norm (x ^ n) \<le> norm x ^ n"
huffman@22852
   905
proof (induct n)
huffman@22852
   906
  case 0 show "norm (x ^ 0) \<le> norm x ^ 0" by simp
huffman@22852
   907
next
huffman@22852
   908
  case (Suc n)
huffman@22852
   909
  have "norm (x * x ^ n) \<le> norm x * norm (x ^ n)"
huffman@22852
   910
    by (rule norm_mult_ineq)
huffman@22852
   911
  also from Suc have "\<dots> \<le> norm x * norm x ^ n"
huffman@22852
   912
    using norm_ge_zero by (rule mult_left_mono)
huffman@22852
   913
  finally show "norm (x ^ Suc n) \<le> norm x ^ Suc n"
huffman@30273
   914
    by simp
huffman@22852
   915
qed
huffman@22852
   916
huffman@20684
   917
lemma norm_power:
haftmann@31017
   918
  fixes x :: "'a::{real_normed_div_algebra}"
huffman@20684
   919
  shows "norm (x ^ n) = norm x ^ n"
huffman@30273
   920
by (induct n) (simp_all add: norm_mult)
huffman@20684
   921
paulson@60762
   922
lemma norm_mult_numeral1 [simp]:
paulson@60762
   923
  fixes a b :: "'a::{real_normed_field, field}"
paulson@60762
   924
  shows "norm (numeral w * a) = numeral w * norm a"
paulson@60762
   925
by (simp add: norm_mult)
paulson@60762
   926
paulson@60762
   927
lemma norm_mult_numeral2 [simp]:
paulson@60762
   928
  fixes a b :: "'a::{real_normed_field, field}"
paulson@60762
   929
  shows "norm (a * numeral w) = norm a * numeral w"
paulson@60762
   930
by (simp add: norm_mult)
paulson@60762
   931
paulson@60762
   932
lemma norm_divide_numeral [simp]:
paulson@60762
   933
  fixes a b :: "'a::{real_normed_field, field}"
paulson@60762
   934
  shows "norm (a / numeral w) = norm a / numeral w"
paulson@60762
   935
by (simp add: norm_divide)
paulson@60762
   936
paulson@60762
   937
lemma norm_of_real_diff [simp]:
paulson@60762
   938
    "norm (of_real b - of_real a :: 'a::real_normed_algebra_1) \<le> \<bar>b - a\<bar>"
paulson@60762
   939
  by (metis norm_of_real of_real_diff order_refl)
paulson@60762
   940
wenzelm@61799
   941
text\<open>Despite a superficial resemblance, \<open>norm_eq_1\<close> is not relevant.\<close>
lp15@59613
   942
lemma square_norm_one:
lp15@59613
   943
  fixes x :: "'a::real_normed_div_algebra"
lp15@59613
   944
  assumes "x^2 = 1" shows "norm x = 1"
lp15@59613
   945
  by (metis assms norm_minus_cancel norm_one power2_eq_1_iff)
lp15@59613
   946
lp15@59658
   947
lemma norm_less_p1:
lp15@59658
   948
  fixes x :: "'a::real_normed_algebra_1"
lp15@59658
   949
  shows "norm x < norm (of_real (norm x) + 1 :: 'a)"
lp15@59658
   950
proof -
lp15@59658
   951
  have "norm x < norm (of_real (norm x + 1) :: 'a)"
lp15@59658
   952
    by (simp add: of_real_def)
lp15@59658
   953
  then show ?thesis
lp15@59658
   954
    by simp
lp15@59658
   955
qed
lp15@59658
   956
lp15@55719
   957
lemma setprod_norm:
lp15@55719
   958
  fixes f :: "'a \<Rightarrow> 'b::{comm_semiring_1,real_normed_div_algebra}"
lp15@55719
   959
  shows "setprod (\<lambda>x. norm(f x)) A = norm (setprod f A)"
hoelzl@57275
   960
  by (induct A rule: infinite_finite_induct) (auto simp: norm_mult)
hoelzl@57275
   961
lp15@60026
   962
lemma norm_setprod_le:
hoelzl@57275
   963
  "norm (setprod f A) \<le> (\<Prod>a\<in>A. norm (f a :: 'a :: {real_normed_algebra_1, comm_monoid_mult}))"
hoelzl@57275
   964
proof (induction A rule: infinite_finite_induct)
hoelzl@57275
   965
  case (insert a A)
hoelzl@57275
   966
  then have "norm (setprod f (insert a A)) \<le> norm (f a) * norm (setprod f A)"
hoelzl@57275
   967
    by (simp add: norm_mult_ineq)
hoelzl@57275
   968
  also have "norm (setprod f A) \<le> (\<Prod>a\<in>A. norm (f a))"
hoelzl@57275
   969
    by (rule insert)
hoelzl@57275
   970
  finally show ?case
hoelzl@57275
   971
    by (simp add: insert mult_left_mono)
hoelzl@57275
   972
qed simp_all
hoelzl@57275
   973
hoelzl@57275
   974
lemma norm_setprod_diff:
hoelzl@57275
   975
  fixes z w :: "'i \<Rightarrow> 'a::{real_normed_algebra_1, comm_monoid_mult}"
hoelzl@57275
   976
  shows "(\<And>i. i \<in> I \<Longrightarrow> norm (z i) \<le> 1) \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> norm (w i) \<le> 1) \<Longrightarrow>
lp15@60026
   977
    norm ((\<Prod>i\<in>I. z i) - (\<Prod>i\<in>I. w i)) \<le> (\<Sum>i\<in>I. norm (z i - w i))"
hoelzl@57275
   978
proof (induction I rule: infinite_finite_induct)
hoelzl@57275
   979
  case (insert i I)
hoelzl@57275
   980
  note insert.hyps[simp]
hoelzl@57275
   981
hoelzl@57275
   982
  have "norm ((\<Prod>i\<in>insert i I. z i) - (\<Prod>i\<in>insert i I. w i)) =
hoelzl@57275
   983
    norm ((\<Prod>i\<in>I. z i) * (z i - w i) + ((\<Prod>i\<in>I. z i) - (\<Prod>i\<in>I. w i)) * w i)"
hoelzl@57275
   984
    (is "_ = norm (?t1 + ?t2)")
hoelzl@57275
   985
    by (auto simp add: field_simps)
hoelzl@57275
   986
  also have "... \<le> norm ?t1 + norm ?t2"
hoelzl@57275
   987
    by (rule norm_triangle_ineq)
hoelzl@57275
   988
  also have "norm ?t1 \<le> norm (\<Prod>i\<in>I. z i) * norm (z i - w i)"
hoelzl@57275
   989
    by (rule norm_mult_ineq)
hoelzl@57275
   990
  also have "\<dots> \<le> (\<Prod>i\<in>I. norm (z i)) * norm(z i - w i)"
hoelzl@57275
   991
    by (rule mult_right_mono) (auto intro: norm_setprod_le)
hoelzl@57275
   992
  also have "(\<Prod>i\<in>I. norm (z i)) \<le> (\<Prod>i\<in>I. 1)"
hoelzl@57275
   993
    by (intro setprod_mono) (auto intro!: insert)
hoelzl@57275
   994
  also have "norm ?t2 \<le> norm ((\<Prod>i\<in>I. z i) - (\<Prod>i\<in>I. w i)) * norm (w i)"
hoelzl@57275
   995
    by (rule norm_mult_ineq)
hoelzl@57275
   996
  also have "norm (w i) \<le> 1"
hoelzl@57275
   997
    by (auto intro: insert)
hoelzl@57275
   998
  also have "norm ((\<Prod>i\<in>I. z i) - (\<Prod>i\<in>I. w i)) \<le> (\<Sum>i\<in>I. norm (z i - w i))"
hoelzl@57275
   999
    using insert by auto
hoelzl@57275
  1000
  finally show ?case
haftmann@57514
  1001
    by (auto simp add: ac_simps mult_right_mono mult_left_mono)
hoelzl@57275
  1002
qed simp_all
hoelzl@57275
  1003
lp15@60026
  1004
lemma norm_power_diff:
hoelzl@57275
  1005
  fixes z w :: "'a::{real_normed_algebra_1, comm_monoid_mult}"
hoelzl@57275
  1006
  assumes "norm z \<le> 1" "norm w \<le> 1"
hoelzl@57275
  1007
  shows "norm (z^m - w^m) \<le> m * norm (z - w)"
hoelzl@57275
  1008
proof -
hoelzl@57275
  1009
  have "norm (z^m - w^m) = norm ((\<Prod> i < m. z) - (\<Prod> i < m. w))"
hoelzl@57275
  1010
    by (simp add: setprod_constant)
hoelzl@57275
  1011
  also have "\<dots> \<le> (\<Sum>i<m. norm (z - w))"
hoelzl@57275
  1012
    by (intro norm_setprod_diff) (auto simp add: assms)
hoelzl@57275
  1013
  also have "\<dots> = m * norm (z - w)"
lp15@61609
  1014
    by simp
hoelzl@57275
  1015
  finally show ?thesis .
lp15@55719
  1016
qed
lp15@55719
  1017
wenzelm@60758
  1018
subsection \<open>Metric spaces\<close>
hoelzl@51531
  1019
hoelzl@62101
  1020
class metric_space = uniformity_dist + open_uniformity +
hoelzl@51531
  1021
  assumes dist_eq_0_iff [simp]: "dist x y = 0 \<longleftrightarrow> x = y"
hoelzl@51531
  1022
  assumes dist_triangle2: "dist x y \<le> dist x z + dist y z"
hoelzl@51531
  1023
begin
hoelzl@51531
  1024
hoelzl@51531
  1025
lemma dist_self [simp]: "dist x x = 0"
hoelzl@51531
  1026
by simp
hoelzl@51531
  1027
hoelzl@51531
  1028
lemma zero_le_dist [simp]: "0 \<le> dist x y"
hoelzl@51531
  1029
using dist_triangle2 [of x x y] by simp
hoelzl@51531
  1030
hoelzl@51531
  1031
lemma zero_less_dist_iff: "0 < dist x y \<longleftrightarrow> x \<noteq> y"
hoelzl@51531
  1032
by (simp add: less_le)
hoelzl@51531
  1033
hoelzl@51531
  1034
lemma dist_not_less_zero [simp]: "\<not> dist x y < 0"
hoelzl@51531
  1035
by (simp add: not_less)
hoelzl@51531
  1036
hoelzl@51531
  1037
lemma dist_le_zero_iff [simp]: "dist x y \<le> 0 \<longleftrightarrow> x = y"
hoelzl@51531
  1038
by (simp add: le_less)
hoelzl@51531
  1039
hoelzl@51531
  1040
lemma dist_commute: "dist x y = dist y x"
hoelzl@51531
  1041
proof (rule order_antisym)
hoelzl@51531
  1042
  show "dist x y \<le> dist y x"
hoelzl@51531
  1043
    using dist_triangle2 [of x y x] by simp
hoelzl@51531
  1044
  show "dist y x \<le> dist x y"
hoelzl@51531
  1045
    using dist_triangle2 [of y x y] by simp
hoelzl@51531
  1046
qed
hoelzl@51531
  1047
hoelzl@51531
  1048
lemma dist_triangle: "dist x z \<le> dist x y + dist y z"
hoelzl@51531
  1049
using dist_triangle2 [of x z y] by (simp add: dist_commute)
hoelzl@51531
  1050
hoelzl@51531
  1051
lemma dist_triangle3: "dist x y \<le> dist a x + dist a y"
hoelzl@51531
  1052
using dist_triangle2 [of x y a] by (simp add: dist_commute)
hoelzl@51531
  1053
hoelzl@51531
  1054
lemma dist_triangle_alt:
hoelzl@51531
  1055
  shows "dist y z <= dist x y + dist x z"
hoelzl@51531
  1056
by (rule dist_triangle3)
hoelzl@51531
  1057
hoelzl@51531
  1058
lemma dist_pos_lt:
hoelzl@51531
  1059
  shows "x \<noteq> y ==> 0 < dist x y"
hoelzl@51531
  1060
by (simp add: zero_less_dist_iff)
hoelzl@51531
  1061
hoelzl@51531
  1062
lemma dist_nz:
hoelzl@51531
  1063
  shows "x \<noteq> y \<longleftrightarrow> 0 < dist x y"
hoelzl@51531
  1064
by (simp add: zero_less_dist_iff)
hoelzl@51531
  1065
paulson@62087
  1066
declare dist_nz [symmetric, simp]
paulson@62087
  1067
hoelzl@51531
  1068
lemma dist_triangle_le:
hoelzl@51531
  1069
  shows "dist x z + dist y z <= e \<Longrightarrow> dist x y <= e"
hoelzl@51531
  1070
by (rule order_trans [OF dist_triangle2])
hoelzl@51531
  1071
hoelzl@51531
  1072
lemma dist_triangle_lt:
hoelzl@51531
  1073
  shows "dist x z + dist y z < e ==> dist x y < e"
hoelzl@51531
  1074
by (rule le_less_trans [OF dist_triangle2])
hoelzl@51531
  1075
hoelzl@51531
  1076
lemma dist_triangle_half_l:
hoelzl@51531
  1077
  shows "dist x1 y < e / 2 \<Longrightarrow> dist x2 y < e / 2 \<Longrightarrow> dist x1 x2 < e"
hoelzl@51531
  1078
by (rule dist_triangle_lt [where z=y], simp)
hoelzl@51531
  1079
hoelzl@51531
  1080
lemma dist_triangle_half_r:
hoelzl@51531
  1081
  shows "dist y x1 < e / 2 \<Longrightarrow> dist y x2 < e / 2 \<Longrightarrow> dist x1 x2 < e"
hoelzl@51531
  1082
by (rule dist_triangle_half_l, simp_all add: dist_commute)
hoelzl@51531
  1083
hoelzl@62101
  1084
subclass uniform_space
hoelzl@51531
  1085
proof
hoelzl@62101
  1086
  fix E x assume "eventually E uniformity"
hoelzl@62101
  1087
  then obtain e where E: "0 < e" "\<And>x y. dist x y < e \<Longrightarrow> E (x, y)"
hoelzl@62101
  1088
    unfolding eventually_uniformity_metric by auto
hoelzl@62101
  1089
  then show "E (x, x)" "\<forall>\<^sub>F (x, y) in uniformity. E (y, x)"
hoelzl@62101
  1090
    unfolding eventually_uniformity_metric by (auto simp: dist_commute)
hoelzl@62101
  1091
hoelzl@62101
  1092
  show "\<exists>D. eventually D uniformity \<and> (\<forall>x y z. D (x, y) \<longrightarrow> D (y, z) \<longrightarrow> E (x, z))"
hoelzl@62101
  1093
    using E dist_triangle_half_l[where e=e] unfolding eventually_uniformity_metric
hoelzl@62101
  1094
    by (intro exI[of _ "\<lambda>(x, y). dist x y < e / 2"] exI[of _ "e/2"] conjI)
hoelzl@62101
  1095
       (auto simp: dist_commute)
hoelzl@51531
  1096
qed
hoelzl@51531
  1097
hoelzl@62101
  1098
lemma open_dist: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
hoelzl@62101
  1099
  unfolding open_uniformity eventually_uniformity_metric by (simp add: dist_commute)
hoelzl@62101
  1100
hoelzl@51531
  1101
lemma open_ball: "open {y. dist x y < d}"
hoelzl@51531
  1102
proof (unfold open_dist, intro ballI)
hoelzl@51531
  1103
  fix y assume *: "y \<in> {y. dist x y < d}"
hoelzl@51531
  1104
  then show "\<exists>e>0. \<forall>z. dist z y < e \<longrightarrow> z \<in> {y. dist x y < d}"
hoelzl@51531
  1105
    by (auto intro!: exI[of _ "d - dist x y"] simp: field_simps dist_triangle_lt)
hoelzl@51531
  1106
qed
hoelzl@51531
  1107
hoelzl@51531
  1108
subclass first_countable_topology
hoelzl@51531
  1109
proof
lp15@60026
  1110
  fix x
hoelzl@51531
  1111
  show "\<exists>A::nat \<Rightarrow> 'a set. (\<forall>i. x \<in> A i \<and> open (A i)) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. A i \<subseteq> S))"
hoelzl@51531
  1112
  proof (safe intro!: exI[of _ "\<lambda>n. {y. dist x y < inverse (Suc n)}"])
hoelzl@51531
  1113
    fix S assume "open S" "x \<in> S"
wenzelm@53374
  1114
    then obtain e where e: "0 < e" and "{y. dist x y < e} \<subseteq> S"
hoelzl@51531
  1115
      by (auto simp: open_dist subset_eq dist_commute)
hoelzl@51531
  1116
    moreover
wenzelm@53374
  1117
    from e obtain i where "inverse (Suc i) < e"
hoelzl@51531
  1118
      by (auto dest!: reals_Archimedean)
hoelzl@51531
  1119
    then have "{y. dist x y < inverse (Suc i)} \<subseteq> {y. dist x y < e}"
hoelzl@51531
  1120
      by auto
hoelzl@51531
  1121
    ultimately show "\<exists>i. {y. dist x y < inverse (Suc i)} \<subseteq> S"
hoelzl@51531
  1122
      by blast
hoelzl@51531
  1123
  qed (auto intro: open_ball)
hoelzl@51531
  1124
qed
hoelzl@51531
  1125
hoelzl@51531
  1126
end
hoelzl@51531
  1127
hoelzl@51531
  1128
instance metric_space \<subseteq> t2_space
hoelzl@51531
  1129
proof
hoelzl@51531
  1130
  fix x y :: "'a::metric_space"
hoelzl@51531
  1131
  assume xy: "x \<noteq> y"
hoelzl@51531
  1132
  let ?U = "{y'. dist x y' < dist x y / 2}"
hoelzl@51531
  1133
  let ?V = "{x'. dist y x' < dist x y / 2}"
hoelzl@51531
  1134
  have th0: "\<And>d x y z. (d x z :: real) \<le> d x y + d y z \<Longrightarrow> d y z = d z y
hoelzl@51531
  1135
               \<Longrightarrow> \<not>(d x y * 2 < d x z \<and> d z y * 2 < d x z)" by arith
hoelzl@51531
  1136
  have "open ?U \<and> open ?V \<and> x \<in> ?U \<and> y \<in> ?V \<and> ?U \<inter> ?V = {}"
hoelzl@51531
  1137
    using dist_pos_lt[OF xy] th0[of dist, OF dist_triangle dist_commute]
hoelzl@51531
  1138
    using open_ball[of _ "dist x y / 2"] by auto
hoelzl@51531
  1139
  then show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
hoelzl@51531
  1140
    by blast
hoelzl@51531
  1141
qed
hoelzl@51531
  1142
wenzelm@60758
  1143
text \<open>Every normed vector space is a metric space.\<close>
huffman@31285
  1144
huffman@31289
  1145
instance real_normed_vector < metric_space
huffman@31289
  1146
proof
huffman@31289
  1147
  fix x y :: 'a show "dist x y = 0 \<longleftrightarrow> x = y"
huffman@31289
  1148
    unfolding dist_norm by simp
huffman@31289
  1149
next
huffman@31289
  1150
  fix x y z :: 'a show "dist x y \<le> dist x z + dist y z"
huffman@31289
  1151
    unfolding dist_norm
huffman@31289
  1152
    using norm_triangle_ineq4 [of "x - z" "y - z"] by simp
huffman@31289
  1153
qed
huffman@31285
  1154
wenzelm@60758
  1155
subsection \<open>Class instances for real numbers\<close>
huffman@31564
  1156
huffman@31564
  1157
instantiation real :: real_normed_field
huffman@31564
  1158
begin
huffman@31564
  1159
hoelzl@51531
  1160
definition dist_real_def:
hoelzl@51531
  1161
  "dist x y = \<bar>x - y\<bar>"
hoelzl@51531
  1162
hoelzl@62101
  1163
definition uniformity_real_def [code del]:
hoelzl@62101
  1164
  "(uniformity :: (real \<times> real) filter) = (INF e:{0 <..}. principal {(x, y). dist x y < e})"
hoelzl@62101
  1165
haftmann@52381
  1166
definition open_real_def [code del]:
hoelzl@62101
  1167
  "open (U :: real set) \<longleftrightarrow> (\<forall>x\<in>U. eventually (\<lambda>(x', y). x' = x \<longrightarrow> y \<in> U) uniformity)"
hoelzl@51531
  1168
huffman@31564
  1169
definition real_norm_def [simp]:
huffman@31564
  1170
  "norm r = \<bar>r\<bar>"
huffman@31564
  1171
huffman@31564
  1172
instance
huffman@31564
  1173
apply (intro_classes, unfold real_norm_def real_scaleR_def)
huffman@31564
  1174
apply (rule dist_real_def)
hoelzl@62101
  1175
apply (simp add: sgn_real_def)
hoelzl@62101
  1176
apply (rule uniformity_real_def)
hoelzl@51531
  1177
apply (rule open_real_def)
huffman@31564
  1178
apply (rule abs_eq_0)
huffman@31564
  1179
apply (rule abs_triangle_ineq)
huffman@31564
  1180
apply (rule abs_mult)
huffman@31564
  1181
apply (rule abs_mult)
huffman@31564
  1182
done
huffman@31564
  1183
huffman@31564
  1184
end
huffman@31564
  1185
hoelzl@62102
  1186
declare uniformity_Abort[where 'a=real, code]
hoelzl@62102
  1187
lp15@60800
  1188
lemma dist_of_real [simp]:
lp15@60800
  1189
  fixes a :: "'a::real_normed_div_algebra"
lp15@60800
  1190
  shows "dist (of_real x :: 'a) (of_real y) = dist x y"
lp15@60800
  1191
by (metis dist_norm norm_of_real of_real_diff real_norm_def)
lp15@60800
  1192
haftmann@54890
  1193
declare [[code abort: "open :: real set \<Rightarrow> bool"]]
haftmann@52381
  1194
hoelzl@51531
  1195
instance real :: linorder_topology
hoelzl@51531
  1196
proof
hoelzl@51531
  1197
  show "(open :: real set \<Rightarrow> bool) = generate_topology (range lessThan \<union> range greaterThan)"
hoelzl@51531
  1198
  proof (rule ext, safe)
hoelzl@51531
  1199
    fix S :: "real set" assume "open S"
wenzelm@53381
  1200
    then obtain f where "\<forall>x\<in>S. 0 < f x \<and> (\<forall>y. dist y x < f x \<longrightarrow> y \<in> S)"
hoelzl@62101
  1201
      unfolding open_dist bchoice_iff ..
hoelzl@51531
  1202
    then have *: "S = (\<Union>x\<in>S. {x - f x <..} \<inter> {..< x + f x})"
hoelzl@51531
  1203
      by (fastforce simp: dist_real_def)
hoelzl@51531
  1204
    show "generate_topology (range lessThan \<union> range greaterThan) S"
hoelzl@51531
  1205
      apply (subst *)
hoelzl@51531
  1206
      apply (intro generate_topology_Union generate_topology.Int)
hoelzl@51531
  1207
      apply (auto intro: generate_topology.Basis)
hoelzl@51531
  1208
      done
hoelzl@51531
  1209
  next
hoelzl@51531
  1210
    fix S :: "real set" assume "generate_topology (range lessThan \<union> range greaterThan) S"
hoelzl@51531
  1211
    moreover have "\<And>a::real. open {..<a}"
hoelzl@62101
  1212
      unfolding open_dist dist_real_def
hoelzl@51531
  1213
    proof clarify
hoelzl@51531
  1214
      fix x a :: real assume "x < a"
hoelzl@51531
  1215
      hence "0 < a - x \<and> (\<forall>y. \<bar>y - x\<bar> < a - x \<longrightarrow> y \<in> {..<a})" by auto
hoelzl@51531
  1216
      thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {..<a}" ..
hoelzl@51531
  1217
    qed
hoelzl@51531
  1218
    moreover have "\<And>a::real. open {a <..}"
hoelzl@62101
  1219
      unfolding open_dist dist_real_def
hoelzl@51531
  1220
    proof clarify
hoelzl@51531
  1221
      fix x a :: real assume "a < x"
hoelzl@51531
  1222
      hence "0 < x - a \<and> (\<forall>y. \<bar>y - x\<bar> < x - a \<longrightarrow> y \<in> {a<..})" by auto
hoelzl@51531
  1223
      thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {a<..}" ..
hoelzl@51531
  1224
    qed
hoelzl@51531
  1225
    ultimately show "open S"
hoelzl@51531
  1226
      by induct auto
hoelzl@51531
  1227
  qed
hoelzl@51531
  1228
qed
hoelzl@51531
  1229
hoelzl@51775
  1230
instance real :: linear_continuum_topology ..
hoelzl@51518
  1231
hoelzl@51531
  1232
lemmas open_real_greaterThan = open_greaterThan[where 'a=real]
hoelzl@51531
  1233
lemmas open_real_lessThan = open_lessThan[where 'a=real]
hoelzl@51531
  1234
lemmas open_real_greaterThanLessThan = open_greaterThanLessThan[where 'a=real]
hoelzl@51531
  1235
lemmas closed_real_atMost = closed_atMost[where 'a=real]
hoelzl@51531
  1236
lemmas closed_real_atLeast = closed_atLeast[where 'a=real]
hoelzl@51531
  1237
lemmas closed_real_atLeastAtMost = closed_atLeastAtMost[where 'a=real]
hoelzl@51531
  1238
wenzelm@60758
  1239
subsection \<open>Extra type constraints\<close>
huffman@31446
  1240
wenzelm@61799
  1241
text \<open>Only allow @{term "open"} in class \<open>topological_space\<close>.\<close>
huffman@31492
  1242
wenzelm@60758
  1243
setup \<open>Sign.add_const_constraint
wenzelm@60758
  1244
  (@{const_name "open"}, SOME @{typ "'a::topological_space set \<Rightarrow> bool"})\<close>
huffman@31492
  1245
hoelzl@62101
  1246
text \<open>Only allow @{term "uniformity"} in class \<open>uniform_space\<close>.\<close>
hoelzl@62101
  1247
hoelzl@62101
  1248
setup \<open>Sign.add_const_constraint
hoelzl@62101
  1249
  (@{const_name "uniformity"}, SOME @{typ "('a::uniformity \<times> 'a) filter"})\<close>
hoelzl@62101
  1250
wenzelm@61799
  1251
text \<open>Only allow @{term dist} in class \<open>metric_space\<close>.\<close>
huffman@31446
  1252
wenzelm@60758
  1253
setup \<open>Sign.add_const_constraint
wenzelm@60758
  1254
  (@{const_name dist}, SOME @{typ "'a::metric_space \<Rightarrow> 'a \<Rightarrow> real"})\<close>
huffman@31446
  1255
wenzelm@61799
  1256
text \<open>Only allow @{term norm} in class \<open>real_normed_vector\<close>.\<close>
huffman@31446
  1257
wenzelm@60758
  1258
setup \<open>Sign.add_const_constraint
wenzelm@60758
  1259
  (@{const_name norm}, SOME @{typ "'a::real_normed_vector \<Rightarrow> real"})\<close>
huffman@31446
  1260
wenzelm@60758
  1261
subsection \<open>Sign function\<close>
huffman@22972
  1262
nipkow@24506
  1263
lemma norm_sgn:
nipkow@24506
  1264
  "norm (sgn(x::'a::real_normed_vector)) = (if x = 0 then 0 else 1)"
huffman@31586
  1265
by (simp add: sgn_div_norm)
huffman@22972
  1266
nipkow@24506
  1267
lemma sgn_zero [simp]: "sgn(0::'a::real_normed_vector) = 0"
nipkow@24506
  1268
by (simp add: sgn_div_norm)
huffman@22972
  1269
nipkow@24506
  1270
lemma sgn_zero_iff: "(sgn(x::'a::real_normed_vector) = 0) = (x = 0)"
nipkow@24506
  1271
by (simp add: sgn_div_norm)
huffman@22972
  1272
nipkow@24506
  1273
lemma sgn_minus: "sgn (- x) = - sgn(x::'a::real_normed_vector)"
nipkow@24506
  1274
by (simp add: sgn_div_norm)
huffman@22972
  1275
nipkow@24506
  1276
lemma sgn_scaleR:
nipkow@24506
  1277
  "sgn (scaleR r x) = scaleR (sgn r) (sgn(x::'a::real_normed_vector))"
haftmann@57514
  1278
by (simp add: sgn_div_norm ac_simps)
huffman@22973
  1279
huffman@22972
  1280
lemma sgn_one [simp]: "sgn (1::'a::real_normed_algebra_1) = 1"
nipkow@24506
  1281
by (simp add: sgn_div_norm)
huffman@22972
  1282
huffman@22972
  1283
lemma sgn_of_real:
huffman@22972
  1284
  "sgn (of_real r::'a::real_normed_algebra_1) = of_real (sgn r)"
huffman@22972
  1285
unfolding of_real_def by (simp only: sgn_scaleR sgn_one)
huffman@22972
  1286
huffman@22973
  1287
lemma sgn_mult:
huffman@22973
  1288
  fixes x y :: "'a::real_normed_div_algebra"
huffman@22973
  1289
  shows "sgn (x * y) = sgn x * sgn y"
haftmann@57512
  1290
by (simp add: sgn_div_norm norm_mult mult.commute)
huffman@22973
  1291
huffman@22972
  1292
lemma real_sgn_eq: "sgn (x::real) = x / \<bar>x\<bar>"
lp15@61649
  1293
  by (simp add: sgn_div_norm divide_inverse)
huffman@22972
  1294
hoelzl@56889
  1295
lemma zero_le_sgn_iff [simp]: "0 \<le> sgn x \<longleftrightarrow> 0 \<le> (x::real)"
hoelzl@56889
  1296
  by (cases "0::real" x rule: linorder_cases) simp_all
lp15@60026
  1297
hoelzl@56889
  1298
lemma sgn_le_0_iff [simp]: "sgn x \<le> 0 \<longleftrightarrow> (x::real) \<le> 0"
hoelzl@56889
  1299
  by (cases "0::real" x rule: linorder_cases) simp_all
lp15@60026
  1300
hoelzl@51474
  1301
lemma norm_conv_dist: "norm x = dist x 0"
hoelzl@51474
  1302
  unfolding dist_norm by simp
huffman@22972
  1303
lp15@60307
  1304
lemma dist_diff [simp]: "dist a (a - b) = norm b"  "dist (a - b) a = norm b"
lp15@60307
  1305
  by (simp_all add: dist_norm)
lp15@61609
  1306
eberlm@61524
  1307
lemma dist_of_int: "dist (of_int m) (of_int n :: 'a :: real_normed_algebra_1) = of_int \<bar>m - n\<bar>"
eberlm@61524
  1308
proof -
eberlm@61524
  1309
  have "dist (of_int m) (of_int n :: 'a) = dist (of_int m :: 'a) (of_int m - (of_int (m - n)))"
eberlm@61524
  1310
    by simp
eberlm@61524
  1311
  also have "\<dots> = of_int \<bar>m - n\<bar>" by (subst dist_diff, subst norm_of_int) simp
eberlm@61524
  1312
  finally show ?thesis .
eberlm@61524
  1313
qed
eberlm@61524
  1314
lp15@61609
  1315
lemma dist_of_nat:
eberlm@61524
  1316
  "dist (of_nat m) (of_nat n :: 'a :: real_normed_algebra_1) = of_int \<bar>int m - int n\<bar>"
eberlm@61524
  1317
  by (subst (1 2) of_int_of_nat_eq [symmetric]) (rule dist_of_int)
lp15@61609
  1318
wenzelm@60758
  1319
subsection \<open>Bounded Linear and Bilinear Operators\<close>
huffman@22442
  1320
huffman@53600
  1321
locale linear = additive f for f :: "'a::real_vector \<Rightarrow> 'b::real_vector" +
huffman@22442
  1322
  assumes scaleR: "f (scaleR r x) = scaleR r (f x)"
huffman@53600
  1323
lp15@60800
  1324
lemma linear_imp_scaleR:
lp15@60800
  1325
  assumes "linear D" obtains d where "D = (\<lambda>x. x *\<^sub>R d)"
lp15@60800
  1326
  by (metis assms linear.scaleR mult.commute mult.left_neutral real_scaleR_def)
lp15@60800
  1327
huffman@53600
  1328
lemma linearI:
huffman@53600
  1329
  assumes "\<And>x y. f (x + y) = f x + f y"
huffman@53600
  1330
  assumes "\<And>c x. f (c *\<^sub>R x) = c *\<^sub>R f x"
huffman@53600
  1331
  shows "linear f"
wenzelm@61169
  1332
  by standard (rule assms)+
huffman@53600
  1333
huffman@53600
  1334
locale bounded_linear = linear f for f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector" +
huffman@22442
  1335
  assumes bounded: "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
huffman@27443
  1336
begin
huffman@22442
  1337
huffman@27443
  1338
lemma pos_bounded:
huffman@22442
  1339
  "\<exists>K>0. \<forall>x. norm (f x) \<le> norm x * K"
huffman@22442
  1340
proof -
huffman@22442
  1341
  obtain K where K: "\<And>x. norm (f x) \<le> norm x * K"
lp15@61649
  1342
    using bounded by blast
huffman@22442
  1343
  show ?thesis
huffman@22442
  1344
  proof (intro exI impI conjI allI)
huffman@22442
  1345
    show "0 < max 1 K"
haftmann@54863
  1346
      by (rule order_less_le_trans [OF zero_less_one max.cobounded1])
huffman@22442
  1347
  next
huffman@22442
  1348
    fix x
huffman@22442
  1349
    have "norm (f x) \<le> norm x * K" using K .
huffman@22442
  1350
    also have "\<dots> \<le> norm x * max 1 K"
haftmann@54863
  1351
      by (rule mult_left_mono [OF max.cobounded2 norm_ge_zero])
huffman@22442
  1352
    finally show "norm (f x) \<le> norm x * max 1 K" .
huffman@22442
  1353
  qed
huffman@22442
  1354
qed
huffman@22442
  1355
huffman@27443
  1356
lemma nonneg_bounded:
huffman@22442
  1357
  "\<exists>K\<ge>0. \<forall>x. norm (f x) \<le> norm x * K"
huffman@22442
  1358
proof -
huffman@22442
  1359
  from pos_bounded
huffman@22442
  1360
  show ?thesis by (auto intro: order_less_imp_le)
huffman@22442
  1361
qed
huffman@22442
  1362
hoelzl@56369
  1363
lemma linear: "linear f" ..
hoelzl@56369
  1364
huffman@27443
  1365
end
huffman@27443
  1366
huffman@44127
  1367
lemma bounded_linear_intro:
huffman@44127
  1368
  assumes "\<And>x y. f (x + y) = f x + f y"
huffman@44127
  1369
  assumes "\<And>r x. f (scaleR r x) = scaleR r (f x)"
huffman@44127
  1370
  assumes "\<And>x. norm (f x) \<le> norm x * K"
huffman@44127
  1371
  shows "bounded_linear f"
lp15@61649
  1372
  by standard (blast intro: assms)+
huffman@44127
  1373
huffman@22442
  1374
locale bounded_bilinear =
huffman@22442
  1375
  fixes prod :: "['a::real_normed_vector, 'b::real_normed_vector]
huffman@22442
  1376
                 \<Rightarrow> 'c::real_normed_vector"
huffman@22442
  1377
    (infixl "**" 70)
huffman@22442
  1378
  assumes add_left: "prod (a + a') b = prod a b + prod a' b"
huffman@22442
  1379
  assumes add_right: "prod a (b + b') = prod a b + prod a b'"
huffman@22442
  1380
  assumes scaleR_left: "prod (scaleR r a) b = scaleR r (prod a b)"
huffman@22442
  1381
  assumes scaleR_right: "prod a (scaleR r b) = scaleR r (prod a b)"
huffman@22442
  1382
  assumes bounded: "\<exists>K. \<forall>a b. norm (prod a b) \<le> norm a * norm b * K"
huffman@27443
  1383
begin
huffman@22442
  1384
huffman@27443
  1385
lemma pos_bounded:
huffman@22442
  1386
  "\<exists>K>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
huffman@22442
  1387
apply (cut_tac bounded, erule exE)
huffman@22442
  1388
apply (rule_tac x="max 1 K" in exI, safe)
haftmann@54863
  1389
apply (rule order_less_le_trans [OF zero_less_one max.cobounded1])
huffman@22442
  1390
apply (drule spec, drule spec, erule order_trans)
haftmann@54863
  1391
apply (rule mult_left_mono [OF max.cobounded2])
huffman@22442
  1392
apply (intro mult_nonneg_nonneg norm_ge_zero)
huffman@22442
  1393
done
huffman@22442
  1394
huffman@27443
  1395
lemma nonneg_bounded:
huffman@22442
  1396
  "\<exists>K\<ge>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
huffman@22442
  1397
proof -
huffman@22442
  1398
  from pos_bounded
huffman@22442
  1399
  show ?thesis by (auto intro: order_less_imp_le)
huffman@22442
  1400
qed
huffman@22442
  1401
huffman@27443
  1402
lemma additive_right: "additive (\<lambda>b. prod a b)"
huffman@22442
  1403
by (rule additive.intro, rule add_right)
huffman@22442
  1404
huffman@27443
  1405
lemma additive_left: "additive (\<lambda>a. prod a b)"
huffman@22442
  1406
by (rule additive.intro, rule add_left)
huffman@22442
  1407
huffman@27443
  1408
lemma zero_left: "prod 0 b = 0"
huffman@22442
  1409
by (rule additive.zero [OF additive_left])
huffman@22442
  1410
huffman@27443
  1411
lemma zero_right: "prod a 0 = 0"
huffman@22442
  1412
by (rule additive.zero [OF additive_right])
huffman@22442
  1413
huffman@27443
  1414
lemma minus_left: "prod (- a) b = - prod a b"
huffman@22442
  1415
by (rule additive.minus [OF additive_left])
huffman@22442
  1416
huffman@27443
  1417
lemma minus_right: "prod a (- b) = - prod a b"
huffman@22442
  1418
by (rule additive.minus [OF additive_right])
huffman@22442
  1419
huffman@27443
  1420
lemma diff_left:
huffman@22442
  1421
  "prod (a - a') b = prod a b - prod a' b"
huffman@22442
  1422
by (rule additive.diff [OF additive_left])
huffman@22442
  1423
huffman@27443
  1424
lemma diff_right:
huffman@22442
  1425
  "prod a (b - b') = prod a b - prod a b'"
huffman@22442
  1426
by (rule additive.diff [OF additive_right])
huffman@22442
  1427
immler@61915
  1428
lemma setsum_left:
immler@61915
  1429
  "prod (setsum g S) x = setsum ((\<lambda>i. prod (g i) x)) S"
immler@61915
  1430
by (rule additive.setsum [OF additive_left])
immler@61915
  1431
immler@61915
  1432
lemma setsum_right:
immler@61915
  1433
  "prod x (setsum g S) = setsum ((\<lambda>i. (prod x (g i)))) S"
immler@61915
  1434
by (rule additive.setsum [OF additive_right])
immler@61915
  1435
immler@61915
  1436
huffman@27443
  1437
lemma bounded_linear_left:
huffman@22442
  1438
  "bounded_linear (\<lambda>a. a ** b)"
huffman@44127
  1439
apply (cut_tac bounded, safe)
huffman@44127
  1440
apply (rule_tac K="norm b * K" in bounded_linear_intro)
huffman@22442
  1441
apply (rule add_left)
huffman@22442
  1442
apply (rule scaleR_left)
haftmann@57514
  1443
apply (simp add: ac_simps)
huffman@22442
  1444
done
huffman@22442
  1445
huffman@27443
  1446
lemma bounded_linear_right:
huffman@22442
  1447
  "bounded_linear (\<lambda>b. a ** b)"
huffman@44127
  1448
apply (cut_tac bounded, safe)
huffman@44127
  1449
apply (rule_tac K="norm a * K" in bounded_linear_intro)
huffman@22442
  1450
apply (rule add_right)
huffman@22442
  1451
apply (rule scaleR_right)
haftmann@57514
  1452
apply (simp add: ac_simps)
huffman@22442
  1453
done
huffman@22442
  1454
huffman@27443
  1455
lemma prod_diff_prod:
huffman@22442
  1456
  "(x ** y - a ** b) = (x - a) ** (y - b) + (x - a) ** b + a ** (y - b)"
huffman@22442
  1457
by (simp add: diff_left diff_right)
huffman@22442
  1458
immler@61916
  1459
lemma flip: "bounded_bilinear (\<lambda>x y. y ** x)"
immler@61916
  1460
  apply standard
immler@61916
  1461
  apply (rule add_right)
immler@61916
  1462
  apply (rule add_left)
immler@61916
  1463
  apply (rule scaleR_right)
immler@61916
  1464
  apply (rule scaleR_left)
immler@61916
  1465
  apply (subst mult.commute)
immler@61916
  1466
  using bounded
immler@61916
  1467
  apply blast
immler@61916
  1468
  done
immler@61916
  1469
immler@61916
  1470
lemma comp1:
immler@61916
  1471
  assumes "bounded_linear g"
immler@61916
  1472
  shows "bounded_bilinear (\<lambda>x. op ** (g x))"
immler@61916
  1473
proof unfold_locales
immler@61916
  1474
  interpret g: bounded_linear g by fact
immler@61916
  1475
  show "\<And>a a' b. g (a + a') ** b = g a ** b + g a' ** b"
immler@61916
  1476
    "\<And>a b b'. g a ** (b + b') = g a ** b + g a ** b'"
immler@61916
  1477
    "\<And>r a b. g (r *\<^sub>R a) ** b = r *\<^sub>R (g a ** b)"
immler@61916
  1478
    "\<And>a r b. g a ** (r *\<^sub>R b) = r *\<^sub>R (g a ** b)"
immler@61916
  1479
    by (auto simp: g.add add_left add_right g.scaleR scaleR_left scaleR_right)
immler@61916
  1480
  from g.nonneg_bounded nonneg_bounded
immler@61916
  1481
  obtain K L
immler@61916
  1482
  where nn: "0 \<le> K" "0 \<le> L"
immler@61916
  1483
    and K: "\<And>x. norm (g x) \<le> norm x * K"
immler@61916
  1484
    and L: "\<And>a b. norm (a ** b) \<le> norm a * norm b * L"
immler@61916
  1485
    by auto
immler@61916
  1486
  have "norm (g a ** b) \<le> norm a * K * norm b * L" for a b
immler@61916
  1487
    by (auto intro!:  order_trans[OF K] order_trans[OF L] mult_mono simp: nn)
immler@61916
  1488
  then show "\<exists>K. \<forall>a b. norm (g a ** b) \<le> norm a * norm b * K"
immler@61916
  1489
    by (auto intro!: exI[where x="K * L"] simp: ac_simps)
immler@61916
  1490
qed
immler@61916
  1491
immler@61916
  1492
lemma comp:
immler@61916
  1493
  "bounded_linear f \<Longrightarrow> bounded_linear g \<Longrightarrow> bounded_bilinear (\<lambda>x y. f x ** g y)"
immler@61916
  1494
  by (rule bounded_bilinear.flip[OF bounded_bilinear.comp1[OF bounded_bilinear.flip[OF comp1]]])
immler@61916
  1495
huffman@27443
  1496
end
huffman@27443
  1497
hoelzl@51642
  1498
lemma bounded_linear_ident[simp]: "bounded_linear (\<lambda>x. x)"
wenzelm@61169
  1499
  by standard (auto intro!: exI[of _ 1])
hoelzl@51642
  1500
hoelzl@51642
  1501
lemma bounded_linear_zero[simp]: "bounded_linear (\<lambda>x. 0)"
wenzelm@61169
  1502
  by standard (auto intro!: exI[of _ 1])
hoelzl@51642
  1503
hoelzl@51642
  1504
lemma bounded_linear_add:
hoelzl@51642
  1505
  assumes "bounded_linear f"
hoelzl@51642
  1506
  assumes "bounded_linear g"
hoelzl@51642
  1507
  shows "bounded_linear (\<lambda>x. f x + g x)"
hoelzl@51642
  1508
proof -
hoelzl@51642
  1509
  interpret f: bounded_linear f by fact
hoelzl@51642
  1510
  interpret g: bounded_linear g by fact
hoelzl@51642
  1511
  show ?thesis
hoelzl@51642
  1512
  proof
hoelzl@51642
  1513
    from f.bounded obtain Kf where Kf: "\<And>x. norm (f x) \<le> norm x * Kf" by blast
hoelzl@51642
  1514
    from g.bounded obtain Kg where Kg: "\<And>x. norm (g x) \<le> norm x * Kg" by blast
hoelzl@51642
  1515
    show "\<exists>K. \<forall>x. norm (f x + g x) \<le> norm x * K"
hoelzl@51642
  1516
      using add_mono[OF Kf Kg]
hoelzl@51642
  1517
      by (intro exI[of _ "Kf + Kg"]) (auto simp: field_simps intro: norm_triangle_ineq order_trans)
hoelzl@51642
  1518
  qed (simp_all add: f.add g.add f.scaleR g.scaleR scaleR_right_distrib)
hoelzl@51642
  1519
qed
hoelzl@51642
  1520
hoelzl@51642
  1521
lemma bounded_linear_minus:
hoelzl@51642
  1522
  assumes "bounded_linear f"
hoelzl@51642
  1523
  shows "bounded_linear (\<lambda>x. - f x)"
hoelzl@51642
  1524
proof -
hoelzl@51642
  1525
  interpret f: bounded_linear f by fact
hoelzl@51642
  1526
  show ?thesis apply (unfold_locales)
hoelzl@51642
  1527
    apply (simp add: f.add)
hoelzl@51642
  1528
    apply (simp add: f.scaleR)
hoelzl@51642
  1529
    apply (simp add: f.bounded)
hoelzl@51642
  1530
    done
hoelzl@51642
  1531
qed
hoelzl@51642
  1532
immler@61915
  1533
lemma bounded_linear_sub: "bounded_linear f \<Longrightarrow> bounded_linear g \<Longrightarrow> bounded_linear (\<lambda>x. f x - g x)"
immler@61915
  1534
  using bounded_linear_add[of f "\<lambda>x. - g x"] bounded_linear_minus[of g]
immler@61915
  1535
  by (auto simp add: algebra_simps)
immler@61915
  1536
immler@61915
  1537
lemma bounded_linear_setsum:
immler@61915
  1538
  fixes f :: "'i \<Rightarrow> 'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
immler@61915
  1539
  assumes "\<And>i. i \<in> I \<Longrightarrow> bounded_linear (f i)"
immler@61915
  1540
  shows "bounded_linear (\<lambda>x. \<Sum>i\<in>I. f i x)"
immler@61915
  1541
proof cases
immler@61915
  1542
  assume "finite I"
immler@61915
  1543
  from this show ?thesis
immler@61915
  1544
    using assms
immler@61915
  1545
    by (induct I) (auto intro!: bounded_linear_add)
immler@61915
  1546
qed simp
immler@61915
  1547
hoelzl@51642
  1548
lemma bounded_linear_compose:
hoelzl@51642
  1549
  assumes "bounded_linear f"
hoelzl@51642
  1550
  assumes "bounded_linear g"
hoelzl@51642
  1551
  shows "bounded_linear (\<lambda>x. f (g x))"
hoelzl@51642
  1552
proof -
hoelzl@51642
  1553
  interpret f: bounded_linear f by fact
hoelzl@51642
  1554
  interpret g: bounded_linear g by fact
hoelzl@51642
  1555
  show ?thesis proof (unfold_locales)
hoelzl@51642
  1556
    fix x y show "f (g (x + y)) = f (g x) + f (g y)"
hoelzl@51642
  1557
      by (simp only: f.add g.add)
hoelzl@51642
  1558
  next
hoelzl@51642
  1559
    fix r x show "f (g (scaleR r x)) = scaleR r (f (g x))"
hoelzl@51642
  1560
      by (simp only: f.scaleR g.scaleR)
hoelzl@51642
  1561
  next
hoelzl@51642
  1562
    from f.pos_bounded
lp15@61649
  1563
    obtain Kf where f: "\<And>x. norm (f x) \<le> norm x * Kf" and Kf: "0 < Kf" by blast
hoelzl@51642
  1564
    from g.pos_bounded
lp15@61649
  1565
    obtain Kg where g: "\<And>x. norm (g x) \<le> norm x * Kg" by blast
hoelzl@51642
  1566
    show "\<exists>K. \<forall>x. norm (f (g x)) \<le> norm x * K"
hoelzl@51642
  1567
    proof (intro exI allI)
hoelzl@51642
  1568
      fix x
hoelzl@51642
  1569
      have "norm (f (g x)) \<le> norm (g x) * Kf"
hoelzl@51642
  1570
        using f .
hoelzl@51642
  1571
      also have "\<dots> \<le> (norm x * Kg) * Kf"
hoelzl@51642
  1572
        using g Kf [THEN order_less_imp_le] by (rule mult_right_mono)
hoelzl@51642
  1573
      also have "(norm x * Kg) * Kf = norm x * (Kg * Kf)"
haftmann@57512
  1574
        by (rule mult.assoc)
hoelzl@51642
  1575
      finally show "norm (f (g x)) \<le> norm x * (Kg * Kf)" .
hoelzl@51642
  1576
    qed
hoelzl@51642
  1577
  qed
hoelzl@51642
  1578
qed
hoelzl@51642
  1579
huffman@44282
  1580
lemma bounded_bilinear_mult:
huffman@44282
  1581
  "bounded_bilinear (op * :: 'a \<Rightarrow> 'a \<Rightarrow> 'a::real_normed_algebra)"
huffman@22442
  1582
apply (rule bounded_bilinear.intro)
webertj@49962
  1583
apply (rule distrib_right)
webertj@49962
  1584
apply (rule distrib_left)
huffman@22442
  1585
apply (rule mult_scaleR_left)
huffman@22442
  1586
apply (rule mult_scaleR_right)
huffman@22442
  1587
apply (rule_tac x="1" in exI)
huffman@22442
  1588
apply (simp add: norm_mult_ineq)
huffman@22442
  1589
done
huffman@22442
  1590
huffman@44282
  1591
lemma bounded_linear_mult_left:
huffman@44282
  1592
  "bounded_linear (\<lambda>x::'a::real_normed_algebra. x * y)"
huffman@44282
  1593
  using bounded_bilinear_mult
huffman@44282
  1594
  by (rule bounded_bilinear.bounded_linear_left)
huffman@22442
  1595
huffman@44282
  1596
lemma bounded_linear_mult_right:
huffman@44282
  1597
  "bounded_linear (\<lambda>y::'a::real_normed_algebra. x * y)"
huffman@44282
  1598
  using bounded_bilinear_mult
huffman@44282
  1599
  by (rule bounded_bilinear.bounded_linear_right)
huffman@23127
  1600
hoelzl@51642
  1601
lemmas bounded_linear_mult_const =
hoelzl@51642
  1602
  bounded_linear_mult_left [THEN bounded_linear_compose]
hoelzl@51642
  1603
hoelzl@51642
  1604
lemmas bounded_linear_const_mult =
hoelzl@51642
  1605
  bounded_linear_mult_right [THEN bounded_linear_compose]
hoelzl@51642
  1606
huffman@44282
  1607
lemma bounded_linear_divide:
huffman@44282
  1608
  "bounded_linear (\<lambda>x::'a::real_normed_field. x / y)"
huffman@44282
  1609
  unfolding divide_inverse by (rule bounded_linear_mult_left)
huffman@23120
  1610
huffman@44282
  1611
lemma bounded_bilinear_scaleR: "bounded_bilinear scaleR"
huffman@22442
  1612
apply (rule bounded_bilinear.intro)
huffman@22442
  1613
apply (rule scaleR_left_distrib)
huffman@22442
  1614
apply (rule scaleR_right_distrib)
huffman@22973
  1615
apply simp
huffman@22442
  1616
apply (rule scaleR_left_commute)
huffman@31586
  1617
apply (rule_tac x="1" in exI, simp)
huffman@22442
  1618
done
huffman@22442
  1619
huffman@44282
  1620
lemma bounded_linear_scaleR_left: "bounded_linear (\<lambda>r. scaleR r x)"
huffman@44282
  1621
  using bounded_bilinear_scaleR
huffman@44282
  1622
  by (rule bounded_bilinear.bounded_linear_left)
huffman@23127
  1623
huffman@44282
  1624
lemma bounded_linear_scaleR_right: "bounded_linear (\<lambda>x. scaleR r x)"
huffman@44282
  1625
  using bounded_bilinear_scaleR
huffman@44282
  1626
  by (rule bounded_bilinear.bounded_linear_right)
huffman@23127
  1627
immler@61915
  1628
lemmas bounded_linear_scaleR_const =
immler@61915
  1629
  bounded_linear_scaleR_left[THEN bounded_linear_compose]
immler@61915
  1630
immler@61915
  1631
lemmas bounded_linear_const_scaleR =
immler@61915
  1632
  bounded_linear_scaleR_right[THEN bounded_linear_compose]
immler@61915
  1633
huffman@44282
  1634
lemma bounded_linear_of_real: "bounded_linear (\<lambda>r. of_real r)"
huffman@44282
  1635
  unfolding of_real_def by (rule bounded_linear_scaleR_left)
huffman@22625
  1636
hoelzl@51642
  1637
lemma real_bounded_linear:
hoelzl@51642
  1638
  fixes f :: "real \<Rightarrow> real"
hoelzl@51642
  1639
  shows "bounded_linear f \<longleftrightarrow> (\<exists>c::real. f = (\<lambda>x. x * c))"
hoelzl@51642
  1640
proof -
hoelzl@51642
  1641
  { fix x assume "bounded_linear f"
hoelzl@51642
  1642
    then interpret bounded_linear f .
hoelzl@51642
  1643
    from scaleR[of x 1] have "f x = x * f 1"
hoelzl@51642
  1644
      by simp }
hoelzl@51642
  1645
  then show ?thesis
hoelzl@51642
  1646
    by (auto intro: exI[of _ "f 1"] bounded_linear_mult_left)
hoelzl@51642
  1647
qed
hoelzl@51642
  1648
lp15@60800
  1649
lemma bij_linear_imp_inv_linear:
lp15@60800
  1650
  assumes "linear f" "bij f" shows "linear (inv f)"
lp15@60800
  1651
  using assms unfolding linear_def linear_axioms_def additive_def
lp15@60800
  1652
  by (auto simp: bij_is_surj bij_is_inj surj_f_inv_f intro!:  Hilbert_Choice.inv_f_eq)
lp15@61609
  1653
huffman@44571
  1654
instance real_normed_algebra_1 \<subseteq> perfect_space
huffman@44571
  1655
proof
huffman@44571
  1656
  fix x::'a
huffman@44571
  1657
  show "\<not> open {x}"
huffman@44571
  1658
    unfolding open_dist dist_norm
huffman@44571
  1659
    by (clarsimp, rule_tac x="x + of_real (e/2)" in exI, simp)
huffman@44571
  1660
qed
huffman@44571
  1661
wenzelm@60758
  1662
subsection \<open>Filters and Limits on Metric Space\<close>
hoelzl@51531
  1663
hoelzl@57448
  1664
lemma (in metric_space) nhds_metric: "nhds x = (INF e:{0 <..}. principal {y. dist y x < e})"
hoelzl@57448
  1665
  unfolding nhds_def
hoelzl@57448
  1666
proof (safe intro!: INF_eq)
hoelzl@57448
  1667
  fix S assume "open S" "x \<in> S"
hoelzl@57448
  1668
  then obtain e where "{y. dist y x < e} \<subseteq> S" "0 < e"
hoelzl@57448
  1669
    by (auto simp: open_dist subset_eq)
hoelzl@57448
  1670
  then show "\<exists>e\<in>{0<..}. principal {y. dist y x < e} \<le> principal S"
hoelzl@57448
  1671
    by auto
hoelzl@57448
  1672
qed (auto intro!: exI[of _ "{y. dist x y < e}" for e] open_ball simp: dist_commute)
hoelzl@57448
  1673
hoelzl@57448
  1674
lemma (in metric_space) tendsto_iff:
wenzelm@61973
  1675
  "(f \<longlongrightarrow> l) F \<longleftrightarrow> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) F)"
hoelzl@57448
  1676
  unfolding nhds_metric filterlim_INF filterlim_principal by auto
hoelzl@57448
  1677
wenzelm@61973
  1678
lemma (in metric_space) tendstoI: "(\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F) \<Longrightarrow> (f \<longlongrightarrow> l) F"
hoelzl@57448
  1679
  by (auto simp: tendsto_iff)
hoelzl@57448
  1680
wenzelm@61973
  1681
lemma (in metric_space) tendstoD: "(f \<longlongrightarrow> l) F \<Longrightarrow> 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
hoelzl@57448
  1682
  by (auto simp: tendsto_iff)
hoelzl@57448
  1683
hoelzl@57448
  1684
lemma (in metric_space) eventually_nhds_metric:
hoelzl@57448
  1685
  "eventually P (nhds a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. dist x a < d \<longrightarrow> P x)"
hoelzl@57448
  1686
  unfolding nhds_metric
hoelzl@57448
  1687
  by (subst eventually_INF_base)
hoelzl@57448
  1688
     (auto simp: eventually_principal Bex_def subset_eq intro: exI[of _ "min a b" for a b])
hoelzl@51531
  1689
hoelzl@51531
  1690
lemma eventually_at:
hoelzl@51641
  1691
  fixes a :: "'a :: metric_space"
hoelzl@51641
  1692
  shows "eventually P (at a within S) \<longleftrightarrow> (\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<and> dist x a < d \<longrightarrow> P x)"
paulson@62087
  1693
  unfolding eventually_at_filter eventually_nhds_metric by auto
hoelzl@51531
  1694
hoelzl@51641
  1695
lemma eventually_at_le:
hoelzl@51641
  1696
  fixes a :: "'a::metric_space"
hoelzl@51641
  1697
  shows "eventually P (at a within S) \<longleftrightarrow> (\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<and> dist x a \<le> d \<longrightarrow> P x)"
hoelzl@51641
  1698
  unfolding eventually_at_filter eventually_nhds_metric
hoelzl@51641
  1699
  apply auto
hoelzl@51641
  1700
  apply (rule_tac x="d / 2" in exI)
hoelzl@51641
  1701
  apply auto
hoelzl@51641
  1702
  done
hoelzl@51531
  1703
eberlm@61531
  1704
lemma eventually_at_left_real: "a > (b :: real) \<Longrightarrow> eventually (\<lambda>x. x \<in> {b<..<a}) (at_left a)"
eberlm@61531
  1705
  by (subst eventually_at, rule exI[of _ "a - b"]) (force simp: dist_real_def)
eberlm@61531
  1706
eberlm@61531
  1707
lemma eventually_at_right_real: "a < (b :: real) \<Longrightarrow> eventually (\<lambda>x. x \<in> {a<..<b}) (at_right a)"
eberlm@61531
  1708
  by (subst eventually_at, rule exI[of _ "b - a"]) (force simp: dist_real_def)
eberlm@61531
  1709
hoelzl@51531
  1710
lemma metric_tendsto_imp_tendsto:
hoelzl@51531
  1711
  fixes a :: "'a :: metric_space" and b :: "'b :: metric_space"
wenzelm@61973
  1712
  assumes f: "(f \<longlongrightarrow> a) F"
hoelzl@51531
  1713
  assumes le: "eventually (\<lambda>x. dist (g x) b \<le> dist (f x) a) F"
wenzelm@61973
  1714
  shows "(g \<longlongrightarrow> b) F"
hoelzl@51531
  1715
proof (rule tendstoI)
hoelzl@51531
  1716
  fix e :: real assume "0 < e"
hoelzl@51531
  1717
  with f have "eventually (\<lambda>x. dist (f x) a < e) F" by (rule tendstoD)
hoelzl@51531
  1718
  with le show "eventually (\<lambda>x. dist (g x) b < e) F"
hoelzl@51531
  1719
    using le_less_trans by (rule eventually_elim2)
hoelzl@51531
  1720
qed
hoelzl@51531
  1721
hoelzl@51531
  1722
lemma filterlim_real_sequentially: "LIM x sequentially. real x :> at_top"
hoelzl@51531
  1723
  unfolding filterlim_at_top
hoelzl@51531
  1724
  apply (intro allI)
wenzelm@61942
  1725
  apply (rule_tac c="nat \<lceil>Z + 1\<rceil>" in eventually_sequentiallyI)
wenzelm@61942
  1726
  apply linarith
wenzelm@61942
  1727
  done
wenzelm@61942
  1728
hoelzl@51531
  1729
wenzelm@60758
  1730
subsubsection \<open>Limits of Sequences\<close>
hoelzl@51531
  1731
wenzelm@61969
  1732
lemma lim_sequentially: "X \<longlonglongrightarrow> (L::'a::metric_space) \<longleftrightarrow> (\<forall>r>0. \<exists>no. \<forall>n\<ge>no. dist (X n) L < r)"
hoelzl@51531
  1733
  unfolding tendsto_iff eventually_sequentially ..
hoelzl@51531
  1734
lp15@60026
  1735
lemmas LIMSEQ_def = lim_sequentially  (*legacy binding*)
lp15@60026
  1736
wenzelm@61969
  1737
lemma LIMSEQ_iff_nz: "X \<longlonglongrightarrow> (L::'a::metric_space) = (\<forall>r>0. \<exists>no>0. \<forall>n\<ge>no. dist (X n) L < r)"
lp15@60017
  1738
  unfolding lim_sequentially by (metis Suc_leD zero_less_Suc)
hoelzl@51531
  1739
hoelzl@51531
  1740
lemma metric_LIMSEQ_I:
wenzelm@61969
  1741
  "(\<And>r. 0 < r \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r) \<Longrightarrow> X \<longlonglongrightarrow> (L::'a::metric_space)"
lp15@60017
  1742
by (simp add: lim_sequentially)
hoelzl@51531
  1743
hoelzl@51531
  1744
lemma metric_LIMSEQ_D:
wenzelm@61969
  1745
  "\<lbrakk>X \<longlonglongrightarrow> (L::'a::metric_space); 0 < r\<rbrakk> \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r"
lp15@60017
  1746
by (simp add: lim_sequentially)
hoelzl@51531
  1747
hoelzl@51531
  1748
wenzelm@60758
  1749
subsubsection \<open>Limits of Functions\<close>
hoelzl@51531
  1750
wenzelm@61976
  1751
lemma LIM_def: "f \<midarrow>(a::'a::metric_space)\<rightarrow> (L::'b::metric_space) =
hoelzl@51531
  1752
     (\<forall>r > 0. \<exists>s > 0. \<forall>x. x \<noteq> a & dist x a < s
hoelzl@51531
  1753
        --> dist (f x) L < r)"
hoelzl@51641
  1754
  unfolding tendsto_iff eventually_at by simp
hoelzl@51531
  1755
hoelzl@51531
  1756
lemma metric_LIM_I:
hoelzl@51531
  1757
  "(\<And>r. 0 < r \<Longrightarrow> \<exists>s>0. \<forall>x. x \<noteq> a \<and> dist x a < s \<longrightarrow> dist (f x) L < r)
wenzelm@61976
  1758
    \<Longrightarrow> f \<midarrow>(a::'a::metric_space)\<rightarrow> (L::'b::metric_space)"
hoelzl@51531
  1759
by (simp add: LIM_def)
hoelzl@51531
  1760
hoelzl@51531
  1761
lemma metric_LIM_D:
wenzelm@61976
  1762
  "\<lbrakk>f \<midarrow>(a::'a::metric_space)\<rightarrow> (L::'b::metric_space); 0 < r\<rbrakk>
hoelzl@51531
  1763
    \<Longrightarrow> \<exists>s>0. \<forall>x. x \<noteq> a \<and> dist x a < s \<longrightarrow> dist (f x) L < r"
hoelzl@51531
  1764
by (simp add: LIM_def)
hoelzl@51531
  1765
hoelzl@51531
  1766
lemma metric_LIM_imp_LIM:
wenzelm@61976
  1767
  assumes f: "f \<midarrow>a\<rightarrow> (l::'a::metric_space)"
hoelzl@51531
  1768
  assumes le: "\<And>x. x \<noteq> a \<Longrightarrow> dist (g x) m \<le> dist (f x) l"
wenzelm@61976
  1769
  shows "g \<midarrow>a\<rightarrow> (m::'b::metric_space)"
hoelzl@51531
  1770
  by (rule metric_tendsto_imp_tendsto [OF f]) (auto simp add: eventually_at_topological le)
hoelzl@51531
  1771
hoelzl@51531
  1772
lemma metric_LIM_equal2:
hoelzl@51531
  1773
  assumes 1: "0 < R"
hoelzl@51531
  1774
  assumes 2: "\<And>x. \<lbrakk>x \<noteq> a; dist x a < R\<rbrakk> \<Longrightarrow> f x = g x"
wenzelm@61976
  1775
  shows "g \<midarrow>a\<rightarrow> l \<Longrightarrow> f \<midarrow>(a::'a::metric_space)\<rightarrow> l"
hoelzl@51531
  1776
apply (rule topological_tendstoI)
hoelzl@51531
  1777
apply (drule (2) topological_tendstoD)
hoelzl@51531
  1778
apply (simp add: eventually_at, safe)
hoelzl@51531
  1779
apply (rule_tac x="min d R" in exI, safe)
hoelzl@51531
  1780
apply (simp add: 1)
hoelzl@51531
  1781
apply (simp add: 2)
hoelzl@51531
  1782
done
hoelzl@51531
  1783
hoelzl@51531
  1784
lemma metric_LIM_compose2:
wenzelm@61976
  1785
  assumes f: "f \<midarrow>(a::'a::metric_space)\<rightarrow> b"
wenzelm@61976
  1786
  assumes g: "g \<midarrow>b\<rightarrow> c"
hoelzl@51531
  1787
  assumes inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<noteq> b"
wenzelm@61976
  1788
  shows "(\<lambda>x. g (f x)) \<midarrow>a\<rightarrow> c"
hoelzl@51641
  1789
  using inj
hoelzl@51641
  1790
  by (intro tendsto_compose_eventually[OF g f]) (auto simp: eventually_at)
hoelzl@51531
  1791
hoelzl@51531
  1792
lemma metric_isCont_LIM_compose2:
hoelzl@51531
  1793
  fixes f :: "'a :: metric_space \<Rightarrow> _"
hoelzl@51531
  1794
  assumes f [unfolded isCont_def]: "isCont f a"
wenzelm@61976
  1795
  assumes g: "g \<midarrow>f a\<rightarrow> l"
hoelzl@51531
  1796
  assumes inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<noteq> f a"
wenzelm@61976
  1797
  shows "(\<lambda>x. g (f x)) \<midarrow>a\<rightarrow> l"
hoelzl@51531
  1798
by (rule metric_LIM_compose2 [OF f g inj])
hoelzl@51531
  1799
wenzelm@60758
  1800
subsection \<open>Complete metric spaces\<close>
hoelzl@51531
  1801
wenzelm@60758
  1802
subsection \<open>Cauchy sequences\<close>
hoelzl@51531
  1803
hoelzl@62101
  1804
lemma (in metric_space) Cauchy_def: "Cauchy X = (\<forall>e>0. \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e)"
hoelzl@62101
  1805
proof -
hoelzl@62101
  1806
  have *: "eventually P (INF M. principal {(X m, X n) | n m. m \<ge> M \<and> n \<ge> M}) =
hoelzl@62101
  1807
    (\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. P (X m, X n))" for P
hoelzl@62101
  1808
  proof (subst eventually_INF_base, goal_cases)
hoelzl@62101
  1809
    case (2 a b) then show ?case
hoelzl@62101
  1810
      by (intro bexI[of _ "max a b"]) (auto simp: eventually_principal subset_eq)
hoelzl@62101
  1811
  qed (auto simp: eventually_principal, blast)
hoelzl@62101
  1812
  have "Cauchy X \<longleftrightarrow> (INF M. principal {(X m, X n) | n m. m \<ge> M \<and> n \<ge> M}) \<le> uniformity"
hoelzl@62101
  1813
    unfolding Cauchy_uniform_iff le_filter_def * ..
hoelzl@62101
  1814
  also have "\<dots> = (\<forall>e>0. \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e)"
hoelzl@62101
  1815
    unfolding uniformity_dist le_INF_iff by (auto simp: * le_principal)
hoelzl@62101
  1816
  finally show ?thesis .
hoelzl@62101
  1817
qed
hoelzl@51531
  1818
hoelzl@62101
  1819
lemma (in metric_space) Cauchy_altdef:
eberlm@61531
  1820
  "Cauchy f = (\<forall>e>0. \<exists>M. \<forall>m\<ge>M. \<forall>n>m. dist (f m) (f n) < e)"
eberlm@61531
  1821
proof
eberlm@61531
  1822
  assume A: "\<forall>e>0. \<exists>M. \<forall>m\<ge>M. \<forall>n>m. dist (f m) (f n) < e"
eberlm@61531
  1823
  show "Cauchy f" unfolding Cauchy_def
eberlm@61531
  1824
  proof (intro allI impI)
eberlm@61531
  1825
    fix e :: real assume e: "e > 0"
eberlm@61531
  1826
    with A obtain M where M: "\<And>m n. m \<ge> M \<Longrightarrow> n > m \<Longrightarrow> dist (f m) (f n) < e" by blast
eberlm@61531
  1827
    have "dist (f m) (f n) < e" if "m \<ge> M" "n \<ge> M" for m n
eberlm@61531
  1828
      using M[of m n] M[of n m] e that by (cases m n rule: linorder_cases) (auto simp: dist_commute)
eberlm@61531
  1829
    thus "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (f m) (f n) < e" by blast
eberlm@61531
  1830
  qed
eberlm@61531
  1831
next
eberlm@61531
  1832
  assume "Cauchy f"
lp15@61609
  1833
  show "\<forall>e>0. \<exists>M. \<forall>m\<ge>M. \<forall>n>m. dist (f m) (f n) < e"
eberlm@61531
  1834
  proof (intro allI impI)
eberlm@61531
  1835
    fix e :: real assume e: "e > 0"
wenzelm@61799
  1836
    with \<open>Cauchy f\<close> obtain M where "\<And>m n. m \<ge> M \<Longrightarrow> n \<ge> M \<Longrightarrow> dist (f m) (f n) < e"
lp15@61649
  1837
      unfolding Cauchy_def by blast
eberlm@61531
  1838
    thus "\<exists>M. \<forall>m\<ge>M. \<forall>n>m. dist (f m) (f n) < e" by (intro exI[of _ M]) force
eberlm@61531
  1839
  qed
eberlm@61531
  1840
qed
hoelzl@51531
  1841
hoelzl@62101
  1842
lemma (in metric_space) metric_CauchyI:
hoelzl@51531
  1843
  "(\<And>e. 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e) \<Longrightarrow> Cauchy X"
hoelzl@51531
  1844
  by (simp add: Cauchy_def)
hoelzl@51531
  1845
hoelzl@62101
  1846
lemma (in metric_space) CauchyI': "(\<And>e. 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n>m. dist (X m) (X n) < e) \<Longrightarrow> Cauchy X"
eberlm@61531
  1847
  unfolding Cauchy_altdef by blast
eberlm@61531
  1848
hoelzl@62101
  1849
lemma (in metric_space) metric_CauchyD:
hoelzl@51531
  1850
  "Cauchy X \<Longrightarrow> 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e"
hoelzl@51531
  1851
  by (simp add: Cauchy_def)
hoelzl@51531
  1852
hoelzl@62101
  1853
lemma (in metric_space) metric_Cauchy_iff2:
hoelzl@51531
  1854
  "Cauchy X = (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. dist (X m) (X n) < inverse(real (Suc j))))"
hoelzl@51531
  1855
apply (simp add: Cauchy_def, auto)
hoelzl@51531
  1856
apply (drule reals_Archimedean, safe)
hoelzl@51531
  1857
apply (drule_tac x = n in spec, auto)
hoelzl@51531
  1858
apply (rule_tac x = M in exI, auto)
hoelzl@51531
  1859
apply (drule_tac x = m in spec, simp)
hoelzl@51531
  1860
apply (drule_tac x = na in spec, auto)
hoelzl@51531
  1861
done
hoelzl@51531
  1862
hoelzl@51531
  1863
lemma Cauchy_iff2:
hoelzl@51531
  1864
  "Cauchy X = (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. \<bar>X m - X n\<bar> < inverse(real (Suc j))))"
hoelzl@51531
  1865
  unfolding metric_Cauchy_iff2 dist_real_def ..
hoelzl@51531
  1866
hoelzl@62101
  1867
lemma lim_1_over_n: "((\<lambda>n. 1 / of_nat n) \<longlongrightarrow> (0::'a::real_normed_field)) sequentially"
hoelzl@62101
  1868
proof (subst lim_sequentially, intro allI impI exI)
hoelzl@62101
  1869
  fix e :: real assume e: "e > 0"
hoelzl@62101
  1870
  fix n :: nat assume n: "n \<ge> nat \<lceil>inverse e + 1\<rceil>"
hoelzl@62101
  1871
  have "inverse e < of_nat (nat \<lceil>inverse e + 1\<rceil>)" by linarith
hoelzl@62101
  1872
  also note n
hoelzl@62101
  1873
  finally show "dist (1 / of_nat n :: 'a) 0 < e" using e
hoelzl@62101
  1874
    by (simp add: divide_simps mult.commute norm_conv_dist[symmetric] norm_divide)
hoelzl@51531
  1875
qed
hoelzl@51531
  1876
hoelzl@62101
  1877
lemma (in metric_space) complete_def:
hoelzl@62101
  1878
  shows "complete S = (\<forall>f. (\<forall>n. f n \<in> S) \<and> Cauchy f \<longrightarrow> (\<exists>l\<in>S. f \<longlonglongrightarrow> l))"
hoelzl@62101
  1879
  unfolding complete_uniform
hoelzl@62101
  1880
proof safe
hoelzl@62101
  1881
  fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> S" "Cauchy f"
hoelzl@62101
  1882
    and *: "\<forall>F\<le>principal S. F \<noteq> bot \<longrightarrow> cauchy_filter F \<longrightarrow> (\<exists>x\<in>S. F \<le> nhds x)"
hoelzl@62101
  1883
  then show "\<exists>l\<in>S. f \<longlonglongrightarrow> l"
hoelzl@62101
  1884
    unfolding filterlim_def using f
hoelzl@62101
  1885
    by (intro *[rule_format])
hoelzl@62101
  1886
       (auto simp: filtermap_sequentually_ne_bot le_principal eventually_filtermap Cauchy_uniform)
hoelzl@62101
  1887
next
hoelzl@62101
  1888
  fix F :: "'a filter" assume "F \<le> principal S" "F \<noteq> bot" "cauchy_filter F"
hoelzl@62101
  1889
  assume seq: "\<forall>f. (\<forall>n. f n \<in> S) \<and> Cauchy f \<longrightarrow> (\<exists>l\<in>S. f \<longlonglongrightarrow> l)"
hoelzl@62101
  1890
hoelzl@62101
  1891
  from \<open>F \<le> principal S\<close> \<open>cauchy_filter F\<close> have FF_le: "F \<times>\<^sub>F F \<le> uniformity_on S"
hoelzl@62101
  1892
    by (simp add: cauchy_filter_def principal_prod_principal[symmetric] prod_filter_mono)
hoelzl@62101
  1893
hoelzl@62101
  1894
  let ?P = "\<lambda>P e. eventually P F \<and> (\<forall>x. P x \<longrightarrow> x \<in> S) \<and> (\<forall>x y. P x \<longrightarrow> P y \<longrightarrow> dist x y < e)"
hoelzl@62101
  1895
hoelzl@62101
  1896
  { fix \<epsilon> :: real assume "0 < \<epsilon>"
hoelzl@62101
  1897
    then have "eventually (\<lambda>(x, y). x \<in> S \<and> y \<in> S \<and> dist x y < \<epsilon>) (uniformity_on S)"
hoelzl@62101
  1898
      unfolding eventually_inf_principal eventually_uniformity_metric by auto
hoelzl@62101
  1899
    from filter_leD[OF FF_le this] have "\<exists>P. ?P P \<epsilon>"
hoelzl@62101
  1900
      unfolding eventually_prod_same by auto }
hoelzl@62101
  1901
  note P = this
hoelzl@62101
  1902
hoelzl@62101
  1903
  have "\<exists>P. \<forall>n. ?P (P n) (1 / Suc n) \<and> P (Suc n) \<le> P n"
hoelzl@62101
  1904
  proof (rule dependent_nat_choice)
hoelzl@62101
  1905
    show "\<exists>P. ?P P (1 / Suc 0)"
hoelzl@62101
  1906
      using P[of 1] by auto
hoelzl@62101
  1907
  next
hoelzl@62101
  1908
    fix P n assume "?P P (1/Suc n)"
hoelzl@62101
  1909
    moreover obtain Q where "?P Q (1 / Suc (Suc n))"
hoelzl@62101
  1910
      using P[of "1/Suc (Suc n)"] by auto
hoelzl@62101
  1911
    ultimately show "\<exists>Q. ?P Q (1 / Suc (Suc n)) \<and> Q \<le> P"
hoelzl@62101
  1912
      by (intro exI[of _ "\<lambda>x. P x \<and> Q x"]) (auto simp: eventually_conj_iff)
hoelzl@62101
  1913
  qed
hoelzl@62101
  1914
  then obtain P where P: "\<And>n. eventually (P n) F" "\<And>n x. P n x \<Longrightarrow> x \<in> S"
hoelzl@62101
  1915
    "\<And>n x y. P n x \<Longrightarrow> P n y \<Longrightarrow> dist x y < 1 / Suc n" "\<And>n. P (Suc n) \<le> P n"
hoelzl@62101
  1916
    by metis
hoelzl@62101
  1917
  have "antimono P"
hoelzl@62101
  1918
    using P(4) unfolding decseq_Suc_iff le_fun_def by blast
hoelzl@62101
  1919
hoelzl@62101
  1920
  obtain X where X: "\<And>n. P n (X n)"
hoelzl@62101
  1921
    using P(1)[THEN eventually_happens'[OF \<open>F \<noteq> bot\<close>]] by metis
hoelzl@62101
  1922
  have "Cauchy X"
hoelzl@62101
  1923
    unfolding metric_Cauchy_iff2 inverse_eq_divide
hoelzl@62101
  1924
  proof (intro exI allI impI)
hoelzl@62101
  1925
    fix j m n :: nat assume "j \<le> m" "j \<le> n"
hoelzl@62101
  1926
    with \<open>antimono P\<close> X have "P j (X m)" "P j (X n)"
hoelzl@62101
  1927
      by (auto simp: antimono_def)
hoelzl@62101
  1928
    then show "dist (X m) (X n) < 1 / Suc j"
hoelzl@62101
  1929
      by (rule P)
hoelzl@62101
  1930
  qed
hoelzl@62101
  1931
  moreover have "\<forall>n. X n \<in> S"
hoelzl@62101
  1932
    using P(2) X by auto
hoelzl@62101
  1933
  ultimately obtain x where "X \<longlonglongrightarrow> x" "x \<in> S"
hoelzl@62101
  1934
    using seq by blast
hoelzl@62101
  1935
hoelzl@62101
  1936
  show "\<exists>x\<in>S. F \<le> nhds x"
hoelzl@62101
  1937
  proof (rule bexI)
hoelzl@62101
  1938
    { fix e :: real assume "0 < e"
hoelzl@62101
  1939
      then have "(\<lambda>n. 1 / Suc n :: real) \<longlonglongrightarrow> 0 \<and> 0 < e / 2"
hoelzl@62101
  1940
        by (subst LIMSEQ_Suc_iff) (auto intro!: lim_1_over_n)
hoelzl@62101
  1941
      then have "\<forall>\<^sub>F n in sequentially. dist (X n) x < e / 2 \<and> 1 / Suc n < e / 2"
hoelzl@62101
  1942
        using \<open>X \<longlonglongrightarrow> x\<close> unfolding tendsto_iff order_tendsto_iff[where 'a=real] eventually_conj_iff by blast
hoelzl@62101
  1943
      then obtain n where "dist x (X n) < e / 2" "1 / Suc n < e / 2"
hoelzl@62101
  1944
        by (auto simp: eventually_sequentially dist_commute)
hoelzl@62101
  1945
      have "eventually (\<lambda>y. dist y x < e) F"
hoelzl@62101
  1946
        using \<open>eventually (P n) F\<close>
hoelzl@62101
  1947
      proof eventually_elim
hoelzl@62101
  1948
        fix y assume "P n y"
hoelzl@62101
  1949
        then have "dist y (X n) < 1 / Suc n"
hoelzl@62101
  1950
          by (intro X P)
hoelzl@62101
  1951
        also have "\<dots> < e / 2" by fact
hoelzl@62101
  1952
        finally show "dist y x < e"
hoelzl@62101
  1953
          by (rule dist_triangle_half_l) fact
hoelzl@62101
  1954
      qed }
hoelzl@62101
  1955
    then show "F \<le> nhds x"
hoelzl@62101
  1956
      unfolding nhds_metric le_INF_iff le_principal by auto
hoelzl@62101
  1957
  qed fact
hoelzl@62101
  1958
qed
hoelzl@62101
  1959
hoelzl@62101
  1960
lemma (in metric_space) totally_bounded_metric:
hoelzl@62101
  1961
  "totally_bounded S \<longleftrightarrow> (\<forall>e>0. \<exists>k. finite k \<and> S \<subseteq> (\<Union>x\<in>k. {y. dist x y < e}))"
hoelzl@62101
  1962
  unfolding totally_bounded_def eventually_uniformity_metric imp_ex
hoelzl@62101
  1963
  apply (subst all_comm)
hoelzl@62101
  1964
  apply (intro arg_cong[where f=All] ext)
hoelzl@62101
  1965
  apply safe
hoelzl@62101
  1966
  subgoal for e
hoelzl@62101
  1967
    apply (erule allE[of _ "\<lambda>(x, y). dist x y < e"])
hoelzl@62101
  1968
    apply auto
hoelzl@62101
  1969
    done
hoelzl@62101
  1970
  subgoal for e P k
hoelzl@62101
  1971
    apply (intro exI[of _ k])
hoelzl@62101
  1972
    apply (force simp: subset_eq)
hoelzl@62101
  1973
    done
hoelzl@62101
  1974
  done
hoelzl@51531
  1975
wenzelm@60758
  1976
subsubsection \<open>Cauchy Sequences are Convergent\<close>
hoelzl@51531
  1977
hoelzl@62101
  1978
(* TODO: update to uniform_space *)
hoelzl@51531
  1979
class complete_space = metric_space +
hoelzl@51531
  1980
  assumes Cauchy_convergent: "Cauchy X \<Longrightarrow> convergent X"
hoelzl@51531
  1981
hoelzl@51531
  1982
lemma Cauchy_convergent_iff:
hoelzl@51531
  1983
  fixes X :: "nat \<Rightarrow> 'a::complete_space"
hoelzl@51531
  1984
  shows "Cauchy X = convergent X"
lp15@61649
  1985
by (blast intro: Cauchy_convergent convergent_Cauchy)
hoelzl@51531
  1986
wenzelm@60758
  1987
subsection \<open>The set of real numbers is a complete metric space\<close>
hoelzl@51531
  1988
wenzelm@60758
  1989
text \<open>
hoelzl@51531
  1990
Proof that Cauchy sequences converge based on the one from
wenzelm@54703
  1991
@{url "http://pirate.shu.edu/~wachsmut/ira/numseq/proofs/cauconv.html"}
wenzelm@60758
  1992
\<close>
hoelzl@51531
  1993
wenzelm@60758
  1994
text \<open>
hoelzl@51531
  1995
  If sequence @{term "X"} is Cauchy, then its limit is the lub of
hoelzl@51531
  1996
  @{term "{r::real. \<exists>N. \<forall>n\<ge>N. r < X n}"}
wenzelm@60758
  1997
\<close>
hoelzl@51531
  1998
hoelzl@51531
  1999
lemma increasing_LIMSEQ:
hoelzl@51531
  2000
  fixes f :: "nat \<Rightarrow> real"
hoelzl@51531
  2001
  assumes inc: "\<And>n. f n \<le> f (Suc n)"
hoelzl@51531
  2002
      and bdd: "\<And>n. f n \<le> l"
hoelzl@51531
  2003
      and en: "\<And>e. 0 < e \<Longrightarrow> \<exists>n. l \<le> f n + e"
wenzelm@61969
  2004
  shows "f \<longlonglongrightarrow> l"
hoelzl@51531
  2005
proof (rule increasing_tendsto)
hoelzl@51531
  2006
  fix x assume "x < l"
hoelzl@51531
  2007
  with dense[of 0 "l - x"] obtain e where "0 < e" "e < l - x"
hoelzl@51531
  2008
    by auto
wenzelm@60758
  2009
  from en[OF \<open>0 < e\<close>] obtain n where "l - e \<le> f n"
hoelzl@51531
  2010
    by (auto simp: field_simps)
wenzelm@60758
  2011
  with \<open>e < l - x\<close> \<open>0 < e\<close> have "x < f n" by simp
hoelzl@51531
  2012
  with incseq_SucI[of f, OF inc] show "eventually (\<lambda>n. x < f n) sequentially"
hoelzl@51531
  2013
    by (auto simp: eventually_sequentially incseq_def intro: less_le_trans)
hoelzl@51531
  2014
qed (insert bdd, auto)
hoelzl@51531
  2015
hoelzl@51531
  2016
lemma real_Cauchy_convergent:
hoelzl@51531
  2017
  fixes X :: "nat \<Rightarrow> real"
hoelzl@51531
  2018
  assumes X: "Cauchy X"
hoelzl@51531
  2019
  shows "convergent X"
hoelzl@51531
  2020
proof -
hoelzl@51531
  2021
  def S \<equiv> "{x::real. \<exists>N. \<forall>n\<ge>N. x < X n}"
hoelzl@51531
  2022
  then have mem_S: "\<And>N x. \<forall>n\<ge>N. x < X n \<Longrightarrow> x \<in> S" by auto
hoelzl@51531
  2023
hoelzl@51531
  2024
  { fix N x assume N: "\<forall>n\<ge>N. X n < x"
hoelzl@51531
  2025
  fix y::real assume "y \<in> S"
hoelzl@51531
  2026
  hence "\<exists>M. \<forall>n\<ge>M. y < X n"
hoelzl@51531
  2027
    by (simp add: S_def)
hoelzl@51531
  2028
  then obtain M where "\<forall>n\<ge>M. y < X n" ..
hoelzl@51531
  2029
  hence "y < X (max M N)" by simp
hoelzl@51531
  2030
  also have "\<dots> < x" using N by simp
hoelzl@54263
  2031
  finally have "y \<le> x"
hoelzl@54263
  2032
    by (rule order_less_imp_le) }
lp15@60026
  2033
  note bound_isUb = this
hoelzl@51531
  2034
hoelzl@51531
  2035
  obtain N where "\<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m) (X n) < 1"
hoelzl@51531
  2036
    using X[THEN metric_CauchyD, OF zero_less_one] by auto
hoelzl@51531
  2037
  hence N: "\<forall>n\<ge>N. dist (X n) (X N) < 1" by simp
hoelzl@54263
  2038
  have [simp]: "S \<noteq> {}"
hoelzl@54263
  2039
  proof (intro exI ex_in_conv[THEN iffD1])
hoelzl@51531
  2040
    from N have "\<forall>n\<ge>N. X N - 1 < X n"
hoelzl@51531
  2041
      by (simp add: abs_diff_less_iff dist_real_def)
hoelzl@51531
  2042
    thus "X N - 1 \<in> S" by (rule mem_S)
hoelzl@51531
  2043
  qed
hoelzl@54263
  2044
  have [simp]: "bdd_above S"
hoelzl@51531
  2045
  proof
hoelzl@51531
  2046
    from N have "\<forall>n\<ge>N. X n < X N + 1"
hoelzl@51531
  2047
      by (simp add: abs_diff_less_iff dist_real_def)
hoelzl@54263
  2048
    thus "\<And>s. s \<in> S \<Longrightarrow>  s \<le> X N + 1"
hoelzl@51531
  2049
      by (rule bound_isUb)
hoelzl@51531
  2050
  qed
wenzelm@61969
  2051
  have "X \<longlonglongrightarrow> Sup S"
hoelzl@51531
  2052
  proof (rule metric_LIMSEQ_I)
hoelzl@51531
  2053
  fix r::real assume "0 < r"
hoelzl@51531
  2054
  hence r: "0 < r/2" by simp
hoelzl@51531
  2055
  obtain N where "\<forall>n\<ge>N. \<forall>m\<ge>N. dist (X n) (X m) < r/2"
hoelzl@51531
  2056
    using metric_CauchyD [OF X r] by auto
hoelzl@51531
  2057
  hence "\<forall>n\<ge>N. dist (X n) (X N) < r/2" by simp
hoelzl@51531
  2058
  hence N: "\<forall>n\<ge>N. X N - r/2 < X n \<and> X n < X N + r/2"
hoelzl@51531
  2059
    by (simp only: dist_real_def abs_diff_less_iff)
hoelzl@51531
  2060
lp15@61649
  2061
  from N have "\<forall>n\<ge>N. X N - r/2 < X n" by blast
hoelzl@51531
  2062
  hence "X N - r/2 \<in> S" by (rule mem_S)
hoelzl@54263
  2063
  hence 1: "X N - r/2 \<le> Sup S" by (simp add: cSup_upper)
hoelzl@51531
  2064
lp15@61649
  2065
  from N have "\<forall>n\<ge>N. X n < X N + r/2" by blast
hoelzl@54263
  2066
  from bound_isUb[OF this]
hoelzl@54263
  2067
  have 2: "Sup S \<le> X N + r/2"
hoelzl@54263
  2068
    by (intro cSup_least) simp_all
hoelzl@51531
  2069
hoelzl@54263
  2070
  show "\<exists>N. \<forall>n\<ge>N. dist (X n) (Sup S) < r"
hoelzl@51531
  2071
  proof (intro exI allI impI)
hoelzl@51531
  2072
    fix n assume n: "N \<le> n"
hoelzl@51531
  2073
    from N n have "X n < X N + r/2" and "X N - r/2 < X n" by simp+
hoelzl@54263
  2074
    thus "dist (X n) (Sup S) < r" using 1 2
hoelzl@51531
  2075
      by (simp add: abs_diff_less_iff dist_real_def)
hoelzl@51531
  2076
  qed
hoelzl@51531
  2077
  qed
hoelzl@51531
  2078
  then show ?thesis unfolding convergent_def by auto
hoelzl@51531
  2079
qed
hoelzl@51531
  2080
hoelzl@51531
  2081
instance real :: complete_space
hoelzl@51531
  2082
  by intro_classes (rule real_Cauchy_convergent)
hoelzl@51531
  2083
hoelzl@51531
  2084
class banach = real_normed_vector + complete_space
hoelzl@51531
  2085
wenzelm@61169
  2086
instance real :: banach ..
hoelzl@51531
  2087
hoelzl@51531
  2088
lemma tendsto_at_topI_sequentially:
hoelzl@57275
  2089
  fixes f :: "real \<Rightarrow> 'b::first_countable_topology"
wenzelm@61969
  2090
  assumes *: "\<And>X. filterlim X at_top sequentially \<Longrightarrow> (\<lambda>n. f (X n)) \<longlonglongrightarrow> y"
wenzelm@61973
  2091
  shows "(f \<longlongrightarrow> y) at_top"
hoelzl@57448
  2092
proof -
hoelzl@57448
  2093
  from nhds_countable[of y] guess A . note A = this
hoelzl@57275
  2094
hoelzl@57448
  2095
  have "\<forall>m. \<exists>k. \<forall>x\<ge>k. f x \<in> A m"
hoelzl@57448
  2096
  proof (rule ccontr)
hoelzl@57448
  2097
    assume "\<not> (\<forall>m. \<exists>k. \<forall>x\<ge>k. f x \<in> A m)"
hoelzl@57448
  2098
    then obtain m where "\<And>k. \<exists>x\<ge>k. f x \<notin> A m"
hoelzl@57448
  2099
      by auto
hoelzl@57448
  2100
    then have "\<exists>X. \<forall>n. (f (X n) \<notin> A m) \<and> max n (X n) + 1 \<le> X (Suc n)"
hoelzl@57448
  2101
      by (intro dependent_nat_choice) (auto simp del: max.bounded_iff)
hoelzl@57448
  2102
    then obtain X where X: "\<And>n. f (X n) \<notin> A m" "\<And>n. max n (X n) + 1 \<le> X (Suc n)"
hoelzl@57448
  2103
      by auto
hoelzl@57448
  2104
    { fix n have "1 \<le> n \<longrightarrow> real n \<le> X n"
hoelzl@57448
  2105
        using X[of "n - 1"] by auto }
hoelzl@57448
  2106
    then have "filterlim X at_top sequentially"
hoelzl@57448
  2107
      by (force intro!: filterlim_at_top_mono[OF filterlim_real_sequentially]
hoelzl@57448
  2108
                simp: eventually_sequentially)
hoelzl@57448
  2109
    from topological_tendstoD[OF *[OF this] A(2, 3), of m] X(1) show False
hoelzl@57448
  2110
      by auto
hoelzl@57275
  2111
  qed
hoelzl@57448
  2112
  then obtain k where "\<And>m x. k m \<le> x \<Longrightarrow> f x \<in> A m"
hoelzl@57448
  2113
    by metis
hoelzl@57448
  2114
  then show ?thesis
hoelzl@57448
  2115
    unfolding at_top_def A
hoelzl@57448
  2116
    by (intro filterlim_base[where i=k]) auto
hoelzl@57275
  2117
qed
hoelzl@57275
  2118
hoelzl@57275
  2119
lemma tendsto_at_topI_sequentially_real:
hoelzl@51531
  2120
  fixes f :: "real \<Rightarrow> real"
hoelzl@51531
  2121
  assumes mono: "mono f"
wenzelm@61969
  2122
  assumes limseq: "(\<lambda>n. f (real n)) \<longlonglongrightarrow> y"
wenzelm@61973
  2123
  shows "(f \<longlongrightarrow> y) at_top"
hoelzl@51531
  2124
proof (rule tendstoI)
hoelzl@51531
  2125
  fix e :: real assume "0 < e"
hoelzl@51531
  2126
  with limseq obtain N :: nat where N: "\<And>n. N \<le> n \<Longrightarrow> \<bar>f (real n) - y\<bar> < e"
lp15@60017
  2127
    by (auto simp: lim_sequentially dist_real_def)
hoelzl@51531
  2128
  { fix x :: real
wenzelm@53381
  2129
    obtain n where "x \<le> real_of_nat n"
wenzelm@53381
  2130
      using ex_le_of_nat[of x] ..
hoelzl@51531
  2131
    note monoD[OF mono this]
hoelzl@51531
  2132
    also have "f (real_of_nat n) \<le> y"
lp15@61649
  2133
      by (rule LIMSEQ_le_const[OF limseq]) (auto intro!: exI[of _ n] monoD[OF mono])
hoelzl@51531
  2134
    finally have "f x \<le> y" . }
hoelzl@51531
  2135
  note le = this
hoelzl@51531
  2136
  have "eventually (\<lambda>x. real N \<le> x) at_top"
hoelzl@51531
  2137
    by (rule eventually_ge_at_top)
hoelzl@51531
  2138
  then show "eventually (\<lambda>x. dist (f x) y < e) at_top"
hoelzl@51531
  2139
  proof eventually_elim
hoelzl@51531
  2140
    fix x assume N': "real N \<le> x"
hoelzl@51531
  2141
    with N[of N] le have "y - f (real N) < e" by auto
hoelzl@51531
  2142
    moreover note monoD[OF mono N']
hoelzl@51531
  2143
    ultimately show "dist (f x) y < e"
hoelzl@51531
  2144
      using le[of x] by (auto simp: dist_real_def field_simps)
hoelzl@51531
  2145
  qed
hoelzl@51531
  2146
qed
hoelzl@51531
  2147
huffman@20504
  2148
end
hoelzl@57276
  2149