# PvsNP/Complexity/NP.lean @ 989456136115376209fd6a56be8e8c3f89c3d9d4

````
import PvsNP.Complexity.Reduction

/-!
# The class NP

`InNP L`: the alphabet is finite and some machine verifies `L` in polynomial time. The input `x` is on tape `0` and a certificate
`w` on tape `1`; the verifier halts within `T |x|` steps whatever the certificate, and `x ∈ L` exactly
when some certificate makes it accept. Because the time bound depends on `|x|` alone, only the first
`T |x|` symbols of a certificate can matter, so certificates are polynomially short in effect
(Sipser, *Introduction to the Theory of Computation*, Definition 7.18).

Certificates are written over the input alphabet. For alphabets with at least two symbols this is the
usual NP. Over a one-symbol alphabet a certificate carries only its length, so `InNP` there is not the
usual "unary inputs, binary certificates" class; the open problem is stated over `Bool`.

Proved here:
- `InP.inNP`: **P ⊆ NP**. A decider, run with tape `1` left alone, is a verifier that ignores its
  certificate.
- `InNP.of_reduces`: NP is closed downward under `≤ₚ`. The reduction runs first, leaving the
  certificate on tape `1` untouched; then the verifier runs on `f x` and the same certificate.
-/

namespace PvsNP

variable {α : Type}

/-- `L ∈ NP`: a machine verifies `L` in polynomial time. -/
def InNP (L : Language α) : Prop :=
  IsFinite α ∧ ∃ (k : Nat) (M : Machine α k) (T : Nat → Nat), PolyBounded T ∧ VerifiesIn M L T

namespace InNP

/-- The time bound of a verifier can be taken of the form `c * (n + 1) ^ d`, hence monotone. -/
theorem polyBound {L : Language α} (h : InNP L) :
    ∃ (k : Nat) (M : Machine α k) (c d : Nat), VerifiesIn M L (PvsNP.polyBound c d) := by
  obtain ⟨_, k, M, T, ⟨c, d, hT⟩, hM⟩ := h
  exact ⟨k, M, c, d, hM.mono hT⟩

end InNP

/-- **P ⊆ NP.** -/
theorem InP.inNP {L : Language α} (h : InP L) : InNP L := by
  obtain ⟨hα, k, M, T, hT, hM⟩ := h
  let E := Embedding.skipOne (Nat.le_refl (k + 1))
  have e : ∀ x w, (M.lift E).init (verifierTapes x w) =
      M.liftCfg E (verifierTapes x w) (M.init (inputTapes x)) := by
    intro x w
    simp only [Machine.init, Machine.liftCfg, E, liftTapes_skipOne_inputTapes]; rfl
  refine ⟨hα, k + 1, M.lift E, T, hT, fun x w => ?_, fun x => ?_⟩
  · rw [e, Machine.run_lift]; exact (hM x).1
  · simp only [e, Machine.run_lift]
    exact ⟨fun hx => ⟨[], (hM x).2.mpr hx⟩, fun ⟨_, hw⟩ => (hM x).2.mp hw⟩

/-! ## Reductions into NP -/

namespace Machine

