1-- The real line as a metric space: the distance function on ℝ × ℝ, its2-- balls and topology, continuity and convergence in metric terms, and3-- completeness4-- Part of Cumulus, the Grace17 mathematics library.5-- Copyright (c) 2026 Grant Passmore.6-- Released under the Apache License 2.0. See LICENSE in the repository root.7-- Authors: Grant Passmore89module Grace.Cumulus.Metric.Line10import Uniform11import Topology.Real1213defgraph realDistance (p ∈ ℝ × ℝ) : ℝ :=14 dist(firstProjection(ℝ, ℝ)(p), secondProjection(ℝ, ℝ)(p))15proof16have first := firstProjection.function at ℝ, ℝ17have second := secondProjection.function at ℝ, ℝ18have realA : Real(firstProjection(ℝ, ℝ)(p))19exact FunctionBetween.apply_closed with first, member20have realB : Real(secondProjection(ℝ, ℝ)(p))21exact FunctionBetween.apply_closed with second, member22exact dist.real with realA, realB2324theorem realDistance.apply (x y : Real)25shows realDistance(x, y) = dist(x, y)26proof27have realX : x ∈ ℝ bysimp [Real]28have realY : y ∈ ℝ bysimp [Real]29have pair := product.pair_membership.backward at x, y, ℝ, ℝ with30⟨realX, realY⟩31have value := realDistance.value with pair32have first := firstProjection.pair at ℝ, ℝ, x, y with realX, realY33have second := secondProjection.pair at ℝ, ℝ, x, y with realX, realY34unfold call235cc3637theorem realDistance.metric38shows IsMetric(ℝ, realDistance)39proof40constructor41 . exact realDistance.function42 . constructor43 . intros x hx y hy44have typedX : Real(x) byexact hx45have typedY : Real(y) byexact hy46have value := realDistance.apply at x, y with*47rw value48exact dist.nonnegative with typedX, typedY49 . constructor50 . intros x hx y hy51have typedX : Real(x) byexact hx52have typedY : Real(y) byexact hy53have value := realDistance.apply at x, y with*54rw value55exact dist.eq_zero_iff at x, y with*56 . constructor57 . intros x hx y hy58have typedX : Real(x) byexact hx59have typedY : Real(y) byexact hy60have valueXY := realDistance.apply at x, y with*61have valueYX := realDistance.apply at y, x with*62rw [valueXY, valueYX]63exact dist.symmetric at x, y with*64 . intros x hx y hy z hz65have typedX : Real(x) byexact hx66have typedY : Real(y) byexact hy67have typedZ : Real(z) byexact hz68have valueXZ := realDistance.apply at x, z with*69have valueXY := realDistance.apply at x, y with*70have valueYZ := realDistance.apply at y, z with*71rw [valueXZ, valueXY, valueYZ]72exact dist.triangle at x, y, z with*7374theorem realDistance.ball (x r : Real)75shows ball(ℝ, realDistance, x, r) = openBall(x, r)76proof77ext y78constructor79 . intro hy80have hyR := ball.point with hy81have near := ball.distance with hy82have typedY : Real(y) byexact hyR83have value := realDistance.apply at x, y with*84have symmetric := dist.symmetric at x, y with*85rw [value, symmetric] at near86apply openBall.membership.backward87exact⟨hyR, near⟩88 . intro hy89obtain⟨hyR, near⟩:= openBall.membership.forward with hy90have typedY : Real(y) byexact hyR91have value := realDistance.apply at x, y with*92have symmetric := dist.symmetric at x, y with*93apply ball.of_distance with hyR94rw [value, symmetric]95exact near9697theorem realDistance.closedBall (x r : Real)98shows closedBall(ℝ, realDistance, x, r) = closedInterval(x - r, x + r)99proof100ext y101constructor102 . intro hy103have hyR := closedBall.point with hy104have near := closedBall.distance with hy105have typedY : Real(y) byexact hyR106have value := realDistance.apply at x, y with*107rw value at near108unfold dist at near109have upper := abs.upper at x - y110have upperNeg := abs.upper_neg at x - y111apply closedInterval.membership.backward112refine⟨hyR, ?_, ?_⟩113 . linarith114 . linarith115 . intro hy116obtain⟨hyR, lower, upper⟩:= closedInterval.membership.forward with hy117have typedY : Real(y) byexact hyR118have value := realDistance.apply at x, y with*119apply closedBall.of_distance with hyR120rw value121unfold dist122have sign := realLess.trichotomy at (0:R), x - y123rcases sign with positive | zero | negative124 . have nonnegative : 0≤ x - y bylinarith125have unfolded := abs.of_nonnegative with nonnegative126rw unfolded127linarith128 . have nonnegative : 0≤ x - y bylinarith129have unfolded := abs.of_nonnegative with nonnegative130rw unfolded131linarith132 . have nonpositive : x - y ≤0bylinarith133have unfolded := abs.of_nonpositive with nonpositive134rw unfolded135linarith136137theorem realDistance.topology138shows metricTopology(ℝ, realDistance) = realTopology139proof140ext U141constructor142 . intro hU143obtain⟨bound, balls⟩:= metricTopology.membership.forward with hU144have inside := powerset.membership_iff.forward with bound145apply realTopology.membership.backward146refine⟨bound, ?_⟩147intros x hx148obtain⟨E, hE, positive, ballInside⟩:= balls with x, hx149have typedX : Real(x) byexact inside with x, hx150have typedE : Real(E) byexact hE151have same := realDistance.ball at x, E with*152rw same at ballInside153exact⟨E, hE, positive, ballInside⟩154 . intro hU155obtain⟨bound, balls⟩:= realTopology.membership.forward with hU156have inside := powerset.membership_iff.forward with bound157apply metricTopology.membership.backward158refine⟨bound, ?_⟩159intros x hx160obtain⟨E, hE, positive, ballInside⟩:= balls with x, hx161have typedX : Real(x) byexact inside with x, hx162have typedE : Real(E) byexact hE163have same := realDistance.ball at x, E with*164rw← same at ballInside165exact⟨E, hE, positive, ballInside⟩166167theorem realDistance.hausdorff168shows IsHausdorff(ℝ, metricTopology(ℝ, realDistance))169proof170exact metricTopology.hausdorff with realDistance.metric171172-- ε–δ continuity of a real function is metric continuity for realDistance.173theorem realDistance.continuous_iff (f : Set)174shows Continuous(f) ↔ IsMetricContinuous(f, ℝ, realDistance, ℝ, realDistance)175proof176have metric := realDistance.metric177have topological := Continuous.continuous_map at f178have metrical := IsMetricContinuous.iff_continuous_map at179 f, ℝ, realDistance, ℝ, realDistance with metric, metric180rw realDistance.topology at metrical181constructor182 . intro continuous183exact metrical.backward with (topological.forward with continuous)184 . intro continuous185exact topological.backward with (metrical.forward with continuous)186187theorem realDistance.converges_iff (u : Set) (L : Real)188shows ConvergesTo(u, L) ↔ ConvergesIn(ℝ, realDistance, u, L)189proof190have realL : L ∈ ℝ bysimp [Real]191constructor192 . intro converges193obtain⟨sequence, tail⟩:= converges194have function : u : ℕ → ℝ byexact sequence195have sequenceIn : SequenceIn(ℝ, u) byexact function196apply ConvergesIn.of_eventually with sequenceIn, realL197intros ε hε positive198obtain⟨N, hN, near⟩:= tail with ε, hε, positive199refine⟨N, hN, ?_⟩200intros n hn le201have inBall := near with n, hn, le202obtain⟨termR, close⟩:= openBall.membership.forward with inBall203have typedN : Real(u(n)) byexact termR204have value := realDistance.apply at L, u(n) with*205have symmetric := dist.symmetric at L, u(n) with*206rw [value, symmetric]207exact close208 . intro converges209have sequenceIn := ConvergesIn.sequence with converges210have function := SequenceIn.function with sequenceIn211have sequence : RealSequence(u) byexact function212constructor213 . exact sequence214 . intros ε hε positive215obtain⟨N, hN, near⟩:= ConvergesIn.eventually with converges, positive216refine⟨N, hN, ?_⟩217intros n hn le218have close := near with n, hn, le219have termR := SequenceIn.term_of_member with sequenceIn, hn220have typedN : Real(u(n)) byexact termR221have value := realDistance.apply at L, u(n) with*222have symmetric := dist.symmetric at L, u(n) with*223rw [value, symmetric] at close224apply openBall.membership.backward225exact⟨termR, close⟩226227-- The real line is complete: a Cauchy sequence is bounded, so it lies in a228-- closed interval, where Bolzano–Weierstrass provides a convergent229-- subsequence, whose limit the whole sequence then approaches.230theorem realDistance.complete231shows IsComplete(ℝ, realDistance)232proof233have metric := realDistance.metric234apply IsComplete.of_limits with metric235intros u cauchy236have sequenceIn := IsCauchy.sequence with cauchy237have function := SequenceIn.function with sequenceIn238have sequence : RealSequence(u) byexact function239obtain⟨r, hr, close⟩:= IsCauchy.bounded with metric, cauchy240have zero : 0∈ ℕ byexact nat.omega.forward at0with241 Naturals.constructor.zero242have term0 := SequenceIn.term_of_member with sequenceIn, zero243have typed0 : Real(u(0)) byexact term0244have typedR : Real(r) byexact hr245have terms : TermsIn(u, closedInterval(u(0) - r, u(0) + r))246intros n hn247have termN := SequenceIn.term_of_member with sequenceIn, hn248have typedN : Real(u(n)) byexact termN249have near := close with n, hn250have value := realDistance.apply at u(0), u(n) with*251rw value at near252unfold dist at near253have upper := abs.upper at u(0) - u(n)254have upperNeg := abs.upper_neg at u(0) - u(n)255apply closedInterval.membership.backward256refine⟨termN, ?_, ?_⟩257 . linarith258 . linarith259obtain⟨k, increasing, L, hL, inInterval, converges⟩:=260 closedInterval.bolzano_weierstrass with sequence, terms261have typedL : Real(L) byexact hL262have bridge := realDistance.converges_iff at subsequence(u, k), L with*263have subConverges := bridge.forward with converges264unfold subsequence at subConverges265have limit := IsCauchy.converges_of_subsequence with266 metric, cauchy, increasing, subConverges267exact⟨L, hL, limit⟩268