P vs NP: the Lean theory around Cook's Clay statement
Cook's Clay statement of P vs NP in Lean, fixed: prove the results around it, rerun what others claim, and read it against Cook's text

fix: prove arithmetic and compose the complete certificate guard
Browse filesChecks
build Program · lake build, no sorry
Details
challenge Program · statement files and build config
Details
+14 −0 Problems/PVersusNP/ComputablyEnumerable.lean View file
| @@ -26,4 +26,18 @@ theorem PolynomialTimeCheckingRelation.toComputableCheckingRelation {α β : Typ | ||
| 26 | 26 | obtain ⟨verifier, machine, correct⟩ := h |
| 27 | 27 | exact ⟨verifier, machine.toTM2ComputableInTime.toTM2Computable, fun a b => correct (a, b)⟩ |
| 28 | 28 | |
| 29 | +/-- Every NP language is computably enumerable, using a computable checker that also | |
| 30 | +rejects certificates longer than the original NP bound. -/ | |
| 31 | +theorem InNondeterministicPolynomialTime.toComputablyEnumerable {α : Type} | |
| 32 | + {ea : FinEncoding α} {L : Language α} | |
| 33 | + (h : InNondeterministicPolynomialTime ea L) : ComputablyEnumerable ea L := by | |
| 34 | + obtain ⟨Γ, hΓ, R, k, hR, correct⟩ := h | |
| 35 | + letI : Fintype Γ := hΓ | |
| 36 | + obtain ⟨verifier, machine, verifies⟩ := hR.toComputableCheckingRelation | |
| 37 | + refine ⟨Γ, hΓ, fun a y => y.length ≤ (ea.encode a).length ^ k ∧ R a y, ?_, correct⟩ | |
| 38 | + exact ⟨_, Machines.CertificateBound.checked machine k, fun a y => by | |
| 39 | + by_cases bound : y.length ≤ (ea.encode a).length ^ k | |
| 40 | + · simpa [bound, Machines.CertificateBound.check] using verifies a y | |
| 41 | + · simp [bound, Machines.CertificateBound.check]⟩ | |
| 42 | + | |
| 29 | 43 | end Millennium |
+83 −9 Problems/PVersusNP/Machines/CertificateBound.lean View file
| @@ -253,8 +253,8 @@ macro "certificate_step" : tactic => do | ||
| 253 | 253 | (funext s |
| 254 | 254 | cases s with |
| 255 | 255 | | inl j => by_cases hj : j = ($machine).k₀ <;> by_cases ho : j = ($machine).k₁ <;> |
| 256 | - (try subst j) <;> simp_all [initList, haltList, Function.update_of_ne] | |
| 257 | - | inr j => cases j <;> simp_all [Function.update_of_ne]))) | |
| 256 | + (try subst j) <;> simp_all [initList, haltList, liftStacks, Function.update] | |
| 257 | + | inr j => cases j <;> simp_all [liftStacks, Function.update_of_ne]))) | |
| 258 | 258 | |
| 259 | 259 | private def scanRight (ys saved : List (tm.Γ tm.k₀)) (base cert : ℕ) : |
| 260 | 260 | Run tm sep reject k |
| @@ -316,9 +316,10 @@ private def mulRun (i : Fin (k + 1)) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : | ||
| 316 | 316 | simp only [Nat.zero_add] at copied returned |
| 317 | 317 | have remaining := ih { w with next := w.next + w.base } |
| 318 | 318 | have rest := EvalsTo.trans _ _ _ _ copied (EvalsTo.trans _ _ _ _ returned remaining) |
| 319 | - refine beginStep ?_ (by | |
| 320 | - simpa [Nat.succ_mul, Nat.add_assoc, Nat.add_left_comm, Nat.add_comm] using rest) | |
| 321 | - certificate_step | |
| 319 | + have sums : w.next + w.base + n * w.base = w.next + (n + 1) * w.base := by | |
| 320 | + rw [Nat.succ_mul] | |
| 321 | + omega | |
| 322 | + exact beginStep (by certificate_step) (sums ▸ rest) | |
| 322 | 323 | |
| 323 | 324 | private def transferRun (i : Fin (k + 1)) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : |
| 324 | 325 | Run tm sep reject k (checkpoint (.transfer i) { w with next := n }) |
| @@ -344,8 +345,9 @@ private def powerRun (n : ℕ) (i : Fin (k + 1)) (hi : i.val = n) (w : Work (tm. | ||
| 344 | 345 | simp only [Nat.zero_add] at multiplied transferred |
| 345 | 346 | have remaining := ih (prev i) hp { w with count := w.count * w.base } |
| 346 | 347 | have rest := EvalsTo.trans _ _ _ _ multiplied (EvalsTo.trans _ _ _ _ transferred remaining) |
| 347 | - refine beginStep ?_ (by simpa [pow_succ', Nat.mul_assoc] using rest) | |
| 348 | - certificate_step | |
| 348 | + have product : (w.count * w.base) * w.base ^ n = w.count * w.base ^ (n + 1) := by | |
| 349 | + rw [pow_succ', Nat.mul_assoc] | |
| 350 | + exact beginStep (by certificate_step) (product ▸ rest) | |
| 349 | 351 | |
| 350 | 352 | private def clearBaseRun (n : ℕ) (w : Work (tm.Γ tm.k₀)) : |
| 351 | 353 | Run tm sep reject k (checkpoint .clearBase { w with base := n }) |
| @@ -399,7 +401,7 @@ private theorem initial (inp : List (tm.Γ tm.k₀)) : | ||
| 399 | 401 | congr |
| 400 | 402 | funext j |
| 401 | 403 | cases j with |
| 402 | - | inl j => by_cases h : j = tm.k₀ <;> simp [h] | |
| 404 | + | inl j => by_cases h : j = tm.k₀ <;> (try subst j) <;> simp [*] | |
| 403 | 405 | | inr j => cases j <;> rfl |
| 404 | 406 | |
| 405 | 407 | private theorem final (out : List (tm.Γ tm.k₁)) : |
| @@ -408,9 +410,81 @@ private theorem final (out : List (tm.Γ tm.k₁)) : | ||
| 408 | 410 | congr |
| 409 | 411 | funext j |
| 410 | 412 | cases j with |
| 411 | - | inl j => by_cases h : j = tm.k₁ <;> simp [liftStacks, h] | |
| 413 | + | inl j => by_cases h : j = tm.k₁ <;> (try subst j) <;> simp [liftStacks, *] | |
| 412 | 414 | | inr j => rfl |
| 413 | 415 | |
| 416 | +/-- The old verifier's answer on certificates meeting the length bound, and false | |
| 417 | +otherwise. Length is measured on the left input's encoding and on the right word. -/ | |
| 418 | +def check {α Γ : Type} (ea : FinEncoding α) (k : ℕ) (verifier : α × List Γ → Bool) | |
| 419 | + (p : α × List Γ) : Bool := | |
| 420 | + if p.2.length ≤ (ea.encode p.1).length ^ k then verifier p else false | |
| 421 | + | |
| 422 | +/-- Adding Cook's certificate length bound preserves computable checking. -/ | |
| 423 | +def checked {α Γ : Type} [Fintype Γ] {ea : FinEncoding α} | |
| 424 | + {verifier : α × List Γ → Bool} | |
| 425 | + (h : TM2Computable (pair_encoding ea (fin_encoding_string Γ)).encode | |
| 426 | + finEncodingBoolBool.encode verifier) (k : ℕ) : | |
| 427 | + TM2Computable (pair_encoding ea (fin_encoding_string Γ)).encode | |
| 428 | + finEncodingBoolBool.encode (check ea k verifier) where | |
| 429 | + tm := guarded h.tm (h.inputAlphabet.symm (Sum.inr none)) (h.outputAlphabet.symm false) k | |
| 430 | + inputAlphabet := h.inputAlphabet | |
| 431 | + outputAlphabet := h.outputAlphabet | |
| 432 | + outputsFun p := by | |
| 433 | + let sep := h.inputAlphabet.symm (Sum.inr none) | |
| 434 | + let reject := h.outputAlphabet.symm false | |
| 435 | + let xs := (ea.encode p.1).map (fun a => h.inputAlphabet.symm (Sum.inl a)) | |
| 436 | + let ys := p.2.map (fun b => h.inputAlphabet.symm (Sum.inr (some b))) | |
| 437 | + have inputEq : | |
| 438 | + List.map h.inputAlphabet.invFun ((pair_encoding ea (fin_encoding_string Γ)).encode p) = | |
| 439 | + xs ++ sep :: ys := by | |
| 440 | + rw [pair_encoding.encode_eq, List.map_append, List.map_cons, List.map_map, List.map_map] | |
| 441 | + rfl | |
| 442 | + have noSep : ∀ x ∈ xs, x ≠ sep := by | |
| 443 | + intro x hx eq | |
| 444 | + obtain ⟨a, _, rfl⟩ := List.mem_map.mp hx | |
| 445 | + have bad := h.inputAlphabet.symm.injective eq | |
| 446 | + cases bad | |
| 447 | + have scan := EvalsTo.trans _ _ _ _ | |
| 448 | + (scanLeft (reject := reject) (k := k) xs ys [] 0 noSep) | |
| 449 | + (scanRight (sep := sep) (reject := reject) (k := k) | |
| 450 | + ys (sep :: (xs.reverse ++ [])) (0 + xs.length) 0) | |
| 451 | + let saved := ys.reverse ++ sep :: (xs.reverse ++ []) | |
| 452 | + have hs : saved.reverse = xs ++ sep :: ys := by | |
| 453 | + simp [saved, List.reverse_append, List.reverse_cons, List.append_assoc] | |
| 454 | + let n := 0 + xs.length | |
| 455 | + let m := 0 + ys.length | |
| 456 | + let cap := 1 * n ^ k | |
| 457 | + have powered := powerRun (sep := sep) (reject := reject) k ⟨k, by omega⟩ rfl | |
| 458 | + { saved := saved, base := n, count := 1, cert := m } | |
| 459 | + have cleared := clearBaseRun (sep := sep) (reject := reject) (k := k) n | |
| 460 | + { saved := saved, count := cap, cert := m } | |
| 461 | + have compared := compareRun (sep := sep) (reject := reject) (k := k) m cap true | |
| 462 | + { saved := saved } | |
| 463 | + have prefixRun := EvalsTo.trans _ _ _ _ scan (EvalsTo.trans _ _ _ _ powered | |
| 464 | + (EvalsTo.trans _ _ _ _ cleared compared)) | |
| 465 | + have lengths : (m ≤ cap) ↔ | |
| 466 | + p.2.length ≤ (ea.encode p.1).length ^ k := by simp [xs, ys, m, cap, n] | |
| 467 | + change Run h.tm sep reject k | |
| 468 | + (initList (guarded h.tm sep reject k) | |
| 469 | + (List.map h.inputAlphabet.invFun ((pair_encoding ea (fin_encoding_string Γ)).encode p))) | |
| 470 | + (haltList (guarded h.tm sep reject k) | |
| 471 | + (List.map h.outputAlphabet.invFun (finEncodingBoolBool.encode (check ea k verifier p)))) | |
| 472 | + have started := initial (sep := sep) (reject := reject) (k := k) (xs ++ sep :: ys) | |
| 473 | + by_cases bound : p.2.length ≤ (ea.encode p.1).length ^ k | |
| 474 | + · have yes := lengths.mpr bound | |
| 475 | + simp only [yes, decide_true, Bool.and_self] at prefixRun | |
| 476 | + have original := h.outputsFun p | |
| 477 | + rw [inputEq] at original | |
| 478 | + have restored := restoreRun (sep := sep) (reject := reject) (k := k) saved [] | |
| 479 | + rw [List.append_nil, hs] at restored | |
| 480 | + have doneRun := EvalsTo.trans _ _ _ _ prefixRun | |
| 481 | + (EvalsTo.trans _ _ _ _ restored (liftRun original)) | |
| 482 | + rw [inputEq, started, check, if_pos bound] | |
| 483 | + exact (final _ ▸ doneRun) | |
| 484 | + · have no := lengths.not.mpr bound | |
| 485 | + simp only [no, decide_false, Bool.and_false] at prefixRun | |
| 486 | + rw [inputEq, started, check, if_neg bound] | |
| 487 | + exact EvalsTo.trans _ _ _ _ prefixRun (rejectRun saved) | |
| 414 | 488 | |
| 415 | 489 | end |
| 416 | 490 | |