Grace/Cumulus/Nat/BigOLemmas.g17

Open in Workbench Download source

Imports

BigO · PowLemmas

Declarations

Source

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)14  requires seqF : f : ℕ → ℕ15  requires bound : NatBigO(f, g)16  shows f ∈ bigO(g)17proof18  have both : (f : ℕ → ℕ) ∧ NatBigO(f, g) := ⟨seqF, bound⟩19  exact bigO.mem_iff.backward with both2021-- A member of the class has the bound.22theorem bigO.bound (f g : Set)23  requires member : f ∈ bigO(g)24  shows NatBigO(f, g)25proof26  obtain ⟨seqF, bound⟩ := bigO.mem_iff.forward with member27  exact bound2829-- Section: Powers30-- The pointwise power n ↦ f(n) ^ i.31defgraph powNatSequence (f : Set, i : Nat) (n ∈ ℕ) : ℕ := f(n) ^ i32requires seqF : f : ℕ → ℕ33proof34  have fn := FunctionBetween.apply_closed at f, ℕ, ℕ, n with seqF, member35  have fNat := nat.omega.backward with fn36  have closed := pow.closed at f(n), i37  typecheck3839-- 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)42  requires seqF : f : ℕ → ℕ43  requires seqG : g : ℕ → ℕ44  requires bound : NatBigO(f, g)45  shows NatBigO(powNatSequence(f, i), powNatSequence(g, i))46proof47  obtain ⟨C, CMember, N, NMember, below⟩ := bound48  have CNat := nat.omega.backward with CMember49  have closed := pow.closed at C, i50  have powerMember : C ^ i ∈ ℕ by typecheck51  refine ⟨C ^ i, powerMember, N, NMember, ?_⟩52  intros n member large53  have fn := FunctionBetween.apply_closed at f, ℕ, ℕ, n with seqF, member54  have gn := FunctionBetween.apply_closed at g, ℕ, ℕ, n with seqG, member55  have fNat := nat.omega.backward with fn56  have gNat := nat.omega.backward with gn57  have fBound := below with member, large58  have fValue := powNatSequence.value at f, i, n with seqF, member59  have gValue := powNatSequence.value at g, i, n with seqG, member60  rw [fValue, gValue]61  have grows := pow.base_monotone at f(n), C * g(n), i with fBound62  have split := pow.mul_base at C, g(n), i63  rwa split at 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)69  requires seqT : t : ℕ → ℕ70  requires seqP : p : ℕ → ℕ71  requires grows : ∀ n ∈ ℕ, n ≤ p(n)72  requires linear : identity(ℕ) ∈ bigO(t)73  shows p ∈ bigO(compose(t, p, ℕ, ℕ, ℕ))74proof75  have seqI := identity.function at ℕ76  have linearBound := bigO.bound at identity(ℕ), t with linear77  have padBound := NatBigO.compose at identity(ℕ), t, p with78    seqI, seqT, seqP, grows, linearBound79  have padIdentity := compose.identity_left at p, ℕ, ℕ with seqP80  rw padIdentity at padBound81  exact bigO.intro at p, compose(t, p, ℕ, ℕ, ℕ) with seqP, padBound82