1-- Riemann integrals of bounded functions pieced together: changing a2-- function at the endpoints keeps its integral, integrals over adjacent3-- intervals add, and a bounded function continuous on the open cells of a4-- partition is integrable, with the fundamental theorem cell by cell.5-- Part of Cumulus, the Grace17 mathematics library.6-- Copyright (c) 2026 Grant Passmore.7-- Released under the Apache License 2.0. See LICENSE in the repository root.8-- Authors: Grant Passmore910module Grace.Cumulus.Real.Analysis.Integration.Piecewise11import FTCFirst1213-- Changing a function at the endpoints1415-- Before the last cell, a tagged sum of f − g picks up at most the first16-- cell when f = g strictly inside (A, B) and f − g ≤ K on [A, B].17theorem riemannSum.interior_prefix (f g p t : Set) (A B K D : Real, N n : Nat)18requires functionF : f : ℝ → ℝ19requires functionG : g : ℝ → ℝ20requires tagged : TaggedPartition(p, t, A, B, N)21requires fine : FinePartition(p, A, B, N, D)22requires bound : ∀ x ∈ closedInterval(A, B), f(x) - g(x) ≤ K23requires nonnegative : 0≤ K24requires agree : ∀ x ∈ ℝ, A < x → x < B → f(x) = g(x)25shows n < N → riemannSum(f, p, t, n) - riemannSum(g, p, t, n) ≤ K * D26proof27induction n28| zero =>29intro before30obtain⟨_, dPositive, _⟩:= fine31rw [riemannSum.zero, riemannSum.zero]32have zeroLe : (0:R) ≤ D bylinarith33have product := realLe.mul_left_of_nonnegative at (0:R), D, K34with zeroLe, nonnegative35have zeroProduct : K * (0:R) = (0:R) byring36have zeroDifference : (0:R) - (0:R) = (0:R) byring37rw zeroDifference38linarith39| succ(m) using ih =>40intro before41obtain⟨partition, tags, cells⟩:= tagged42obtain⟨points, start, finish, increasing⟩:= partition43obtain⟨_, dPositive, widths⟩:= fine44have mBefore : m < N byarith45have previous := ih with mBefore46have sumF := riemannSum.real at f, p, t, m with functionF, points, tags47have sumG := riemannSum.real at g, p, t, m with functionG, points, tags48have stepF := riemannSum.succ at f, p, t, m with functionF, points, tags49have stepG := riemannSum.succ at g, p, t, m with functionG, points, tags50have mMember : m ∈ ℕ bytypecheck51have tagReal := RealSequence.value at t, m with tags52have tagMember : t[m] ∈ ℝ byexact tagReal53have fTag := FunctionBetween.apply_closed with functionF, tagMember54have gTag := FunctionBetween.apply_closed with functionG, tagMember55have fTagReal : Real(f(t[m])) byexact fTag56have gTagReal : Real(g(t[m])) byexact gTag57haveleft:= RealSequence.value at p, m with points58haveright:= RealSequence.value at p, succ(m) with points59have width := widths with m, mMember, mBefore60have positiveWidth := increasing with m, mMember, mBefore61obtain⟨tagLower, tagUpper⟩:= TaggedPartition.tag_bounds at p, t, A, B, N,62 m with⟨⟨points, start, finish, increasing⟩, tags, cells⟩, mBefore63rw [stepF, stepG]64have alternatives := natural.zero_or_successor at m65cases alternatives with zero | successor66 . -- the first cell67have mLe : m ≤ N byarith68have succLe : succ(m) ≤ N byarith69obtain⟨atLeast, _⟩:= IntervalPartition.point_bounds at p, A, B, N, m70with⟨points, start, finish, increasing⟩, mLe71obtain⟨_, atMost⟩:= IntervalPartition.point_bounds at p, A, B, N,72 succ(m) with⟨points, start, finish, increasing⟩, succLe73have inside : t[m] ∈ closedInterval(A, B)74apply closedInterval.membership.backward75refine⟨tagMember, ?_, ?_⟩76 . linarith77 . linarith78have difference := bound with t[m], inside79have widthNonnegative : (0:R) ≤ p[succ(m)] - p[m] bylinarith80have scaled := realLe.mul_right_of_nonnegative81at f(t[m]) - g(t[m]), K, p[succ(m)] - p[m]82with difference, widthNonnegative83have widthLe : p[succ(m)] - p[m] ≤ D bylinarith84have capped := realLe.mul_left_of_nonnegative85at p[succ(m)] - p[m], D, K with widthLe, nonnegative86have expand : realAddValue(riemannSum(f, p, t, m),87 realMulValue(f(t[m]), realSubValue(p[succ(m)], p[m]))) -88 realAddValue(riemannSum(g, p, t, m),89 realMulValue(g(t[m]), realSubValue(p[succ(m)], p[m]))) =90 riemannSum(f, p, t, m) - riemannSum(g, p, t, m) +91 (f(t[m]) - g(t[m])) * (p[succ(m)] - p[m])92ring93rw [expand, zero, riemannSum.zero, riemannSum.zero]94rw zero at scaled, capped95linarith96 . -- a middle cell: the tag lies strictly inside (A, B)97obtain⟨k, kNat, shape⟩:= successor98have zeroBefore : 0 < m byarith99have succBound : succ(m) ≤ N byarith100have nBound : N≤ N byarith101have mLe : m ≤ N byarith102have afterStart := IntervalPartition.strict_indices at p, A, B, N, 0, m103with⟨points, start, finish, increasing⟩, zeroBefore, mLe104have beforeEnd := IntervalPartition.strict_indices at p, A, B, N,105 succ(m), N with⟨points, start, finish, increasing⟩, before, nBound106rw [start] at afterStart107rw [finish] at beforeEnd108have tagAbove : A < t[m] bylinarith109have tagBelow : t[m] < B bylinarith110have same := agree with t[m], tagMember, tagAbove, tagBelow111have expand : realAddValue(riemannSum(f, p, t, m),112 realMulValue(f(t[m]), realSubValue(p[succ(m)], p[m]))) -113 realAddValue(riemannSum(g, p, t, m),114 realMulValue(g(t[m]), realSubValue(p[succ(m)], p[m]))) =115 riemannSum(f, p, t, m) - riemannSum(g, p, t, m) +116 (f(t[m]) - g(t[m])) * (p[succ(m)] - p[m])117ring118rw [expand, same]119have vanish : (g(t[m]) - g(t[m])) * (p[succ(m)] - p[m]) = (0:R)120byring121rw vanish122linarith123124-- On a δ-fine tagged partition, the tagged sums of f and g differ by at most125-- 2Kδ when f = g strictly inside (A, B) and f − g ≤ K on [A, B].126theorem riemannSum.interior_difference (f g p t : Set) (A B K D : Real, N : Nat)127requires functionF : f : ℝ → ℝ128requires functionG : g : ℝ → ℝ129requires tagged : TaggedPartition(p, t, A, B, N)130requires fine : FinePartition(p, A, B, N, D)131requires bound : ∀ x ∈ closedInterval(A, B), f(x) - g(x) ≤ K132requires nonnegative : 0≤ K133requires agree : ∀ x ∈ ℝ, A < x → x < B → f(x) = g(x)134shows riemannSum(f, p, t, N) - riemannSum(g, p, t, N) ≤ K * D + K * D135proof136have taggedCopy := tagged137have fineCopy := fine138obtain⟨partition, tags, cells⟩:= tagged139obtain⟨points, start, finish, increasing⟩:= partition140obtain⟨_, dPositive, widths⟩:= fine141have zeroLe : (0:R) ≤ D bylinarith142have product := realLe.mul_left_of_nonnegative at (0:R), D, K143with zeroLe, nonnegative144have zeroProduct : K * (0:R) = (0:R) byring145have alternatives := natural.zero_or_successor at N146cases alternatives with zero | successor147 . rw [zero, riemannSum.zero, riemannSum.zero]148have zeroDifference : (0:R) - (0:R) = (0:R) byring149rw zeroDifference150linarith151obtain⟨M, mNat, shape⟩:= successor152have before : M < N byarith153haveprefix:= riemannSum.interior_prefix at f, g, p, t, A, B, K, D, N, M154with functionF, functionG, taggedCopy, fineCopy, bound, nonnegative, agree155have earlier :=prefixwith before156have sumF := riemannSum.real at f, p, t, M with functionF, points, tags157have sumG := riemannSum.real at g, p, t, M with functionG, points, tags158have stepF := riemannSum.succ at f, p, t, M with functionF, points, tags159have stepG := riemannSum.succ at g, p, t, M with functionG, points, tags160have mMember : M ∈ ℕ bytypecheck161have tagReal := RealSequence.value at t, M with tags162have tagMember : t[M] ∈ ℝ byexact tagReal163have fTag := FunctionBetween.apply_closed with functionF, tagMember164have gTag := FunctionBetween.apply_closed with functionG, tagMember165have fTagReal : Real(f(t[M])) byexact fTag166have gTagReal : Real(g(t[M])) byexact gTag167haveleft:= RealSequence.value at p, M with points168haveright:= RealSequence.value at p, succ(M) with points169have width := widths with M, mMember, before170have positiveWidth := increasing with M, mMember, before171obtain⟨tagLower, tagUpper⟩:= TaggedPartition.tag_bounds at p, t, A, B, N,172 M with⟨⟨points, start, finish, increasing⟩, tags, cells⟩, before173have mLe : M ≤ N byarith174have succLe : succ(M) ≤ N byarith175obtain⟨atLeast, _⟩:= IntervalPartition.point_bounds at p, A, B, N, M176with⟨points, start, finish, increasing⟩, mLe177obtain⟨_, atMost⟩:= IntervalPartition.point_bounds at p, A, B, N,178 succ(M) with⟨points, start, finish, increasing⟩, succLe179have inside : t[M] ∈ closedInterval(A, B)180apply closedInterval.membership.backward181refine⟨tagMember, ?_, ?_⟩182 . linarith183 . linarith184have difference := bound with t[M], inside185have widthNonnegative : (0:R) ≤ p[succ(M)] - p[M] bylinarith186have scaled := realLe.mul_right_of_nonnegative187at f(t[M]) - g(t[M]), K, p[succ(M)] - p[M]188with difference, widthNonnegative189have widthLe : p[succ(M)] - p[M] ≤ D bylinarith190have capped := realLe.mul_left_of_nonnegative191at p[succ(M)] - p[M], D, K with widthLe, nonnegative192have expand : realAddValue(riemannSum(f, p, t, M),193 realMulValue(f(t[M]), realSubValue(p[succ(M)], p[M]))) -194 realAddValue(riemannSum(g, p, t, M),195 realMulValue(g(t[M]), realSubValue(p[succ(M)], p[M]))) =196 riemannSum(f, p, t, M) - riemannSum(g, p, t, M) +197 (f(t[M]) - g(t[M])) * (p[succ(M)] - p[M])198ring199rw [shape, stepF, stepG, expand]200linarith201202theorem HasRiemannIntegral.congr_interior (f g : Set) (A B I : Real)203requires integral : HasRiemannIntegral(g, A, B, I)204requires function : f : ℝ → ℝ205requires agree : ∀ x ∈ ℝ, A < x → x < B → f(x) = g(x)206shows HasRiemannIntegral(f, A, B, I)207proof208obtain⟨functionG, ordered, control⟩:= integral209have aMember : A ∈ ℝ bytypecheck210have bMember : B ∈ ℝ bytypecheck211have fA := FunctionBetween.apply_closed with function, aMember212have gA := FunctionBetween.apply_closed with functionG, aMember213have fB := FunctionBetween.apply_closed with function, bMember214have gB := FunctionBetween.apply_closed with functionG, bMember215have fAReal : Real(f(A)) byexact fA216have gAReal : Real(g(A)) byexact gA217have fBReal : Real(f(B)) byexact fB218have gBReal : Real(g(B)) byexact gB219have absA := abs.nonnegative at f(A) - g(A)220have absB := abs.nonnegative at f(B) - g(B)221have upperA := abs.upper at f(A) - g(A)222have upperB := abs.upper at f(B) - g(B)223have lowerA := abs.upper_neg at f(A) - g(A)224have lowerB := abs.upper_neg at f(B) - g(B)225have absAReal := abs.real at f(A) - g(A)226have absBReal := abs.real at f(B) - g(B)227have nonnegative : 0≤ abs(f(A) - g(A)) + abs(f(B) - g(B)) bylinarith228-- On [A, B], f − g is 0 inside and bounded by K at the endpoints.229havecases : ∀ x ∈ closedInterval(A, B), x = A ∨ x = B ∨ f(x) = g(x)230intros x inside231obtain⟨xMember, lower, upper⟩:= closedInterval.membership.forward232with inside233have xReal : Real(x) byexact xMember234have first := realLess.trichotomy at A, x235rcases first with above | same | below236 . have second := realLess.trichotomy at x, B237rcases second with inner | atB | beyond238 . right239right240exact agree with x, xMember, above, inner241 . right242left243exact atB244 . exfalso245linarith246 . left247rw same248 . exfalso249linarith250have boundFG : ∀ x ∈ closedInterval(A, B),251 f(x) - g(x) ≤ abs(f(A) - g(A)) + abs(f(B) - g(B))252intros x inside253have alternatives :=caseswith x, inside254rcases alternatives with atA | atB | inner255 . rw atA256linarith257 . rw atB258linarith259 . rw inner260obtain⟨xMember, _, _⟩:= closedInterval.membership.forward with inside261have gX := FunctionBetween.apply_closed with functionG, xMember262have gXReal : Real(g(x)) byexact gX263linarith264have boundGF : ∀ x ∈ closedInterval(A, B),265 g(x) - f(x) ≤ abs(f(A) - g(A)) + abs(f(B) - g(B))266intros x inside267have alternatives :=caseswith x, inside268rcases alternatives with atA | atB | inner269 . rw atA270linarith271 . rw atB272linarith273 . rw inner274obtain⟨xMember, _, _⟩:= closedInterval.membership.forward with inside275have gX := FunctionBetween.apply_closed with functionG, xMember276have gXReal : Real(g(x)) byexact gX277linarith278have agreeBack : ∀ x ∈ ℝ, A < x → x < B → g(x) = f(x)279intros x xMember lower upper280have value := agree with x, xMember, lower, upper281rw value282refine⟨function, ordered, ?_⟩283intros E eMember positive284have eReal : Real(E) byexact eMember285have halfPositive := half.positive with positive286have halfReal := half.real at E287have quarterReal := half.real at half(E)288have halves := half.add_self at E289have quarters := half.add_self at half(E)290obtain⟨R, rMember, rPositive, estimate⟩:= control with half(E),291 halfReal, halfPositive292have shifted : 0 < abs(f(A) - g(A)) + abs(f(B) - g(B)) +1bylinarith293have radius := scaledRadius.real at half(E),294 abs(f(A) - g(A)) + abs(f(B) - g(B)) +1295have radiusPositive := scaledRadius.positive with halfPositive, shifted296have cancel := scaledRadius.cancel at half(E),297 abs(f(A) - g(A)) + abs(f(B) - g(B)) +1with shifted298have rReal : Real(R) byexact rMember299obtain⟨D, dMember, dPositive, dR, dRadius⟩:= positiveRadius.common300with rPositive, radiusPositive301have dReal : Real(D) byexact dMember302refine⟨D, dMember, dPositive, ?_⟩303intros p t N nMember tagged fine304have nNat := nat.omega.backward with nMember305have fineR := FinePartition.weaken with fine, dR306have near := estimate with p, t, N, nMember, tagged, fineR307have above := riemannSum.interior_difference with function, functionG,308 tagged, fine, boundFG, nonnegative, agree309have below := riemannSum.interior_difference with functionG, function,310 tagged, fine, boundGF, nonnegative, agreeBack311obtain⟨⟨points, _, _, _⟩, tags, _⟩:= tagged312have sumF := riemannSum.real at f, p, t, N with function, points, tags313have sumG := riemannSum.real at g, p, t, N with functionG, points, tags314unfold dist at near315have nearUpper := abs.upper at riemannSum(g, p, t, N) - I316have nearLower := abs.upper_neg at riemannSum(g, p, t, N) - I317have distanceReal := abs.real at riemannSum(g, p, t, N) - I318have smaller := realLe.mul_left_of_nonnegative at D,319 scaledRadius(half(E), abs(f(A) - g(A)) + abs(f(B) - g(B)) +1),320 abs(f(A) - g(A)) + abs(f(B) - g(B)) with dRadius, nonnegative321have radiusNonnegative : (0:R) ≤ scaledRadius(half(E),322 abs(f(A) - g(A)) + abs(f(B) - g(B)) +1) bylinarith323have unit : abs(f(A) - g(A)) + abs(f(B) - g(B)) ≤324 abs(f(A) - g(A)) + abs(f(B) - g(B)) +1bylinarith325have larger := realLe.mul_right_of_nonnegative326at abs(f(A) - g(A)) + abs(f(B) - g(B)),327 abs(f(A) - g(A)) + abs(f(B) - g(B)) +1,328 scaledRadius(half(E), abs(f(A) - g(A)) + abs(f(B) - g(B)) +1)329with unit, radiusNonnegative330apply dist.lt_of_two_sided_error at riemannSum(f, p, t, N), I,331 abs(riemannSum(g, p, t, N) - I) +332 ((abs(f(A) - g(A)) + abs(f(B) - g(B))) * D +333 (abs(f(A) - g(A)) + abs(f(B) - g(B))) * D), E334 . linarith335 . linarith336 . linarith337338-- Integrals over adjacent intervals add339340-- The real sequence following u before index k and v from k on.341defunique switchValue (u v : Set, k n : Nat) (X ∈ ℝ) :=342 (n < k ∧ X = u[n]) ∨ (k ≤ n ∧ X = v[n])343requires first : RealSequence(u)344requires second : RealSequence(v)345proof346have alternatives := Less.or_LessEqual at n, k347cases alternatives with before | after348 . have value := RealSequence.value at u, n with first349have member : u[n] ∈ ℝ byexact value350refine⟨u[n], member, ?_⟩351left352refine⟨before, rfl⟩353 . have value := RealSequence.value at v, n with second354have member : v[n] ∈ ℝ byexact value355refine⟨v[n], member, ?_⟩356right357refine⟨after, rfl⟩358uniqueness359intros X xMember xProperty Y yMember yProperty360rcases xProperty with⟨xBefore, xValue⟩|⟨xAfter, xValue⟩361 . rcases yProperty with⟨yBefore, yValue⟩|⟨yAfter, yValue⟩362 . rw [xValue, yValue]363 . exfalso364arith365 . rcases yProperty with⟨yBefore, yValue⟩|⟨yAfter, yValue⟩366 . exfalso367arith368 . rw [xValue, yValue]369370defgraph switchSequence (u v : Set, k : Nat) (n ∈ ℕ) : ℝ := switchValue(u, v, k, n)371requires sequenceU : RealSequence(u)372requires sequenceV : RealSequence(v)373proof374have nNat := nat.omega.backward with member375have selected := switchValue.selected at u, v, k, n with sequenceU, sequenceV376exact selected.left377378theorem switchSequence.before (u v : Set, k n : Nat)379requires sequenceU : RealSequence(u)380requires sequenceV : RealSequence(v)381requires earlier : n < k382shows switchSequence(u, v, k)[n] = u[n]383proof384have value := switchSequence.value at u, v, k, n with sequenceU, sequenceV385obtain⟨_, property⟩:= switchValue.selected at u, v, k, n with sequenceU, sequenceV386rcases property with⟨_, equal⟩|⟨later, _⟩387 . rw [value, equal]388 . exfalso389arith390391theorem switchSequence.after (u v : Set, k n : Nat)392requires sequenceU : RealSequence(u)393requires sequenceV : RealSequence(v)394requires later : k ≤ n395shows switchSequence(u, v, k)[n] = v[n]396proof397have value := switchSequence.value at u, v, k, n with sequenceU, sequenceV398obtain⟨_, property⟩:= switchValue.selected at u, v, k, n with sequenceU, sequenceV399rcases property with⟨earlier, _⟩|⟨_, equal⟩400 . exfalso401arith402 . rw [value, equal]403404theorem switchSequence.sequence (u v : Set, k : Nat)405requires sequenceU : RealSequence(u)406requires sequenceV : RealSequence(v)407shows RealSequence(switchSequence(u, v, k))408proof409unfold RealSequence410exact switchSequence.function with sequenceU, sequenceV411412-- Splitting a tagged partition at a point C inside its cell k: the cells413-- left of C keep their tags and the new cell [pₖ, C] is tagged by C.414theorem TaggedPartition.insert_left (p t : Set) (A B C D : Real, k L : Nat)415requires tagged : TaggedPartition(p, t, A, B, succ(add(k, L)))416requires fine : FinePartition(p, A, B, succ(add(k, L)), D)417requiresleft : (p[k]:R) < C418requiresright : C < (p[succ(k)]:R)419shows TaggedPartition(indexedInsertedPoints(p, C, k),420 switchSequence(t, constantSequence(C), k), A, C, succ(k))421proof422have identityFunction := identity.function at ℝ423obtain⟨fineLeft, _⟩:= FinePartition.insert_indexed_split424at identity(ℝ), p, A, B, C, D, k, L with identityFunction, fine, left,425right426obtain⟨partition, _, _⟩:= fineLeft427obtain⟨⟨points, _, _, _⟩, tags, cells⟩:= tagged428have constant := constantSequence.sequence at C429have switched := switchSequence.sequence at t, constantSequence(C), k430with tags, constant431refine⟨partition, switched, ?_⟩432intros i iMember before433have iNat := nat.omega.backward with iMember434have atMost : i ≤ k byarith435have lower := indexedInsertedPoints.before at p, C, k, i with points, atMost436have alternatives := Less.or_LessEqual at i, k437cases alternatives with earlier | later438 . have next : succ(i) ≤ k byarith439have upper := indexedInsertedPoints.before at p, C, k, succ(i)440with points, next441have tag := switchSequence.before at t, constantSequence(C), k, i442with tags, constant, earlier443have inside : i < succ(add(k, L)) byarith444have cell := cells with i, iMember, inside445rw [lower, upper, tag]446exact cell447 . have same : i = k byarith448have tag := switchSequence.after at t, constantSequence(C), k, i449with tags, constant, later450have value := constantSequence.value at C, i451have upper := indexedInsertedPoints.inserted at p, C, k with points452have cReal : Real(C) bytypecheck453have cMember : C ∈ ℝ bytypecheck454have pReal := RealSequence.value at p, k with points455have below : (p[k]:R) ≤ C bylinarith456have reflexive := realLe.reflexive at C457rw [lower, tag, value, same, upper]458exact closedInterval.membership.backward with⟨cMember, below, reflexive⟩459460-- The cells right of C keep their tags and the new cell [C, pₖ₊₁] is461-- tagged by C.462theorem TaggedPartition.insert_right (p t : Set) (A B C D : Real, k L : Nat)463requires tagged : TaggedPartition(p, t, A, B, succ(add(k, L)))464requires fine : FinePartition(p, A, B, succ(add(k, L)), D)465requiresleft : (p[k]:R) < C466requiresright : C < (p[succ(k)]:R)467shows TaggedPartition(468 shiftedRealSequence(indexedInsertedPoints(p, C, k), succ(k)),469 shiftedRealSequence(switchSequence(constantSequence(C), t, succ(k)), k),470 C, B, succ(L))471proof472have identityFunction := identity.function at ℝ473obtain⟨_, fineRight, _⟩:= FinePartition.insert_indexed_split474at identity(ℝ), p, A, B, C, D, k, L with identityFunction, fine, left,475right476obtain⟨partition, _, _⟩:= fineRight477obtain⟨⟨points, _, _, _⟩, tags, cells⟩:= tagged478have constant := constantSequence.sequence at C479have switched := switchSequence.sequence at constantSequence(C), t,480 succ(k) with constant, tags481have shifted := shiftedRealSequence.sequence482at switchSequence(constantSequence(C), t, succ(k)), k with switched483have inserted := indexedInsertedPoints.sequence at p, C, k with points484refine⟨partition, shifted, ?_⟩485intros j jMember before486have jNat := nat.omega.backward with jMember487have tag := shiftedRealSequence.value488at switchSequence(constantSequence(C), t, succ(k)), k, j with switched489have lower := shiftedRealSequence.value490at indexedInsertedPoints(p, C, k), succ(k), j with inserted491have upper := shiftedRealSequence.value492at indexedInsertedPoints(p, C, k), succ(k), succ(j) with inserted493rw [tag, lower, upper]494have alternatives := natural.zero_or_successor at j495cases alternatives with zero | successor496 . -- the new cell [C, pₖ₊₁]497have first : add(k, j) < succ(k) byarith498have value := switchSequence.before at constantSequence(C), t, succ(k),499 add(k, j) with constant, tags, first500have constantValue := constantSequence.value at C, add(k, j)501have atStart : add(succ(k), j) = succ(k) byarith502have atNext : add(succ(k), succ(j)) = succ(succ(add(k, 0))) byarith503have startValue := indexedInsertedPoints.inserted at p, C, k with points504have nextValue := indexedInsertedPoints.after at p, C, k, 0with points505have shape : succ(add(k, 0)) = succ(k) byarith506rw [value, constantValue, atStart, atNext, startValue, nextValue, shape]507have cReal : Real(C) bytypecheck508have cMember : C ∈ ℝ bytypecheck509have pReal := RealSequence.value at p, succ(k) with points510have above : C ≤ (p[succ(k)]:R) bylinarith511have reflexive := realLe.reflexive at C512exact closedInterval.membership.backward with⟨cMember, reflexive, above⟩513 . -- an old cell, shifted by one514obtain⟨i, iNat, shape⟩:= successor515have later : succ(k) ≤ add(k, j) byarith516have value := switchSequence.after at constantSequence(C), t, succ(k),517 add(k, j) with constant, tags, later518have atStart : add(succ(k), j) = succ(succ(add(k, i))) byarith519have atNext : add(succ(k), succ(j)) = succ(succ(add(k, j))) byarith520have startValue := indexedInsertedPoints.after at p, C, k, i with points521have nextValue := indexedInsertedPoints.after at p, C, k, j with points522have index : succ(add(k, i)) = add(k, j) byarith523rw [value, atStart, atNext, startValue, nextValue, index]524have inside : add(k, j) < succ(add(k, L)) byarith525have member : add(k, j) ∈ ℕ bytypecheck526exact cells with add(k, j), member, inside527528-- The tagged sum over [A, B] is the sum of the two sums above, up to the529-- change of tag in cell k.530theorem riemannSum.insert_split (f p t : Set) (A B C : Real, k L : Nat)531requires function : f : ℝ → ℝ532requires tagged : TaggedPartition(p, t, A, B, succ(add(k, L)))533shows riemannSum(f, p, t, succ(add(k, L))) =534 riemannSum(f, indexedInsertedPoints(p, C, k),535 switchSequence(t, constantSequence(C), k), succ(k)) +536 riemannSum(f, shiftedRealSequence(indexedInsertedPoints(p, C, k), succ(k)),537 shiftedRealSequence(switchSequence(constantSequence(C), t, succ(k)), k),538 succ(L)) +539 (f(t[k]) - f(C)) * (p[succ(k)] - p[k])540proof541obtain⟨⟨points, _, _, _⟩, tags, _⟩:= tagged542have cReal : Real(C) bytypecheck543have cMember : C ∈ ℝ bytypecheck544have constant := constantSequence.sequence at C545have inserted := indexedInsertedPoints.sequence at p, C, k with points546have leftTags := switchSequence.sequence at t, constantSequence(C), k547with tags, constant548have switched := switchSequence.sequence at constantSequence(C), t,549 succ(k) with constant, tags550have rightPoints := shiftedRealSequence.sequence551at indexedInsertedPoints(p, C, k), succ(k) with inserted552have rightTags := shiftedRealSequence.sequence553at switchSequence(constantSequence(C), t, succ(k)), k with switched554-- The sum over p splits after cell k.555have whole := riemannSum.split_at at f, p, t, succ(k), L556with function, points, tags557have count : add(succ(k), L) = succ(add(k, L)) byarith558rw count at whole559have firstK := riemannSum.succ at f, p, t, k with function, points, tags560-- The left sum: the first k cells agree, then the cell [pₖ, C].561have cellsLeft : ∀ i ∈ ℕ, i < k →562 indexedInsertedPoints(p, C, k)[i] = p[i] ∧563 indexedInsertedPoints(p, C, k)[succ(i)] = p[succ(i)] ∧564 switchSequence(t, constantSequence(C), k)[i] = t[i]565intros i iMember before566have iNat := nat.omega.backward with iMember567have atMost : i ≤ k byarith568have next : succ(i) ≤ k byarith569have lower := indexedInsertedPoints.before at p, C, k, i with points, atMost570have upper := indexedInsertedPoints.before at p, C, k, succ(i)571with points, next572have tag := switchSequence.before at t, constantSequence(C), k, i573with tags, constant, before574refine⟨lower, upper, tag⟩575have prefixSame := riemannSum.congr_cells576at f, indexedInsertedPoints(p, C, k), switchSequence(t, constantSequence(C),577 k), p, t, k with function, inserted, points, leftTags, tags, cellsLeft578have leftStep := riemannSum.succ at f, indexedInsertedPoints(p, C, k),579 switchSequence(t, constantSequence(C), k), k with function, inserted,580 leftTags581have reflexive : k ≤ k byarith582have leftTag := switchSequence.after at t, constantSequence(C), k, k583with tags, constant, reflexive584have constantK := constantSequence.value at C, k585have leftEnd := indexedInsertedPoints.inserted at p, C, k with points586have leftStart := indexedInsertedPoints.before at p, C, k, k587with points, reflexive588rw [leftTag, constantK, leftEnd, leftStart, prefixSame] at leftStep589-- The right sum: the cell [C, pₖ₊₁], then the later cells of p.590have rightSplit := riemannSum.split_at at f,591 shiftedRealSequence(indexedInsertedPoints(p, C, k), succ(k)),592 shiftedRealSequence(switchSequence(constantSequence(C), t, succ(k)), k),593 succ(0), L with function, rightPoints, rightTags594have rightCount : add(succ(0), L) = succ(L) byarith595rw rightCount at rightSplit596have rightFirst := riemannSum.succ at f,597 shiftedRealSequence(indexedInsertedPoints(p, C, k), succ(k)),598 shiftedRealSequence(switchSequence(constantSequence(C), t, succ(k)), k), 0599with function, rightPoints, rightTags600have tagZero := shiftedRealSequence.value601at switchSequence(constantSequence(C), t, succ(k)), k, 0with switched602have firstIndex : add(k, 0) < succ(k) byarith603have switchZero := switchSequence.before at constantSequence(C), t, succ(k),604 add(k, 0) with constant, tags, firstIndex605have constantZero := constantSequence.value at C, add(k, 0)606have pointZero := shiftedRealSequence.value607at indexedInsertedPoints(p, C, k), succ(k), 0with inserted608have pointOne := shiftedRealSequence.value609at indexedInsertedPoints(p, C, k), succ(k), succ(0) with inserted610have zeroIndex : add(succ(k), 0) = succ(k) byarith611have oneIndex : add(succ(k), succ(0)) = succ(succ(add(k, 0))) byarith612have nextValue := indexedInsertedPoints.after at p, C, k, 0with points613have shape : succ(add(k, 0)) = succ(k) byarith614rw [tagZero, switchZero, constantZero, pointZero, pointOne, zeroIndex,615 oneIndex, nextValue, shape, leftEnd, riemannSum.zero] at rightFirst616have cellsRight : ∀ i ∈ ℕ, i < L →617 shiftedRealSequence(shiftedRealSequence(indexedInsertedPoints(p, C, k),618 succ(k)), succ(0))[i] = shiftedRealSequence(p, succ(k))[i] ∧619 shiftedRealSequence(shiftedRealSequence(indexedInsertedPoints(p, C, k),620 succ(k)), succ(0))[succ(i)] =621 shiftedRealSequence(p, succ(k))[succ(i)] ∧622 shiftedRealSequence(shiftedRealSequence(switchSequence(623 constantSequence(C), t, succ(k)), k), succ(0))[i] =624 shiftedRealSequence(t, succ(k))[i]625intros i iMember before626have iNat := nat.omega.backward with iMember627have outerLower := shiftedRealSequence.value628at shiftedRealSequence(indexedInsertedPoints(p, C, k), succ(k)),629 succ(0), i with rightPoints630have outerUpper := shiftedRealSequence.value631at shiftedRealSequence(indexedInsertedPoints(p, C, k), succ(k)),632 succ(0), succ(i) with rightPoints633have innerLower := shiftedRealSequence.value634at indexedInsertedPoints(p, C, k), succ(k), add(succ(0), i) with inserted635have innerUpper := shiftedRealSequence.value636at indexedInsertedPoints(p, C, k), succ(k), add(succ(0), succ(i))637with inserted638have lowerIndex : add(succ(k), add(succ(0), i)) = succ(succ(add(k, i)))639byarith640have upperIndex : add(succ(k), add(succ(0), succ(i))) =641 succ(succ(add(k, succ(i)))) byarith642have lowerValue := indexedInsertedPoints.after at p, C, k, i with points643have upperValue := indexedInsertedPoints.after at p, C, k, succ(i)644with points645have pLower := shiftedRealSequence.value at p, succ(k), i with points646have pUpper := shiftedRealSequence.value at p, succ(k), succ(i)647with points648have pLowerIndex : add(succ(k), i) = succ(add(k, i)) byarith649have pUpperIndex : add(succ(k), succ(i)) = succ(add(k, succ(i))) byarith650have outerTag := shiftedRealSequence.value651at shiftedRealSequence(switchSequence(constantSequence(C), t, succ(k)),652 k), succ(0), i with rightTags653have innerTag := shiftedRealSequence.value654at switchSequence(constantSequence(C), t, succ(k)), k, add(succ(0), i)655with switched656have later : succ(k) ≤ add(k, add(succ(0), i)) byarith657have switchTag := switchSequence.after at constantSequence(C), t, succ(k),658 add(k, add(succ(0), i)) with constant, tags, later659have tTag := shiftedRealSequence.value at t, succ(k), i with tags660have tagIndex : add(k, add(succ(0), i)) = add(succ(k), i) byarith661rw [outerLower, outerUpper, innerLower, innerUpper, lowerIndex, upperIndex,662 lowerValue, upperValue, pLower, pUpper, pLowerIndex, pUpperIndex,663 outerTag, innerTag, switchTag, tTag, tagIndex]664refine⟨rfl, rfl, rfl⟩665have pShift := shiftedRealSequence.sequence at p, succ(k) with points666have tShift := shiftedRealSequence.sequence at t, succ(k) with tags667have rightShiftPoints := shiftedRealSequence.sequence668at shiftedRealSequence(indexedInsertedPoints(p, C, k), succ(k)), succ(0)669with rightPoints670have rightShiftTags := shiftedRealSequence.sequence671at shiftedRealSequence(switchSequence(constantSequence(C), t, succ(k)), k),672 succ(0) with rightTags673have tailSame := riemannSum.congr_cells at f,674 shiftedRealSequence(shiftedRealSequence(indexedInsertedPoints(p, C, k),675 succ(k)), succ(0)),676 shiftedRealSequence(shiftedRealSequence(switchSequence(677 constantSequence(C), t, succ(k)), k), succ(0)),678 shiftedRealSequence(p, succ(k)), shiftedRealSequence(t, succ(k)), L679with function, rightShiftPoints, pShift, rightShiftTags, tShift, cellsRight680rw [tailSame, rightFirst] at rightSplit681-- Collect the pieces.682have tagReal := RealSequence.value at t, k with tags683have tagMember : t[k] ∈ ℝ byexact tagReal684have fTag := FunctionBetween.apply_closed with function, tagMember685have fTagReal : Real(f(t[k])) byexact fTag686have fC := FunctionBetween.apply_closed with function, cMember687have fCReal : Real(f(C)) byexact fC688have pK := RealSequence.value at p, k with points689have pNext := RealSequence.value at p, succ(k) with points690have prefixReal := riemannSum.real at f, p, t, k with function, points, tags691have tailReal := riemannSum.real at f, shiftedRealSequence(p, succ(k)),692 shiftedRealSequence(t, succ(k)), L with function, pShift, tShift693rw [whole, firstK, leftStep, rightSplit]694ring695696-- A δ-fine tagged partition of [A, B] gives δ-fine tagged partitions of697-- [A, C] and [C, B] whose sums add up to its sum within 2Mδ, for f698-- bounded by M on [A, B].699theorem TaggedPartition.split_bounded (f p t : Set) (A C B M D : Real, N : Nat)700requires function : f : ℝ → ℝ701requires tagged : TaggedPartition(p, t, A, B, N)702requires fine : FinePartition(p, A, B, N, D)703requiresleft : A ≤ C704requiresright : C ≤ B705requires bounded : ∀ x ∈ closedInterval(A, B), abs(f(x)) ≤ M706shows∃ q, ∃ s, ∃ K ∈ ℕ, ∃ u, ∃ v, ∃ J ∈ ℕ,707 TaggedPartition(q, s, A, C, K) ∧ FinePartition(q, A, C, K, D) ∧708 TaggedPartition(u, v, C, B, J) ∧ FinePartition(u, C, B, J, D) ∧709 riemannSum(f, p, t, N) - riemannSum(f, q, s, K) -710 riemannSum(f, u, v, J) ≤ (M + M) * D ∧711 -((M + M) * D) ≤ riemannSum(f, p, t, N) - riemannSum(f, q, s, K) -712 riemannSum(f, u, v, J)713proof714have taggedCopy := tagged715have fineCopy := fine716obtain⟨partition, tags, cells⟩:= tagged717obtain⟨points, start, finish, increasing⟩:= partition718obtain⟨_, dPositive, widths⟩:= fine719haveordered : A ≤ B bylinarith720have aMember : A ∈ ℝ bytypecheck721have reflexiveA := realLe.reflexive at A722have aInside := closedInterval.membership.backward723with⟨aMember, reflexiveA, ordered⟩724have atA := bounded with A, aInside725have fA := FunctionBetween.apply_closed with function, aMember726have fAReal : Real(f(A)) byexact fA727have absNonnegative := abs.nonnegative at f(A)728have mNonnegative : 0≤ M bylinarith729have doubleNonnegative : 0≤ M + M bylinarith730have dNonnegative : (0:R) ≤ D bylinarith731have zeroProduct := realLe.mul_left_of_nonnegative at (0:R), D, M + M732with dNonnegative, doubleNonnegative733have zeroValue : (M + M) * (0:R) = (0:R) byring734have location : (∃ i ∈ ℕ, i ≤ N ∧ p[i] = C) ∨735 (∃ k ∈ ℕ, k < N ∧ (p[k]:R) < C ∧ C < (p[succ(k)]:R))736have first := realLess.trichotomy at A, C737rcases first with inner | same | outer738 . have second := realLess.trichotomy at C, B739rcases second with inside | atB | beyond740 . exact IntervalPartition.locate_interior741with⟨points, start, finish, increasing⟩, inner, inside742 . left743have nMember : N∈ ℕ bytypecheck744have reflexive : N≤ N byarith745rw atB746refine⟨N, nMember, reflexive, finish⟩747 . exfalso748linarith749 . left750have zeroMember : 0∈ ℕ bytypecheck751have zeroLe : 0≤ N byarith752rw← same753refine⟨0, zeroMember, zeroLe, start⟩754 . exfalso755linarith756cases location with vertex | interior757 . obtain⟨i, iMember, bound, value⟩:= vertex758have iNat := nat.omega.backward with iMember759obtain⟨j, jMember, _, fineL, fineR, taggedL, taggedR, sum⟩:=760 FinePartition.split_at_vertex with function, fineCopy, taggedCopy, bound,761 value762have shiftedPoints := shiftedRealSequence.sequence at p, i with points763have shiftedTags := shiftedRealSequence.sequence at t, i with tags764have jNat := nat.omega.backward with jMember765have sumL := riemannSum.real at f, p, t, i with function, points, tags766have sumR := riemannSum.real at f, shiftedRealSequence(p, i),767 shiftedRealSequence(t, i), j with function, shiftedPoints, shiftedTags768refine⟨p, t, i, iMember, shiftedRealSequence(p, i),769 shiftedRealSequence(t, i), j, jMember, taggedL, fineL, taggedR, fineR,770 ?_, ?_⟩771 . rw sum772linarith773 . rw sum774linarith775obtain⟨k, kMember, before, lower, upper⟩:= interior776have kNat := nat.omega.backward with kMember777obtain⟨L, lNat, length⟩:= before778have shape : N= succ(add(k, L)) byarith779rw shape at taggedCopy, fineCopy780have taggedL := TaggedPartition.insert_left with taggedCopy, fineCopy, lower,781 upper782have taggedR := TaggedPartition.insert_right with taggedCopy, fineCopy,783 lower, upper784obtain⟨fineL, fineR, _⟩:= FinePartition.insert_indexed_split785with function, fineCopy, lower, upper786have sum := riemannSum.insert_split at f, p, t, A, B, C, k, L787with function, taggedCopy788have kSucc : succ(k) ∈ ℕ bytypecheck789have lSucc : succ(L) ∈ ℕ bytypecheck790have cReal : Real(C) bytypecheck791have constant := constantSequence.sequence at C792have inserted := indexedInsertedPoints.sequence at p, C, k with points793have leftTags := switchSequence.sequence at t, constantSequence(C), k794with tags, constant795have switched := switchSequence.sequence at constantSequence(C), t,796 succ(k) with constant, tags797have rightPoints := shiftedRealSequence.sequence798at indexedInsertedPoints(p, C, k), succ(k) with inserted799have rightTags := shiftedRealSequence.sequence800at switchSequence(constantSequence(C), t, succ(k)), k with switched801have sumL := riemannSum.real at f, indexedInsertedPoints(p, C, k),802 switchSequence(t, constantSequence(C), k), succ(k)803with function, inserted, leftTags804have sumR := riemannSum.real at f,805 shiftedRealSequence(indexedInsertedPoints(p, C, k), succ(k)),806 shiftedRealSequence(switchSequence(constantSequence(C), t, succ(k)), k),807 succ(L) with function, rightPoints, rightTags808refine⟨indexedInsertedPoints(p, C, k),809 switchSequence(t, constantSequence(C), k), succ(k), kSucc,810 shiftedRealSequence(indexedInsertedPoints(p, C, k), succ(k)),811 shiftedRealSequence(switchSequence(constantSequence(C), t, succ(k)), k),812 succ(L), lSucc, taggedL, fineL, taggedR, fineR, ?_⟩813-- The change of tag in cell k costs at most 2M times its width.814have kBefore : k < N byarith815have kLe : k ≤ N byarith816have succLe : succ(k) ≤ N byarith817obtain⟨tagLower, tagUpper⟩:= TaggedPartition.tag_bounds at p, t, A, B, N,818 k with⟨⟨points, start, finish, increasing⟩, tags, cells⟩, kBefore819obtain⟨atLeast, _⟩:= IntervalPartition.point_bounds at p, A, B, N, k820with⟨points, start, finish, increasing⟩, kLe821obtain⟨_, atMost⟩:= IntervalPartition.point_bounds at p, A, B, N,822 succ(k) with⟨points, start, finish, increasing⟩, succLe823have tagReal := RealSequence.value at t, k with tags824have tagMember : t[k] ∈ ℝ byexact tagReal825have cMember : C ∈ ℝ bytypecheck826have pK := RealSequence.value at p, k with points827have pNext := RealSequence.value at p, succ(k) with points828have tagInside : t[k] ∈ closedInterval(A, B)829apply closedInterval.membership.backward830refine⟨tagMember, ?_, ?_⟩831 . linarith832 . linarith833have cInside : C ∈ closedInterval(A, B)834apply closedInterval.membership.backward835refine⟨cMember, ?_, ?_⟩836 . linarith837 . linarith838have tagBound := bounded with t[k], tagInside839have cBound := bounded with C, cInside840have fTag := FunctionBetween.apply_closed with function, tagMember841have fTagReal : Real(f(t[k])) byexact fTag842have fC := FunctionBetween.apply_closed with function, cMember843have fCReal : Real(f(C)) byexact fC844have tagUpperValue := abs.upper at f(t[k])845have tagLowerValue := abs.upper_neg at f(t[k])846have cUpperValue := abs.upper at f(C)847have cLowerValue := abs.upper_neg at f(C)848have width := widths with k, kMember, kBefore849have widthNonnegative : (0:R) ≤ p[succ(k)] - p[k] bylinarith850have widthLe : p[succ(k)] - p[k] ≤ D bylinarith851have gapUpper : f(t[k]) - f(C) ≤ M + M bylinarith852have gapLower : -(M + M) ≤ f(t[k]) - f(C) bylinarith853have scaledUpper := realLe.mul_right_of_nonnegative854at f(t[k]) - f(C), M + M, p[succ(k)] - p[k]855with gapUpper, widthNonnegative856have scaledLower := realLe.mul_right_of_nonnegative857at -(M + M), f(t[k]) - f(C), p[succ(k)] - p[k]858with gapLower, widthNonnegative859have capped := realLe.mul_left_of_nonnegative860at p[succ(k)] - p[k], D, M + M with widthLe, doubleNonnegative861have flip : -(M + M) * (p[succ(k)] - p[k]) =862 -((M + M) * (p[succ(k)] - p[k])) byring863rw flip at scaledLower864rw shape865rw sum866constructor867 . linarith868 . linarith869870-- Integrals of a bounded function over adjacent intervals add.871theorem HasRiemannIntegral.add_adjacent (f : Set) (A C B IL IR M : Real)872requires integralL : HasRiemannIntegral(f, A, C, IL)873requires integralR : HasRiemannIntegral(f, C, B, IR)874requires bounded : ∀ x ∈ closedInterval(A, B), abs(f(x)) ≤ M875shows HasRiemannIntegral(f, A, B, IL + IR)876proof877obtain⟨function, left, controlL⟩:= integralL878obtain⟨_, right, controlR⟩:= integralR879haveordered : A ≤ B bylinarith880have aMember : A ∈ ℝ bytypecheck881have reflexiveA := realLe.reflexive at A882have aInside := closedInterval.membership.backward883with⟨aMember, reflexiveA, ordered⟩884have atA := bounded with A, aInside885have fA := FunctionBetween.apply_closed with function, aMember886have fAReal : Real(f(A)) byexact fA887have absNonnegative := abs.nonnegative at f(A)888have doubleNonnegative : 0≤ M + M bylinarith889refine⟨function, ordered, ?_⟩890intros E eMember positive891have eReal : Real(E) byexact eMember892have halfPositive := half.positive with positive893have halfReal := half.real at E894have quarterPositive := half.positive with halfPositive895have quarterReal := half.real at half(E)896have quarterMember : half(half(E)) ∈ ℝ bytypecheck897have halves := half.add_self at E898have quarters := half.add_self at half(E)899obtain⟨DL, dlMember, dlPositive, estimateL⟩:= controlL with half(half(E)),900 quarterMember, quarterPositive901obtain⟨DR, drMember, drPositive, estimateR⟩:= controlR with half(half(E)),902 quarterMember, quarterPositive903have shifted : 0 < M + M +1bylinarith904have radius := scaledRadius.real at half(E), M + M +1905have radiusPositive := scaledRadius.positive with halfPositive, shifted906have cancel := scaledRadius.cancel at half(E), M + M +1with shifted907obtain⟨D, dMember, dPositive, dL, dR, dRadius⟩:= positiveMesh.common_three908with dlPositive, drPositive, radiusPositive909have dReal : Real(D) byexact dMember910refine⟨D, dMember, dPositive, ?_⟩911intros p t N nMember tagged fine912obtain⟨q, s, K, kMember, u, v, J, jMember, taggedL, fineL, taggedR, fineR,913 upper, lower⟩:= TaggedPartition.split_bounded with function, tagged, fine,914left, right, bounded915have fineLocalL := FinePartition.weaken with fineL, dL916have fineLocalR := FinePartition.weaken with fineR, dR917have nearL := estimateL with q, s, K, kMember, taggedL, fineLocalL918have nearR := estimateR with u, v, J, jMember, taggedR, fineLocalR919have nNat := nat.omega.backward with nMember920have kNat := nat.omega.backward with kMember921have jNat := nat.omega.backward with jMember922obtain⟨⟨points, _, _, _⟩, tags, _⟩:= tagged923obtain⟨⟨pointsL, _, _, _⟩, tagsL, _⟩:= taggedL924obtain⟨⟨pointsR, _, _, _⟩, tagsR, _⟩:= taggedR925have sum := riemannSum.real at f, p, t, N with function, points, tags926have sumL := riemannSum.real at f, q, s, K with function, pointsL, tagsL927have sumR := riemannSum.real at f, u, v, J with function, pointsR, tagsR928unfold dist at nearL, nearR929have upperL := abs.upper at riemannSum(f, q, s, K) - IL930have lowerL := abs.upper_neg at riemannSum(f, q, s, K) - IL931have upperR := abs.upper at riemannSum(f, u, v, J) - IR932have lowerR := abs.upper_neg at riemannSum(f, u, v, J) - IR933have absL := abs.real at riemannSum(f, q, s, K) - IL934have absR := abs.real at riemannSum(f, u, v, J) - IR935have smaller := realLe.mul_left_of_nonnegative at D,936 scaledRadius(half(E), M + M +1), M + M with dRadius, doubleNonnegative937have radiusNonnegative : (0:R) ≤ scaledRadius(half(E), M + M +1)938bylinarith939have unit : M + M ≤ M + M +1bylinarith940have larger := realLe.mul_right_of_nonnegative941at M + M, M + M +1, scaledRadius(half(E), M + M +1)942with unit, radiusNonnegative943apply dist.lt_of_two_sided_error at riemannSum(f, p, t, N), IL + IR,944 abs(riemannSum(f, q, s, K) - IL) + abs(riemannSum(f, u, v, J) - IR) +945 (M + M) * D, E946 . linarith947 . linarith948 . linarith949950theorem RiemannIntegrable.add_adjacent (f : Set) (A C B M : Real)951requires integrableL : RiemannIntegrable(f, A, C)952requires integrableR : RiemannIntegrable(f, C, B)953requires bounded : ∀ x ∈ closedInterval(A, B), abs(f(x)) ≤ M954shows RiemannIntegrable(f, A, B)955proof956obtain⟨IL, lMember, integralL⟩:= integrableL957obtain⟨IR, rMember, integralR⟩:= integrableR958have lReal : Real(IL) byexact lMember959have rReal : Real(IR) byexact rMember960have sum := HasRiemannIntegral.add_adjacent with integralL, integralR,961 bounded962have member : IL + IR ∈ ℝ bytypecheck963exact⟨IL + IR, member, sum⟩964965-- Functions continuous on the open cells of a partition966967-- A function that agrees inside (A, B) with a function continuous on968-- [A, B] has the same integral.969theorem HasRiemannIntegral.of_continuous_interior (f g : Set) (A B : Real)970requires function : f : ℝ → ℝ971requiresordered : A ≤ B972requires continuous : ContinuousOn(g, closedInterval(A, B))973requires agree : ∀ x ∈ ℝ, A < x → x < B → f(x) = g(x)974shows HasRiemannIntegral(f, A, B, riemannIntegral(g, A, B))975proof976have integrable := ContinuousOn.integrable_interval withordered, continuous977have integral := riemannIntegral.integral with integrable978exact HasRiemannIntegral.congr_interior with integral, function, agree979980-- Integrals over the cells of a partition of [A, B] add up to the integral981-- over [A, B], for a function bounded on [A, B]: the prefix form.982theorem HasRiemannIntegral.partition_prefix (f p I : Set) (A B M : Real, N n : Nat)983requires function : f : ℝ → ℝ984requires partition : IntervalPartition(p, A, B, N)985requires values : RealSequence(I)986requires bounded : ∀ x ∈ closedInterval(A, B), abs(f(x)) ≤ M987requires cells : ∀ i ∈ ℕ, i < N → HasRiemannIntegral(f, p[i], p[succ(i)], I[i])988shows n ≤ N → HasRiemannIntegral(f, A, p[n], ∑ i < n, I(i))989proof990induction n991| zero =>992intro bound993obtain⟨_, start, _, _⟩:= partition994have point := HasRiemannIntegral.point at f, A with function995rw [start, realPartialSum.zero]996exact point997| succ(m) using ih =>998intro bound999have mLe : m ≤ N byarith1000have before : m < N byarith1001have previous := ih with mLe1002have mMember : m ∈ ℕ bytypecheck1003have cell := cells with m, mMember, before1004obtain⟨_, atMost⟩:= IntervalPartition.point_bounds at p, A, B, N,1005 succ(m) with partition, bound1006obtain⟨points, _, _, _⟩:= partition1007have pNext := RealSequence.value at p, succ(m) with points1008have aReal : Real(A) bytypecheck1009have reflexive := realLe.reflexive at A1010have nested := closedInterval.nested at A, p[succ(m)], A, B1011with reflexive, atMost1012have smaller : ∀ x ∈ closedInterval(A, p[succ(m)]), abs(f(x)) ≤ M1013intros x inside1014have wider := nested with x, inside1015exact bounded with x, wider1016have sum := HasRiemannIntegral.add_adjacent with previous, cell, smaller1017have prefixReal := realPartialSum.real at I, m with values1018have term := RealSequence.value at I, m with values1019have bridge : realAddValue(∑ i < m, I(i), I(m)) = (∑ i < m, I(i)) + I(m)1020byring1021rw [realPartialSum.succ, bridge]1022exact sum10231024-- Integrals over the cells of a partition add up, for a bounded function.1025theorem HasRiemannIntegral.of_partition (f p I : Set) (A B M : Real, N : Nat)1026requires function : f : ℝ → ℝ1027requires partition : IntervalPartition(p, A, B, N)1028requires values : RealSequence(I)1029requires bounded : ∀ x ∈ closedInterval(A, B), abs(f(x)) ≤ M1030requires cells : ∀ i ∈ ℕ, i < N → HasRiemannIntegral(f, p[i], p[succ(i)], I[i])1031shows HasRiemannIntegral(f, A, B, ∑ i < N, I(i))1032proof1033haveprefix:= HasRiemannIntegral.partition_prefix at f, p, I, A, B, M, N, N1034with function, partition, values, bounded, cells1035have reflexive : N≤ N byarith1036have whole :=prefixwith reflexive1037obtain⟨_, _, finish, _⟩:= partition1038 rwa finish at whole10391040-- The integrals over the cells of a partition.1041defgraph cellIntegrals (f p : Set) (i ∈ ℕ) : ℝ :=1042 riemannIntegral(f, p[i], p[succ(i)])1043requires points : RealSequence(p)1044proof1045have iNat := nat.omega.backward with member1046haveleft:= RealSequence.value at p, i with points1047haveright:= RealSequence.value at p, succ(i) with points1048have selected := riemannIntegral.selected at f, p[i], p[succ(i)]1049exact selected.left10501051-- A bounded function integrable on each cell of a partition is integrable.1052theorem RiemannIntegrable.of_partition (f p : Set) (A B M : Real, N : Nat)1053requires function : f : ℝ → ℝ1054requires partition : IntervalPartition(p, A, B, N)1055requires bounded : ∀ x ∈ closedInterval(A, B), abs(f(x)) ≤ M1056requires cells : ∀ i ∈ ℕ, i < N → RiemannIntegrable(f, p[i], p[succ(i)])1057shows RiemannIntegrable(f, A, B)1058proof1059have points : RealSequence(p)1060obtain⟨points, _, _, _⟩:= partition1061exact points1062have values : RealSequence(cellIntegrals(f, p))1063unfold RealSequence1064exact cellIntegrals.function with points1065have integrals : ∀ i ∈ ℕ, i < N →1066 HasRiemannIntegral(f, p[i], p[succ(i)], cellIntegrals(f, p)[i])1067intros i iMember before1068have iNat := nat.omega.backward with iMember1069haveleft:= RealSequence.value at p, i with points1070haveright:= RealSequence.value at p, succ(i) with points1071have value := cellIntegrals.value at f, p, i with points1072have integrable := cells with i, iMember, before1073rw value1074exact riemannIntegral.integral with integrable1075have whole := HasRiemannIntegral.of_partition with function, partition,1076 values, bounded, integrals1077have sum := realPartialSum.real at cellIntegrals(f, p), N with values1078have member : (∑ i < N, cellIntegrals(f, p)(i)) ∈ ℝ byexact sum1079exact⟨∑ i < N, cellIntegrals(f, p)(i), member, whole⟩10801081-- A bounded function that agrees, inside each cell of a partition, with a1082-- function continuous on the closed cell is integrable.1083theorem RiemannIntegrable.of_piecewise_continuous (f p : Set) (A B M : Real, N : Nat)1084requires function : f : ℝ → ℝ1085requires partition : IntervalPartition(p, A, B, N)1086requires bounded : ∀ x ∈ closedInterval(A, B), abs(f(x)) ≤ M1087requires cells : ∀ i ∈ ℕ, i < N →∃ g,1088 ContinuousOn(g, closedInterval(p[i], p[succ(i)])) ∧1089 (∀ x ∈ ℝ, p[i] < x → x < p[succ(i)] → f(x) = g(x))1090shows RiemannIntegrable(f, A, B)1091proof1092have integrable : ∀ i ∈ ℕ, i < N → RiemannIntegrable(f, p[i], p[succ(i)])1093intros i iMember before1094obtain⟨g, continuous, agree⟩:= cells with i, iMember, before1095obtain⟨points, _, _, increasing⟩:= partition1096have strict := increasing with i, iMember, before1097haveordered : p[i] ≤ p[succ(i)] bylinarith1098have integral := HasRiemannIntegral.of_continuous_interior1099with function, ordered, continuous, agree1100haveleft:= RealSequence.value at p, i with points1101haveright:= RealSequence.value at p, succ(i) with points1102obtain⟨member, _⟩:= riemannIntegral.selected at g, p[i], p[succ(i)]1103exact⟨riemannIntegral(g, p[i], p[succ(i)]), member, integral⟩1104exact RiemannIntegrable.of_partition with function, partition, bounded,1105 integrable1106