Grace/Cumulus/Real/Analysis/Integration/Piecewise.g17

Open in Workbench Download source

Imports

FTCFirst

Declarations

Source

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)18  requires functionF : f : ℝ → ℝ19  requires functionG : g : ℝ → ℝ20  requires tagged : TaggedPartition(p, t, A, B, N)21  requires fine : FinePartition(p, A, B, N, D)22  requires bound : ∀ x ∈ closedInterval(A, B), f(x) - g(x) ≤ K23  requires nonnegative : 0 ≤ K24  requires agree : ∀ x ∈ ℝ, A < x → x < B → f(x) = g(x)25  shows n < N → riemannSum(f, p, t, n) - riemannSum(g, p, t, n) ≤ K * D26proof27  induction n28  | zero =>29    intro before30    obtain ⟨_, dPositive, _⟩ := fine31    rw [riemannSum.zero, riemannSum.zero]32    have zeroLe : (0:R) ≤ D by linarith33    have product := realLe.mul_left_of_nonnegative at (0:R), D, K34      with zeroLe, nonnegative35    have zeroProduct : K * (0:R) = (0:R) by ring36    have zeroDifference : (0:R) - (0:R) = (0:R) by ring37    rw zeroDifference38    linarith39  | succ(m) using ih =>40    intro before41    obtain ⟨partition, tags, cells⟩ := tagged42    obtain ⟨points, start, finish, increasing⟩ := partition43    obtain ⟨_, dPositive, widths⟩ := fine44    have mBefore : m < N by arith45    have previous := ih with mBefore46    have sumF := riemannSum.real at f, p, t, m with functionF, points, tags47    have sumG := riemannSum.real at g, p, t, m with functionG, points, tags48    have stepF := riemannSum.succ at f, p, t, m with functionF, points, tags49    have stepG := riemannSum.succ at g, p, t, m with functionG, points, tags50    have mMember : m ∈ ℕ by typecheck51    have tagReal := RealSequence.value at t, m with tags52    have tagMember : t[m] ∈ ℝ by exact tagReal53    have fTag := FunctionBetween.apply_closed with functionF, tagMember54    have gTag := FunctionBetween.apply_closed with functionG, tagMember55    have fTagReal : Real(f(t[m])) by exact fTag56    have gTagReal : Real(g(t[m])) by exact gTag57    have left := RealSequence.value at p, m with points58    have right := RealSequence.value at p, succ(m) with points59    have width := widths with m, mMember, mBefore60    have positiveWidth := increasing with m, mMember, mBefore61    obtain ⟨tagLower, tagUpper⟩ := TaggedPartition.tag_bounds at p, t, A, B, N,62      m with ⟨⟨points, start, finish, increasing⟩, tags, cells⟩, mBefore63    rw [stepF, stepG]64    have alternatives := natural.zero_or_successor at m65    cases alternatives with zero | successor66    . -- the first cell67      have mLe : m ≤ N by arith68      have succLe : succ(m) ≤ N by arith69      obtain ⟨atLeast, _⟩ := IntervalPartition.point_bounds at p, A, B, N, m70        with ⟨points, start, finish, increasing⟩, mLe71      obtain ⟨_, atMost⟩ := IntervalPartition.point_bounds at p, A, B, N,72        succ(m) with ⟨points, start, finish, increasing⟩, succLe73      have inside : t[m] ∈ closedInterval(A, B)74        apply closedInterval.membership.backward75        refine ⟨tagMember, ?_, ?_⟩76        . linarith77        . linarith78      have difference := bound with t[m], inside79      have widthNonnegative : (0:R) ≤ p[succ(m)] - p[m] by linarith80      have scaled := realLe.mul_right_of_nonnegative81        at f(t[m]) - g(t[m]), K, p[succ(m)] - p[m]82        with difference, widthNonnegative83      have widthLe : p[succ(m)] - p[m] ≤ D by linarith84      have capped := realLe.mul_left_of_nonnegative85        at p[succ(m)] - p[m], D, K with widthLe, nonnegative86      have 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])92        ring93      rw [expand, zero, riemannSum.zero, riemannSum.zero]94      rw zero at scaled, capped95      linarith96    . -- a middle cell: the tag lies strictly inside (A, B)97      obtain ⟨k, kNat, shape⟩ := successor98      have zeroBefore : 0 < m by arith99      have succBound : succ(m) ≤ N by arith100      have nBound : N ≤ N by arith101      have mLe : m ≤ N by arith102      have afterStart := IntervalPartition.strict_indices at p, A, B, N, 0, m103        with ⟨points, start, finish, increasing⟩, zeroBefore, mLe104      have beforeEnd := IntervalPartition.strict_indices at p, A, B, N,105        succ(m), N with ⟨points, start, finish, increasing⟩, before, nBound106      rw [start] at afterStart107      rw [finish] at beforeEnd108      have tagAbove : A < t[m] by linarith109      have tagBelow : t[m] < B by linarith110      have same := agree with t[m], tagMember, tagAbove, tagBelow111      have 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])117        ring118      rw [expand, same]119      have vanish : (g(t[m]) - g(t[m])) * (p[succ(m)] - p[m]) = (0:R)120        by ring121      rw vanish122      linarith123124-- 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)127  requires functionF : f : ℝ → ℝ128  requires functionG : g : ℝ → ℝ129  requires tagged : TaggedPartition(p, t, A, B, N)130  requires fine : FinePartition(p, A, B, N, D)131  requires bound : ∀ x ∈ closedInterval(A, B), f(x) - g(x) ≤ K132  requires nonnegative : 0 ≤ K133  requires agree : ∀ x ∈ ℝ, A < x → x < B → f(x) = g(x)134  shows riemannSum(f, p, t, N) - riemannSum(g, p, t, N) ≤ K * D + K * D135proof136  have taggedCopy := tagged137  have fineCopy := fine138  obtain ⟨partition, tags, cells⟩ := tagged139  obtain ⟨points, start, finish, increasing⟩ := partition140  obtain ⟨_, dPositive, widths⟩ := fine141  have zeroLe : (0:R) ≤ D by linarith142  have product := realLe.mul_left_of_nonnegative at (0:R), D, K143    with zeroLe, nonnegative144  have zeroProduct : K * (0:R) = (0:R) by ring145  have alternatives := natural.zero_or_successor at N146  cases alternatives with zero | successor147  . rw [zero, riemannSum.zero, riemannSum.zero]148    have zeroDifference : (0:R) - (0:R) = (0:R) by ring149    rw zeroDifference150    linarith151  obtain ⟨M, mNat, shape⟩ := successor152  have before : M < N by arith153  have prefix := riemannSum.interior_prefix at f, g, p, t, A, B, K, D, N, M154    with functionF, functionG, taggedCopy, fineCopy, bound, nonnegative, agree155  have earlier := prefix with before156  have sumF := riemannSum.real at f, p, t, M with functionF, points, tags157  have sumG := riemannSum.real at g, p, t, M with functionG, points, tags158  have stepF := riemannSum.succ at f, p, t, M with functionF, points, tags159  have stepG := riemannSum.succ at g, p, t, M with functionG, points, tags160  have mMember : M ∈ ℕ by typecheck161  have tagReal := RealSequence.value at t, M with tags162  have tagMember : t[M] ∈ ℝ by exact tagReal163  have fTag := FunctionBetween.apply_closed with functionF, tagMember164  have gTag := FunctionBetween.apply_closed with functionG, tagMember165  have fTagReal : Real(f(t[M])) by exact fTag166  have gTagReal : Real(g(t[M])) by exact gTag167  have left := RealSequence.value at p, M with points168  have right := RealSequence.value at p, succ(M) with points169  have width := widths with M, mMember, before170  have positiveWidth := increasing with M, mMember, before171  obtain ⟨tagLower, tagUpper⟩ := TaggedPartition.tag_bounds at p, t, A, B, N,172    M with ⟨⟨points, start, finish, increasing⟩, tags, cells⟩, before173  have mLe : M ≤ N by arith174  have succLe : succ(M) ≤ N by arith175  obtain ⟨atLeast, _⟩ := IntervalPartition.point_bounds at p, A, B, N, M176    with ⟨points, start, finish, increasing⟩, mLe177  obtain ⟨_, atMost⟩ := IntervalPartition.point_bounds at p, A, B, N,178    succ(M) with ⟨points, start, finish, increasing⟩, succLe179  have inside : t[M] ∈ closedInterval(A, B)180    apply closedInterval.membership.backward181    refine ⟨tagMember, ?_, ?_⟩182    . linarith183    . linarith184  have difference := bound with t[M], inside185  have widthNonnegative : (0:R) ≤ p[succ(M)] - p[M] by linarith186  have scaled := realLe.mul_right_of_nonnegative187    at f(t[M]) - g(t[M]), K, p[succ(M)] - p[M]188    with difference, widthNonnegative189  have widthLe : p[succ(M)] - p[M] ≤ D by linarith190  have capped := realLe.mul_left_of_nonnegative191    at p[succ(M)] - p[M], D, K with widthLe, nonnegative192  have 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])198    ring199  rw [shape, stepF, stepG, expand]200  linarith201202theorem HasRiemannIntegral.congr_interior (f g : Set) (A B I : Real)203  requires integral : HasRiemannIntegral(g, A, B, I)204  requires function : f : ℝ → ℝ205  requires agree : ∀ x ∈ ℝ, A < x → x < B → f(x) = g(x)206  shows HasRiemannIntegral(f, A, B, I)207proof208  obtain ⟨functionG, ordered, control⟩ := integral209  have aMember : A ∈ ℝ by typecheck210  have bMember : B ∈ ℝ by typecheck211  have fA := FunctionBetween.apply_closed with function, aMember212  have gA := FunctionBetween.apply_closed with functionG, aMember213  have fB := FunctionBetween.apply_closed with function, bMember214  have gB := FunctionBetween.apply_closed with functionG, bMember215  have fAReal : Real(f(A)) by exact fA216  have gAReal : Real(g(A)) by exact gA217  have fBReal : Real(f(B)) by exact fB218  have gBReal : Real(g(B)) by exact gB219  have absA := abs.nonnegative at f(A) - g(A)220  have absB := abs.nonnegative at f(B) - g(B)221  have upperA := abs.upper at f(A) - g(A)222  have upperB := abs.upper at f(B) - g(B)223  have lowerA := abs.upper_neg at f(A) - g(A)224  have lowerB := abs.upper_neg at f(B) - g(B)225  have absAReal := abs.real at f(A) - g(A)226  have absBReal := abs.real at f(B) - g(B)227  have nonnegative : 0 ≤ abs(f(A) - g(A)) + abs(f(B) - g(B)) by linarith228  -- On [A, B], f − g is 0 inside and bounded by K at the endpoints.229  have cases : ∀ x ∈ closedInterval(A, B), x = A ∨ x = B ∨ f(x) = g(x)230    intros x inside231    obtain ⟨xMember, lower, upper⟩ := closedInterval.membership.forward232      with inside233    have xReal : Real(x) by exact xMember234    have first := realLess.trichotomy at A, x235    rcases first with above | same | below236    . have second := realLess.trichotomy at x, B237      rcases second with inner | atB | beyond238      . right239        right240        exact agree with x, xMember, above, inner241      . right242        left243        exact atB244      . exfalso245        linarith246    . left247      rw same248    . exfalso249      linarith250  have boundFG : ∀ x ∈ closedInterval(A, B),251      f(x) - g(x) ≤ abs(f(A) - g(A)) + abs(f(B) - g(B))252    intros x inside253    have alternatives := cases with x, inside254    rcases alternatives with atA | atB | inner255    . rw atA256      linarith257    . rw atB258      linarith259    . rw inner260      obtain ⟨xMember, _, _⟩ := closedInterval.membership.forward with inside261      have gX := FunctionBetween.apply_closed with functionG, xMember262      have gXReal : Real(g(x)) by exact gX263      linarith264  have boundGF : ∀ x ∈ closedInterval(A, B),265      g(x) - f(x) ≤ abs(f(A) - g(A)) + abs(f(B) - g(B))266    intros x inside267    have alternatives := cases with x, inside268    rcases alternatives with atA | atB | inner269    . rw atA270      linarith271    . rw atB272      linarith273    . rw inner274      obtain ⟨xMember, _, _⟩ := closedInterval.membership.forward with inside275      have gX := FunctionBetween.apply_closed with functionG, xMember276      have gXReal : Real(g(x)) by exact gX277      linarith278  have agreeBack : ∀ x ∈ ℝ, A < x → x < B → g(x) = f(x)279    intros x xMember lower upper280    have value := agree with x, xMember, lower, upper281    rw value282  refine ⟨function, ordered, ?_⟩283  intros E eMember positive284  have eReal : Real(E) by exact eMember285  have halfPositive := half.positive with positive286  have halfReal := half.real at E287  have quarterReal := half.real at half(E)288  have halves := half.add_self at E289  have quarters := half.add_self at half(E)290  obtain ⟨R, rMember, rPositive, estimate⟩ := control with half(E),291    halfReal, halfPositive292  have shifted : 0 < abs(f(A) - g(A)) + abs(f(B) - g(B)) + 1 by linarith293  have radius := scaledRadius.real at half(E),294    abs(f(A) - g(A)) + abs(f(B) - g(B)) + 1295  have radiusPositive := scaledRadius.positive with halfPositive, shifted296  have cancel := scaledRadius.cancel at half(E),297    abs(f(A) - g(A)) + abs(f(B) - g(B)) + 1 with shifted298  have rReal : Real(R) by exact rMember299  obtain ⟨D, dMember, dPositive, dR, dRadius⟩ := positiveRadius.common300    with rPositive, radiusPositive301  have dReal : Real(D) by exact dMember302  refine ⟨D, dMember, dPositive, ?_⟩303  intros p t N nMember tagged fine304  have nNat := nat.omega.backward with nMember305  have fineR := FinePartition.weaken with fine, dR306  have near := estimate with p, t, N, nMember, tagged, fineR307  have above := riemannSum.interior_difference with function, functionG,308    tagged, fine, boundFG, nonnegative, agree309  have below := riemannSum.interior_difference with functionG, function,310    tagged, fine, boundGF, nonnegative, agreeBack311  obtain ⟨⟨points, _, _, _⟩, tags, _⟩ := tagged312  have sumF := riemannSum.real at f, p, t, N with function, points, tags313  have sumG := riemannSum.real at g, p, t, N with functionG, points, tags314  unfold dist at near315  have nearUpper := abs.upper at riemannSum(g, p, t, N) - I316  have nearLower := abs.upper_neg at riemannSum(g, p, t, N) - I317  have distanceReal := abs.real at riemannSum(g, p, t, N) - I318  have 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, nonnegative321  have radiusNonnegative : (0:R) ≤ scaledRadius(half(E),322      abs(f(A) - g(A)) + abs(f(B) - g(B)) + 1) by linarith323  have unit : abs(f(A) - g(A)) + abs(f(B) - g(B)) ≤324      abs(f(A) - g(A)) + abs(f(B) - g(B)) + 1 by linarith325  have larger := realLe.mul_right_of_nonnegative326    at 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)329    with unit, radiusNonnegative330  apply 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)345proof346  have alternatives := Less.or_LessEqual at n, k347  cases alternatives with before | after348  . have value := RealSequence.value at u, n with first349    have member : u[n] ∈ ℝ by exact value350    refine ⟨u[n], member, ?_⟩351    left352    refine ⟨before, rfl⟩353  . have value := RealSequence.value at v, n with second354    have member : v[n] ∈ ℝ by exact value355    refine ⟨v[n], member, ?_⟩356    right357    refine ⟨after, rfl⟩358uniqueness359  intros X xMember xProperty Y yMember yProperty360  rcases xProperty with ⟨xBefore, xValue⟩ | ⟨xAfter, xValue⟩361  . rcases yProperty with ⟨yBefore, yValue⟩ | ⟨yAfter, yValue⟩362    . rw [xValue, yValue]363    . exfalso364      arith365  . rcases yProperty with ⟨yBefore, yValue⟩ | ⟨yAfter, yValue⟩366    . exfalso367      arith368    . rw [xValue, yValue]369370defgraph switchSequence (u v : Set, k : Nat) (n ∈ ℕ) : ℝ := switchValue(u, v, k, n)371requires sequenceU : RealSequence(u)372requires sequenceV : RealSequence(v)373proof374  have nNat := nat.omega.backward with member375  have selected := switchValue.selected at u, v, k, n with sequenceU, sequenceV376  exact selected.left377378theorem switchSequence.before (u v : Set, k n : Nat)379  requires sequenceU : RealSequence(u)380  requires sequenceV : RealSequence(v)381  requires earlier : n < k382  shows switchSequence(u, v, k)[n] = u[n]383proof384  have value := switchSequence.value at u, v, k, n with sequenceU, sequenceV385  obtain ⟨_, property⟩ := switchValue.selected at u, v, k, n with sequenceU, sequenceV386  rcases property with ⟨_, equal⟩ | ⟨later, _⟩387  . rw [value, equal]388  . exfalso389    arith390391theorem switchSequence.after (u v : Set, k n : Nat)392  requires sequenceU : RealSequence(u)393  requires sequenceV : RealSequence(v)394  requires later : k ≤ n395  shows switchSequence(u, v, k)[n] = v[n]396proof397  have value := switchSequence.value at u, v, k, n with sequenceU, sequenceV398  obtain ⟨_, property⟩ := switchValue.selected at u, v, k, n with sequenceU, sequenceV399  rcases property with ⟨earlier, _⟩ | ⟨_, equal⟩400  . exfalso401    arith402  . rw [value, equal]403404theorem switchSequence.sequence (u v : Set, k : Nat)405  requires sequenceU : RealSequence(u)406  requires sequenceV : RealSequence(v)407  shows RealSequence(switchSequence(u, v, k))408proof409  unfold RealSequence410  exact 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)415  requires tagged : TaggedPartition(p, t, A, B, succ(add(k, L)))416  requires fine : FinePartition(p, A, B, succ(add(k, L)), D)417  requires left : (p[k]:R) < C418  requires right : C < (p[succ(k)]:R)419  shows TaggedPartition(indexedInsertedPoints(p, C, k),420    switchSequence(t, constantSequence(C), k), A, C, succ(k))421proof422  have identityFunction := identity.function at ℝ423  obtain ⟨fineLeft, _⟩ := FinePartition.insert_indexed_split424    at identity(ℝ), p, A, B, C, D, k, L with identityFunction, fine, left,425    right426  obtain ⟨partition, _, _⟩ := fineLeft427  obtain ⟨⟨points, _, _, _⟩, tags, cells⟩ := tagged428  have constant := constantSequence.sequence at C429  have switched := switchSequence.sequence at t, constantSequence(C), k430    with tags, constant431  refine ⟨partition, switched, ?_⟩432  intros i iMember before433  have iNat := nat.omega.backward with iMember434  have atMost : i ≤ k by arith435  have lower := indexedInsertedPoints.before at p, C, k, i with points, atMost436  have alternatives := Less.or_LessEqual at i, k437  cases alternatives with earlier | later438  . have next : succ(i) ≤ k by arith439    have upper := indexedInsertedPoints.before at p, C, k, succ(i)440      with points, next441    have tag := switchSequence.before at t, constantSequence(C), k, i442      with tags, constant, earlier443    have inside : i < succ(add(k, L)) by arith444    have cell := cells with i, iMember, inside445    rw [lower, upper, tag]446    exact cell447  . have same : i = k by arith448    have tag := switchSequence.after at t, constantSequence(C), k, i449      with tags, constant, later450    have value := constantSequence.value at C, i451    have upper := indexedInsertedPoints.inserted at p, C, k with points452    have cReal : Real(C) by typecheck453    have cMember : C ∈ ℝ by typecheck454    have pReal := RealSequence.value at p, k with points455    have below : (p[k]:R) ≤ C by linarith456    have reflexive := realLe.reflexive at C457    rw [lower, tag, value, same, upper]458    exact 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)463  requires tagged : TaggedPartition(p, t, A, B, succ(add(k, L)))464  requires fine : FinePartition(p, A, B, succ(add(k, L)), D)465  requires left : (p[k]:R) < C466  requires right : C < (p[succ(k)]:R)467  shows TaggedPartition(468    shiftedRealSequence(indexedInsertedPoints(p, C, k), succ(k)),469    shiftedRealSequence(switchSequence(constantSequence(C), t, succ(k)), k),470    C, B, succ(L))471proof472  have identityFunction := identity.function at ℝ473  obtain ⟨_, fineRight, _⟩ := FinePartition.insert_indexed_split474    at identity(ℝ), p, A, B, C, D, k, L with identityFunction, fine, left,475    right476  obtain ⟨partition, _, _⟩ := fineRight477  obtain ⟨⟨points, _, _, _⟩, tags, cells⟩ := tagged478  have constant := constantSequence.sequence at C479  have switched := switchSequence.sequence at constantSequence(C), t,480    succ(k) with constant, tags481  have shifted := shiftedRealSequence.sequence482    at switchSequence(constantSequence(C), t, succ(k)), k with switched483  have inserted := indexedInsertedPoints.sequence at p, C, k with points484  refine ⟨partition, shifted, ?_⟩485  intros j jMember before486  have jNat := nat.omega.backward with jMember487  have tag := shiftedRealSequence.value488    at switchSequence(constantSequence(C), t, succ(k)), k, j with switched489  have lower := shiftedRealSequence.value490    at indexedInsertedPoints(p, C, k), succ(k), j with inserted491  have upper := shiftedRealSequence.value492    at indexedInsertedPoints(p, C, k), succ(k), succ(j) with inserted493  rw [tag, lower, upper]494  have alternatives := natural.zero_or_successor at j495  cases alternatives with zero | successor496  . -- the new cell [C, pₖ₊₁]497    have first : add(k, j) < succ(k) by arith498    have value := switchSequence.before at constantSequence(C), t, succ(k),499      add(k, j) with constant, tags, first500    have constantValue := constantSequence.value at C, add(k, j)501    have atStart : add(succ(k), j) = succ(k) by arith502    have atNext : add(succ(k), succ(j)) = succ(succ(add(k, 0))) by arith503    have startValue := indexedInsertedPoints.inserted at p, C, k with points504    have nextValue := indexedInsertedPoints.after at p, C, k, 0 with points505    have shape : succ(add(k, 0)) = succ(k) by arith506    rw [value, constantValue, atStart, atNext, startValue, nextValue, shape]507    have cReal : Real(C) by typecheck508    have cMember : C ∈ ℝ by typecheck509    have pReal := RealSequence.value at p, succ(k) with points510    have above : C ≤ (p[succ(k)]:R) by linarith511    have reflexive := realLe.reflexive at C512    exact closedInterval.membership.backward with ⟨cMember, reflexive, above⟩513  . -- an old cell, shifted by one514    obtain ⟨i, iNat, shape⟩ := successor515    have later : succ(k) ≤ add(k, j) by arith516    have value := switchSequence.after at constantSequence(C), t, succ(k),517      add(k, j) with constant, tags, later518    have atStart : add(succ(k), j) = succ(succ(add(k, i))) by arith519    have atNext : add(succ(k), succ(j)) = succ(succ(add(k, j))) by arith520    have startValue := indexedInsertedPoints.after at p, C, k, i with points521    have nextValue := indexedInsertedPoints.after at p, C, k, j with points522    have index : succ(add(k, i)) = add(k, j) by arith523    rw [value, atStart, atNext, startValue, nextValue, index]524    have inside : add(k, j) < succ(add(k, L)) by arith525    have member : add(k, j) ∈ ℕ by typecheck526    exact 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)531  requires function : f : ℝ → ℝ532  requires tagged : TaggedPartition(p, t, A, B, succ(add(k, L)))533  shows 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])540proof541  obtain ⟨⟨points, _, _, _⟩, tags, _⟩ := tagged542  have cReal : Real(C) by typecheck543  have cMember : C ∈ ℝ by typecheck544  have constant := constantSequence.sequence at C545  have inserted := indexedInsertedPoints.sequence at p, C, k with points546  have leftTags := switchSequence.sequence at t, constantSequence(C), k547    with tags, constant548  have switched := switchSequence.sequence at constantSequence(C), t,549    succ(k) with constant, tags550  have rightPoints := shiftedRealSequence.sequence551    at indexedInsertedPoints(p, C, k), succ(k) with inserted552  have rightTags := shiftedRealSequence.sequence553    at switchSequence(constantSequence(C), t, succ(k)), k with switched554  -- The sum over p splits after cell k.555  have whole := riemannSum.split_at at f, p, t, succ(k), L556    with function, points, tags557  have count : add(succ(k), L) = succ(add(k, L)) by arith558  rw count at whole559  have 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].561  have 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]565    intros i iMember before566    have iNat := nat.omega.backward with iMember567    have atMost : i ≤ k by arith568    have next : succ(i) ≤ k by arith569    have lower := indexedInsertedPoints.before at p, C, k, i with points, atMost570    have upper := indexedInsertedPoints.before at p, C, k, succ(i)571      with points, next572    have tag := switchSequence.before at t, constantSequence(C), k, i573      with tags, constant, before574    refine ⟨lower, upper, tag⟩575  have prefixSame := riemannSum.congr_cells576    at f, indexedInsertedPoints(p, C, k), switchSequence(t, constantSequence(C),577      k), p, t, k with function, inserted, points, leftTags, tags, cellsLeft578  have leftStep := riemannSum.succ at f, indexedInsertedPoints(p, C, k),579    switchSequence(t, constantSequence(C), k), k with function, inserted,580    leftTags581  have reflexive : k ≤ k by arith582  have leftTag := switchSequence.after at t, constantSequence(C), k, k583    with tags, constant, reflexive584  have constantK := constantSequence.value at C, k585  have leftEnd := indexedInsertedPoints.inserted at p, C, k with points586  have leftStart := indexedInsertedPoints.before at p, C, k, k587    with points, reflexive588  rw [leftTag, constantK, leftEnd, leftStart, prefixSame] at leftStep589  -- The right sum: the cell [C, pₖ₊₁], then the later cells of p.590  have 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, rightTags594  have rightCount : add(succ(0), L) = succ(L) by arith595  rw rightCount at rightSplit596  have rightFirst := riemannSum.succ at f,597    shiftedRealSequence(indexedInsertedPoints(p, C, k), succ(k)),598    shiftedRealSequence(switchSequence(constantSequence(C), t, succ(k)), k), 0599    with function, rightPoints, rightTags600  have tagZero := shiftedRealSequence.value601    at switchSequence(constantSequence(C), t, succ(k)), k, 0 with switched602  have firstIndex : add(k, 0) < succ(k) by arith603  have switchZero := switchSequence.before at constantSequence(C), t, succ(k),604    add(k, 0) with constant, tags, firstIndex605  have constantZero := constantSequence.value at C, add(k, 0)606  have pointZero := shiftedRealSequence.value607    at indexedInsertedPoints(p, C, k), succ(k), 0 with inserted608  have pointOne := shiftedRealSequence.value609    at indexedInsertedPoints(p, C, k), succ(k), succ(0) with inserted610  have zeroIndex : add(succ(k), 0) = succ(k) by arith611  have oneIndex : add(succ(k), succ(0)) = succ(succ(add(k, 0))) by arith612  have nextValue := indexedInsertedPoints.after at p, C, k, 0 with points613  have shape : succ(add(k, 0)) = succ(k) by arith614  rw [tagZero, switchZero, constantZero, pointZero, pointOne, zeroIndex,615    oneIndex, nextValue, shape, leftEnd, riemannSum.zero] at rightFirst616  have 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]625    intros i iMember before626    have iNat := nat.omega.backward with iMember627    have outerLower := shiftedRealSequence.value628      at shiftedRealSequence(indexedInsertedPoints(p, C, k), succ(k)),629        succ(0), i with rightPoints630    have outerUpper := shiftedRealSequence.value631      at shiftedRealSequence(indexedInsertedPoints(p, C, k), succ(k)),632        succ(0), succ(i) with rightPoints633    have innerLower := shiftedRealSequence.value634      at indexedInsertedPoints(p, C, k), succ(k), add(succ(0), i) with inserted635    have innerUpper := shiftedRealSequence.value636      at indexedInsertedPoints(p, C, k), succ(k), add(succ(0), succ(i))637      with inserted638    have lowerIndex : add(succ(k), add(succ(0), i)) = succ(succ(add(k, i)))639      by arith640    have upperIndex : add(succ(k), add(succ(0), succ(i))) =641      succ(succ(add(k, succ(i)))) by arith642    have lowerValue := indexedInsertedPoints.after at p, C, k, i with points643    have upperValue := indexedInsertedPoints.after at p, C, k, succ(i)644      with points645    have pLower := shiftedRealSequence.value at p, succ(k), i with points646    have pUpper := shiftedRealSequence.value at p, succ(k), succ(i)647      with points648    have pLowerIndex : add(succ(k), i) = succ(add(k, i)) by arith649    have pUpperIndex : add(succ(k), succ(i)) = succ(add(k, succ(i))) by arith650    have outerTag := shiftedRealSequence.value651      at shiftedRealSequence(switchSequence(constantSequence(C), t, succ(k)),652        k), succ(0), i with rightTags653    have innerTag := shiftedRealSequence.value654      at switchSequence(constantSequence(C), t, succ(k)), k, add(succ(0), i)655      with switched656    have later : succ(k) ≤ add(k, add(succ(0), i)) by arith657    have switchTag := switchSequence.after at constantSequence(C), t, succ(k),658      add(k, add(succ(0), i)) with constant, tags, later659    have tTag := shiftedRealSequence.value at t, succ(k), i with tags660    have tagIndex : add(k, add(succ(0), i)) = add(succ(k), i) by arith661    rw [outerLower, outerUpper, innerLower, innerUpper, lowerIndex, upperIndex,662      lowerValue, upperValue, pLower, pUpper, pLowerIndex, pUpperIndex,663      outerTag, innerTag, switchTag, tTag, tagIndex]664    refine ⟨rfl, rfl, rfl⟩665  have pShift := shiftedRealSequence.sequence at p, succ(k) with points666  have tShift := shiftedRealSequence.sequence at t, succ(k) with tags667  have rightShiftPoints := shiftedRealSequence.sequence668    at shiftedRealSequence(indexedInsertedPoints(p, C, k), succ(k)), succ(0)669    with rightPoints670  have rightShiftTags := shiftedRealSequence.sequence671    at shiftedRealSequence(switchSequence(constantSequence(C), t, succ(k)), k),672      succ(0) with rightTags673  have 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)), L679    with function, rightShiftPoints, pShift, rightShiftTags, tShift, cellsRight680  rw [tailSame, rightFirst] at rightSplit681  -- Collect the pieces.682  have tagReal := RealSequence.value at t, k with tags683  have tagMember : t[k] ∈ ℝ by exact tagReal684  have fTag := FunctionBetween.apply_closed with function, tagMember685  have fTagReal : Real(f(t[k])) by exact fTag686  have fC := FunctionBetween.apply_closed with function, cMember687  have fCReal : Real(f(C)) by exact fC688  have pK := RealSequence.value at p, k with points689  have pNext := RealSequence.value at p, succ(k) with points690  have prefixReal := riemannSum.real at f, p, t, k with function, points, tags691  have tailReal := riemannSum.real at f, shiftedRealSequence(p, succ(k)),692    shiftedRealSequence(t, succ(k)), L with function, pShift, tShift693  rw [whole, firstK, leftStep, rightSplit]694  ring695696-- 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)700  requires function : f : ℝ → ℝ701  requires tagged : TaggedPartition(p, t, A, B, N)702  requires fine : FinePartition(p, A, B, N, D)703  requires left : A ≤ C704  requires right : C ≤ B705  requires bounded : ∀ x ∈ closedInterval(A, B), abs(f(x)) ≤ M706  shows ∃ 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)713proof714  have taggedCopy := tagged715  have fineCopy := fine716  obtain ⟨partition, tags, cells⟩ := tagged717  obtain ⟨points, start, finish, increasing⟩ := partition718  obtain ⟨_, dPositive, widths⟩ := fine719  have ordered : A ≤ B by linarith720  have aMember : A ∈ ℝ by typecheck721  have reflexiveA := realLe.reflexive at A722  have aInside := closedInterval.membership.backward723    with ⟨aMember, reflexiveA, ordered⟩724  have atA := bounded with A, aInside725  have fA := FunctionBetween.apply_closed with function, aMember726  have fAReal : Real(f(A)) by exact fA727  have absNonnegative := abs.nonnegative at f(A)728  have mNonnegative : 0 ≤ M by linarith729  have doubleNonnegative : 0 ≤ M + M by linarith730  have dNonnegative : (0:R) ≤ D by linarith731  have zeroProduct := realLe.mul_left_of_nonnegative at (0:R), D, M + M732    with dNonnegative, doubleNonnegative733  have zeroValue : (M + M) * (0:R) = (0:R) by ring734  have location : (∃ i ∈ ℕ, i ≤ N ∧ p[i] = C) ∨735      (∃ k ∈ ℕ, k < N ∧ (p[k]:R) < C ∧ C < (p[succ(k)]:R))736    have first := realLess.trichotomy at A, C737    rcases first with inner | same | outer738    . have second := realLess.trichotomy at C, B739      rcases second with inside | atB | beyond740      . exact IntervalPartition.locate_interior741          with ⟨points, start, finish, increasing⟩, inner, inside742      . left743        have nMember : N ∈ ℕ by typecheck744        have reflexive : N ≤ N by arith745        rw atB746        refine ⟨N, nMember, reflexive, finish⟩747      . exfalso748        linarith749    . left750      have zeroMember : 0 ∈ ℕ by typecheck751      have zeroLe : 0 ≤ N by arith752      rw ← same753      refine ⟨0, zeroMember, zeroLe, start⟩754    . exfalso755      linarith756  cases location with vertex | interior757  . obtain ⟨i, iMember, bound, value⟩ := vertex758    have iNat := nat.omega.backward with iMember759    obtain ⟨j, jMember, _, fineL, fineR, taggedL, taggedR, sum⟩ :=760      FinePartition.split_at_vertex with function, fineCopy, taggedCopy, bound,761        value762    have shiftedPoints := shiftedRealSequence.sequence at p, i with points763    have shiftedTags := shiftedRealSequence.sequence at t, i with tags764    have jNat := nat.omega.backward with jMember765    have sumL := riemannSum.real at f, p, t, i with function, points, tags766    have sumR := riemannSum.real at f, shiftedRealSequence(p, i),767      shiftedRealSequence(t, i), j with function, shiftedPoints, shiftedTags768    refine ⟨p, t, i, iMember, shiftedRealSequence(p, i),769      shiftedRealSequence(t, i), j, jMember, taggedL, fineL, taggedR, fineR,770      ?_, ?_⟩771    . rw sum772      linarith773    . rw sum774      linarith775  obtain ⟨k, kMember, before, lower, upper⟩ := interior776  have kNat := nat.omega.backward with kMember777  obtain ⟨L, lNat, length⟩ := before778  have shape : N = succ(add(k, L)) by arith779  rw shape at taggedCopy, fineCopy780  have taggedL := TaggedPartition.insert_left with taggedCopy, fineCopy, lower,781    upper782  have taggedR := TaggedPartition.insert_right with taggedCopy, fineCopy,783    lower, upper784  obtain ⟨fineL, fineR, _⟩ := FinePartition.insert_indexed_split785    with function, fineCopy, lower, upper786  have sum := riemannSum.insert_split at f, p, t, A, B, C, k, L787    with function, taggedCopy788  have kSucc : succ(k) ∈ ℕ by typecheck789  have lSucc : succ(L) ∈ ℕ by typecheck790  have cReal : Real(C) by typecheck791  have constant := constantSequence.sequence at C792  have inserted := indexedInsertedPoints.sequence at p, C, k with points793  have leftTags := switchSequence.sequence at t, constantSequence(C), k794    with tags, constant795  have switched := switchSequence.sequence at constantSequence(C), t,796    succ(k) with constant, tags797  have rightPoints := shiftedRealSequence.sequence798    at indexedInsertedPoints(p, C, k), succ(k) with inserted799  have rightTags := shiftedRealSequence.sequence800    at switchSequence(constantSequence(C), t, succ(k)), k with switched801  have sumL := riemannSum.real at f, indexedInsertedPoints(p, C, k),802    switchSequence(t, constantSequence(C), k), succ(k)803    with function, inserted, leftTags804  have 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, rightTags808  refine ⟨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.814  have kBefore : k < N by arith815  have kLe : k ≤ N by arith816  have succLe : succ(k) ≤ N by arith817  obtain ⟨tagLower, tagUpper⟩ := TaggedPartition.tag_bounds at p, t, A, B, N,818    k with ⟨⟨points, start, finish, increasing⟩, tags, cells⟩, kBefore819  obtain ⟨atLeast, _⟩ := IntervalPartition.point_bounds at p, A, B, N, k820    with ⟨points, start, finish, increasing⟩, kLe821  obtain ⟨_, atMost⟩ := IntervalPartition.point_bounds at p, A, B, N,822    succ(k) with ⟨points, start, finish, increasing⟩, succLe823  have tagReal := RealSequence.value at t, k with tags824  have tagMember : t[k] ∈ ℝ by exact tagReal825  have cMember : C ∈ ℝ by typecheck826  have pK := RealSequence.value at p, k with points827  have pNext := RealSequence.value at p, succ(k) with points828  have tagInside : t[k] ∈ closedInterval(A, B)829    apply closedInterval.membership.backward830    refine ⟨tagMember, ?_, ?_⟩831    . linarith832    . linarith833  have cInside : C ∈ closedInterval(A, B)834    apply closedInterval.membership.backward835    refine ⟨cMember, ?_, ?_⟩836    . linarith837    . linarith838  have tagBound := bounded with t[k], tagInside839  have cBound := bounded with C, cInside840  have fTag := FunctionBetween.apply_closed with function, tagMember841  have fTagReal : Real(f(t[k])) by exact fTag842  have fC := FunctionBetween.apply_closed with function, cMember843  have fCReal : Real(f(C)) by exact fC844  have tagUpperValue := abs.upper at f(t[k])845  have tagLowerValue := abs.upper_neg at f(t[k])846  have cUpperValue := abs.upper at f(C)847  have cLowerValue := abs.upper_neg at f(C)848  have width := widths with k, kMember, kBefore849  have widthNonnegative : (0:R) ≤ p[succ(k)] - p[k] by linarith850  have widthLe : p[succ(k)] - p[k] ≤ D by linarith851  have gapUpper : f(t[k]) - f(C) ≤ M + M by linarith852  have gapLower : -(M + M) ≤ f(t[k]) - f(C) by linarith853  have scaledUpper := realLe.mul_right_of_nonnegative854    at f(t[k]) - f(C), M + M, p[succ(k)] - p[k]855    with gapUpper, widthNonnegative856  have scaledLower := realLe.mul_right_of_nonnegative857    at -(M + M), f(t[k]) - f(C), p[succ(k)] - p[k]858    with gapLower, widthNonnegative859  have capped := realLe.mul_left_of_nonnegative860    at p[succ(k)] - p[k], D, M + M with widthLe, doubleNonnegative861  have flip : -(M + M) * (p[succ(k)] - p[k]) =862    -((M + M) * (p[succ(k)] - p[k])) by ring863  rw flip at scaledLower864  rw shape865  rw sum866  constructor867  . linarith868  . linarith869870-- Integrals of a bounded function over adjacent intervals add.871theorem HasRiemannIntegral.add_adjacent (f : Set) (A C B IL IR M : Real)872  requires integralL : HasRiemannIntegral(f, A, C, IL)873  requires integralR : HasRiemannIntegral(f, C, B, IR)874  requires bounded : ∀ x ∈ closedInterval(A, B), abs(f(x)) ≤ M875  shows HasRiemannIntegral(f, A, B, IL + IR)876proof877  obtain ⟨function, left, controlL⟩ := integralL878  obtain ⟨_, right, controlR⟩ := integralR879  have ordered : A ≤ B by linarith880  have aMember : A ∈ ℝ by typecheck881  have reflexiveA := realLe.reflexive at A882  have aInside := closedInterval.membership.backward883    with ⟨aMember, reflexiveA, ordered⟩884  have atA := bounded with A, aInside885  have fA := FunctionBetween.apply_closed with function, aMember886  have fAReal : Real(f(A)) by exact fA887  have absNonnegative := abs.nonnegative at f(A)888  have doubleNonnegative : 0 ≤ M + M by linarith889  refine ⟨function, ordered, ?_⟩890  intros E eMember positive891  have eReal : Real(E) by exact eMember892  have halfPositive := half.positive with positive893  have halfReal := half.real at E894  have quarterPositive := half.positive with halfPositive895  have quarterReal := half.real at half(E)896  have quarterMember : half(half(E)) ∈ ℝ by typecheck897  have halves := half.add_self at E898  have quarters := half.add_self at half(E)899  obtain ⟨DL, dlMember, dlPositive, estimateL⟩ := controlL with half(half(E)),900    quarterMember, quarterPositive901  obtain ⟨DR, drMember, drPositive, estimateR⟩ := controlR with half(half(E)),902    quarterMember, quarterPositive903  have shifted : 0 < M + M + 1 by linarith904  have radius := scaledRadius.real at half(E), M + M + 1905  have radiusPositive := scaledRadius.positive with halfPositive, shifted906  have cancel := scaledRadius.cancel at half(E), M + M + 1 with shifted907  obtain ⟨D, dMember, dPositive, dL, dR, dRadius⟩ := positiveMesh.common_three908    with dlPositive, drPositive, radiusPositive909  have dReal : Real(D) by exact dMember910  refine ⟨D, dMember, dPositive, ?_⟩911  intros p t N nMember tagged fine912  obtain ⟨q, s, K, kMember, u, v, J, jMember, taggedL, fineL, taggedR, fineR,913    upper, lower⟩ := TaggedPartition.split_bounded with function, tagged, fine,914    left, right, bounded915  have fineLocalL := FinePartition.weaken with fineL, dL916  have fineLocalR := FinePartition.weaken with fineR, dR917  have nearL := estimateL with q, s, K, kMember, taggedL, fineLocalL918  have nearR := estimateR with u, v, J, jMember, taggedR, fineLocalR919  have nNat := nat.omega.backward with nMember920  have kNat := nat.omega.backward with kMember921  have jNat := nat.omega.backward with jMember922  obtain ⟨⟨points, _, _, _⟩, tags, _⟩ := tagged923  obtain ⟨⟨pointsL, _, _, _⟩, tagsL, _⟩ := taggedL924  obtain ⟨⟨pointsR, _, _, _⟩, tagsR, _⟩ := taggedR925  have sum := riemannSum.real at f, p, t, N with function, points, tags926  have sumL := riemannSum.real at f, q, s, K with function, pointsL, tagsL927  have sumR := riemannSum.real at f, u, v, J with function, pointsR, tagsR928  unfold dist at nearL, nearR929  have upperL := abs.upper at riemannSum(f, q, s, K) - IL930  have lowerL := abs.upper_neg at riemannSum(f, q, s, K) - IL931  have upperR := abs.upper at riemannSum(f, u, v, J) - IR932  have lowerR := abs.upper_neg at riemannSum(f, u, v, J) - IR933  have absL := abs.real at riemannSum(f, q, s, K) - IL934  have absR := abs.real at riemannSum(f, u, v, J) - IR935  have smaller := realLe.mul_left_of_nonnegative at D,936    scaledRadius(half(E), M + M + 1), M + M with dRadius, doubleNonnegative937  have radiusNonnegative : (0:R) ≤ scaledRadius(half(E), M + M + 1)938    by linarith939  have unit : M + M ≤ M + M + 1 by linarith940  have larger := realLe.mul_right_of_nonnegative941    at M + M, M + M + 1, scaledRadius(half(E), M + M + 1)942    with unit, radiusNonnegative943  apply 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)951  requires integrableL : RiemannIntegrable(f, A, C)952  requires integrableR : RiemannIntegrable(f, C, B)953  requires bounded : ∀ x ∈ closedInterval(A, B), abs(f(x)) ≤ M954  shows RiemannIntegrable(f, A, B)955proof956  obtain ⟨IL, lMember, integralL⟩ := integrableL957  obtain ⟨IR, rMember, integralR⟩ := integrableR958  have lReal : Real(IL) by exact lMember959  have rReal : Real(IR) by exact rMember960  have sum := HasRiemannIntegral.add_adjacent with integralL, integralR,961    bounded962  have member : IL + IR ∈ ℝ by typecheck963  exact ⟨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)970  requires function : f : ℝ → ℝ971  requires ordered : A ≤ B972  requires continuous : ContinuousOn(g, closedInterval(A, B))973  requires agree : ∀ x ∈ ℝ, A < x → x < B → f(x) = g(x)974  shows HasRiemannIntegral(f, A, B, riemannIntegral(g, A, B))975proof976  have integrable := ContinuousOn.integrable_interval with ordered, continuous977  have integral := riemannIntegral.integral with integrable978  exact 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)983  requires function : f : ℝ → ℝ984  requires partition : IntervalPartition(p, A, B, N)985  requires values : RealSequence(I)986  requires bounded : ∀ x ∈ closedInterval(A, B), abs(f(x)) ≤ M987  requires cells : ∀ i ∈ ℕ, i < N → HasRiemannIntegral(f, p[i], p[succ(i)], I[i])988  shows n ≤ N → HasRiemannIntegral(f, A, p[n], ∑ i < n, I(i))989proof990  induction n991  | zero =>992    intro bound993    obtain ⟨_, start, _, _⟩ := partition994    have point := HasRiemannIntegral.point at f, A with function995    rw [start, realPartialSum.zero]996    exact point997  | succ(m) using ih =>998    intro bound999    have mLe : m ≤ N by arith1000    have before : m < N by arith1001    have previous := ih with mLe1002    have mMember : m ∈ ℕ by typecheck1003    have cell := cells with m, mMember, before1004    obtain ⟨_, atMost⟩ := IntervalPartition.point_bounds at p, A, B, N,1005      succ(m) with partition, bound1006    obtain ⟨points, _, _, _⟩ := partition1007    have pNext := RealSequence.value at p, succ(m) with points1008    have aReal : Real(A) by typecheck1009    have reflexive := realLe.reflexive at A1010    have nested := closedInterval.nested at A, p[succ(m)], A, B1011      with reflexive, atMost1012    have smaller : ∀ x ∈ closedInterval(A, p[succ(m)]), abs(f(x)) ≤ M1013      intros x inside1014      have wider := nested with x, inside1015      exact bounded with x, wider1016    have sum := HasRiemannIntegral.add_adjacent with previous, cell, smaller1017    have prefixReal := realPartialSum.real at I, m with values1018    have term := RealSequence.value at I, m with values1019    have bridge : realAddValue(∑ i < m, I(i), I(m)) = (∑ i < m, I(i)) + I(m)1020      by ring1021    rw [realPartialSum.succ, bridge]1022    exact 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)1026  requires function : f : ℝ → ℝ1027  requires partition : IntervalPartition(p, A, B, N)1028  requires values : RealSequence(I)1029  requires bounded : ∀ x ∈ closedInterval(A, B), abs(f(x)) ≤ M1030  requires cells : ∀ i ∈ ℕ, i < N → HasRiemannIntegral(f, p[i], p[succ(i)], I[i])1031  shows HasRiemannIntegral(f, A, B, ∑ i < N, I(i))1032proof1033  have prefix := HasRiemannIntegral.partition_prefix at f, p, I, A, B, M, N, N1034    with function, partition, values, bounded, cells1035  have reflexive : N ≤ N by arith1036  have whole := prefix with reflexive1037  obtain ⟨_, _, 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)1044proof1045  have iNat := nat.omega.backward with member1046  have left := RealSequence.value at p, i with points1047  have right := RealSequence.value at p, succ(i) with points1048  have selected := riemannIntegral.selected at f, p[i], p[succ(i)]1049  exact 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)1053  requires function : f : ℝ → ℝ1054  requires partition : IntervalPartition(p, A, B, N)1055  requires bounded : ∀ x ∈ closedInterval(A, B), abs(f(x)) ≤ M1056  requires cells : ∀ i ∈ ℕ, i < N → RiemannIntegrable(f, p[i], p[succ(i)])1057  shows RiemannIntegrable(f, A, B)1058proof1059  have points : RealSequence(p)1060    obtain ⟨points, _, _, _⟩ := partition1061    exact points1062  have values : RealSequence(cellIntegrals(f, p))1063    unfold RealSequence1064    exact cellIntegrals.function with points1065  have integrals : ∀ i ∈ ℕ, i < N →1066      HasRiemannIntegral(f, p[i], p[succ(i)], cellIntegrals(f, p)[i])1067    intros i iMember before1068    have iNat := nat.omega.backward with iMember1069    have left := RealSequence.value at p, i with points1070    have right := RealSequence.value at p, succ(i) with points1071    have value := cellIntegrals.value at f, p, i with points1072    have integrable := cells with i, iMember, before1073    rw value1074    exact riemannIntegral.integral with integrable1075  have whole := HasRiemannIntegral.of_partition with function, partition,1076    values, bounded, integrals1077  have sum := realPartialSum.real at cellIntegrals(f, p), N with values1078  have member : (∑ i < N, cellIntegrals(f, p)(i)) ∈ ℝ by exact sum1079  exact ⟨∑ 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)1084  requires function : f : ℝ → ℝ1085  requires partition : IntervalPartition(p, A, B, N)1086  requires bounded : ∀ x ∈ closedInterval(A, B), abs(f(x)) ≤ M1087  requires 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))1090  shows RiemannIntegrable(f, A, B)1091proof1092  have integrable : ∀ i ∈ ℕ, i < N → RiemannIntegrable(f, p[i], p[succ(i)])1093    intros i iMember before1094    obtain ⟨g, continuous, agree⟩ := cells with i, iMember, before1095    obtain ⟨points, _, _, increasing⟩ := partition1096    have strict := increasing with i, iMember, before1097    have ordered : p[i] ≤ p[succ(i)] by linarith1098    have integral := HasRiemannIntegral.of_continuous_interior1099      with function, ordered, continuous, agree1100    have left := RealSequence.value at p, i with points1101    have right := RealSequence.value at p, succ(i) with points1102    obtain ⟨member, _⟩ := riemannIntegral.selected at g, p[i], p[succ(i)]1103    exact ⟨riemannIntegral(g, p[i], p[succ(i)]), member, integral⟩1104  exact RiemannIntegrable.of_partition with function, partition, bounded,1105    integrable1106