{"repo":"pvsnp","commit":{"oid":"f5592d3aedb2a3159eb374b0c8fe8cc6eec1d4da","tree":"a816ba108214f8c8479aec4d9f11a56d21ebdb43","parents":["c8e434ba058c72a281a2c0f4d9ac19451c2bdc33"],"author":{"name":"Claude","email":"noreply@anthropic.com","time_ms":1795944959000},"committer":{"name":"Claude","email":"noreply@anthropic.com","time_ms":1795944959000},"subject":"Add polynomial-time reductions; they compose","message":"Add polynomial-time reductions; they compose\n\nPolyTimeComputable f: a machine computes f in polynomial time and |f x|\nis polynomially bounded. L ≤ₚ L' is a Karp reduction.\n\nProved: the identity is polynomial-time, polynomial-time functions\ncompose, ≤ₚ is reflexive and transitive, a reduction is also one between\nthe complements, and P is closed downward under ≤ₚ. The composite machine\nlifts both machines onto a common set of tapes and runs them in sequence.\n\nCo-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>\n"},"diff":{"base":"c8e434ba058c72a281a2c0f4d9ac19451c2bdc33","head":"f5592d3aedb2a3159eb374b0c8fe8cc6eec1d4da","stat":{"files":2,"additions":144,"deletions":0},"files":[{"path":"PvsNP.lean","status":"modified","additions":1,"deletions":0,"binary":false,"patch":"--- a/PvsNP.lean\n+++ b/PvsNP.lean\n@@ -7,6 +7,7 @@\n import PvsNP.Machine.Compose\n import PvsNP.Complexity.Time\n import PvsNP.Complexity.P\n+import PvsNP.Complexity.Reduction\n \n /-! # P versus NP in Lean 4\n \n","truncated":false},{"path":"PvsNP/Complexity/Reduction.lean","status":"added","additions":143,"deletions":0,"binary":false,"patch":"--- /dev/null\n+++ b/PvsNP/Complexity/Reduction.lean\n@@ -0,0 +1,143 @@\n+import PvsNP.Complexity.P\n+\n+/-!\n+# Polynomial-time reductions\n+\n+`PolyTimeComputable f`: a machine computes `f` in polynomial time, and `|f x|` is polynomially bounded\n+in `|x|`. (The output bound follows from the time bound in any reasonable model; we state it rather\n+than 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+\n+Proved here: the identity is polynomial-time, polynomial-time functions compose, `≤ₚ` is reflexive\n+and transitive, and P is closed downward under `≤ₚ`.\n+-/\n+\n+namespace PvsNP\n+\n+variable {α : Type}\n+\n+/-- `f` is computable in polynomial time, with polynomially bounded output. -/\n+def PolyTimeComputable (f : List α → List α) : Prop :=\n+  ∃ (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. -/\n+def 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+\n+namespace Machine\n+\n+variable {k : Nat}\n+\n+/-- Run `M₁` on `x`, then `M₂` on `f x`, `M₁` computing `f`. -/\n+theorem 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. -/\n+theorem 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+\n+end Machine\n+\n+/-- Decide `L` on `f x`: the composite decides `x ↦ L (f x)`. -/\n+theorem 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`. -/\n+theorem 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+\n+namespace PolyTimeComputable\n+\n+/-- A polynomial-time function in normal form: monotone time bound, on any number of extra tapes. -/\n+theorem 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. -/\n+theorem id : PolyTimeComputable (fun x : List α => x) :=\n+  ⟨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.** -/\n+theorem 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 ⟨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+\n+end PolyTimeComputable\n+\n+namespace Reduces\n+\n+/-- `≤ₚ` is reflexive. -/\n+theorem refl (L : Language α) : L ≤ₚ L := ⟨_, PolyTimeComputable.id, fun _ => Iff.rfl⟩\n+\n+/-- **Reductions compose**: `≤ₚ` is transitive. -/\n+theorem 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. -/\n+theorem 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+\n+end Reduces\n+\n+/-- **P is closed under reductions**: if `L ≤ₚ L'` and `L' ∈ P`, then `L ∈ P`. -/\n+theorem 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 ⟨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+\n+end PvsNP\n","truncated":false}],"truncated":false},"jit":null,"api":"/v1/repos/pvsnp/commit?rev=f5592d3aedb2a3159eb374b0c8fe8cc6eec1d4da","api_url":"https://git.neoopensource.com/v1/repos/pvsnp/commit?rev=f5592d3aedb2a3159eb374b0c8fe8cc6eec1d4da"}