-- Limits, continuity, and derivatives at a point depend only on the values -- near the point: one-sided agreement with two functions that have the same -- limit (gluing), and agreement on an open interval around the point. -- 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.Locality import Differentiation import OpenIntervals -- A function equal to g on (B, A) and to h on (A, C), where g and h both -- tend to L at A, tends to L at A. theorem TendsToAt.glue (f g h : Set) (L A B C : Real) requires limitG : TendsToAt(g, L, A) requires limitH : TendsToAt(h, L, A) requires function : f : ℝ → ℝ requires below : B < A requires above : A < C requires left : ∀ x ∈ ℝ, B < x → x < A → f(x) = g(x) requires right : ∀ x ∈ ℝ, A < x → x < C → f(x) = h(x) shows TendsToAt(f, L, A) proof refine ⟨function, ?_⟩ intros e eMember positive obtain ⟨R, rMember, rPositive, mapsG⟩ := TendsToAt.eventually with limitG, positive obtain ⟨S, sMember, sPositive, mapsH⟩ := TendsToAt.eventually with limitH, positive have rReal : Real(R) by exact rMember have sReal : Real(S) by exact sMember have leftPositive : 0 < A - B by linarith have rightPositive : 0 < C - A by linarith obtain ⟨P, pMember, pPositive, pR, pS⟩ := positiveRadius.common with rPositive, sPositive obtain ⟨Q, qMember, qPositive, qLeft, qRight⟩ := positiveRadius.common with leftPositive, rightPositive have pReal : Real(P) by exact pMember have qReal : Real(Q) by exact qMember obtain ⟨d, dMember, dPositive, dP, dQ⟩ := positiveRadius.common with pPositive, qPositive refine ⟨d, dMember, dPositive, ?_⟩ intros x xMember distinct near have xReal : Real(x) by exact xMember obtain ⟨_, distance⟩ := openBall.membership.forward with near have upper := abs.upper at x - A have lower := abs.upper_neg at x - A unfold dist at distance have order := realLess.trichotomy at x, A rcases order with smaller | equal | larger . have radius : d ≤ R by linarith have nearG := openBall.mono at x, A, d, R with near, radius have lowerX : B < x by linarith have value := left with x, xMember, lowerX, smaller rw value exact mapsG with x, xMember, distinct, nearG . exfalso exact distinct with equal . have radius : d ≤ S by linarith have nearH := openBall.mono at x, A, d, S with near, radius have upperX : x < C by linarith have value := right with x, xMember, larger, upperX rw value exact mapsH with x, xMember, distinct, nearH -- Limits are local: functions equal on an open interval around A, except -- perhaps at A, have the same limits at A. theorem TendsToAt.congr_local (f g : Set) (L A B C : Real) requires limit : TendsToAt(g, L, A) requires function : f : ℝ → ℝ requires below : B < A requires above : A < C requires agree : ∀ x ∈ openInterval(B, C), x ≠ A → f(x) = g(x) shows TendsToAt(f, L, A) proof have side : ∀ x ∈ ℝ, B < x → x < C → x ≠ A → f(x) = g(x) intros x xMember lower upper distinct have inside := openInterval.membership.backward with ⟨xMember, lower, upper⟩ exact agree with x, inside, distinct have left : ∀ x ∈ ℝ, B < x → x < A → f(x) = g(x) intros x xMember lower upper have xReal : Real(x) by exact xMember have within : x < C by linarith have distinct : x ≠ A by linarith exact side with x, xMember, lower, within, distinct have right : ∀ x ∈ ℝ, A < x → x < C → f(x) = g(x) intros x xMember lower upper have xReal : Real(x) by exact xMember have within : B < x by linarith have distinct : x ≠ A by linarith exact side with x, xMember, within, upper, distinct exact TendsToAt.glue with limit, limit, function, below, above, left, right theorem TendsToAt.local_iff (f g : Set) (L A B C : Real) requires functionF : f : ℝ → ℝ requires functionG : g : ℝ → ℝ requires below : B < A requires above : A < C requires agree : ∀ x ∈ openInterval(B, C), x ≠ A → f(x) = g(x) shows TendsToAt(f, L, A) ↔ TendsToAt(g, L, A) proof have symmetric : ∀ x ∈ openInterval(B, C), x ≠ A → g(x) = f(x) intros x inside distinct have value := agree with x, inside, distinct rw value constructor . intro limit exact TendsToAt.congr_local with limit, functionG, below, above, symmetric . intro limit exact TendsToAt.congr_local with limit, functionF, below, above, agree -- A function equal to g on (B, A] and to h on [A, C), both continuous at -- A, is continuous at A. theorem ContinuousAt.glue (f g h : Set) (A B C : Real) requires continuousG : ContinuousAt(g, A) requires continuousH : ContinuousAt(h, A) requires function : f : ℝ → ℝ requires below : B < A requires above : A < C requires left : ∀ x ∈ ℝ, B < x → x ≤ A → f(x) = g(x) requires right : ∀ x ∈ ℝ, A ≤ x → x < C → f(x) = h(x) shows ContinuousAt(f, A) proof have aMember : A ∈ ℝ by typecheck have leftOpen : ∀ x ∈ ℝ, B < x → x < A → f(x) = g(x) intros x xMember lower upper have xReal : Real(x) by exact xMember have weak : x ≤ A by linarith exact left with x, xMember, lower, weak have rightOpen : ∀ x ∈ ℝ, A < x → x < C → f(x) = h(x) intros x xMember lower upper have xReal : Real(x) by exact xMember have weak : A ≤ x by linarith exact right with x, xMember, weak, upper have reflexive := realLe.reflexive at A have atG := left with A, aMember, below, reflexive have atH := right with A, aMember, reflexive, above have limitG := ContinuousAt.tends_to with continuousG have limitH := ContinuousAt.tends_to with continuousH rw [← atG] at limitG rw [← atH] at limitH have limit := TendsToAt.glue with limitG, limitH, function, below, above, leftOpen, rightOpen exact TendsToAt.continuous_at with limit, rfl -- Continuity is local: functions equal on an open interval around A are -- continuous at A together. theorem ContinuousAt.local_iff (f g : Set) (A B C : Real) requires functionF : f : ℝ → ℝ requires functionG : g : ℝ → ℝ requires below : B < A requires above : A < C requires agree : ∀ x ∈ openInterval(B, C), f(x) = g(x) shows ContinuousAt(f, A) ↔ ContinuousAt(g, A) proof have forward : ∀ x ∈ ℝ, B < x → x < C → f(x) = g(x) intros x xMember lower upper have inside := openInterval.membership.backward with ⟨xMember, lower, upper⟩ exact agree with x, inside have backward : ∀ x ∈ ℝ, B < x → x < C → g(x) = f(x) intros x xMember lower upper have value := forward with x, xMember, lower, upper rw value constructor . intro continuous have left : ∀ x ∈ ℝ, B < x → x ≤ A → g(x) = f(x) intros x xMember lower upper have xReal : Real(x) by exact xMember have within : x < C by linarith exact backward with x, xMember, lower, within have right : ∀ x ∈ ℝ, A ≤ x → x < C → g(x) = f(x) intros x xMember lower upper have xReal : Real(x) by exact xMember have within : B < x by linarith exact backward with x, xMember, within, upper exact ContinuousAt.glue with continuous, continuous, functionG, below, above, left, right . intro continuous have left : ∀ x ∈ ℝ, B < x → x ≤ A → f(x) = g(x) intros x xMember lower upper have xReal : Real(x) by exact xMember have within : x < C by linarith exact forward with x, xMember, lower, within have right : ∀ x ∈ ℝ, A ≤ x → x < C → f(x) = g(x) intros x xMember lower upper have xReal : Real(x) by exact xMember have within : B < x by linarith exact forward with x, xMember, within, upper exact ContinuousAt.glue with continuous, continuous, functionF, below, above, left, right -- A function equal to g on (B, A] and to h on [A, C), where g and h both -- have derivative D at A, has derivative D at A. theorem HasDerivativeAt.glue (f g h : Set) (D A B C : Real) requires derivativeG : HasDerivativeAt(g, D, A) requires derivativeH : HasDerivativeAt(h, D, A) requires function : f : ℝ → ℝ requires below : B < A requires above : A < C requires left : ∀ x ∈ ℝ, B < x → x ≤ A → f(x) = g(x) requires right : ∀ x ∈ ℝ, A ≤ x → x < C → f(x) = h(x) shows HasDerivativeAt(f, D, A) proof have gFunction := HasDerivativeAt.function with derivativeG have hFunction := HasDerivativeAt.function with derivativeH have limitG := HasDerivativeAt.limit with derivativeG have limitH := HasDerivativeAt.limit with derivativeH have quotient := differenceQuotient.function at f, A with function have aMember : A ∈ ℝ by typecheck have reflexive := realLe.reflexive at A have atG := left with A, aMember, below, reflexive have atH := right with A, aMember, reflexive, above have leftShift : ∀ t ∈ ℝ, B - A < t → t < realZero → differenceQuotient(f, A)(t) = differenceQuotient(g, A)(t) intros t tMember lower upper have tReal : Real(t) by exact tMember have moved : A + t ∈ ℝ by typecheck have lowerMoved : B < A + t by linarith have upperMoved : A + t ≤ A by linarith have value := left with A + t, moved, lowerMoved, upperMoved have quotientF := differenceQuotient.value at f, A, t with function have quotientG := differenceQuotient.value at g, A, t with gFunction rw [quotientF, quotientG, value, atG] have rightShift : ∀ t ∈ ℝ, realZero < t → t < C - A → differenceQuotient(f, A)(t) = differenceQuotient(h, A)(t) intros t tMember lower upper have tReal : Real(t) by exact tMember have moved : A + t ∈ ℝ by typecheck have lowerMoved : A ≤ A + t by linarith have upperMoved : A + t < C by linarith have value := right with A + t, moved, lowerMoved, upperMoved have quotientF := differenceQuotient.value at f, A, t with function have quotientH := differenceQuotient.value at h, A, t with hFunction rw [quotientF, quotientH, value, atH] have shiftedBelow : B - A < realZero by linarith have shiftedAbove : realZero < C - A by linarith have limit := TendsToAt.glue with limitG, limitH, quotient, shiftedBelow, shiftedAbove, leftShift, rightShift refine ⟨function, limit⟩ -- Derivatives are local: functions equal on an open interval around A have -- the same derivatives at A. theorem HasDerivativeAt.local_iff (f g : Set) (D A B C : Real) requires functionF : f : ℝ → ℝ requires functionG : g : ℝ → ℝ requires below : B < A requires above : A < C requires agree : ∀ x ∈ openInterval(B, C), f(x) = g(x) shows HasDerivativeAt(f, D, A) ↔ HasDerivativeAt(g, D, A) proof have forward : ∀ x ∈ ℝ, B < x → x < C → f(x) = g(x) intros x xMember lower upper have inside := openInterval.membership.backward with ⟨xMember, lower, upper⟩ exact agree with x, inside have backward : ∀ x ∈ ℝ, B < x → x < C → g(x) = f(x) intros x xMember lower upper have value := forward with x, xMember, lower, upper rw value constructor . intro derivative have left : ∀ x ∈ ℝ, B < x → x ≤ A → g(x) = f(x) intros x xMember lower upper have xReal : Real(x) by exact xMember have within : x < C by linarith exact backward with x, xMember, lower, within have right : ∀ x ∈ ℝ, A ≤ x → x < C → g(x) = f(x) intros x xMember lower upper have xReal : Real(x) by exact xMember have within : B < x by linarith exact backward with x, xMember, within, upper exact HasDerivativeAt.glue with derivative, derivative, functionG, below, above, left, right . intro derivative have left : ∀ x ∈ ℝ, B < x → x ≤ A → f(x) = g(x) intros x xMember lower upper have xReal : Real(x) by exact xMember have within : x < C by linarith exact forward with x, xMember, lower, within have right : ∀ x ∈ ℝ, A ≤ x → x < C → f(x) = g(x) intros x xMember lower upper have xReal : Real(x) by exact xMember have within : B < x by linarith exact forward with x, xMember, within, upper exact HasDerivativeAt.glue with derivative, derivative, functionF, below, above, left, right