mathematics_in_lean

My solutions for this book

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. 163
  164. 164
  165. 165
  166. 166
  167. 167
  168. 168
  169. 169
  170. 170
  171. 171
  172. 172
  173. 173
  174. 174
  175. 175
  176. 176
  177. 177
  178. 178
  179. 179
  180. 180
  181. 181
  182. 182
  183. 183
  184. 184
  185. 185
  186. 186
  187. 187
  188. 188
  189. 189
  190. 190
  191. 191
  192. 192
  193. 193
  194. 194
  195. 195
  196. 196
  197. 197
  198. 198
  199. 199
  200. 200
  201. 201
  202. 202
  203. 203
  204. 204
  205. 205
  206. 206
  207. 207
import Mathlib.Tactic
import Mathlib.Topology.Instances.Real
import Mathlib.Analysis.NormedSpace.BanachSteinhaus

open Set Filter
open Topology Filter

variable {X : Type _} [MetricSpace X] (a b c : X)

#check (dist a b : )
#check (dist_nonneg : 0  dist a b)
#check (dist_eq_zero : dist a b = 0  a = b)
#check (dist_comm a b : dist a b = dist b a)
#check (dist_triangle a b c : dist a c  dist a b + dist b c)

-- Note the next three lines are not quoted, their purpose is to make sure those things don't get renamed while we're looking elsewhere.
#check EMetricSpace
#check PseudoMetricSpace
#check PseudoEMetricSpace

example {u :   X} {a : X} :
    Tendsto u atTop (𝓝 a)   ε > 0,  N,  n  N, dist (u n) a < ε :=
  Metric.tendsto_atTop