/-- Run `M₁` (computing `f`) on `x` with tape `1` left alone, then `V` on `f x` and the certificate. -/
theorem run_seq_verifier {k₁ K : Nat} (M₁ : Machine α k₁) (h : k₁ + 1 ≤ K) (V : Machine α K)
    {f : List α → List α} {T₁ : Nat → Nat} (h₁ : ComputesIn M₁ f T₁) (x w : List α) (t₂ t : Nat)
    (h₂ : V.halted (V.run (V.init (verifierTapes (f x) w)) t₂).state = true)
    (ht : T₁ x.length + 1 + t₂ ≤ t) :
    ((M₁.lift (Embedding.skipOne h)).seq V).run
        (((M₁.lift (Embedding.skipOne h)).seq V).init (verifierTapes x w)) t =
      inrCfg _ V (V.run (V.init (verifierTapes (f x) w)) t₂) := by
  let E := Embedding.skipOne h
  have e : ((M₁.lift E).seq V).init (verifierTapes x w) =
      inlCfg (M₁.lift E) V (M₁.liftCfg E (verifierTapes x w) (M₁.init (inputTapes x))) := by
    simp only [init, inlCfg, liftCfg, E, liftTapes_skipOne_inputTapes]; rfl
  have hrun := M₁.run_lift E (verifierTapes x w) (M₁.init (inputTapes x)) (T₁ x.length)
  have htapes : ((M₁.lift E).run (M₁.liftCfg E (verifierTapes x w) (M₁.init (inputTapes x)))
      (T₁ x.length)).tapes = verifierTapes (f x) w := by
    rw [hrun]; simp only [liftCfg, (h₁ x).2, E, liftTapes_skipOne_inputTapes]
  have hhalt : (M₁.lift E).halted ((M₁.lift E).run
      (M₁.liftCfg E (verifierTapes x w) (M₁.init (inputTapes x))) (T₁ x.length)).state = true := by
    rw [hrun]; exact (h₁ x).1
  have key := run_seq (M₁.lift E) V _ (T₁ x.length) t₂ hhalt (by rw [htapes]; exact h₂)
  rw [htapes] at key
  show ((M₁.lift E).seq V).run (((M₁.lift E).seq V).init (verifierTapes x w)) t = _
  rw [e, run_stable _ (by rw [key]; exact h₂) ht, key]

end Machine

/-- **NP is closed under reductions**: if `L ≤ₚ L'` and `L' ∈ NP`, then `L ∈ NP`. -/
theorem InNP.of_reduces {L L' : Language α} (h : L ≤ₚ L') (hL' : InNP L') : InNP L := by
  obtain ⟨f, hf, hfL⟩ := h
  obtain ⟨k₁, M₁, c₁, d₁, b, e, hM₁, hB⟩ := hf.normalize
  obtain ⟨k₂, V, c₂, d₂, hV⟩ := hL'.polyBound
  have hk₁ : k₁ + 1 ≤ k₁ + k₂ + 1 := by omega
  have hk₂ : k₂ ≤ k₁ + k₂ + 1 := by omega
  have hV' := hV.lift hk₂
  let V' := V.lift (Embedding.castLE hk₂)
  let T := fun n => PvsNP.polyBound c₁ d₁ n + 1 + PvsNP.polyBound c₂ d₂ (PvsNP.polyBound b e n)
  have key : ∀ x w, ((M₁.lift (Embedding.skipOne hk₁)).seq V').run
      (((M₁.lift (Embedding.skipOne hk₁)).seq V').init (verifierTapes x w)) (T x.length) =
      Machine.inrCfg _ V' (V'.run (V'.init (verifierTapes (f x) w))
        (PvsNP.polyBound c₂ d₂ (f x).length)) := fun x w =>
    Machine.run_seq_verifier M₁ hk₁ V' hM₁ x w _ _ (hV'.1 (f x) w)
      (Nat.add_le_add_left (polyBound.mono c₂ d₂ (hB x)) _)
  refine ⟨hf.1, k₁ + k₂ + 1, (M₁.lift (Embedding.skipOne hk₁)).seq V', T, ?_, fun x w => ?_, fun x => ?_⟩
  · exact ((PolyBounded.polyBound c₁ d₁).add (PolyBounded.const 1)).add
      ((PolyBounded.polyBound c₂ d₂).comp (PolyBounded.polyBound b e))
  · rw [key]; exact hV'.1 (f x) w
  · rw [hfL x, hV'.2 (f x)]
    exact exists_congr fun w => by rw [key]; rfl

end PvsNP

````

API: `https://git.neoopensource.com/v1/repos/pvsnp/blob?rev=989456136115376209fd6a56be8e8c3f89c3d9d4&path=PvsNP%2FComplexity%2FNP.lean`
