1-- Induction along a closed interval: a property that holds at the left2-- endpoint and spreads past every point reaches the right endpoint. From it,3-- a function continuous on [A, B] is bounded, stays a positive distance from4-- every value it does not take, and is uniformly continuous; no point is5-- selected, only the supremum of the points reached is taken.6-- Part of Cumulus, the Grace17 mathematics library.7-- Copyright (c) 2026 Grant Passmore.8-- Released under the Apache License 2.0. See LICENSE in the repository root.9-- Authors: Grant Passmore1011module Grace.Cumulus.Real.Analysis.RealInduction12import SequentialCompactness1314theorem realPositive.common_lower (D E : Real)15requires positiveD : 0 < D16requires positiveE : 0 < E17shows∃ F ∈ ℝ, 0 < F ∧ F ≤ D ∧ F ≤ E18proof19havesplit : D ≤ E ∨¬(D ≤ E) bytauto20casessplitwith below | above21 . have realD : D ∈ ℝ bysimp [Real]22witness D23refine⟨realD, positiveD, realLe.reflexive at D, below⟩24 . have less := realLess.of_not_le with above25have weak := realLess.implies_le with less26have realE : E ∈ ℝ bysimp [Real]27witness E28refine⟨realE, positiveE, weak, realLe.reflexive at E⟩2930theorem realBound.common_upper (M N : Real)31shows∃ K ∈ ℝ, M ≤ K ∧ N ≤ K32proof33havesplit : M ≤ N ∨¬(M ≤ N) bytauto34casessplitwith below | above35 . have realN : N∈ ℝ bysimp [Real]36witness N37refine⟨realN, below, realLe.reflexive at N⟩38 . have less := realLess.of_not_le with above39have weak := realLess.implies_le with less40have realM : M ∈ ℝ bysimp [Real]41witness M42refine⟨realM, realLe.reflexive at M, weak⟩4344-- The points of [A, B] that lie in P.45def reachedPoints (P : Set) (A B : Real) : Set:=46 { Point ∈ closedInterval(A, B) | Point ∈ P }4748-- Real induction. Let C be the supremum of the points of [A, B] in P. A point49-- of P just below C carries P past C, so C + D/2 would be a larger point of50-- P unless it lies beyond B; then B itself is carried.51theorem closedInterval.real_induction (P : Set) (A B : Real)52requiresordered : A ≤ B53requires start : A ∈ P54requires extend : ∀ C ∈ closedInterval(A, B), ∃ D ∈ ℝ, 0 < D ∧55∀ Z ∈ closedInterval(A, B), (Z ∈ P → (C:R) - (D:R) < (Z:R) →56∀ W ∈ closedInterval(A, B), ((W:R) < (C:R) + (D:R) → W ∈ P))57shows B ∈ P58proof59have leftMember := closedInterval.left_mem withordered60have rightMember := closedInterval.right_mem withordered61have aData : A ∈ closedInterval(A, B) ∧ A ∈ P :=⟨leftMember, start⟩62have aIn := reachedPoints.membership.backward at P, A, B, A with aData63have inhabited : Nonempty(reachedPoints(P, A, B)) :=⟨A, aIn⟩64have pointsReal : reachedPoints(P, A, B) ⊆ ℝ65intros X member66obtain⟨inInterval, inP⟩:= reachedPoints.membership.forward with member67obtain⟨realX, lowerX, upperX⟩:= closedInterval.membership.forward with inInterval68exact realX69have upper : RealUpperBound(reachedPoints(P, A, B), B)70unfold RealUpperBound71have typedB : Real(B) byassumption72refine⟨typedB, pointsReal, _⟩73intros X member74obtain⟨inInterval, inP⟩:= reachedPoints.membership.forward with member75obtain⟨realX, lowerX, upperX⟩:= closedInterval.membership.forward with inInterval76exact realLe.subset with*77have bounded : ∃ D, RealUpperBound(reachedPoints(P, A, B), D) :=⟨B, upper⟩78obtain⟨C, lubC⟩:=Real.least_upper_bound_property at reachedPoints(P, A, B) with*79have least := lubC80rcases lubC with⟨⟨typedC, pointsSubset, boundsC⟩, leastC⟩81have realC : C ∈ ℝ byexact typedC82have aSubset := boundsC with A, aIn83have aLe := realLe.of_subset at A, C with*84have cSubset := leastC with B, upper85have cLe := realLe.of_subset at C, B with*86have cData : C ∈ ℝ ∧ A ≤ C ∧ C ≤ B byrefine⟨realC, aLe, cLe⟩87have cIn : C ∈ closedInterval(A, B)88exact closedInterval.membership.backward with cData89obtain⟨D, realD, positiveD, carries⟩:= extend with C, cIn90obtain⟨Z, zReached, near⟩:= RealLeastUpperBound.approached with least, positiveD91obtain⟨zIn, zP⟩:= reachedPoints.membership.forward with zReached92have spread := carries with Z, zIn, zP, near93have halfPositive := half.positive at D with*94have halfLess := half.lt_self at D with*95have reach : C + half(D) ≤ B ∨¬(C + half(D) ≤ B) bytauto96cases reach with inside | outside97 . exfalso98have xReal := realAddValue.real at C, half(D) with*99have xMember : C + half(D) ∈ ℝ byexact xReal100have xData : C + half(D) ∈ ℝ ∧ A ≤ C + half(D) ∧ C + half(D) ≤ B101refine⟨xMember, _, inside⟩102linarith103have xIn : C + half(D) ∈ closedInterval(A, B)104exact closedInterval.membership.backward with xData105have xBelow : C + half(D) < C + D bylinarith106have xP := spread with C + half(D), xIn, xBelow107have xPoints : C + half(D) ∈ closedInterval(A, B) ∧ C + half(D) ∈ P :=⟨xIn, xP⟩108have xReached := reachedPoints.membership.backward at P, A, B, C + half(D) with xPoints109have xSubset := boundsC with C + half(D), xReached110have xLe := realLe.of_subset at C + half(D), C with*111linarith112 . have bLess := realLess.of_not_le with outside113have bBelow : B < C + D bylinarith114exact spread with B, rightMember, bBelow115116-- The points Z for which f is bounded above on the part of [A, B] up to Z.117def boundedPrefixes (f : Set) (A B : Real) : Set:=118 { Reach ∈ ℝ |∃ Ceiling ∈ ℝ, ∀ Point ∈ closedInterval(A, B),119 (Point:R) ≤ (Reach:R) → (f(Point):R) ≤ (Ceiling:R) }120121theorem ContinuousOn.closed_interval_bounded_above (f : Set) (A B : Real)122requiresordered : A ≤ B123requires continuous : ContinuousOn(f, closedInterval(A, B))124shows∃ Ceiling ∈ ℝ, ∀ X ∈ closedInterval(A, B), (f(X):R) ≤ (Ceiling:R)125proof126rcases continuous with⟨function, subset, pointwise⟩127have leftMember := closedInterval.left_mem withordered128have realA : A ∈ ℝ bysimp [Real]129have imageA := FunctionBetween.apply_closed with function, realA130have start : A ∈ boundedPrefixes(f, A, B)131apply boundedPrefixes.membership.backward132refine⟨realA, f(A), imageA, _⟩133intros X xIn xBelow134obtain⟨realX, lowerX, upperX⟩:= closedInterval.membership.forward with xIn135have same := realLe.antisymmetric with xBelow, lowerX136rw same137exact realLe.reflexive138have extend : ∀ C ∈ closedInterval(A, B), ∃ D ∈ ℝ, 0 < D ∧139∀ Z ∈ closedInterval(A, B), (Z ∈ boundedPrefixes(f, A, B) →140 (C:R) - (D:R) < (Z:R) →141∀ W ∈ closedInterval(A, B), ((W:R) < (C:R) + (D:R) →142 W ∈ boundedPrefixes(f, A, B)))143intros C cIn144obtain⟨realC, lowerC, upperC⟩:= closedInterval.membership.forward with cIn145have within := pointwise with C, cIn146have imageC := FunctionBetween.apply_closed with function, realC147obtain⟨D, realD, positiveD, maps⟩:=148 ContinuousWithinAt.maps_ball at f, closedInterval(A, B), C, realOne with149 within, realZero.lt_one150witness D151refine⟨realD, positiveD, _⟩152intros Z zIn zPrefix near W wIn wBelow153obtain⟨realZ, M, realM, boundM⟩:= boundedPrefixes.membership.forward with zPrefix154obtain⟨realW, lowerW, upperW⟩:= closedInterval.membership.forward with wIn155have localReal := realAddValue.real at f(C), realOne with imageC, realOne.real156obtain⟨K, realK, mK, localK⟩:= realBound.common_upper at M, f(C) +1157apply boundedPrefixes.membership.backward158refine⟨realW, K, realK, _⟩159intros X xIn xBelow160obtain⟨realX, lowerX, upperX⟩:= closedInterval.membership.forward with xIn161have imageX := FunctionBetween.apply_closed with function, realX162havesplit : (X:R) ≤ (Z:R) ∨¬((X:R) ≤ (Z:R)) bytauto163casessplitwith early | late164 . have bound := boundM with X, xIn, early165exact realLe.transitive with bound, mK166 . have after := realLess.of_not_le with late167have nearData : C - D < X ∧ X < C + D168constructor169 . linarith170 . linarith171have nearX := openBall.order_characterization.backward with nearData172have mapped := maps with X, realX, xIn, nearX173obtain⟨lowerImage, upperImage⟩:= openBall.order_characterization.forward with mapped174linarith175have reached := closedInterval.real_induction withordered, start, extend176obtain⟨realB, M, realM, boundM⟩:= boundedPrefixes.membership.forward with reached177witness M178refine⟨realM, _⟩179intros X xIn180obtain⟨realX, lowerX, upperX⟩:= closedInterval.membership.forward with xIn181exact boundM with X, xIn, upperX182183-- The points Z up to which f stays a fixed positive distance away from V.184def separatedPrefixes (f : Set) (A B V : Real) : Set:=185 { Reach ∈ ℝ |∃ Margin ∈ ℝ, 0 < Margin ∧∀ Point ∈ closedInterval(A, B),186 (Point:R) ≤ (Reach:R) → (Margin:R) ≤ dist(f(Point), V) }187188-- A continuous function on [A, B] that never takes the value V keeps a189-- positive distance from it.190theorem ContinuousOn.closed_interval_gap (f : Set) (A B V : Real)191requiresordered : A ≤ B192requires continuous : ContinuousOn(f, closedInterval(A, B))193requires avoids : ∀ X ∈ closedInterval(A, B), f(X) ≠ V194shows∃ Margin ∈ ℝ, 0 < Margin ∧195∀ X ∈ closedInterval(A, B), (Margin:R) ≤ dist(f(X), V)196proof197rcases continuous with⟨function, subset, pointwise⟩198have leftMember := closedInterval.left_mem withordered199have realA : A ∈ ℝ bysimp [Real]200have imageA := FunctionBetween.apply_closed with function, realA201have start : A ∈ separatedPrefixes(f, A, B, V)202apply separatedPrefixes.membership.backward203have unequal := avoids with A, leftMember204have positive := dist.positive_of_ne with unequal205have distReal := dist.real at f(A), V206have distMember : dist(f(A), V) ∈ ℝ bysimp [Real]207refine⟨realA, dist(f(A), V), distMember, positive, _⟩208intros X xIn xBelow209obtain⟨realX, lowerX, upperX⟩:= closedInterval.membership.forward with xIn210have same := realLe.antisymmetric with xBelow, lowerX211rw same212exact realLe.reflexive213have extend : ∀ C ∈ closedInterval(A, B), ∃ D ∈ ℝ, 0 < D ∧214∀ Z ∈ closedInterval(A, B), (Z ∈ separatedPrefixes(f, A, B, V) →215 (C:R) - (D:R) < (Z:R) →216∀ W ∈ closedInterval(A, B), ((W:R) < (C:R) + (D:R) →217 W ∈ separatedPrefixes(f, A, B, V)))218intros C cIn219obtain⟨realC, lowerC, upperC⟩:= closedInterval.membership.forward with cIn220have within := pointwise with C, cIn221have imageC := FunctionBetween.apply_closed with function, realC222have unequal := avoids with C, cIn223have positive := dist.positive_of_ne with unequal224have halfPositive := half.positive with positive225obtain⟨D, realD, positiveD, maps⟩:=226 ContinuousWithinAt.maps_ball at f, closedInterval(A, B), C, half(dist(f(C), V)) with227 within, halfPositive228witness D229refine⟨realD, positiveD, _⟩230intros Z zIn zPrefix near W wIn wBelow231obtain⟨realZ, E, realE, positiveE, boundE⟩:=232 separatedPrefixes.membership.forward with zPrefix233obtain⟨realW, lowerW, upperW⟩:= closedInterval.membership.forward with wIn234obtain⟨F, realF, positiveF, fE, fHalf⟩:=235 realPositive.common_lower with positiveE, halfPositive236apply separatedPrefixes.membership.backward237refine⟨realW, F, realF, positiveF, _⟩238intros X xIn xBelow239obtain⟨realX, lowerX, upperX⟩:= closedInterval.membership.forward with xIn240have imageX := FunctionBetween.apply_closed with function, realX241havesplit : (X:R) ≤ (Z:R) ∨¬((X:R) ≤ (Z:R)) bytauto242casessplitwith early | late243 . have bound := boundE with X, xIn, early244exact realLe.transitive with fE, bound245 . have after := realLess.of_not_le with late246have nearData : C - D < X ∧ X < C + D247constructor248 . linarith249 . linarith250have nearX := openBall.order_characterization.backward with nearData251have mapped := maps with X, realX, xIn, nearX252obtain⟨imageMember, close⟩:= openBall.membership.forward with mapped253have triangle := dist.triangle at f(C), f(X), V254have symmetric := dist.symmetric at f(C), f(X)255rw symmetric at triangle256have halves := half.add_self at dist(f(C), V)257have realXV := dist.real at f(X), V258have realXC := dist.real at f(X), f(C)259have realCV := dist.real at f(C), V260have realHalf := half.real at dist(f(C), V)261linarith262have reached := closedInterval.real_induction withordered, start, extend263obtain⟨realB, E, realE, positiveE, boundE⟩:=264 separatedPrefixes.membership.forward with reached265witness E266refine⟨realE, positiveE, _⟩267intros X xIn268obtain⟨realX, lowerX, upperX⟩:= closedInterval.membership.forward with xIn269exact boundE with X, xIn, upperX270271-- The points Z up to which one input radius keeps f within E on [A, B].272def uniformPrefixes (f : Set) (A B E : Real) : Set:=273 { Reach ∈ ℝ |∃ Radius ∈ ℝ, 0 < Radius ∧∀ First ∈ closedInterval(A, B),274∀ Second ∈ closedInterval(A, B), (First:R) ≤ (Reach:R) →275 (Second:R) ≤ (Reach:R) → First ∈ openBall(Second, Radius) →276 f(First) ∈ openBall(f(Second), E) }277278-- Heine–Cantor on [A, B] from continuity at each point of it: near a point C279-- both inputs of a close pair beyond the reached part lie within the radius280-- for E/2 at C, so their values lie within E of each other.281theorem closedInterval.uniform_of_local_control (f : Set) (A B E : Real)282requiresordered : A ≤ B283requires values : ∀ X ∈ closedInterval(A, B), f(X) ∈ ℝ284requires local : ∀ C ∈ ℝ, C ∈ closedInterval(A, B) →∀ R ∈ ℝ, 0 < R →285∃ D ∈ ℝ, 0 < D ∧∀ X ∈ ℝ, (X ∈ closedInterval(A, B) →286 (X ∈ openBall(C, D) → f(X) ∈ openBall(f(C), R)))287requires positive : 0 < E288shows∃ Radius ∈ ℝ, 0 < Radius ∧∀ X ∈ ℝ, (X ∈ closedInterval(A, B) →289∀ Y ∈ ℝ, (Y ∈ closedInterval(A, B) →290 (X ∈ openBall(Y, Radius) → f(X) ∈ openBall(f(Y), E))))291proof292have leftMember := closedInterval.left_mem withordered293have realA : A ∈ ℝ bysimp [Real]294have halfE := half.positive with positive295have halfEReal := half.real at E296have halfEMember : half(E) ∈ ℝ bysimp [Real]297have start : A ∈ uniformPrefixes(f, A, B, E)298apply uniformPrefixes.membership.backward299have oneMember := realOne.real300unfoldRealat oneMember301refine⟨realA, realOne, oneMember, realZero.lt_one, _⟩302intros X xIn Y yIn xBelow yBelow near303obtain⟨realX, lowerX, upperX⟩:= closedInterval.membership.forward with xIn304obtain⟨realY, lowerY, upperY⟩:= closedInterval.membership.forward with yIn305have sameX := realLe.antisymmetric with xBelow, lowerX306have sameY := realLe.antisymmetric with yBelow, lowerY307have imageA := values with A, leftMember308have typedImage : Real(f(A)) bysimp [Real]309rw [sameX, sameY]310exact openBall.center with typedImage, positive311have extend : ∀ C ∈ closedInterval(A, B), ∃ D ∈ ℝ, 0 < D ∧312∀ Z ∈ closedInterval(A, B), (Z ∈ uniformPrefixes(f, A, B, E) →313 (C:R) - (D:R) < (Z:R) →314∀ W ∈ closedInterval(A, B), ((W:R) < (C:R) + (D:R) →315 W ∈ uniformPrefixes(f, A, B, E)))316intros C cIn317obtain⟨realC, lowerC, upperC⟩:= closedInterval.membership.forward with cIn318obtain⟨R, realR, positiveR, maps⟩:=319 local with C, realC, cIn, half(E), halfEMember, halfE320have halfR := half.positive with positiveR321have halfRReal := half.real at R322have halfRMember : half(R) ∈ ℝ bysimp [Real]323have halvesR := half.add_self at R324witness half(R)325refine⟨halfRMember, halfR, _⟩326intros Z zIn zPrefix near W wIn wBelow327obtain⟨realZ, DZ, realDZ, positiveDZ, uniformZ⟩:=328 uniformPrefixes.membership.forward with zPrefix329obtain⟨realW, lowerW, upperW⟩:= closedInterval.membership.forward with wIn330obtain⟨F, realF, positiveF, fDZ, fHalf⟩:=331 realPositive.common_lower with positiveDZ, halfR332apply uniformPrefixes.membership.backward333refine⟨realW, F, realF, positiveF, _⟩334intros X xIn Y yIn xBelow yBelow nearXY335obtain⟨realX, lowerX, upperX⟩:= closedInterval.membership.forward with xIn336obtain⟨realY, lowerY, upperY⟩:= closedInterval.membership.forward with yIn337havesplit : ((X:R) ≤ (Z:R) ∧ (Y:R) ≤ (Z:R)) ∨338¬((X:R) ≤ (Z:R) ∧ (Y:R) ≤ (Z:R)) bytauto339casessplitwith both | notBoth340 . obtain⟨xEarly, yEarly⟩:= both341have wider := openBall.mono with nearXY, fDZ342exact uniformZ with X, xIn, Y, yIn, xEarly, yEarly, wider343 . obtain⟨lowerXY, upperXY⟩:= openBall.order_characterization.forward with nearXY344have lows : C - R < X ∧ C - R < Y345have early : (X:R) ≤ (Z:R) ∨¬((X:R) ≤ (Z:R)) bytauto346cases early with xEarly | xLate347 . have yAfter : Z < Y348by_contra notAfter349have yEarly := realLe.of_not_less with notAfter350apply notBoth351exact⟨xEarly, yEarly⟩352constructor353 . linarith354 . linarith355 . have xAfter := realLess.of_not_le with xLate356constructor357 . linarith358 . linarith359obtain⟨lowX, lowY⟩:= lows360have nearXData : C - R < X ∧ X < C + R361constructor362 . exact lowX363 . linarith364have nearYData : C - R < Y ∧ Y < C + R365constructor366 . exact lowY367 . linarith368have nearX := openBall.order_characterization.backward with nearXData369have nearY := openBall.order_characterization.backward with nearYData370have mappedX := maps with X, realX, xIn, nearX371have mappedY := maps with Y, realY, yIn, nearY372have imageX := values with X, xIn373have imageY := values with Y, yIn374have imageC := values with C, cIn375have typedX : Real(f(X)) bysimp [Real]376have typedY : Real(f(Y)) bysimp [Real]377have typedC : Real(f(C)) bysimp [Real]378have backY := openBall.symmetric.forward at f(Y), f(C), half(E) with379 typedY, typedC, halfEReal, mappedY380have joined := openBall.triangle at f(X), f(C), f(Y), half(E), half(E) with381 typedX, typedC, typedY, halfEReal, halfEReal, mappedX, backY382have halvesE := half.add_self at E383 rwa halvesE at joined384have reached := closedInterval.real_induction withordered, start, extend385obtain⟨realB, D, realD, positiveD, uniformB⟩:=386 uniformPrefixes.membership.forward with reached387witness D388refine⟨realD, positiveD, _⟩389intros X realX xIn Y realY yIn near390obtain⟨memberX, lowerX, upperX⟩:= closedInterval.membership.forward with xIn391obtain⟨memberY, lowerY, upperY⟩:= closedInterval.membership.forward with yIn392exact uniformB with X, xIn, Y, yIn, upperX, upperY, near393