# PvsNP/Complexity/Reduction.lean @ f0679799a068ef3eadad81d312dbccca601dc438

````
import PvsNP.Complexity.P

/-!
# Polynomial-time reductions

`PolyTimeComputable f`: a machine computes `f` in polynomial time, and `|f x|` is polynomially bounded
in `|x|`. (The output bound follows from the time bound in any reasonable model; we state it rather
than derive it. Deriving it is on the roadmap.)

`L ≤ₚ L'` (Karp reduction): some polynomial-time `f` has `x ∈ L ↔ f x ∈ L'`.

Proved here: the identity is polynomial-time, polynomial-time functions compose, `≤ₚ` is reflexive
and transitive, and P is closed downward under `≤ₚ`.
-/

namespace PvsNP

variable {α : Type}

/-- `f` is computable in polynomial time, with polynomially bounded output. -/
def PolyTimeComputable (f : List α → List α) : Prop :=
  ∃ (k : Nat) (M : Machine α k) (T B : Nat → Nat), PolyBounded T ∧ PolyBounded B ∧
    ComputesIn M f T ∧ ∀ x, (f x).length ≤ B x.length

/-- `L` many-one reduces to `L'` in polynomial time. -/
def Reduces (L L' : Language α) : Prop :=
  ∃ f : List α → List α, PolyTimeComputable f ∧ ∀ x, L x ↔ L' (f x)

@[inherit_doc] infix:50 " ≤ₚ " => Reduces

/-! ## Running a computed output through a second machine -/

namespace Machine

variable {k : Nat}

/-- Run `M₁` on `x`, then `M₂` on `f x`, `M₁` computing `f`. -/
theorem run_seq_input (M₁ M₂ : Machine α k) {f : List α → List α} {T₁ : Nat → Nat}
    (h₁ : ComputesIn M₁ f T₁) (x : List α) (t₂ : Nat)
    (h₂ : M₂.halted (M₂.run (M₂.init (inputTapes (f x))) t₂).state = true) :
    (M₁.seq M₂).run ((M₁.seq M₂).init (inputTapes x)) (T₁ x.length + 1 + t₂) =
      inrCfg M₁ M₂ (M₂.run (M₂.init (inputTapes (f x))) t₂) := by
  have e : (M₁.seq M₂).init (inputTapes x) = inlCfg M₁ M₂ (M₁.init (inputTapes x)) := rfl
  have h₂' := h₂
  rw [← (h₁ x).2] at h₂'
  rw [e, run_seq M₁ M₂ _ _ _ (h₁ x).1 h₂', (h₁ x).2]

/-- The same, at any later time. -/
theorem run_seq_input_le (M₁ M₂ : Machine α k) {f : List α → List α} {T₁ : Nat → Nat}
    (h₁ : ComputesIn M₁ f T₁) (x : List α) (t₂ t : Nat)
    (h₂ : M₂.halted (M₂.run (M₂.init (inputTapes (f x))) t₂).state = true)
    (ht : T₁ x.length + 1 + t₂ ≤ t) :
    (M₁.seq M₂).run ((M₁.seq M₂).init (inputTapes x)) t =
      inrCfg M₁ M₂ (M₂.run (M₂.init (inputTapes (f x))) t₂) := by
  have key := run_seq_input M₁ M₂ h₁ x t₂ h₂
  rw [run_stable _ (by rw [key]; exact h₂) ht, key]

end Machine

/-- Decide `L` on `f x`: the composite decides `x ↦ L (f x)`. -/
theorem DecidesIn.seq {k : Nat} {M₁ M₂ : Machine α k} {f : List α → List α} {T₁ B : Nat → Nat}
    {L : Language α} {c d : Nat} (h₁ : ComputesIn M₁ f T₁) (hB : ∀ x, (f x).length ≤ B x.length)
    (h₂ : DecidesIn M₂ L (polyBound c d)) :
    DecidesIn (M₁.seq M₂) (fun x => L (f x)) (fun n => T₁ n + 1 + polyBound c d (B n)) := by
  intro x
  rw [Machine.run_seq_input_le M₁ M₂ h₁ x _ _ (h₂ (f x)).1
    (Nat.add_le_add_left (polyBound.mono c d (hB x)) _)]
  exact h₂ (f x)

/-- Compute `g` on `f x`: the composite computes `g ∘ f`. -/
theorem ComputesIn.seq {k : Nat} {M₁ M₂ : Machine α k} {f g : List α → List α} {T₁ B : Nat → Nat}
    {c d : Nat} (h₁ : ComputesIn M₁ f T₁) (hB : ∀ x, (f x).length ≤ B x.length)
    (h₂ : ComputesIn M₂ g (polyBound c d)) :
    ComputesIn (M₁.seq M₂) (fun x => g (f x)) (fun n => T₁ n + 1 + polyBound c d (B n)) := by
  intro x
  rw [Machine.run_seq_input_le M₁ M₂ h₁ x _ _ (h₂ (f x)).1
    (Nat.add_le_add_left (polyBound.mono c d (hB x)) _)]
  exact h₂ (f x)

namespace PolyTimeComputable

/-- A polynomial-time function in normal form: monotone time bound, on any number of extra tapes. -/
theorem normalize {f : List α → List α} (h : PolyTimeComputable f) :
    ∃ (k : Nat) (M : Machine α k) (c d b e : Nat), ComputesIn M f (polyBound c d) ∧
      ∀ x, (f x).length ≤ polyBound b e x.length := by
  obtain ⟨k, M, T, B, ⟨c, d, hT⟩, ⟨b, e, hB⟩, hM, hfB⟩ := h
  exact ⟨k, M, c, d, b, e, hM.mono hT, fun x => Nat.le_trans (hfB x) (hB _)⟩

/-- The identity: the machine that halts at once. -/
theorem id : PolyTimeComputable (fun x : List α => x) :=
  ⟨0, Machine.const α 0 true, fun _ => 0, fun n => n, PolyBounded.const 0, PolyBounded.id,
    fun _ => ⟨rfl, rfl⟩, fun _ => Nat.le_refl _⟩

/-- **Polynomial-time functions compose.** -/
theorem comp {f g : List α → List α} (hf : PolyTimeComputable f) (hg : PolyTimeComputable g) :
    PolyTimeComputable (fun x => g (f x)) := by
  obtain ⟨k₁, M₁, c₁, d₁, b₁, e₁, hM₁, hB₁⟩ := hf.normalize
  obtain ⟨k₂, M₂, c₂, d₂, b₂, e₂, hM₂, hB₂⟩ := hg.normalize
  have hl₁ := hM₁.lift (Nat.le_add_right k₁ k₂)
  have hl₂ := hM₂.lift (Nat.le_add_left k₂ k₁)
  refine ⟨k₁ + k₂, _, _, fun n => polyBound b₂ e₂ (polyBound b₁ e₁ n),
    ?_, ?_, hl₁.seq hB₁ hl₂, fun x => ?_⟩
  · exact ((PolyBounded.polyBound c₁ d₁).add (PolyBounded.const 1)).add
      ((PolyBounded.polyBound c₂ d₂).comp (PolyBounded.polyBound b₁ e₁))
  · exact (PolyBounded.polyBound b₂ e₂).comp (PolyBounded.polyBound b₁ e₁)
  · exact Nat.le_trans (hB₂ (f x)) (polyBound.mono b₂ e₂ (hB₁ x))

end PolyTimeComputable

namespace Reduces

/-- `≤ₚ` is reflexive. -/
theorem refl (L : Language α) : L ≤ₚ L := ⟨_, PolyTimeComputable.id, fun _ => Iff.rfl⟩

/-- **Reductions compose**: `≤ₚ` is transitive. -/
theorem trans {L₁ L₂ L₃ : Language α} (h₁₂ : L₁ ≤ₚ L₂) (h₂₃ : L₂ ≤ₚ L₃) : L₁ ≤ₚ L₃ := by
  obtain ⟨f, hf, hfL⟩ := h₁₂
  obtain ⟨g, hg, hgL⟩ := h₂₃
  exact ⟨_, hf.comp hg, fun x => (hfL x).trans (hgL (f x))⟩

/-- A reduction of `L` to `L'` is one of their complements. -/
theorem compl {L L' : Language α} (h : L ≤ₚ L') : L.compl ≤ₚ L'.compl := by
  obtain ⟨f, hf, hfL⟩ := h
  exact ⟨f, hf, fun x => not_congr (hfL x)⟩

end Reduces

/-- **P is closed under reductions**: if `L ≤ₚ L'` and `L' ∈ P`, then `L ∈ P`. -/
theorem InP.of_reduces {L L' : Language α} (h : L ≤ₚ L') (hL' : InP L') : InP L := by
  obtain ⟨f, hf, hfL⟩ := h
  obtain ⟨k₁, M₁, c₁, d₁, b, e, hM₁, hB⟩ := hf.normalize
  obtain ⟨k₂, M₂, c₂, d₂, hM₂⟩ := hL'.polyBound
  have hd := DecidesIn.seq (hM₁.lift (Nat.le_add_right k₁ k₂)) hB (hM₂.lift (Nat.le_add_left k₂ k₁))
  refine ⟨k₁ + k₂,
    (M₁.lift (Embedding.castLE (Nat.le_add_right k₁ k₂))).seq
      (M₂.lift (Embedding.castLE (Nat.le_add_left k₂ k₁))),
    fun n => PvsNP.polyBound c₁ d₁ n + 1 + PvsNP.polyBound c₂ d₂ (PvsNP.polyBound b e n), ?_, ?_⟩
  · exact ((PolyBounded.polyBound c₁ d₁).add (PolyBounded.const 1)).add
      ((PolyBounded.polyBound c₂ d₂).comp (PolyBounded.polyBound b e))
  · intro x
    exact ⟨(hd x).1, (hd x).2.trans (hfL x).symm⟩

end PvsNP

````

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