1import PvsNP.Complexity.P
2
3/-!
4# Polynomial-time reductions
5
6`PolyTimeComputable f`: the alphabet is finite and a machine computes `f` in polynomial time, and `|f x|` is polynomially bounded
7in `|x|`. (The output bound follows from the time bound in any reasonable model; we state it rather
8than derive it. Deriving it is on the roadmap.)
9
10`L ≤ₚ L'` (Karp reduction): some polynomial-time `f` has `x ∈ L ↔ f x ∈ L'`.
11
12Proved here: the identity is polynomial-time, polynomial-time functions compose, `≤ₚ` is reflexive
13and transitive, and P is closed downward under `≤ₚ`.
14-/
15
16namespace PvsNP
17
18variable {α : Type}
19
20/-- `f` is computable in polynomial time, with polynomially bounded output. -/
21def PolyTimeComputable (f : List α → List α) : Prop :=
22 IsFinite α ∧ ∃ (k : Nat) (M : Machine α k) (T B : Nat → Nat), PolyBounded T ∧ PolyBounded B ∧
23 ComputesIn M f T ∧ ∀ x, (f x).length ≤ B x.length
24
25/-- `L` many-one reduces to `L'` in polynomial time. -/
26def Reduces (L L' : Language α) : Prop :=
27 ∃ f : List α → List α, PolyTimeComputable f ∧ ∀ x, L x ↔ L' (f x)
28
29@[inherit_doc] infix:50 " ≤ₚ " => Reduces
30
31/-! ## Running a computed output through a second machine -/
32
33namespace Machine
34
35variable {k : Nat}
36
37/-- Run `M₁` on `x`, then `M₂` on `f x`, `M₁` computing `f`. -/
38theorem run_seq_input (M₁ M₂ : Machine α k) {f : List α → List α} {T₁ : Nat → Nat}
39 (h₁ : ComputesIn M₁ f T₁) (x : List α) (t₂ : Nat)
40 (h₂ : M₂.halted (M₂.run (M₂.init (inputTapes (f x))) t₂).state = true) :
41 (M₁.seq M₂).run ((M₁.seq M₂).init (inputTapes x)) (T₁ x.length + 1 + t₂) =
42 inrCfg M₁ M₂ (M₂.run (M₂.init (inputTapes (f x))) t₂) := by
43 have e : (M₁.seq M₂).init (inputTapes x) = inlCfg M₁ M₂ (M₁.init (inputTapes x)) := rfl
44 have h₂' := h₂
45 rw [← (h₁ x).2] at h₂'
46 rw [e, run_seq M₁ M₂ _ _ _ (h₁ x).1 h₂', (h₁ x).2]
47
48/-- The same, at any later time. -/
49theorem run_seq_input_le (M₁ M₂ : Machine α k) {f : List α → List α} {T₁ : Nat → Nat}
50 (h₁ : ComputesIn M₁ f T₁) (x : List α) (t₂ t : Nat)
51 (h₂ : M₂.halted (M₂.run (M₂.init (inputTapes (f x))) t₂).state = true)
52 (ht : T₁ x.length + 1 + t₂ ≤ t) :
53 (M₁.seq M₂).run ((M₁.seq M₂).init (inputTapes x)) t =
54 inrCfg M₁ M₂ (M₂.run (M₂.init (inputTapes (f x))) t₂) := by
55 have key := run_seq_input M₁ M₂ h₁ x t₂ h₂
56 rw [run_stable _ (by rw [key]; exact h₂) ht, key]
57
58end Machine
59
60/-- Decide `L` on `f x`: the composite decides `x ↦ L (f x)`. -/
61theorem DecidesIn.seq {k : Nat} {M₁ M₂ : Machine α k} {f : List α → List α} {T₁ B : Nat → Nat}
62 {L : Language α} {c d : Nat} (h₁ : ComputesIn M₁ f T₁) (hB : ∀ x, (f x).length ≤ B x.length)
63 (h₂ : DecidesIn M₂ L (polyBound c d)) :
64 DecidesIn (M₁.seq M₂) (fun x => L (f x)) (fun n => T₁ n + 1 + polyBound c d (B n)) := by
65 intro x
66 rw [Machine.run_seq_input_le M₁ M₂ h₁ x _ _ (h₂ (f x)).1
67 (Nat.add_le_add_left (polyBound.mono c d (hB x)) _)]
68 exact h₂ (f x)
69
70/-- Compute `g` on `f x`: the composite computes `g ∘ f`. -/
71theorem ComputesIn.seq {k : Nat} {M₁ M₂ : Machine α k} {f g : List α → List α} {T₁ B : Nat → Nat}
72 {c d : Nat} (h₁ : ComputesIn M₁ f T₁) (hB : ∀ x, (f x).length ≤ B x.length)
73 (h₂ : ComputesIn M₂ g (polyBound c d)) :
74 ComputesIn (M₁.seq M₂) (fun x => g (f x)) (fun n => T₁ n + 1 + polyBound c d (B n)) := by
75 intro x
76 rw [Machine.run_seq_input_le M₁ M₂ h₁ x _ _ (h₂ (f x)).1
77 (Nat.add_le_add_left (polyBound.mono c d (hB x)) _)]
78 exact h₂ (f x)
79
80namespace PolyTimeComputable
81
82/-- A polynomial-time function in normal form: monotone time bound, on any number of extra tapes. -/
83theorem normalize {f : List α → List α} (h : PolyTimeComputable f) :
84 ∃ (k : Nat) (M : Machine α k) (c d b e : Nat), ComputesIn M f (polyBound c d) ∧
85 ∀ x, (f x).length ≤ polyBound b e x.length := by
86 obtain ⟨_, k, M, T, B, ⟨c, d, hT⟩, ⟨b, e, hB⟩, hM, hfB⟩ := h
87 exact ⟨k, M, c, d, b, e, hM.mono hT, fun x => Nat.le_trans (hfB x) (hB _)⟩
88
89/-- The identity: the machine that halts at once. -/
90theorem id [IsFinite α] : PolyTimeComputable (fun x : List α => x) :=
91 ⟨inferInstance, 0, Machine.const α 0 true, fun _ => 0, fun n => n, PolyBounded.const 0, PolyBounded.id,
92 fun _ => ⟨rfl, rfl⟩, fun _ => Nat.le_refl _⟩
93
94/-- **Polynomial-time functions compose.** -/
95theorem comp {f g : List α → List α} (hf : PolyTimeComputable f) (hg : PolyTimeComputable g) :
96 PolyTimeComputable (fun x => g (f x)) := by
97 obtain ⟨k₁, M₁, c₁, d₁, b₁, e₁, hM₁, hB₁⟩ := hf.normalize
98 obtain ⟨k₂, M₂, c₂, d₂, b₂, e₂, hM₂, hB₂⟩ := hg.normalize
99 have hl₁ := hM₁.lift (Nat.le_add_right k₁ k₂)
100 have hl₂ := hM₂.lift (Nat.le_add_left k₂ k₁)
101 refine ⟨hf.1, k₁ + k₂, _, _, fun n => polyBound b₂ e₂ (polyBound b₁ e₁ n),
102 ?_, ?_, hl₁.seq hB₁ hl₂, fun x => ?_⟩
103 · exact ((PolyBounded.polyBound c₁ d₁).add (PolyBounded.const 1)).add
104 ((PolyBounded.polyBound c₂ d₂).comp (PolyBounded.polyBound b₁ e₁))
105 · exact (PolyBounded.polyBound b₂ e₂).comp (PolyBounded.polyBound b₁ e₁)
106 · exact Nat.le_trans (hB₂ (f x)) (polyBound.mono b₂ e₂ (hB₁ x))
107
108end PolyTimeComputable
109
110namespace Reduces
111
112/-- `≤ₚ` is reflexive. -/
113theorem refl [IsFinite α] (L : Language α) : L ≤ₚ L := ⟨_, PolyTimeComputable.id, fun _ => Iff.rfl⟩
114
115/-- **Reductions compose**: `≤ₚ` is transitive. -/
116theorem trans {L₁ L₂ L₃ : Language α} (h₁₂ : L₁ ≤ₚ L₂) (h₂₃ : L₂ ≤ₚ L₃) : L₁ ≤ₚ L₃ := by
117 obtain ⟨f, hf, hfL⟩ := h₁₂
118 obtain ⟨g, hg, hgL⟩ := h₂₃
119 exact ⟨_, hf.comp hg, fun x => (hfL x).trans (hgL (f x))⟩
120
121/-- A reduction of `L` to `L'` is one of their complements. -/
122theorem compl {L L' : Language α} (h : L ≤ₚ L') : L.compl ≤ₚ L'.compl := by
123 obtain ⟨f, hf, hfL⟩ := h
124 exact ⟨f, hf, fun x => not_congr (hfL x)⟩
125
126end Reduces
127
128/-- **P is closed under reductions**: if `L ≤ₚ L'` and `L' ∈ P`, then `L ∈ P`. -/
129theorem InP.of_reduces {L L' : Language α} (h : L ≤ₚ L') (hL' : InP L') : InP L := by
130 obtain ⟨f, hf, hfL⟩ := h
131 obtain ⟨k₁, M₁, c₁, d₁, b, e, hM₁, hB⟩ := hf.normalize
132 obtain ⟨k₂, M₂, c₂, d₂, hM₂⟩ := hL'.polyBound
133 have hd := DecidesIn.seq (hM₁.lift (Nat.le_add_right k₁ k₂)) hB (hM₂.lift (Nat.le_add_left k₂ k₁))
134 refine ⟨hL'.1, k₁ + k₂,
135 (M₁.lift (Embedding.castLE (Nat.le_add_right k₁ k₂))).seq
136 (M₂.lift (Embedding.castLE (Nat.le_add_left k₂ k₁))),
137 fun n => PvsNP.polyBound c₁ d₁ n + 1 + PvsNP.polyBound c₂ d₂ (PvsNP.polyBound b e n), ?_, ?_⟩
138 · exact ((PolyBounded.polyBound c₁ d₁).add (PolyBounded.const 1)).add
139 ((PolyBounded.polyBound c₂ d₂).comp (PolyBounded.polyBound b e))
140 · intro x
141 exact ⟨(hd x).1, (hd x).2.trans (hfL x).symm⟩
142
143end PvsNP