5bdcd988 / PvsNP / Complexity / NP.lean

1import PvsNP.Complexity.Reduction
2
3/-!
4# The class NP
5
6`InNP L`: the alphabet is finite and some machine verifies `L` in polynomial time. The input `x` is on tape `0` and a certificate
7`w` on tape `1`; the verifier halts within `T |x|` steps whatever the certificate, and `x ∈ L` exactly
8when some certificate makes it accept. Because the time bound depends on `|x|` alone, only the first
9`T |x|` symbols of a certificate can matter, so certificates are polynomially short in effect
10(Sipser, *Introduction to the Theory of Computation*, Definition 7.18).
11
12Proved here:
13- `InP.inNP`: **P ⊆ NP**. A decider, run with tape `1` left alone, is a verifier that ignores its
14 certificate.
15- `InNP.of_reduces`: NP is closed downward under `≤ₚ`. The reduction runs first, leaving the
16 certificate on tape `1` untouched; then the verifier runs on `f x` and the same certificate.
17-/
18
19namespace PvsNP
20
21variable {α : Type}
22
23/-- `L ∈ NP`: a machine verifies `L` in polynomial time. -/
24def InNP (L : Language α) : Prop :=
25 IsFinite α ∧ ∃ (k : Nat) (M : Machine α k) (T : Nat → Nat), PolyBounded T ∧ VerifiesIn M L T
26
27namespace InNP
28
29/-- The time bound of a verifier can be taken of the form `c * (n + 1) ^ d`, hence monotone. -/
30theorem polyBound {L : Language α} (h : InNP L) :
31 ∃ (k : Nat) (M : Machine α k) (c d : Nat), VerifiesIn M L (PvsNP.polyBound c d) := by
32 obtain ⟨_, k, M, T, ⟨c, d, hT⟩, hM⟩ := h
33 exact ⟨k, M, c, d, hM.mono hT⟩
34
35end InNP
36
37/-- **P ⊆ NP.** -/
38theorem InP.inNP {L : Language α} (h : InP L) : InNP L := by
39 obtain ⟨hα, k, M, T, hT, hM⟩ := h
40 let E := Embedding.skipOne (Nat.le_refl (k + 1))
41 have e : ∀ x w, (M.lift E).init (verifierTapes x w) =
42 M.liftCfg E (verifierTapes x w) (M.init (inputTapes x)) := by
43 intro x w
44 simp only [Machine.init, Machine.liftCfg, E, liftTapes_skipOne_inputTapes]; rfl
45 refine ⟨hα, k + 1, M.lift E, T, hT, fun x w => ?_, fun x => ?_⟩
46 · rw [e, Machine.run_lift]; exact (hM x).1
47 · simp only [e, Machine.run_lift]
48 exact ⟨fun hx => ⟨[], (hM x).2.mpr hx⟩, fun ⟨_, hw⟩ => (hM x).2.mp hw⟩
49
50/-! ## Reductions into NP -/
51
52namespace Machine
53
54/-- Run `M₁` (computing `f`) on `x` with tape `1` left alone, then `V` on `f x` and the certificate. -/
55theorem run_seq_verifier {k₁ K : Nat} (M₁ : Machine α k₁) (h : k₁ + 1 ≤ K) (V : Machine α K)
56 {f : List α → List α} {T₁ : Nat → Nat} (h₁ : ComputesIn M₁ f T₁) (x w : List α) (t₂ t : Nat)
57 (h₂ : V.halted (V.run (V.init (verifierTapes (f x) w)) t₂).state = true)
58 (ht : T₁ x.length + 1 + t₂ ≤ t) :
59 ((M₁.lift (Embedding.skipOne h)).seq V).run
60 (((M₁.lift (Embedding.skipOne h)).seq V).init (verifierTapes x w)) t =
61 inrCfg _ V (V.run (V.init (verifierTapes (f x) w)) t₂) := by
62 let E := Embedding.skipOne h
63 have e : ((M₁.lift E).seq V).init (verifierTapes x w) =
64 inlCfg (M₁.lift E) V (M₁.liftCfg E (verifierTapes x w) (M₁.init (inputTapes x))) := by
65 simp only [init, inlCfg, liftCfg, E, liftTapes_skipOne_inputTapes]; rfl
66 have hrun := M₁.run_lift E (verifierTapes x w) (M₁.init (inputTapes x)) (T₁ x.length)
67 have htapes : ((M₁.lift E).run (M₁.liftCfg E (verifierTapes x w) (M₁.init (inputTapes x)))
68 (T₁ x.length)).tapes = verifierTapes (f x) w := by
69 rw [hrun]; simp only [liftCfg, (h₁ x).2, E, liftTapes_skipOne_inputTapes]
70 have hhalt : (M₁.lift E).halted ((M₁.lift E).run
71 (M₁.liftCfg E (verifierTapes x w) (M₁.init (inputTapes x))) (T₁ x.length)).state = true := by
72 rw [hrun]; exact (h₁ x).1
73 have key := run_seq (M₁.lift E) V _ (T₁ x.length) t₂ hhalt (by rw [htapes]; exact h₂)
74 rw [htapes] at key
75 show ((M₁.lift E).seq V).run (((M₁.lift E).seq V).init (verifierTapes x w)) t = _
76 rw [e, run_stable _ (by rw [key]; exact h₂) ht, key]
77
78end Machine
79
80/-- **NP is closed under reductions**: if `L ≤ₚ L'` and `L' ∈ NP`, then `L ∈ NP`. -/
81theorem InNP.of_reduces {L L' : Language α} (h : L ≤ₚ L') (hL' : InNP L') : InNP L := by
82 obtain ⟨f, hf, hfL⟩ := h
83 obtain ⟨k₁, M₁, c₁, d₁, b, e, hM₁, hB⟩ := hf.normalize
84 obtain ⟨k₂, V, c₂, d₂, hV⟩ := hL'.polyBound
85 have hk₁ : k₁ + 1 ≤ k₁ + k₂ + 1 := by omega
86 have hk₂ : k₂ ≤ k₁ + k₂ + 1 := by omega
87 have hV' := hV.lift hk₂
88 let V' := V.lift (Embedding.castLE hk₂)
89 let T := fun n => PvsNP.polyBound c₁ d₁ n + 1 + PvsNP.polyBound c₂ d₂ (PvsNP.polyBound b e n)
90 have key : ∀ x w, ((M₁.lift (Embedding.skipOne hk₁)).seq V').run
91 (((M₁.lift (Embedding.skipOne hk₁)).seq V').init (verifierTapes x w)) (T x.length) =
92 Machine.inrCfg _ V' (V'.run (V'.init (verifierTapes (f x) w))
93 (PvsNP.polyBound c₂ d₂ (f x).length)) := fun x w =>
94 Machine.run_seq_verifier M₁ hk₁ V' hM₁ x w _ _ (hV'.1 (f x) w)
95 (Nat.add_le_add_left (polyBound.mono c₂ d₂ (hB x)) _)
96 refine ⟨hf.1, k₁ + k₂ + 1, (M₁.lift (Embedding.skipOne hk₁)).seq V', T, ?_, fun x w => ?_, fun x => ?_⟩
97 · exact ((PolyBounded.polyBound c₁ d₁).add (PolyBounded.const 1)).add
98 ((PolyBounded.polyBound c₂ d₂).comp (PolyBounded.polyBound b e))
99 · rw [key]; exact hV'.1 (f x) w
100 · rw [hfL x, hV'.2 (f x)]
101 exact exists_congr fun w => by rw [key]; rfl
102
103end PvsNP