{"repo":"pvsnp","rev":"5775f68a625381372449ec219c1ffa1a4b854544","blob":{"commit":"5775f68a625381372449ec219c1ffa1a4b854544","path":"PvsNP/Complexity/NP.lean","oid":"f8a494726c2f9ceb2d5f82e5c6c69927d759c67c","size":5286,"range_start":0,"range_end":5286,"truncated":false,"binary":false,"encoding":"utf8","content":"import PvsNP.Complexity.Reduction\n\n/-!\n# The class NP\n\n`InNP L`: some machine verifies `L` in polynomial time. The input `x` is on tape `0` and a certificate\n`w` on tape `1`; the verifier halts within `T |x|` steps whatever the certificate, and `x ∈ L` exactly\nwhen some certificate makes it accept. Because the time bound depends on `|x|` alone, only the first\n`T |x|` symbols of a certificate can matter, so certificates are polynomially short in effect\n(Sipser, *Introduction to the Theory of Computation*, Definition 7.18).\n\nProved here:\n- `InP.inNP`: **P ⊆ NP**. A decider, run with tape `1` left alone, is a verifier that ignores its\n  certificate.\n- `InNP.of_reduces`: NP is closed downward under `≤ₚ`. The reduction runs first, leaving the\n  certificate on tape `1` untouched; then the verifier runs on `f x` and the same certificate.\n-/\n\nnamespace PvsNP\n\nvariable {α : Type}\n\n/-- `L ∈ NP`: a machine verifies `L` in polynomial time. -/\ndef InNP (L : Language α) : Prop :=\n  ∃ (k : Nat) (M : Machine α k) (T : Nat → Nat), PolyBounded T ∧ VerifiesIn M L T\n\nnamespace InNP\n\n/-- The time bound of a verifier can be taken of the form `c * (n + 1) ^ d`, hence monotone. -/\ntheorem polyBound {L : Language α} (h : InNP L) :\n    ∃ (k : Nat) (M : Machine α k) (c d : Nat), VerifiesIn M L (PvsNP.polyBound c d) := by\n  obtain ⟨k, M, T, ⟨c, d, hT⟩, hM⟩ := h\n  exact ⟨k, M, c, d, hM.mono hT⟩\n\nend InNP\n\n/-- **P ⊆ NP.** -/\ntheorem InP.inNP {L : Language α} (h : InP L) : InNP L := by\n  obtain ⟨k, M, T, hT, hM⟩ := h\n  let E := Embedding.skipOne (Nat.le_refl (k + 1))\n  have e : ∀ x w, (M.lift E).init (verifierTapes x w) =\n      M.liftCfg E (verifierTapes x w) (M.init (inputTapes x)) := by\n    intro x w\n    simp only [Machine.init, Machine.liftCfg, E, liftTapes_skipOne_inputTapes]; rfl\n  refine ⟨k + 1, M.lift E, T, hT, fun x w => ?_, fun x => ?_⟩\n  · rw [e, Machine.run_lift]; exact (hM x).1\n  · simp only [e, Machine.run_lift]\n    exact ⟨fun hx => ⟨[], (hM x).2.mpr hx⟩, fun ⟨_, hw⟩ => (hM x).2.mp hw⟩\n\n/-! ## Reductions into NP -/\n\nnamespace Machine\n\n/-- Run `M₁` (computing `f`) on `x` with tape `1` left alone, then `V` on `f x` and the certificate. -/\ntheorem run_seq_verifier {k₁ K : Nat} (M₁ : Machine α k₁) (h : k₁ + 1 ≤ K) (V : Machine α K)\n    {f : List α → List α} {T₁ : Nat → Nat} (h₁ : ComputesIn M₁ f T₁) (x w : List α) (t₂ t : Nat)\n    (h₂ : V.halted (V.run (V.init (verifierTapes (f x) w)) t₂).state = true)\n    (ht : T₁ x.length + 1 + t₂ ≤ t) :\n    ((M₁.lift (Embedding.skipOne h)).seq V).run\n        (((M₁.lift (Embedding.skipOne h)).seq V).init (verifierTapes x w)) t =\n      inrCfg _ V (V.run (V.init (verifierTapes (f x) w)) t₂) := by\n  let E := Embedding.skipOne h\n  have e : ((M₁.lift E).seq V).init (verifierTapes x w) =\n      inlCfg (M₁.lift E) V (M₁.liftCfg E (verifierTapes x w) (M₁.init (inputTapes x))) := by\n    simp only [init, inlCfg, liftCfg, E, liftTapes_skipOne_inputTapes]; rfl\n  have hrun := M₁.run_lift E (verifierTapes x w) (M₁.init (inputTapes x)) (T₁ x.length)\n  have htapes : ((M₁.lift E).run (M₁.liftCfg E (verifierTapes x w) (M₁.init (inputTapes x)))\n      (T₁ x.length)).tapes = verifierTapes (f x) w := by\n    rw [hrun]; simp only [liftCfg, (h₁ x).2, E, liftTapes_skipOne_inputTapes]\n  have hhalt : (M₁.lift E).halted ((M₁.lift E).run\n      (M₁.liftCfg E (verifierTapes x w) (M₁.init (inputTapes x))) (T₁ x.length)).state = true := by\n    rw [hrun]; exact (h₁ x).1\n  have key := run_seq (M₁.lift E) V _ (T₁ x.length) t₂ hhalt (by rw [htapes]; exact h₂)\n  rw [htapes] at key\n  show ((M₁.lift E).seq V).run (((M₁.lift E).seq V).init (verifierTapes x w)) t = _\n  rw [e, run_stable _ (by rw [key]; exact h₂) ht, key]\n\nend Machine\n\n/-- **NP is closed under reductions**: if `L ≤ₚ L'` and `L' ∈ NP`, then `L ∈ NP`. -/\ntheorem InNP.of_reduces {L L' : Language α} (h : L ≤ₚ L') (hL' : InNP L') : InNP L := by\n  obtain ⟨f, hf, hfL⟩ := h\n  obtain ⟨k₁, M₁, c₁, d₁, b, e, hM₁, hB⟩ := hf.normalize\n  obtain ⟨k₂, V, c₂, d₂, hV⟩ := hL'.polyBound\n  have hk₁ : k₁ + 1 ≤ k₁ + k₂ + 1 := by omega\n  have hk₂ : k₂ ≤ k₁ + k₂ + 1 := by omega\n  have hV' := hV.lift hk₂\n  let V' := V.lift (Embedding.castLE hk₂)\n  let T := fun n => PvsNP.polyBound c₁ d₁ n + 1 + PvsNP.polyBound c₂ d₂ (PvsNP.polyBound b e n)\n  have key : ∀ x w, ((M₁.lift (Embedding.skipOne hk₁)).seq V').run\n      (((M₁.lift (Embedding.skipOne hk₁)).seq V').init (verifierTapes x w)) (T x.length) =\n      Machine.inrCfg _ V' (V'.run (V'.init (verifierTapes (f x) w))\n        (PvsNP.polyBound c₂ d₂ (f x).length)) := fun x w =>\n    Machine.run_seq_verifier M₁ hk₁ V' hM₁ x w _ _ (hV'.1 (f x) w)\n      (Nat.add_le_add_left (polyBound.mono c₂ d₂ (hB x)) _)\n  refine ⟨k₁ + k₂ + 1, (M₁.lift (Embedding.skipOne hk₁)).seq V', T, ?_, fun x w => ?_, fun x => ?_⟩\n  · exact ((PolyBounded.polyBound c₁ d₁).add (PolyBounded.const 1)).add\n      ((PolyBounded.polyBound c₂ d₂).comp (PolyBounded.polyBound b e))\n  · rw [key]; exact hV'.1 (f x) w\n  · rw [hfL x, hV'.2 (f x)]\n    exact exists_congr fun w => by rw [key]; rfl\n\nend PvsNP\n"},"raw":"/r/pvsnp/raw/5775f68a625381372449ec219c1ffa1a4b854544/PvsNP/Complexity/NP.lean","jit":null,"api":"/v1/repos/pvsnp/blob?rev=5775f68a625381372449ec219c1ffa1a4b854544&path=PvsNP%2FComplexity%2FNP.lean","api_url":"https://git.neoopensource.com/v1/repos/pvsnp/blob?rev=5775f68a625381372449ec219c1ffa1a4b854544&path=PvsNP%2FComplexity%2FNP.lean"}