{"repo":"pvsnp","rev":"f0679799a068ef3eadad81d312dbccca601dc438","blob":{"commit":"f0679799a068ef3eadad81d312dbccca601dc438","path":"PvsNP/Basic/Poly.lean","oid":"f699a5e776136b94faa48e66a74d1ea08672f65f","size":3783,"range_start":0,"range_end":3783,"truncated":false,"binary":false,"encoding":"utf8","content":"/-!\n# Polynomial bounds\n\nA function `f : Nat → Nat` is *polynomially bounded* when `f n ≤ c * (n + 1) ^ d` for some constants.\nEvery polynomial with natural coefficients is dominated by a bound of this shape, and the shape is\nmonotone in `n`, which is what composing time bounds needs.\n\nThe closure lemmas here (sums, products, composition) are all that the complexity theory uses.\n-/\n\nnamespace PvsNP\n\n/-- The canonical monotone polynomial bound `n ↦ c * (n + 1) ^ d`. -/\ndef polyBound (c d : Nat) (n : Nat) : Nat := c * (n + 1) ^ d\n\n/-- `f` is bounded by a polynomial. -/\ndef PolyBounded (f : Nat → Nat) : Prop :=\n  ∃ c d, ∀ n, f n ≤ polyBound c d n\n\nnamespace polyBound\n\ntheorem mono (c d : Nat) {m n : Nat} (h : m ≤ n) : polyBound c d m ≤ polyBound c d n :=\n  Nat.mul_le_mul_left c (Nat.pow_le_pow_left (Nat.succ_le_succ h) d)\n\ntheorem one_le_pow (n d : Nat) : 1 ≤ (n + 1) ^ d :=\n  Nat.pow_pos (Nat.succ_pos n)\n\ntheorem pow_mono (n : Nat) {d e : Nat} (h : d ≤ e) : (n + 1) ^ d ≤ (n + 1) ^ e :=\n  Nat.pow_le_pow_right (Nat.succ_pos n) h\n\nend polyBound\n\nnamespace PolyBounded\n\ntheorem of_le {f g : Nat → Nat} (hg : PolyBounded g) (h : ∀ n, f n ≤ g n) : PolyBounded f := by\n  obtain ⟨c, d, hcd⟩ := hg\n  exact ⟨c, d, fun n => Nat.le_trans (h n) (hcd n)⟩\n\ntheorem polyBound (c d : Nat) : PolyBounded (polyBound c d) :=\n  ⟨c, d, fun _ => Nat.le_refl _⟩\n\ntheorem const (c : Nat) : PolyBounded (fun _ => c) :=\n  ⟨c, 0, fun n => by simp [PvsNP.polyBound]⟩\n\ntheorem id : PolyBounded (fun n => n) :=\n  ⟨1, 1, fun n => by simp [PvsNP.polyBound]⟩\n\ntheorem add {f g : Nat → Nat} (hf : PolyBounded f) (hg : PolyBounded g) :\n    PolyBounded (fun n => f n + g n) := by\n  obtain ⟨c₁, d₁, h₁⟩ := hf\n  obtain ⟨c₂, d₂, h₂⟩ := hg\n  refine ⟨c₁ + c₂, d₁ + d₂, fun n => ?_⟩\n  have e₁ := polyBound.pow_mono n (Nat.le_add_right d₁ d₂)\n  have e₂ := polyBound.pow_mono n (Nat.le_add_left d₂ d₁)\n  calc f n + g n ≤ c₁ * (n + 1) ^ d₁ + c₂ * (n + 1) ^ d₂ := Nat.add_le_add (h₁ n) (h₂ n)\n    _ ≤ c₁ * (n + 1) ^ (d₁ + d₂) + c₂ * (n + 1) ^ (d₁ + d₂) :=\n        Nat.add_le_add (Nat.mul_le_mul_left c₁ e₁) (Nat.mul_le_mul_left c₂ e₂)\n    _ = PvsNP.polyBound (c₁ + c₂) (d₁ + d₂) n := by simp [PvsNP.polyBound, Nat.add_mul]\n\ntheorem mul {f g : Nat → Nat} (hf : PolyBounded f) (hg : PolyBounded g) :\n    PolyBounded (fun n => f n * g n) := by\n  obtain ⟨c₁, d₁, h₁⟩ := hf\n  obtain ⟨c₂, d₂, h₂⟩ := hg\n  refine ⟨c₁ * c₂, d₁ + d₂, fun n => ?_⟩\n  calc f n * g n ≤ (c₁ * (n + 1) ^ d₁) * (c₂ * (n + 1) ^ d₂) := Nat.mul_le_mul (h₁ n) (h₂ n)\n    _ = PvsNP.polyBound (c₁ * c₂) (d₁ + d₂) n := by\n        simp only [PvsNP.polyBound, Nat.pow_add]\n        rw [Nat.mul_assoc, Nat.mul_assoc, Nat.mul_left_comm ((n + 1) ^ d₁)]\n\n/-- Polynomially bounded functions compose. No monotonicity of `f` is needed: its bound is. -/\ntheorem comp {f g : Nat → Nat} (hf : PolyBounded f) (hg : PolyBounded g) :\n    PolyBounded (fun n => f (g n)) := by\n  obtain ⟨c₁, d₁, h₁⟩ := hf\n  obtain ⟨c₂, d₂, h₂⟩ := hg\n  refine ⟨c₁ * (c₂ + 1) ^ d₁, d₂ * d₁, fun n => ?_⟩\n  have hgn : g n + 1 ≤ (c₂ + 1) * (n + 1) ^ d₂ := by\n    have := h₂ n\n    have := polyBound.one_le_pow n d₂\n    simp only [PvsNP.polyBound] at *\n    rw [Nat.add_mul, Nat.one_mul]\n    omega\n  calc f (g n) ≤ c₁ * (g n + 1) ^ d₁ := h₁ (g n)\n    _ ≤ c₁ * ((c₂ + 1) * (n + 1) ^ d₂) ^ d₁ :=\n        Nat.mul_le_mul_left c₁ (Nat.pow_le_pow_left hgn d₁)\n    _ = PvsNP.polyBound (c₁ * (c₂ + 1) ^ d₁) (d₂ * d₁) n := by\n        simp only [PvsNP.polyBound, Nat.mul_pow, Nat.pow_mul, Nat.mul_assoc]\n\nend PolyBounded\n\nend PvsNP\n"},"raw":"/r/pvsnp/raw/f0679799a068ef3eadad81d312dbccca601dc438/PvsNP/Basic/Poly.lean","jit":null,"api":"/v1/repos/pvsnp/blob?rev=f0679799a068ef3eadad81d312dbccca601dc438&path=PvsNP%2FBasic%2FPoly.lean","api_url":"https://git.neoopensource.com/v1/repos/pvsnp/blob?rev=f0679799a068ef3eadad81d312dbccca601dc438&path=PvsNP%2FBasic%2FPoly.lean"}