example {X Y : Type _} [MetricSpace X] [MetricSpace Y] {f : X  Y} :
    Continuous f 
       x : X,  ε > 0,  δ > 0,  x', dist x' x < δ  dist (f x') (f x) < ε :=
  Metric.continuous_iff

example {X Y : Type _} [MetricSpace X] [MetricSpace Y] {f : X  Y} (hf : Continuous f) :
    Continuous fun p : X × X  dist (f p.1) (f p.2) := by continuity

example {X Y : Type _} [MetricSpace X] [MetricSpace Y] {f : X  Y} (hf : Continuous f) :
    Continuous fun p : X × X  dist (f p.1) (f p.2) :=
  continuous_dist.comp ((hf.comp continuous_fst).prod_mk (hf.comp continuous_snd))

example {X Y : Type _} [MetricSpace X] [MetricSpace Y] {f : X  Y} (hf : Continuous f) :
    Continuous fun p : X × X  dist (f p.1) (f p.2) := by
  apply Continuous.dist
  exact hf.comp continuous_fst
  exact hf.comp continuous_snd

example {X Y : Type _} [MetricSpace X] [MetricSpace Y] {f : X  Y} (hf : Continuous f) :
    Continuous fun p : X × X  dist (f p.1) (f p.2) :=
  (hf.comp continuous_fst).dist (hf.comp continuous_snd)

example {X Y : Type _} [MetricSpace X] [MetricSpace Y] {f : X  Y} (hf : Continuous f) :
    Continuous fun p : X × X  dist (f p.1) (f p.2) :=
  hf.fst'.dist hf.snd'

example {f :   X} (hf : Continuous f) : Continuous fun x :   f (x ^ 2 + x) :=
  sorry

example {X Y : Type _} [MetricSpace X] [MetricSpace Y] (f : X  Y) (a : X) :
    ContinuousAt f a   ε > 0,  δ > 0,  {x}, dist x a < δ  dist (f x) (f a) < ε :=
  Metric.continuousAt_iff

variable (r : )

example : Metric.ball a r = { b | dist b a < r } :=
  rfl

example : Metric.closedBall a r = { b | dist b a  r } :=
  rfl

example (hr : 0 < r) : a  Metric.ball a r :=
  Metric.mem_ball_self hr

example (hr : 0  r) : a  Metric.closedBall a r :=
  Metric.mem_closedBall_self hr

example (s : Set X) : IsOpen s   x  s,  ε > 0, Metric.ball x ε  s :=
  Metric.isOpen_iff

example {s : Set X} : IsClosed s  IsOpen (s) :=
  isOpen_compl_iff.symm

example {s : Set X} (hs : IsClosed s) {u :   X} (hu : Tendsto u atTop (𝓝 a))
    (hus :  n, u n  s) : a  s :=
  hs.mem_of_tendsto hu (eventually_of_forall hus)

example {s : Set X} : a  closure s   ε > 0,  b  s, a  Metric.ball b ε :=
  Metric.mem_closure_iff

example {u :   X} (hu : Tendsto u atTop (𝓝 a)) {s : Set X} (hs :  n, u n  s) :
    a  closure s :=
  sorry

example {x : X} {s : Set X} : s  𝓝 x   ε > 0, Metric.ball x ε  s :=
  Metric.nhds_basis_ball.mem_iff

example {x : X} {s : Set X} : s  𝓝 x   ε > 0, Metric.closedBall x ε  s :=
  Metric.nhds_basis_closedBall.mem_iff

example : IsCompact (Set.Icc 0 1 : Set ) :=
  isCompact_Icc

example {s : Set X} (hs : IsCompact s) {u :   X} (hu :  n, u n  s) :
     a  s,  φ :   , StrictMono φ  Tendsto (u  φ) atTop (𝓝 a) :=
  hs.tendsto_subseq hu

example {s : Set X} (hs : IsCompact s) (hs' : s.Nonempty) {f : X  }
      (hfs : ContinuousOn f s) :
     x  s,  y  s, f x  f y :=
  hs.exists_forall_le hs' hfs

example {s : Set X} (hs : IsCompact s) (hs' : s.Nonempty) {f : X  }
      (hfs : ContinuousOn f s) :
     x  s,  y  s, f y  f x :=
  hs.exists_forall_ge hs' hfs

example {s : Set X} (hs : IsCompact s) : IsClosed s :=
  hs.isClosed

example {X : Type _} [MetricSpace X] [CompactSpace X] : IsCompact (univ : Set X) :=
  isCompact_univ

#check IsCompact.isClosed

example {X : Type _} [MetricSpace X] {Y : Type _} [MetricSpace Y] {f : X  Y} :
    UniformContinuous f 
       ε > 0,  δ > 0,  {a b : X}, dist a b < δ  dist (f a) (f b) < ε :=
  Metric.uniformContinuous_iff

example {X : Type _} [MetricSpace X] [CompactSpace X]
      {Y : Type _} [MetricSpace Y] {f : X  Y}
    (hf : Continuous f) : UniformContinuous f :=
  sorry

example (u :   X) :
    CauchySeq u   ε > 0,  N : ,  m  N,  n  N, dist (u m) (u n) < ε :=
  Metric.cauchySeq_iff

example (u :   X) :
    CauchySeq u   ε > 0,  N : ,  n  N, dist (u n) (u N) < ε :=
  Metric.cauchySeq_iff'

example [CompleteSpace X] (u :   X) (hu : CauchySeq u) :
     x, Tendsto u atTop (𝓝 x) :=
  cauchySeq_tendsto_of_complete hu

open BigOperators

open Finset

theorem cauchySeq_of_le_geometric_two' {u :   X}
    (hu :  n : , dist (u n) (u (n + 1))  (1 / 2) ^ n) : CauchySeq u := by
  rw [Metric.cauchySeq_iff']
  intro ε ε_pos
  obtain N, hN :  N : , 1 / 2 ^ N * 2 < ε := by sorry
  use N
  intro n hn
  obtain k, rfl : n = N + k := le_iff_exists_add.mp hn
  calc
    dist (u (N + k)) (u N) = dist (u (N + 0)) (u (N + k)) := sorry
    _   i in range k, dist (u (N + i)) (u (N + (i + 1))) := sorry
    _   i in range k, (1 / 2 : ) ^ (N + i) := sorry
    _ = 1 / 2 ^ N *  i in range k, (1 / 2) ^ i := sorry
    _  1 / 2 ^ N * 2 := sorry
    _ < ε := sorry


open Metric

example [CompleteSpace X] (f :   Set X) (ho :  n, IsOpen (f n)) (hd :  n, Dense (f n)) :
    Dense ( n, f n) := by
  let B :    := fun n  (1 / 2) ^ n
  have Bpos :  n, 0 < B n
  sorry
  /- Translate the density assumption into two functions `center` and `radius` associating
    to any n, x, δ, δpos a center and a positive radius such that
    `closedBall center radius` is included both in `f n` and in `closedBall x δ`.
    We can also require `radius ≤ (1/2)^(n+1)`, to ensure we get a Cauchy sequence later. -/
  have :
     (n : ) (x : X),
       δ > 0,  y : X,  r > 0, r  B (n + 1)  closedBall y r  closedBall x δ  f n :=
    by sorry
  choose! center radius Hpos HB Hball using this
  intro x
  rw [mem_closure_iff_nhds_basis nhds_basis_closedBall]
  intro ε εpos
  /- `ε` is positive. We have to find a point in the ball of radius `ε` around `x`
    belonging to all `f n`. For this, we construct inductively a sequence
    `F n = (c n, r n)` such that the closed ball `closedBall (c n) (r n)` is included
    in the previous ball and in `f n`, and such that `r n` is small enough to ensure
    that `c n` is a Cauchy sequence. Then `c n` converges to a limit which belongs
    to all the `f n`. -/
  let F :   X ×  := fun n 
    Nat.recOn n (Prod.mk x (min ε (B 0)))
      fun n p  Prod.mk (center n p.1 p.2) (radius n p.1 p.2)
  let c :   X := fun n  (F n).1
  let r :    := fun n  (F n).2
  have rpos :  n, 0 < r n := by sorry
  have rB :  n, r n  B n := by sorry
  have incl :  n, closedBall (c (n + 1)) (r (n + 1))  closedBall (c n) (r n)  f n := by
    sorry
  have cdist :  n, dist (c n) (c (n + 1))  B n := by sorry
  have : CauchySeq c := cauchySeq_of_le_geometric_two' cdist
  -- as the sequence `c n` is Cauchy in a complete space, it converges to a limit `y`.
  rcases cauchySeq_tendsto_of_complete this with y, ylim
  -- this point `y` will be the desired point. We will check that it belongs to all
  -- `f n` and to `ball x ε`.
  use y
  have I :  n,  m  n, closedBall (c m) (r m)  closedBall (c n) (r n) := by sorry
  have yball :  n, y  closedBall (c n) (r n) := by sorry
  sorry