Require a finite alphabet in P, NP and polynomial-time functions
Review finding: Machine requires finite states but nothing required a finite alphabet. Over α := Nat a machine reading one symbol can test membership in the halting set, so firstSymbol_inP put an undecidable language in P. InP, InNP and PolyTimeComputable now start with IsFinite α, so every class is empty over an infinite alphabet. The P ≠ NP statement (over Bool) is unchanged. Theorems that build a machine from nothing (univ, empty, the identity, refl, firstSymbol_inP) take [IsFinite α]. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diff
4 files changed +36 -29
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 | /-! |
| 4 | 4 | # The class NP |
| 5 | 5 | |
| 6 | -`InNP L`: some machine verifies `L` in polynomial time. The input `x` is on tape `0` and a certificate | |
| 6 | +`InNP L`: the alphabet is finite and some machine verifies `L` in polynomial time. The input `x` is on tape `0` and a certificate | |
| 7 | 7 | `w` on tape `1`; the verifier halts within `T |x|` steps whatever the certificate, and `x ∈ L` exactly |
| 8 | 8 | when some certificate makes it accept. Because the time bound depends on `|x|` alone, only the first |
| 9 | 9 | `T |x|` symbols of a certificate can matter, so certificates are polynomially short in effect |
| @@ -22,27 +22,27 @@ | ||
| 22 | 22 | |
| 23 | 23 | /-- `L ∈ NP`: a machine verifies `L` in polynomial time. -/ |
| 24 | 24 | def InNP (L : Language α) : Prop := |
| 25 | - ∃ (k : Nat) (M : Machine α k) (T : Nat → Nat), PolyBounded T ∧ VerifiesIn M L T | |
| 25 | + IsFinite α ∧ ∃ (k : Nat) (M : Machine α k) (T : Nat → Nat), PolyBounded T ∧ VerifiesIn M L T | |
| 26 | 26 | |
| 27 | 27 | namespace InNP |
| 28 | 28 | |
| 29 | 29 | /-- The time bound of a verifier can be taken of the form `c * (n + 1) ^ d`, hence monotone. -/ |
| 30 | 30 | theorem polyBound {L : Language α} (h : InNP L) : |
| 31 | 31 | ∃ (k : Nat) (M : Machine α k) (c d : Nat), VerifiesIn M L (PvsNP.polyBound c d) := by |
| 32 | - obtain ⟨k, M, T, ⟨c, d, hT⟩, hM⟩ := h | |
| 32 | + obtain ⟨_, k, M, T, ⟨c, d, hT⟩, hM⟩ := h | |
| 33 | 33 | exact ⟨k, M, c, d, hM.mono hT⟩ |
| 34 | 34 | |
| 35 | 35 | end InNP |
| 36 | 36 | |
| 37 | 37 | /-- **P ⊆ NP.** -/ |
| 38 | 38 | theorem InP.inNP {L : Language α} (h : InP L) : InNP L := by |
| 39 | - obtain ⟨k, M, T, hT, hM⟩ := h | |
| 39 | + obtain ⟨hα, k, M, T, hT, hM⟩ := h | |
| 40 | 40 | let E := Embedding.skipOne (Nat.le_refl (k + 1)) |
| 41 | 41 | have e : ∀ x w, (M.lift E).init (verifierTapes x w) = |
| 42 | 42 | M.liftCfg E (verifierTapes x w) (M.init (inputTapes x)) := by |
| 43 | 43 | intro x w |
| 44 | 44 | simp only [Machine.init, Machine.liftCfg, E, liftTapes_skipOne_inputTapes]; rfl |
| 45 | - refine ⟨k + 1, M.lift E, T, hT, fun x w => ?_, fun x => ?_⟩ | |
| 45 | + refine ⟨hα, k + 1, M.lift E, T, hT, fun x w => ?_, fun x => ?_⟩ | |
| 46 | 46 | · rw [e, Machine.run_lift]; exact (hM x).1 |
| 47 | 47 | · simp only [e, Machine.run_lift] |
| 48 | 48 | exact ⟨fun hx => ⟨[], (hM x).2.mpr hx⟩, fun ⟨_, hw⟩ => (hM x).2.mp hw⟩ |
| @@ -93,7 +93,7 @@ | ||
| 93 | 93 | (PvsNP.polyBound c₂ d₂ (f x).length)) := fun x w => |
| 94 | 94 | Machine.run_seq_verifier M₁ hk₁ V' hM₁ x w _ _ (hV'.1 (f x) w) |
| 95 | 95 | (Nat.add_le_add_left (polyBound.mono c₂ d₂ (hB x)) _) |
| 96 | - refine ⟨k₁ + k₂ + 1, (M₁.lift (Embedding.skipOne hk₁)).seq V', T, ?_, fun x w => ?_, fun x => ?_⟩ | |
| 96 | + refine ⟨hf.1, k₁ + k₂ + 1, (M₁.lift (Embedding.skipOne hk₁)).seq V', T, ?_, fun x w => ?_, fun x => ?_⟩ | |
| 97 | 97 | · exact ((PolyBounded.polyBound c₁ d₁).add (PolyBounded.const 1)).add |
| 98 | 98 | ((PolyBounded.polyBound c₂ d₂).comp (PolyBounded.polyBound b e)) |
| 99 | 99 | · rw [key]; exact hV'.1 (f x) w |
| @@ -4,7 +4,10 @@ | ||
| 4 | 4 | /-! |
| 5 | 5 | # The class P |
| 6 | 6 | |
| 7 | -`InP L`: some multi-tape machine decides `L` within a polynomial number of steps. | |
| 7 | +`InP L`: the alphabet is finite and some multi-tape machine decides `L` within a polynomial number | |
| 8 | +of steps. Finiteness of the alphabet is part of the definition: over an infinite alphabet a single | |
| 9 | +symbol carries unbounded information (a machine reading one `Nat` could test membership in the | |
| 10 | +halting set), so P is empty there by definition rather than meaningless. | |
| 8 | 11 | |
| 9 | 12 | Proved here: the full and the empty language are in P, and P is closed under complement. |
| 10 | 13 | -/ |
| @@ -13,9 +16,9 @@ | ||
| 13 | 16 | |
| 14 | 17 | variable {α : Type} |
| 15 | 18 | |
| 16 | -/-- `L ∈ P`: a machine decides `L` in polynomial time. -/ | |
| 19 | +/-- `L ∈ P`: over a finite alphabet, a machine decides `L` in polynomial time. -/ | |
| 17 | 20 | def InP (L : Language α) : Prop := |
| 18 | - ∃ (k : Nat) (M : Machine α k) (T : Nat → Nat), PolyBounded T ∧ DecidesIn M L T | |
| 21 | + IsFinite α ∧ ∃ (k : Nat) (M : Machine α k) (T : Nat → Nat), PolyBounded T ∧ DecidesIn M L T | |
| 19 | 22 | |
| 20 | 23 | /-- A machine that halts at once, accepting or not as `b` says, touching nothing. -/ |
| 21 | 24 | def Machine.const (α : Type) (k : Nat) (b : Bool) : Machine α k where |
| @@ -31,21 +34,24 @@ | ||
| 31 | 34 | /-- The time bound of a language in P can be taken of the form `c * (n + 1) ^ d`, hence monotone. -/ |
| 32 | 35 | theorem polyBound {L : Language α} (h : InP L) : |
| 33 | 36 | ∃ (k : Nat) (M : Machine α k) (c d : Nat), DecidesIn M L (PvsNP.polyBound c d) := by |
| 34 | - obtain ⟨k, M, T, ⟨c, d, hT⟩, hM⟩ := h | |
| 37 | + obtain ⟨_, k, M, T, ⟨c, d, hT⟩, hM⟩ := h | |
| 35 | 38 | exact ⟨k, M, c, d, hM.mono hT⟩ |
| 36 | 39 | |
| 37 | -theorem univ : InP (Language.univ : Language α) := | |
| 38 | - ⟨0, Machine.const α 0 true, fun _ => 0, PolyBounded.const 0, fun _ => | |
| 40 | +/-- Only languages over finite alphabets are in P. -/ | |
| 41 | +theorem isFinite {L : Language α} (h : InP L) : IsFinite α := h.1 | |
| 42 | + | |
| 43 | +theorem univ [IsFinite α] : InP (Language.univ : Language α) := | |
| 44 | + ⟨inferInstance, 0, Machine.const α 0 true, fun _ => 0, PolyBounded.const 0, fun _ => | |
| 39 | 45 | ⟨rfl, by simp [Machine.const, Language.univ, Machine.init]⟩⟩ |
| 40 | 46 | |
| 41 | -theorem empty : InP (Language.empty : Language α) := | |
| 42 | - ⟨0, Machine.const α 0 false, fun _ => 0, PolyBounded.const 0, fun _ => | |
| 47 | +theorem empty [IsFinite α] : InP (Language.empty : Language α) := | |
| 48 | + ⟨inferInstance, 0, Machine.const α 0 false, fun _ => 0, PolyBounded.const 0, fun _ => | |
| 43 | 49 | ⟨rfl, by simp [Machine.const, Language.empty, Machine.init]⟩⟩ |
| 44 | 50 | |
| 45 | 51 | /-- **P is closed under complement**: swap accepting and rejecting halted states. -/ |
| 46 | 52 | theorem compl {L : Language α} (h : InP L) : InP L.compl := by |
| 47 | - obtain ⟨k, M, T, hT, hM⟩ := h | |
| 48 | - refine ⟨k, M.withAccepting (fun q => !M.accepting q), T, hT, fun x => ?_⟩ | |
| 53 | + obtain ⟨hα, k, M, T, hT, hM⟩ := h | |
| 54 | + refine ⟨hα, k, M.withAccepting (fun q => !M.accepting q), T, hT, fun x => ?_⟩ | |
| 49 | 55 | have e : (M.withAccepting (fun q => !M.accepting q)).init (inputTapes x) = M.init (inputTapes x) := |
| 50 | 56 | rfl |
| 51 | 57 | rw [e, Machine.run_withAccepting] |
| @@ -54,8 +60,8 @@ | ||
| 54 | 60 | ← (hM x).2, Bool.not_eq_true] |
| 55 | 61 | |
| 56 | 62 | theorem of_compl {L : Language α} (h : InP L.compl) : InP L := by |
| 57 | - obtain ⟨k, M, T, hT, hM⟩ := h.compl | |
| 58 | - exact ⟨k, M, T, hT, fun x => ⟨(hM x).1, by rw [(hM x).2, Language.compl_compl]⟩⟩ | |
| 63 | + obtain ⟨hα, k, M, T, hT, hM⟩ := h.compl | |
| 64 | + exact ⟨hα, k, M, T, hT, fun x => ⟨(hM x).1, by rw [(hM x).2, Language.compl_compl]⟩⟩ | |
| 59 | 65 | |
| 60 | 66 | theorem compl_iff {L : Language α} : InP L.compl ↔ InP L := ⟨of_compl, compl⟩ |
| 61 | 67 | |
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 | /-! |
| 4 | 4 | # Polynomial-time reductions |
| 5 | 5 | |
| 6 | -`PolyTimeComputable f`: a machine computes `f` in polynomial time, and `|f x|` is polynomially bounded | |
| 6 | +`PolyTimeComputable f`: the alphabet is finite and a machine computes `f` in polynomial time, and `|f x|` is polynomially bounded | |
| 7 | 7 | in `|x|`. (The output bound follows from the time bound in any reasonable model; we state it rather |
| 8 | 8 | than derive it. Deriving it is on the roadmap.) |
| 9 | 9 | |
| @@ -19,7 +19,7 @@ | ||
| 19 | 19 | |
| 20 | 20 | /-- `f` is computable in polynomial time, with polynomially bounded output. -/ |
| 21 | 21 | def PolyTimeComputable (f : List α → List α) : Prop := |
| 22 | - ∃ (k : Nat) (M : Machine α k) (T B : Nat → Nat), PolyBounded T ∧ PolyBounded B ∧ | |
| 22 | + IsFinite α ∧ ∃ (k : Nat) (M : Machine α k) (T B : Nat → Nat), PolyBounded T ∧ PolyBounded B ∧ | |
| 23 | 23 | ComputesIn M f T ∧ ∀ x, (f x).length ≤ B x.length |
| 24 | 24 | |
| 25 | 25 | /-- `L` many-one reduces to `L'` in polynomial time. -/ |
| @@ -83,12 +83,12 @@ | ||
| 83 | 83 | theorem normalize {f : List α → List α} (h : PolyTimeComputable f) : |
| 84 | 84 | ∃ (k : Nat) (M : Machine α k) (c d b e : Nat), ComputesIn M f (polyBound c d) ∧ |
| 85 | 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 | |
| 86 | + obtain ⟨_, k, M, T, B, ⟨c, d, hT⟩, ⟨b, e, hB⟩, hM, hfB⟩ := h | |
| 87 | 87 | exact ⟨k, M, c, d, b, e, hM.mono hT, fun x => Nat.le_trans (hfB x) (hB _)⟩ |
| 88 | 88 | |
| 89 | 89 | /-- The identity: the machine that halts at once. -/ |
| 90 | -theorem id : PolyTimeComputable (fun x : List α => x) := | |
| 91 | - ⟨0, Machine.const α 0 true, fun _ => 0, fun n => n, PolyBounded.const 0, PolyBounded.id, | |
| 90 | +theorem 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 | 92 | fun _ => ⟨rfl, rfl⟩, fun _ => Nat.le_refl _⟩ |
| 93 | 93 | |
| 94 | 94 | /-- **Polynomial-time functions compose.** -/ |
| @@ -98,7 +98,7 @@ | ||
| 98 | 98 | obtain ⟨k₂, M₂, c₂, d₂, b₂, e₂, hM₂, hB₂⟩ := hg.normalize |
| 99 | 99 | have hl₁ := hM₁.lift (Nat.le_add_right k₁ k₂) |
| 100 | 100 | have hl₂ := hM₂.lift (Nat.le_add_left k₂ k₁) |
| 101 | - refine ⟨k₁ + k₂, _, _, fun n => polyBound b₂ e₂ (polyBound b₁ e₁ n), | |
| 101 | + refine ⟨hf.1, k₁ + k₂, _, _, fun n => polyBound b₂ e₂ (polyBound b₁ e₁ n), | |
| 102 | 102 | ?_, ?_, hl₁.seq hB₁ hl₂, fun x => ?_⟩ |
| 103 | 103 | · exact ((PolyBounded.polyBound c₁ d₁).add (PolyBounded.const 1)).add |
| 104 | 104 | ((PolyBounded.polyBound c₂ d₂).comp (PolyBounded.polyBound b₁ e₁)) |
| @@ -110,7 +110,7 @@ | ||
| 110 | 110 | namespace Reduces |
| 111 | 111 | |
| 112 | 112 | /-- `≤ₚ` is reflexive. -/ |
| 113 | -theorem refl (L : Language α) : L ≤ₚ L := ⟨_, PolyTimeComputable.id, fun _ => Iff.rfl⟩ | |
| 113 | +theorem refl [IsFinite α] (L : Language α) : L ≤ₚ L := ⟨_, PolyTimeComputable.id, fun _ => Iff.rfl⟩ | |
| 114 | 114 | |
| 115 | 115 | /-- **Reductions compose**: `≤ₚ` is transitive. -/ |
| 116 | 116 | theorem trans {L₁ L₂ L₃ : Language α} (h₁₂ : L₁ ≤ₚ L₂) (h₂₃ : L₂ ≤ₚ L₃) : L₁ ≤ₚ L₃ := by |
| @@ -131,7 +131,7 @@ | ||
| 131 | 131 | obtain ⟨k₁, M₁, c₁, d₁, b, e, hM₁, hB⟩ := hf.normalize |
| 132 | 132 | obtain ⟨k₂, M₂, c₂, d₂, hM₂⟩ := hL'.polyBound |
| 133 | 133 | have hd := DecidesIn.seq (hM₁.lift (Nat.le_add_right k₁ k₂)) hB (hM₂.lift (Nat.le_add_left k₂ k₁)) |
| 134 | - refine ⟨k₁ + k₂, | |
| 134 | + refine ⟨hL'.1, k₁ + k₂, | |
| 135 | 135 | (M₁.lift (Embedding.castLE (Nat.le_add_right k₁ k₂))).seq |
| 136 | 136 | (M₂.lift (Embedding.castLE (Nat.le_add_left k₂ k₁))), |
| 137 | 137 | fun n => PvsNP.polyBound c₁ d₁ n + 1 + PvsNP.polyBound c₂ d₂ (PvsNP.polyBound b e n), ?_, ?_⟩ |
| @@ -7,7 +7,8 @@ | ||
| 7 | 7 | `0` once and halts, accepting when a test `p` on that symbol passes. It decides |
| 8 | 8 | `{x | p x.head? = true}` in one step, so each such language is in P. |
| 9 | 9 | |
| 10 | -Two instances over `Bool`: the words that start with `true`, and the empty word. | |
| 10 | +It needs a finite alphabet, like every class here: over `Nat`, a test on one symbol could be the | |
| 11 | +halting problem. Two instances over `Bool`: the words that start with `true`, and the empty word. | |
| 11 | 12 | -/ |
| 12 | 13 | |
| 13 | 14 | namespace PvsNP.Examples |
| @@ -48,8 +49,8 @@ | ||
| 48 | 49 | Tape.read_ofWord] |
| 49 | 50 | |
| 50 | 51 | /-- Every language decided by the first symbol is in P, within one step. -/ |
| 51 | -theorem firstSymbol_inP (p : Option α → Bool) : InP (FirstSymbol p) := by | |
| 52 | - refine ⟨0, firstSymbolMachine p, fun _ => 1, PolyBounded.const 1, fun x => ?_⟩ | |
| 52 | +theorem firstSymbol_inP [IsFinite α] (p : Option α → Bool) : InP (FirstSymbol p) := by | |
| 53 | + refine ⟨inferInstance, 0, firstSymbolMachine p, fun _ => 1, PolyBounded.const 1, fun x => ?_⟩ | |
| 53 | 54 | rw [firstSymbolMachine_run] |
| 54 | 55 | cases h : p x.head? <;> simp [firstSymbolMachine, FirstSymbol, h] |
| 55 | 56 | |