# PvsNP/Basic/Poly.lean @ 4186ab2739015c0efa3783c76b4987259c8e34b7

````
/-!
# Polynomial bounds

A function `f : Nat → Nat` is *polynomially bounded* when `f n ≤ c * (n + 1) ^ d` for some constants.
Every polynomial with natural coefficients is dominated by a bound of this shape, and the shape is
monotone in `n`, which is what composing time bounds needs.

The closure lemmas here (sums, products, composition) are all that the complexity theory uses.
-/

namespace PvsNP

/-- The canonical monotone polynomial bound `n ↦ c * (n + 1) ^ d`. -/
def polyBound (c d : Nat) (n : Nat) : Nat := c * (n + 1) ^ d

/-- `f` is bounded by a polynomial. -/
def PolyBounded (f : Nat → Nat) : Prop :=
  ∃ c d, ∀ n, f n ≤ polyBound c d n

namespace polyBound

theorem mono (c d : Nat) {m n : Nat} (h : m ≤ n) : polyBound c d m ≤ polyBound c d n :=
  Nat.mul_le_mul_left c (Nat.pow_le_pow_left (Nat.succ_le_succ h) d)

theorem one_le_pow (n d : Nat) : 1 ≤ (n + 1) ^ d :=
  Nat.pow_pos (Nat.succ_pos n)

theorem pow_mono (n : Nat) {d e : Nat} (h : d ≤ e) : (n + 1) ^ d ≤ (n + 1) ^ e :=
  Nat.pow_le_pow_right (Nat.succ_pos n) h

end polyBound

namespace PolyBounded

theorem of_le {f g : Nat → Nat} (hg : PolyBounded g) (h : ∀ n, f n ≤ g n) : PolyBounded f := by
  obtain ⟨c, d, hcd⟩ := hg
  exact ⟨c, d, fun n => Nat.le_trans (h n) (hcd n)⟩

theorem polyBound (c d : Nat) : PolyBounded (polyBound c d) :=
  ⟨c, d, fun _ => Nat.le_refl _⟩

theorem const (c : Nat) : PolyBounded (fun _ => c) :=
  ⟨c, 0, fun n => by simp [PvsNP.polyBound]⟩

theorem id : PolyBounded (fun n => n) :=
  ⟨1, 1, fun n => by simp [PvsNP.polyBound]⟩

theorem add {f g : Nat → Nat} (hf : PolyBounded f) (hg : PolyBounded g) :
    PolyBounded (fun n => f n + g n) := by
  obtain ⟨c₁, d₁, h₁⟩ := hf
  obtain ⟨c₂, d₂, h₂⟩ := hg
  refine ⟨c₁ + c₂, d₁ + d₂, fun n => ?_⟩
  have e₁ := polyBound.pow_mono n (Nat.le_add_right d₁ d₂)
  have e₂ := polyBound.pow_mono n (Nat.le_add_left d₂ d₁)
  calc f n + g n ≤ c₁ * (n + 1) ^ d₁ + c₂ * (n + 1) ^ d₂ := Nat.add_le_add (h₁ n) (h₂ n)
    _ ≤ c₁ * (n + 1) ^ (d₁ + d₂) + c₂ * (n + 1) ^ (d₁ + d₂) :=
        Nat.add_le_add (Nat.mul_le_mul_left c₁ e₁) (Nat.mul_le_mul_left c₂ e₂)
    _ = PvsNP.polyBound (c₁ + c₂) (d₁ + d₂) n := by simp [PvsNP.polyBound, Nat.add_mul]

theorem mul {f g : Nat → Nat} (hf : PolyBounded f) (hg : PolyBounded g) :
    PolyBounded (fun n => f n * g n) := by
  obtain ⟨c₁, d₁, h₁⟩ := hf
  obtain ⟨c₂, d₂, h₂⟩ := hg
  refine ⟨c₁ * c₂, d₁ + d₂, fun n => ?_⟩
  calc f n * g n ≤ (c₁ * (n + 1) ^ d₁) * (c₂ * (n + 1) ^ d₂) := Nat.mul_le_mul (h₁ n) (h₂ n)
    _ = PvsNP.polyBound (c₁ * c₂) (d₁ + d₂) n := by
        simp only [PvsNP.polyBound, Nat.pow_add]
        rw [Nat.mul_assoc, Nat.mul_assoc, Nat.mul_left_comm ((n + 1) ^ d₁)]

/-- Polynomially bounded functions compose. No monotonicity of `f` is needed: its bound is. -/
theorem comp {f g : Nat → Nat} (hf : PolyBounded f) (hg : PolyBounded g) :
    PolyBounded (fun n => f (g n)) := by
  obtain ⟨c₁, d₁, h₁⟩ := hf
  obtain ⟨c₂, d₂, h₂⟩ := hg
  refine ⟨c₁ * (c₂ + 1) ^ d₁, d₂ * d₁, fun n => ?_⟩
  have hgn : g n + 1 ≤ (c₂ + 1) * (n + 1) ^ d₂ := by
    have := h₂ n
    have := polyBound.one_le_pow n d₂
    simp only [PvsNP.polyBound] at *
    rw [Nat.add_mul, Nat.one_mul]
    omega
  calc f (g n) ≤ c₁ * (g n + 1) ^ d₁ := h₁ (g n)
    _ ≤ c₁ * ((c₂ + 1) * (n + 1) ^ d₂) ^ d₁ :=
        Nat.mul_le_mul_left c₁ (Nat.pow_le_pow_left hgn d₁)
    _ = PvsNP.polyBound (c₁ * (c₂ + 1) ^ d₁) (d₂ * d₁) n := by
        simp only [PvsNP.polyBound, Nat.mul_pow, Nat.pow_mul, Nat.mul_assoc]

end PolyBounded

end PvsNP

````

API: `https://git.neoopensource.com/v1/repos/pvsnp/blob?rev=4186ab2739015c0efa3783c76b4987259c8e34b7&path=PvsNP%2FBasic%2FPoly.lean`
