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