Require a finite alphabet in P, NP and polynomial-time functions

5bdcd988Claude · 6d agoparent 396d8a0dfiles
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 @@
33 /-!
44 # The class NP
55
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
77 `w` on tape `1`; the verifier halts within `T |x|` steps whatever the certificate, and `x ∈ L` exactly
88 when some certificate makes it accept. Because the time bound depends on `|x|` alone, only the first
99 `T |x|` symbols of a certificate can matter, so certificates are polynomially short in effect
@@ -22,27 +22,27 @@
2222
2323 /-- `L ∈ NP`: a machine verifies `L` in polynomial time. -/
2424 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
2626
2727 namespace InNP
2828
2929 /-- The time bound of a verifier can be taken of the form `c * (n + 1) ^ d`, hence monotone. -/
3030 theorem polyBound {L : Language α} (h : InNP L) :
3131 ∃ (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
3333 exact ⟨k, M, c, d, hM.mono hT⟩
3434
3535 end InNP
3636
3737 /-- **P ⊆ NP.** -/
3838 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
4040 let E := Embedding.skipOne (Nat.le_refl (k + 1))
4141 have e : ∀ x w, (M.lift E).init (verifierTapes x w) =
4242 M.liftCfg E (verifierTapes x w) (M.init (inputTapes x)) := by
4343 intro x w
4444 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 => ?_⟩
4646 · rw [e, Machine.run_lift]; exact (hM x).1
4747 · simp only [e, Machine.run_lift]
4848 exact ⟨fun hx => ⟨[], (hM x).2.mpr hx⟩, fun ⟨_, hw⟩ => (hM x).2.mp hw⟩
@@ -93,7 +93,7 @@
9393 (PvsNP.polyBound c₂ d₂ (f x).length)) := fun x w =>
9494 Machine.run_seq_verifier M₁ hk₁ V' hM₁ x w _ _ (hV'.1 (f x) w)
9595 (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 => ?_⟩
9797 · exact ((PolyBounded.polyBound c₁ d₁).add (PolyBounded.const 1)).add
9898 ((PolyBounded.polyBound c₂ d₂).comp (PolyBounded.polyBound b e))
9999 · rw [key]; exact hV'.1 (f x) w
@@ -4,7 +4,10 @@
44 /-!
55 # The class P
66
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.
811
912 Proved here: the full and the empty language are in P, and P is closed under complement.
1013 -/
@@ -13,9 +16,9 @@
1316
1417 variable {α : Type}
1518
16-/-- `L ∈ P`: a machine decides `L` in polynomial time. -/
19+/-- `L ∈ P`: over a finite alphabet, a machine decides `L` in polynomial time. -/
1720 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
1922
2023 /-- A machine that halts at once, accepting or not as `b` says, touching nothing. -/
2124 def Machine.const (α : Type) (k : Nat) (b : Bool) : Machine α k where
@@ -31,21 +34,24 @@
3134 /-- The time bound of a language in P can be taken of the form `c * (n + 1) ^ d`, hence monotone. -/
3235 theorem polyBound {L : Language α} (h : InP L) :
3336 ∃ (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
3538 exact ⟨k, M, c, d, hM.mono hT⟩
3639
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 _ =>
3945 ⟨rfl, by simp [Machine.const, Language.univ, Machine.init]⟩⟩
4046
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 _ =>
4349 ⟨rfl, by simp [Machine.const, Language.empty, Machine.init]⟩⟩
4450
4551 /-- **P is closed under complement**: swap accepting and rejecting halted states. -/
4652 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 => ?_⟩
4955 have e : (M.withAccepting (fun q => !M.accepting q)).init (inputTapes x) = M.init (inputTapes x) :=
5056 rfl
5157 rw [e, Machine.run_withAccepting]
@@ -54,8 +60,8 @@
5460 ← (hM x).2, Bool.not_eq_true]
5561
5662 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]⟩⟩
5965
6066 theorem compl_iff {L : Language α} : InP L.compl ↔ InP L := ⟨of_compl, compl⟩
6167
@@ -3,7 +3,7 @@
33 /-!
44 # Polynomial-time reductions
55
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
77 in `|x|`. (The output bound follows from the time bound in any reasonable model; we state it rather
88 than derive it. Deriving it is on the roadmap.)
99
@@ -19,7 +19,7 @@
1919
2020 /-- `f` is computable in polynomial time, with polynomially bounded output. -/
2121 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 ∧
2323 ComputesIn M f T ∧ ∀ x, (f x).length ≤ B x.length
2424
2525 /-- `L` many-one reduces to `L'` in polynomial time. -/
@@ -83,12 +83,12 @@
8383 theorem normalize {f : List α → List α} (h : PolyTimeComputable f) :
8484 ∃ (k : Nat) (M : Machine α k) (c d b e : Nat), ComputesIn M f (polyBound c d) ∧
8585 ∀ 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
8787 exact ⟨k, M, c, d, b, e, hM.mono hT, fun x => Nat.le_trans (hfB x) (hB _)⟩
8888
8989 /-- 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,
9292 fun _ => ⟨rfl, rfl⟩, fun _ => Nat.le_refl _⟩
9393
9494 /-- **Polynomial-time functions compose.** -/
@@ -98,7 +98,7 @@
9898 obtain ⟨k₂, M₂, c₂, d₂, b₂, e₂, hM₂, hB₂⟩ := hg.normalize
9999 have hl₁ := hM₁.lift (Nat.le_add_right k₁ k₂)
100100 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),
102102 ?_, ?_, hl₁.seq hB₁ hl₂, fun x => ?_⟩
103103 · exact ((PolyBounded.polyBound c₁ d₁).add (PolyBounded.const 1)).add
104104 ((PolyBounded.polyBound c₂ d₂).comp (PolyBounded.polyBound b₁ e₁))
@@ -110,7 +110,7 @@
110110 namespace Reduces
111111
112112 /-- `≤ₚ` 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⟩
114114
115115 /-- **Reductions compose**: `≤ₚ` is transitive. -/
116116 theorem trans {L₁ L₂ L₃ : Language α} (h₁₂ : L₁ ≤ₚ L₂) (h₂₃ : L₂ ≤ₚ L₃) : L₁ ≤ₚ L₃ := by
@@ -131,7 +131,7 @@
131131 obtain ⟨k₁, M₁, c₁, d₁, b, e, hM₁, hB⟩ := hf.normalize
132132 obtain ⟨k₂, M₂, c₂, d₂, hM₂⟩ := hL'.polyBound
133133 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₂,
135135 (M₁.lift (Embedding.castLE (Nat.le_add_right k₁ k₂))).seq
136136 (M₂.lift (Embedding.castLE (Nat.le_add_left k₂ k₁))),
137137 fun n => PvsNP.polyBound c₁ d₁ n + 1 + PvsNP.polyBound c₂ d₂ (PvsNP.polyBound b e n), ?_, ?_⟩
@@ -7,7 +7,8 @@
77 `0` once and halts, accepting when a test `p` on that symbol passes. It decides
88 `{x | p x.head? = true}` in one step, so each such language is in P.
99
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.
1112 -/
1213
1314 namespace PvsNP.Examples
@@ -48,8 +49,8 @@
4849 Tape.read_ofWord]
4950
5051 /-- 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 => ?_⟩
5354 rw [firstSymbolMachine_run]
5455 cases h : p x.head? <;> simp [firstSymbolMachine, FirstSymbol, h]
5556