1-- Big-O for sequences of naturals: membership, powers and paddings2-- Part of Cumulus, the Grace17 mathematics library.3-- Copyright (c) 2026 Grant Passmore.4-- Released under the Apache License 2.0. See LICENSE in the repository root.5-- Authors: András Salamon67module Grace.Cumulus.Nat.BigOLemmas8import BigO9import PowLemmas1011-- Section: Membership12-- A function ℕ → ℕ with a big-O bound is a member of the class.13theorem bigO.intro (f g : Set)14requires seqF : f : ℕ → ℕ15requires bound : NatBigO(f, g)16shows f ∈ bigO(g)17proof18have both : (f : ℕ → ℕ) ∧ NatBigO(f, g) :=⟨seqF, bound⟩19exact bigO.mem_iff.backward with both2021-- A member of the class has the bound.22theorem bigO.bound (f g : Set)23requires member : f ∈ bigO(g)24shows NatBigO(f, g)25proof26obtain⟨seqF, bound⟩:= bigO.mem_iff.forward with member27exact bound2829-- Section: Powers30-- The pointwise power n ↦ f(n) ^ i.31defgraph powNatSequence (f : Set, i : Nat) (n ∈ ℕ) : ℕ := f(n) ^ i32requires seqF : f : ℕ → ℕ33proof34have fn := FunctionBetween.apply_closed at f, ℕ, ℕ, n with seqF, member35have fNat := nat.omega.backward with fn36have closed := pow.closed at f(n), i37typecheck3839-- Powers keep the order: f ∈ O(g) gives f ^ i ∈ O(g ^ i), with constant40-- C ^ i.41theorem NatBigO.pow (f g : Set, i : Nat)42requires seqF : f : ℕ → ℕ43requires seqG : g : ℕ → ℕ44requires bound : NatBigO(f, g)45shows NatBigO(powNatSequence(f, i), powNatSequence(g, i))46proof47obtain⟨C, CMember, N, NMember, below⟩:= bound48have CNat := nat.omega.backward with CMember49have closed := pow.closed at C, i50have powerMember : C ^ i ∈ ℕ bytypecheck51refine⟨C ^ i, powerMember, N, NMember, ?_⟩52intros n member large53have fn := FunctionBetween.apply_closed at f, ℕ, ℕ, n with seqF, member54have gn := FunctionBetween.apply_closed at g, ℕ, ℕ, n with seqG, member55have fNat := nat.omega.backward with fn56have gNat := nat.omega.backward with gn57have fBound := below with member, large58have fValue := powNatSequence.value at f, i, n with seqF, member59have gValue := powNatSequence.value at g, i, n with seqG, member60rw [fValue, gValue]61have grows := pow.base_monotone at f(n), C * g(n), i with fBound62havesplit:= pow.mul_base at C, g(n), i63 rwa splitat grows6465-- Section: Paddings66-- A padding stays within the order of the padded bound: if n ∈ O(t) and67-- n ≤ p(n), then p ∈ O(t ∘ p).68theorem bigO.pad (t p : Set)69requires seqT : t : ℕ → ℕ70requires seqP : p : ℕ → ℕ71requires grows : ∀ n ∈ ℕ, n ≤ p(n)72requires linear : identity(ℕ) ∈ bigO(t)73shows p ∈ bigO(compose(t, p, ℕ, ℕ, ℕ))74proof75have seqI := identity.function at ℕ76have linearBound := bigO.bound at identity(ℕ), t with linear77have padBound := NatBigO.compose at identity(ℕ), t, p with78 seqI, seqT, seqP, grows, linearBound79have padIdentity := compose.identity_left at p, ℕ, ℕ with seqP80rw padIdentity at padBound81exact bigO.intro at p, compose(t, p, ℕ, ℕ, ℕ) with seqP, padBound82