-- Induction along a closed interval: a property that holds at the left -- endpoint and spreads past every point reaches the right endpoint. From it, -- a function continuous on [A, B] is bounded, stays a positive distance from -- every value it does not take, and is uniformly continuous; no point is -- selected, only the supremum of the points reached is taken. -- Part of Cumulus, the Grace17 mathematics library. -- Copyright (c) 2026 Grant Passmore. -- Released under the Apache License 2.0. See LICENSE in the repository root. -- Authors: Grant Passmore module Grace.Cumulus.Real.Analysis.RealInduction import SequentialCompactness theorem realPositive.common_lower (D E : Real) requires positiveD : 0 < D requires positiveE : 0 < E shows ∃ F ∈ ℝ, 0 < F ∧ F ≤ D ∧ F ≤ E proof have split : D ≤ E ∨ ¬(D ≤ E) by tauto cases split with below | above . have realD : D ∈ ℝ by simp [Real] witness D refine ⟨realD, positiveD, realLe.reflexive at D, below⟩ . have less := realLess.of_not_le with above have weak := realLess.implies_le with less have realE : E ∈ ℝ by simp [Real] witness E refine ⟨realE, positiveE, weak, realLe.reflexive at E⟩ theorem realBound.common_upper (M N : Real) shows ∃ K ∈ ℝ, M ≤ K ∧ N ≤ K proof have split : M ≤ N ∨ ¬(M ≤ N) by tauto cases split with below | above . have realN : N ∈ ℝ by simp [Real] witness N refine ⟨realN, below, realLe.reflexive at N⟩ . have less := realLess.of_not_le with above have weak := realLess.implies_le with less have realM : M ∈ ℝ by simp [Real] witness M refine ⟨realM, realLe.reflexive at M, weak⟩ -- The points of [A, B] that lie in P. def reachedPoints (P : Set) (A B : Real) : Set := { Point ∈ closedInterval(A, B) | Point ∈ P } -- Real induction. Let C be the supremum of the points of [A, B] in P. A point -- of P just below C carries P past C, so C + D/2 would be a larger point of -- P unless it lies beyond B; then B itself is carried. theorem closedInterval.real_induction (P : Set) (A B : Real) requires ordered : A ≤ B requires start : A ∈ P requires extend : ∀ C ∈ closedInterval(A, B), ∃ D ∈ ℝ, 0 < D ∧ ∀ Z ∈ closedInterval(A, B), (Z ∈ P → (C:R) - (D:R) < (Z:R) → ∀ W ∈ closedInterval(A, B), ((W:R) < (C:R) + (D:R) → W ∈ P)) shows B ∈ P proof have leftMember := closedInterval.left_mem with ordered have rightMember := closedInterval.right_mem with ordered have aData : A ∈ closedInterval(A, B) ∧ A ∈ P := ⟨leftMember, start⟩ have aIn := reachedPoints.membership.backward at P, A, B, A with aData have inhabited : Nonempty(reachedPoints(P, A, B)) := ⟨A, aIn⟩ have pointsReal : reachedPoints(P, A, B) ⊆ ℝ intros X member obtain ⟨inInterval, inP⟩ := reachedPoints.membership.forward with member obtain ⟨realX, lowerX, upperX⟩ := closedInterval.membership.forward with inInterval exact realX have upper : RealUpperBound(reachedPoints(P, A, B), B) unfold RealUpperBound have typedB : Real(B) by assumption refine ⟨typedB, pointsReal, _⟩ intros X member obtain ⟨inInterval, inP⟩ := reachedPoints.membership.forward with member obtain ⟨realX, lowerX, upperX⟩ := closedInterval.membership.forward with inInterval exact realLe.subset with * have bounded : ∃ D, RealUpperBound(reachedPoints(P, A, B), D) := ⟨B, upper⟩ obtain ⟨C, lubC⟩ := Real.least_upper_bound_property at reachedPoints(P, A, B) with * have least := lubC rcases lubC with ⟨⟨typedC, pointsSubset, boundsC⟩, leastC⟩ have realC : C ∈ ℝ by exact typedC have aSubset := boundsC with A, aIn have aLe := realLe.of_subset at A, C with * have cSubset := leastC with B, upper have cLe := realLe.of_subset at C, B with * have cData : C ∈ ℝ ∧ A ≤ C ∧ C ≤ B by refine ⟨realC, aLe, cLe⟩ have cIn : C ∈ closedInterval(A, B) exact closedInterval.membership.backward with cData obtain ⟨D, realD, positiveD, carries⟩ := extend with C, cIn obtain ⟨Z, zReached, near⟩ := RealLeastUpperBound.approached with least, positiveD obtain ⟨zIn, zP⟩ := reachedPoints.membership.forward with zReached have spread := carries with Z, zIn, zP, near have halfPositive := half.positive at D with * have halfLess := half.lt_self at D with * have reach : C + half(D) ≤ B ∨ ¬(C + half(D) ≤ B) by tauto cases reach with inside | outside . exfalso have xReal := realAddValue.real at C, half(D) with * have xMember : C + half(D) ∈ ℝ by exact xReal have xData : C + half(D) ∈ ℝ ∧ A ≤ C + half(D) ∧ C + half(D) ≤ B refine ⟨xMember, _, inside⟩ linarith have xIn : C + half(D) ∈ closedInterval(A, B) exact closedInterval.membership.backward with xData have xBelow : C + half(D) < C + D by linarith have xP := spread with C + half(D), xIn, xBelow have xPoints : C + half(D) ∈ closedInterval(A, B) ∧ C + half(D) ∈ P := ⟨xIn, xP⟩ have xReached := reachedPoints.membership.backward at P, A, B, C + half(D) with xPoints have xSubset := boundsC with C + half(D), xReached have xLe := realLe.of_subset at C + half(D), C with * linarith . have bLess := realLess.of_not_le with outside have bBelow : B < C + D by linarith exact spread with B, rightMember, bBelow -- The points Z for which f is bounded above on the part of [A, B] up to Z. def boundedPrefixes (f : Set) (A B : Real) : Set := { Reach ∈ ℝ | ∃ Ceiling ∈ ℝ, ∀ Point ∈ closedInterval(A, B), (Point:R) ≤ (Reach:R) → (f(Point):R) ≤ (Ceiling:R) } theorem ContinuousOn.closed_interval_bounded_above (f : Set) (A B : Real) requires ordered : A ≤ B requires continuous : ContinuousOn(f, closedInterval(A, B)) shows ∃ Ceiling ∈ ℝ, ∀ X ∈ closedInterval(A, B), (f(X):R) ≤ (Ceiling:R) proof rcases continuous with ⟨function, subset, pointwise⟩ have leftMember := closedInterval.left_mem with ordered have realA : A ∈ ℝ by simp [Real] have imageA := FunctionBetween.apply_closed with function, realA have start : A ∈ boundedPrefixes(f, A, B) apply boundedPrefixes.membership.backward refine ⟨realA, f(A), imageA, _⟩ intros X xIn xBelow obtain ⟨realX, lowerX, upperX⟩ := closedInterval.membership.forward with xIn have same := realLe.antisymmetric with xBelow, lowerX rw same exact realLe.reflexive have extend : ∀ C ∈ closedInterval(A, B), ∃ D ∈ ℝ, 0 < D ∧ ∀ Z ∈ closedInterval(A, B), (Z ∈ boundedPrefixes(f, A, B) → (C:R) - (D:R) < (Z:R) → ∀ W ∈ closedInterval(A, B), ((W:R) < (C:R) + (D:R) → W ∈ boundedPrefixes(f, A, B))) intros C cIn obtain ⟨realC, lowerC, upperC⟩ := closedInterval.membership.forward with cIn have within := pointwise with C, cIn have imageC := FunctionBetween.apply_closed with function, realC obtain ⟨D, realD, positiveD, maps⟩ := ContinuousWithinAt.maps_ball at f, closedInterval(A, B), C, realOne with within, realZero.lt_one witness D refine ⟨realD, positiveD, _⟩ intros Z zIn zPrefix near W wIn wBelow obtain ⟨realZ, M, realM, boundM⟩ := boundedPrefixes.membership.forward with zPrefix obtain ⟨realW, lowerW, upperW⟩ := closedInterval.membership.forward with wIn have localReal := realAddValue.real at f(C), realOne with imageC, realOne.real obtain ⟨K, realK, mK, localK⟩ := realBound.common_upper at M, f(C) + 1 apply boundedPrefixes.membership.backward refine ⟨realW, K, realK, _⟩ intros X xIn xBelow obtain ⟨realX, lowerX, upperX⟩ := closedInterval.membership.forward with xIn have imageX := FunctionBetween.apply_closed with function, realX have split : (X:R) ≤ (Z:R) ∨ ¬((X:R) ≤ (Z:R)) by tauto cases split with early | late . have bound := boundM with X, xIn, early exact realLe.transitive with bound, mK . have after := realLess.of_not_le with late have nearData : C - D < X ∧ X < C + D constructor . linarith . linarith have nearX := openBall.order_characterization.backward with nearData have mapped := maps with X, realX, xIn, nearX obtain ⟨lowerImage, upperImage⟩ := openBall.order_characterization.forward with mapped linarith have reached := closedInterval.real_induction with ordered, start, extend obtain ⟨realB, M, realM, boundM⟩ := boundedPrefixes.membership.forward with reached witness M refine ⟨realM, _⟩ intros X xIn obtain ⟨realX, lowerX, upperX⟩ := closedInterval.membership.forward with xIn exact boundM with X, xIn, upperX -- The points Z up to which f stays a fixed positive distance away from V. def separatedPrefixes (f : Set) (A B V : Real) : Set := { Reach ∈ ℝ | ∃ Margin ∈ ℝ, 0 < Margin ∧ ∀ Point ∈ closedInterval(A, B), (Point:R) ≤ (Reach:R) → (Margin:R) ≤ dist(f(Point), V) } -- A continuous function on [A, B] that never takes the value V keeps a -- positive distance from it. theorem ContinuousOn.closed_interval_gap (f : Set) (A B V : Real) requires ordered : A ≤ B requires continuous : ContinuousOn(f, closedInterval(A, B)) requires avoids : ∀ X ∈ closedInterval(A, B), f(X) ≠ V shows ∃ Margin ∈ ℝ, 0 < Margin ∧ ∀ X ∈ closedInterval(A, B), (Margin:R) ≤ dist(f(X), V) proof rcases continuous with ⟨function, subset, pointwise⟩ have leftMember := closedInterval.left_mem with ordered have realA : A ∈ ℝ by simp [Real] have imageA := FunctionBetween.apply_closed with function, realA have start : A ∈ separatedPrefixes(f, A, B, V) apply separatedPrefixes.membership.backward have unequal := avoids with A, leftMember have positive := dist.positive_of_ne with unequal have distReal := dist.real at f(A), V have distMember : dist(f(A), V) ∈ ℝ by simp [Real] refine ⟨realA, dist(f(A), V), distMember, positive, _⟩ intros X xIn xBelow obtain ⟨realX, lowerX, upperX⟩ := closedInterval.membership.forward with xIn have same := realLe.antisymmetric with xBelow, lowerX rw same exact realLe.reflexive have extend : ∀ C ∈ closedInterval(A, B), ∃ D ∈ ℝ, 0 < D ∧ ∀ Z ∈ closedInterval(A, B), (Z ∈ separatedPrefixes(f, A, B, V) → (C:R) - (D:R) < (Z:R) → ∀ W ∈ closedInterval(A, B), ((W:R) < (C:R) + (D:R) → W ∈ separatedPrefixes(f, A, B, V))) intros C cIn obtain ⟨realC, lowerC, upperC⟩ := closedInterval.membership.forward with cIn have within := pointwise with C, cIn have imageC := FunctionBetween.apply_closed with function, realC have unequal := avoids with C, cIn have positive := dist.positive_of_ne with unequal have halfPositive := half.positive with positive obtain ⟨D, realD, positiveD, maps⟩ := ContinuousWithinAt.maps_ball at f, closedInterval(A, B), C, half(dist(f(C), V)) with within, halfPositive witness D refine ⟨realD, positiveD, _⟩ intros Z zIn zPrefix near W wIn wBelow obtain ⟨realZ, E, realE, positiveE, boundE⟩ := separatedPrefixes.membership.forward with zPrefix obtain ⟨realW, lowerW, upperW⟩ := closedInterval.membership.forward with wIn obtain ⟨F, realF, positiveF, fE, fHalf⟩ := realPositive.common_lower with positiveE, halfPositive apply separatedPrefixes.membership.backward refine ⟨realW, F, realF, positiveF, _⟩ intros X xIn xBelow obtain ⟨realX, lowerX, upperX⟩ := closedInterval.membership.forward with xIn have imageX := FunctionBetween.apply_closed with function, realX have split : (X:R) ≤ (Z:R) ∨ ¬((X:R) ≤ (Z:R)) by tauto cases split with early | late . have bound := boundE with X, xIn, early exact realLe.transitive with fE, bound . have after := realLess.of_not_le with late have nearData : C - D < X ∧ X < C + D constructor . linarith . linarith have nearX := openBall.order_characterization.backward with nearData have mapped := maps with X, realX, xIn, nearX obtain ⟨imageMember, close⟩ := openBall.membership.forward with mapped have triangle := dist.triangle at f(C), f(X), V have symmetric := dist.symmetric at f(C), f(X) rw symmetric at triangle have halves := half.add_self at dist(f(C), V) have realXV := dist.real at f(X), V have realXC := dist.real at f(X), f(C) have realCV := dist.real at f(C), V have realHalf := half.real at dist(f(C), V) linarith have reached := closedInterval.real_induction with ordered, start, extend obtain ⟨realB, E, realE, positiveE, boundE⟩ := separatedPrefixes.membership.forward with reached witness E refine ⟨realE, positiveE, _⟩ intros X xIn obtain ⟨realX, lowerX, upperX⟩ := closedInterval.membership.forward with xIn exact boundE with X, xIn, upperX -- The points Z up to which one input radius keeps f within E on [A, B]. def uniformPrefixes (f : Set) (A B E : Real) : Set := { Reach ∈ ℝ | ∃ Radius ∈ ℝ, 0 < Radius ∧ ∀ First ∈ closedInterval(A, B), ∀ Second ∈ closedInterval(A, B), (First:R) ≤ (Reach:R) → (Second:R) ≤ (Reach:R) → First ∈ openBall(Second, Radius) → f(First) ∈ openBall(f(Second), E) } -- Heine–Cantor on [A, B] from continuity at each point of it: near a point C -- both inputs of a close pair beyond the reached part lie within the radius -- for E/2 at C, so their values lie within E of each other. theorem closedInterval.uniform_of_local_control (f : Set) (A B E : Real) requires ordered : A ≤ B requires values : ∀ X ∈ closedInterval(A, B), f(X) ∈ ℝ requires local : ∀ C ∈ ℝ, C ∈ closedInterval(A, B) → ∀ R ∈ ℝ, 0 < R → ∃ D ∈ ℝ, 0 < D ∧ ∀ X ∈ ℝ, (X ∈ closedInterval(A, B) → (X ∈ openBall(C, D) → f(X) ∈ openBall(f(C), R))) requires positive : 0 < E shows ∃ Radius ∈ ℝ, 0 < Radius ∧ ∀ X ∈ ℝ, (X ∈ closedInterval(A, B) → ∀ Y ∈ ℝ, (Y ∈ closedInterval(A, B) → (X ∈ openBall(Y, Radius) → f(X) ∈ openBall(f(Y), E)))) proof have leftMember := closedInterval.left_mem with ordered have realA : A ∈ ℝ by simp [Real] have halfE := half.positive with positive have halfEReal := half.real at E have halfEMember : half(E) ∈ ℝ by simp [Real] have start : A ∈ uniformPrefixes(f, A, B, E) apply uniformPrefixes.membership.backward have oneMember := realOne.real unfold Real at oneMember refine ⟨realA, realOne, oneMember, realZero.lt_one, _⟩ intros X xIn Y yIn xBelow yBelow near obtain ⟨realX, lowerX, upperX⟩ := closedInterval.membership.forward with xIn obtain ⟨realY, lowerY, upperY⟩ := closedInterval.membership.forward with yIn have sameX := realLe.antisymmetric with xBelow, lowerX have sameY := realLe.antisymmetric with yBelow, lowerY have imageA := values with A, leftMember have typedImage : Real(f(A)) by simp [Real] rw [sameX, sameY] exact openBall.center with typedImage, positive have extend : ∀ C ∈ closedInterval(A, B), ∃ D ∈ ℝ, 0 < D ∧ ∀ Z ∈ closedInterval(A, B), (Z ∈ uniformPrefixes(f, A, B, E) → (C:R) - (D:R) < (Z:R) → ∀ W ∈ closedInterval(A, B), ((W:R) < (C:R) + (D:R) → W ∈ uniformPrefixes(f, A, B, E))) intros C cIn obtain ⟨realC, lowerC, upperC⟩ := closedInterval.membership.forward with cIn obtain ⟨R, realR, positiveR, maps⟩ := local with C, realC, cIn, half(E), halfEMember, halfE have halfR := half.positive with positiveR have halfRReal := half.real at R have halfRMember : half(R) ∈ ℝ by simp [Real] have halvesR := half.add_self at R witness half(R) refine ⟨halfRMember, halfR, _⟩ intros Z zIn zPrefix near W wIn wBelow obtain ⟨realZ, DZ, realDZ, positiveDZ, uniformZ⟩ := uniformPrefixes.membership.forward with zPrefix obtain ⟨realW, lowerW, upperW⟩ := closedInterval.membership.forward with wIn obtain ⟨F, realF, positiveF, fDZ, fHalf⟩ := realPositive.common_lower with positiveDZ, halfR apply uniformPrefixes.membership.backward refine ⟨realW, F, realF, positiveF, _⟩ intros X xIn Y yIn xBelow yBelow nearXY obtain ⟨realX, lowerX, upperX⟩ := closedInterval.membership.forward with xIn obtain ⟨realY, lowerY, upperY⟩ := closedInterval.membership.forward with yIn have split : ((X:R) ≤ (Z:R) ∧ (Y:R) ≤ (Z:R)) ∨ ¬((X:R) ≤ (Z:R) ∧ (Y:R) ≤ (Z:R)) by tauto cases split with both | notBoth . obtain ⟨xEarly, yEarly⟩ := both have wider := openBall.mono with nearXY, fDZ exact uniformZ with X, xIn, Y, yIn, xEarly, yEarly, wider . obtain ⟨lowerXY, upperXY⟩ := openBall.order_characterization.forward with nearXY have lows : C - R < X ∧ C - R < Y have early : (X:R) ≤ (Z:R) ∨ ¬((X:R) ≤ (Z:R)) by tauto cases early with xEarly | xLate . have yAfter : Z < Y by_contra notAfter have yEarly := realLe.of_not_less with notAfter apply notBoth exact ⟨xEarly, yEarly⟩ constructor . linarith . linarith . have xAfter := realLess.of_not_le with xLate constructor . linarith . linarith obtain ⟨lowX, lowY⟩ := lows have nearXData : C - R < X ∧ X < C + R constructor . exact lowX . linarith have nearYData : C - R < Y ∧ Y < C + R constructor . exact lowY . linarith have nearX := openBall.order_characterization.backward with nearXData have nearY := openBall.order_characterization.backward with nearYData have mappedX := maps with X, realX, xIn, nearX have mappedY := maps with Y, realY, yIn, nearY have imageX := values with X, xIn have imageY := values with Y, yIn have imageC := values with C, cIn have typedX : Real(f(X)) by simp [Real] have typedY : Real(f(Y)) by simp [Real] have typedC : Real(f(C)) by simp [Real] have backY := openBall.symmetric.forward at f(Y), f(C), half(E) with typedY, typedC, halfEReal, mappedY have joined := openBall.triangle at f(X), f(C), f(Y), half(E), half(E) with typedX, typedC, typedY, halfEReal, halfEReal, mappedX, backY have halvesE := half.add_self at E rwa halvesE at joined have reached := closedInterval.real_induction with ordered, start, extend obtain ⟨realB, D, realD, positiveD, uniformB⟩ := uniformPrefixes.membership.forward with reached witness D refine ⟨realD, positiveD, _⟩ intros X realX xIn Y realY yIn near obtain ⟨memberX, lowerX, upperX⟩ := closedInterval.membership.forward with xIn obtain ⟨memberY, lowerY, upperY⟩ := closedInterval.membership.forward with yIn exact uniformB with X, xIn, Y, yIn, upperX, upperY, near