{"repo":"pvsnp","rev":"989456136115376209fd6a56be8e8c3f89c3d9d4","blob":{"commit":"989456136115376209fd6a56be8e8c3f89c3d9d4","path":"PvsNP/Complexity/Reduction.lean","oid":"121a3aa26aaca9ef6aec58a7b786ba822c3339ed","size":7141,"range_start":0,"range_end":7141,"truncated":false,"binary":false,"encoding":"utf8","content":"import PvsNP.Complexity.P\n\n/-!\n# Polynomial-time reductions\n\n`PolyTimeComputable f`: the alphabet is finite and a machine computes `f` in polynomial time, and `|f x|` is polynomially bounded\nin `|x|`. (The output bound follows from the time bound in any reasonable model; we state it rather\nthan derive it. Deriving it is on the roadmap.)\n\n`L ≤ₚ L'` (Karp reduction): some polynomial-time `f` has `x ∈ L ↔ f x ∈ L'`.\n\nProved here: the identity is polynomial-time, polynomial-time functions compose, `≤ₚ` is reflexive\nand transitive, and P is closed downward under `≤ₚ`.\n-/\n\nnamespace PvsNP\n\nvariable {α : Type}\n\n/-- `f` is computable in polynomial time, with polynomially bounded output. -/\ndef PolyTimeComputable (f : List α → List α) : Prop :=\n  IsFinite α ∧ ∃ (k : Nat) (M : Machine α k) (T B : Nat → Nat), PolyBounded T ∧ PolyBounded B ∧\n    ComputesIn M f T ∧ ∀ x, (f x).length ≤ B x.length\n\n/-- `L` many-one reduces to `L'` in polynomial time. -/\ndef Reduces (L L' : Language α) : Prop :=\n  ∃ f : List α → List α, PolyTimeComputable f ∧ ∀ x, L x ↔ L' (f x)\n\n@[inherit_doc] infix:50 \" ≤ₚ \" => Reduces\n\n/-! ## Running a computed output through a second machine -/\n\nnamespace Machine\n\nvariable {k : Nat}\n\n/-- Run `M₁` on `x`, then `M₂` on `f x`, `M₁` computing `f`. -/\ntheorem run_seq_input (M₁ M₂ : Machine α k) {f : List α → List α} {T₁ : Nat → Nat}\n    (h₁ : ComputesIn M₁ f T₁) (x : List α) (t₂ : Nat)\n    (h₂ : M₂.halted (M₂.run (M₂.init (inputTapes (f x))) t₂).state = true) :\n    (M₁.seq M₂).run ((M₁.seq M₂).init (inputTapes x)) (T₁ x.length + 1 + t₂) =\n      inrCfg M₁ M₂ (M₂.run (M₂.init (inputTapes (f x))) t₂) := by\n  have e : (M₁.seq M₂).init (inputTapes x) = inlCfg M₁ M₂ (M₁.init (inputTapes x)) := rfl\n  have h₂' := h₂\n  rw [← (h₁ x).2] at h₂'\n  rw [e, run_seq M₁ M₂ _ _ _ (h₁ x).1 h₂', (h₁ x).2]\n\n/-- The same, at any later time. -/\ntheorem run_seq_input_le (M₁ M₂ : Machine α k) {f : List α → List α} {T₁ : Nat → Nat}\n    (h₁ : ComputesIn M₁ f T₁) (x : List α) (t₂ t : Nat)\n    (h₂ : M₂.halted (M₂.run (M₂.init (inputTapes (f x))) t₂).state = true)\n    (ht : T₁ x.length + 1 + t₂ ≤ t) :\n    (M₁.seq M₂).run ((M₁.seq M₂).init (inputTapes x)) t =\n      inrCfg M₁ M₂ (M₂.run (M₂.init (inputTapes (f x))) t₂) := by\n  have key := run_seq_input M₁ M₂ h₁ x t₂ h₂\n  rw [run_stable _ (by rw [key]; exact h₂) ht, key]\n\nend Machine\n\n/-- Decide `L` on `f x`: the composite decides `x ↦ L (f x)`. -/\ntheorem DecidesIn.seq {k : Nat} {M₁ M₂ : Machine α k} {f : List α → List α} {T₁ B : Nat → Nat}\n    {L : Language α} {c d : Nat} (h₁ : ComputesIn M₁ f T₁) (hB : ∀ x, (f x).length ≤ B x.length)\n    (h₂ : DecidesIn M₂ L (polyBound c d)) :\n    DecidesIn (M₁.seq M₂) (fun x => L (f x)) (fun n => T₁ n + 1 + polyBound c d (B n)) := by\n  intro x\n  rw [Machine.run_seq_input_le M₁ M₂ h₁ x _ _ (h₂ (f x)).1\n    (Nat.add_le_add_left (polyBound.mono c d (hB x)) _)]\n  exact h₂ (f x)\n\n/-- Compute `g` on `f x`: the composite computes `g ∘ f`. -/\ntheorem ComputesIn.seq {k : Nat} {M₁ M₂ : Machine α k} {f g : List α → List α} {T₁ B : Nat → Nat}\n    {c d : Nat} (h₁ : ComputesIn M₁ f T₁) (hB : ∀ x, (f x).length ≤ B x.length)\n    (h₂ : ComputesIn M₂ g (polyBound c d)) :\n    ComputesIn (M₁.seq M₂) (fun x => g (f x)) (fun n => T₁ n + 1 + polyBound c d (B n)) := by\n  intro x\n  rw [Machine.run_seq_input_le M₁ M₂ h₁ x _ _ (h₂ (f x)).1\n    (Nat.add_le_add_left (polyBound.mono c d (hB x)) _)]\n  exact h₂ (f x)\n\nnamespace PolyTimeComputable\n\n/-- A polynomial-time function in normal form: monotone time bound, on any number of extra tapes. -/\ntheorem normalize {f : List α → List α} (h : PolyTimeComputable f) :\n    ∃ (k : Nat) (M : Machine α k) (c d b e : Nat), ComputesIn M f (polyBound c d) ∧\n      ∀ x, (f x).length ≤ polyBound b e x.length := by\n  obtain ⟨_, k, M, T, B, ⟨c, d, hT⟩, ⟨b, e, hB⟩, hM, hfB⟩ := h\n  exact ⟨k, M, c, d, b, e, hM.mono hT, fun x => Nat.le_trans (hfB x) (hB _)⟩\n\n/-- The identity: the machine that halts at once. -/\ntheorem id [IsFinite α] : PolyTimeComputable (fun x : List α => x) :=\n  ⟨inferInstance, 0, Machine.const α 0 true, fun _ => 0, fun n => n, PolyBounded.const 0, PolyBounded.id,\n    fun _ => ⟨rfl, rfl⟩, fun _ => Nat.le_refl _⟩\n\n/-- **Polynomial-time functions compose.** -/\ntheorem comp {f g : List α → List α} (hf : PolyTimeComputable f) (hg : PolyTimeComputable g) :\n    PolyTimeComputable (fun x => g (f x)) := by\n  obtain ⟨k₁, M₁, c₁, d₁, b₁, e₁, hM₁, hB₁⟩ := hf.normalize\n  obtain ⟨k₂, M₂, c₂, d₂, b₂, e₂, hM₂, hB₂⟩ := hg.normalize\n  have hl₁ := hM₁.lift (Nat.le_add_right k₁ k₂)\n  have hl₂ := hM₂.lift (Nat.le_add_left k₂ k₁)\n  refine ⟨hf.1, k₁ + k₂, _, _, fun n => polyBound b₂ e₂ (polyBound b₁ e₁ n),\n    ?_, ?_, hl₁.seq hB₁ hl₂, fun x => ?_⟩\n  · exact ((PolyBounded.polyBound c₁ d₁).add (PolyBounded.const 1)).add\n      ((PolyBounded.polyBound c₂ d₂).comp (PolyBounded.polyBound b₁ e₁))\n  · exact (PolyBounded.polyBound b₂ e₂).comp (PolyBounded.polyBound b₁ e₁)\n  · exact Nat.le_trans (hB₂ (f x)) (polyBound.mono b₂ e₂ (hB₁ x))\n\nend PolyTimeComputable\n\nnamespace Reduces\n\n/-- `≤ₚ` is reflexive. -/\ntheorem refl [IsFinite α] (L : Language α) : L ≤ₚ L := ⟨_, PolyTimeComputable.id, fun _ => Iff.rfl⟩\n\n/-- **Reductions compose**: `≤ₚ` is transitive. -/\ntheorem trans {L₁ L₂ L₃ : Language α} (h₁₂ : L₁ ≤ₚ L₂) (h₂₃ : L₂ ≤ₚ L₃) : L₁ ≤ₚ L₃ := by\n  obtain ⟨f, hf, hfL⟩ := h₁₂\n  obtain ⟨g, hg, hgL⟩ := h₂₃\n  exact ⟨_, hf.comp hg, fun x => (hfL x).trans (hgL (f x))⟩\n\n/-- A reduction of `L` to `L'` is one of their complements. -/\ntheorem compl {L L' : Language α} (h : L ≤ₚ L') : L.compl ≤ₚ L'.compl := by\n  obtain ⟨f, hf, hfL⟩ := h\n  exact ⟨f, hf, fun x => not_congr (hfL x)⟩\n\nend Reduces\n\n/-- **P is closed under reductions**: if `L ≤ₚ L'` and `L' ∈ P`, then `L ∈ P`. -/\ntheorem InP.of_reduces {L L' : Language α} (h : L ≤ₚ L') (hL' : InP L') : InP L := by\n  obtain ⟨f, hf, hfL⟩ := h\n  obtain ⟨k₁, M₁, c₁, d₁, b, e, hM₁, hB⟩ := hf.normalize\n  obtain ⟨k₂, M₂, c₂, d₂, hM₂⟩ := hL'.polyBound\n  have hd := DecidesIn.seq (hM₁.lift (Nat.le_add_right k₁ k₂)) hB (hM₂.lift (Nat.le_add_left k₂ k₁))\n  refine ⟨hL'.1, k₁ + k₂,\n    (M₁.lift (Embedding.castLE (Nat.le_add_right k₁ k₂))).seq\n      (M₂.lift (Embedding.castLE (Nat.le_add_left k₂ k₁))),\n    fun n => PvsNP.polyBound c₁ d₁ n + 1 + PvsNP.polyBound c₂ d₂ (PvsNP.polyBound b e n), ?_, ?_⟩\n  · exact ((PolyBounded.polyBound c₁ d₁).add (PolyBounded.const 1)).add\n      ((PolyBounded.polyBound c₂ d₂).comp (PolyBounded.polyBound b e))\n  · intro x\n    exact ⟨(hd x).1, (hd x).2.trans (hfL x).symm⟩\n\nend PvsNP\n"},"raw":"/r/pvsnp/raw/989456136115376209fd6a56be8e8c3f89c3d9d4/PvsNP/Complexity/Reduction.lean","jit":null,"api":"/v1/repos/pvsnp/blob?rev=989456136115376209fd6a56be8e8c3f89c3d9d4&path=PvsNP%2FComplexity%2FReduction.lean","api_url":"https://git.neoopensource.com/v1/repos/pvsnp/blob?rev=989456136115376209fd6a56be8e8c3f89c3d9d4&path=PvsNP%2FComplexity%2FReduction.lean"}