5775f68a / PvsNP / Basic / Poly.lean

1/-!
2# Polynomial bounds
3
4A function `f : Nat → Nat` is *polynomially bounded* when `f n ≤ c * (n + 1) ^ d` for some constants.
5Every polynomial with natural coefficients is dominated by a bound of this shape, and the shape is
6monotone in `n`, which is what composing time bounds needs.
7
8The closure lemmas here (sums, products, composition) are all that the complexity theory uses.
9-/
10
11namespace PvsNP
12
13/-- The canonical monotone polynomial bound `n ↦ c * (n + 1) ^ d`. -/
14def polyBound (c d : Nat) (n : Nat) : Nat := c * (n + 1) ^ d
15
16/-- `f` is bounded by a polynomial. -/
17def PolyBounded (f : Nat → Nat) : Prop :=
18 ∃ c d, ∀ n, f n ≤ polyBound c d n
19
20namespace polyBound
21
22theorem mono (c d : Nat) {m n : Nat} (h : m ≤ n) : polyBound c d m ≤ polyBound c d n :=
23 Nat.mul_le_mul_left c (Nat.pow_le_pow_left (Nat.succ_le_succ h) d)
24
25theorem one_le_pow (n d : Nat) : 1 ≤ (n + 1) ^ d :=
26 Nat.pow_pos (Nat.succ_pos n)
27
28theorem pow_mono (n : Nat) {d e : Nat} (h : d ≤ e) : (n + 1) ^ d ≤ (n + 1) ^ e :=
29 Nat.pow_le_pow_right (Nat.succ_pos n) h
30
31end polyBound
32
33namespace PolyBounded
34
35theorem of_le {f g : Nat → Nat} (hg : PolyBounded g) (h : ∀ n, f n ≤ g n) : PolyBounded f := by
36 obtain ⟨c, d, hcd⟩ := hg
37 exact ⟨c, d, fun n => Nat.le_trans (h n) (hcd n)⟩
38
39theorem polyBound (c d : Nat) : PolyBounded (polyBound c d) :=
40 ⟨c, d, fun _ => Nat.le_refl _⟩
41
42theorem const (c : Nat) : PolyBounded (fun _ => c) :=
43 ⟨c, 0, fun n => by simp [PvsNP.polyBound]⟩
44
45theorem id : PolyBounded (fun n => n) :=
46 ⟨1, 1, fun n => by simp [PvsNP.polyBound]⟩
47
48theorem add {f g : Nat → Nat} (hf : PolyBounded f) (hg : PolyBounded g) :
49 PolyBounded (fun n => f n + g n) := by
50 obtain ⟨c₁, d₁, h₁⟩ := hf
51 obtain ⟨c₂, d₂, h₂⟩ := hg
52 refine ⟨c₁ + c₂, d₁ + d₂, fun n => ?_⟩
53 have e₁ := polyBound.pow_mono n (Nat.le_add_right d₁ d₂)
54 have e₂ := polyBound.pow_mono n (Nat.le_add_left d₂ d₁)
55 calc f n + g n ≤ c₁ * (n + 1) ^ d₁ + c₂ * (n + 1) ^ d₂ := Nat.add_le_add (h₁ n) (h₂ n)
56 _ ≤ c₁ * (n + 1) ^ (d₁ + d₂) + c₂ * (n + 1) ^ (d₁ + d₂) :=
57 Nat.add_le_add (Nat.mul_le_mul_left c₁ e₁) (Nat.mul_le_mul_left c₂ e₂)
58 _ = PvsNP.polyBound (c₁ + c₂) (d₁ + d₂) n := by simp [PvsNP.polyBound, Nat.add_mul]
59
60theorem mul {f g : Nat → Nat} (hf : PolyBounded f) (hg : PolyBounded g) :
61 PolyBounded (fun n => f n * g n) := by
62 obtain ⟨c₁, d₁, h₁⟩ := hf
63 obtain ⟨c₂, d₂, h₂⟩ := hg
64 refine ⟨c₁ * c₂, d₁ + d₂, fun n => ?_⟩
65 calc f n * g n ≤ (c₁ * (n + 1) ^ d₁) * (c₂ * (n + 1) ^ d₂) := Nat.mul_le_mul (h₁ n) (h₂ n)
66 _ = PvsNP.polyBound (c₁ * c₂) (d₁ + d₂) n := by
67 simp only [PvsNP.polyBound, Nat.pow_add]
68 rw [Nat.mul_assoc, Nat.mul_assoc, Nat.mul_left_comm ((n + 1) ^ d₁)]
69
70/-- Polynomially bounded functions compose. No monotonicity of `f` is needed: its bound is. -/
71theorem comp {f g : Nat → Nat} (hf : PolyBounded f) (hg : PolyBounded g) :
72 PolyBounded (fun n => f (g n)) := by
73 obtain ⟨c₁, d₁, h₁⟩ := hf
74 obtain ⟨c₂, d₂, h₂⟩ := hg
75 refine ⟨c₁ * (c₂ + 1) ^ d₁, d₂ * d₁, fun n => ?_⟩
76 have hgn : g n + 1 ≤ (c₂ + 1) * (n + 1) ^ d₂ := by
77 have := h₂ n
78 have := polyBound.one_le_pow n d₂
79 simp only [PvsNP.polyBound] at *
80 rw [Nat.add_mul, Nat.one_mul]
81 omega
82 calc f (g n) ≤ c₁ * (g n + 1) ^ d₁ := h₁ (g n)
83 _ ≤ c₁ * ((c₂ + 1) * (n + 1) ^ d₂) ^ d₁ :=
84 Nat.mul_le_mul_left c₁ (Nat.pow_le_pow_left hgn d₁)
85 _ = PvsNP.polyBound (c₁ * (c₂ + 1) ^ d₁) (d₂ * d₁) n := by
86 simp only [PvsNP.polyBound, Nat.mul_pow, Nat.pow_mul, Nat.mul_assoc]
87
88end PolyBounded
89
90end PvsNP