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

refactor: verify certificate guard scanning before arithmetic
Browse filesChecks
build Program · lake build, no sorry
Details
challenge Program · statement files and build config
Details
+0 −14 Problems/PVersusNP/ComputablyEnumerable.lean View file
| @@ -26,18 +26,4 @@ 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 | - | |
| 43 | 29 | end Millennium |
+0 −215 Problems/PVersusNP/Machines/CertificateBound.lean View file
| @@ -20,8 +20,6 @@ open Computability Turing StateTransition | ||
| 20 | 20 | |
| 21 | 21 | noncomputable section |
| 22 | 22 | |
| 23 | -set_option maxErrors 1 | |
| 24 | - | |
| 25 | 23 | private inductive Aux |
| 26 | 24 | | saved | base | temp | count | next | cert |
| 27 | 25 | deriving DecidableEq, Fintype |
| @@ -272,219 +270,6 @@ private def scanRight (ys saved : List (tm.Γ tm.k₀)) (base cert : ℕ) : | ||
| 272 | 270 | ih (y :: saved) (cert + 1)) |
| 273 | 271 | certificate_step |
| 274 | 272 | |
| 275 | -private def scanLeft (xs ys saved : List (tm.Γ tm.k₀)) (base : ℕ) | |
| 276 | - (noSep : ∀ x ∈ xs, x ≠ sep) : | |
| 277 | - Run tm sep reject k | |
| 278 | - (checkpoint (.scan false) { input := xs ++ sep :: ys, saved := saved, base := base }) | |
| 279 | - (checkpoint (.scan true) | |
| 280 | - { input := ys, saved := sep :: (xs.reverse ++ saved), base := base + xs.length, count := 1 }) := by | |
| 281 | - induction xs generalizing saved base with | |
| 282 | - | nil => apply oneStep; certificate_step | |
| 283 | - | cons x xs ih => | |
| 284 | - have hx : x ≠ sep := noSep x (by simp) | |
| 285 | - refine beginStep ?_ (by | |
| 286 | - simpa [List.reverse_cons, List.append_assoc, Nat.add_assoc, Nat.add_comm 1] using | |
| 287 | - ih (x :: saved) (base + 1) (fun z hz => noSep z (by simp [hz]))) | |
| 288 | - certificate_step | |
| 289 | - | |
| 290 | -private def copyRun (i : Fin (k + 1)) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : | |
| 291 | - Run tm sep reject k (checkpoint (.copy i) { w with base := n }) | |
| 292 | - (checkpoint (.putBack i) { w with base := 0, temp := w.temp + n, next := w.next + n }) := by | |
| 293 | - induction n generalizing w with | |
| 294 | - | zero => apply oneStep; certificate_step | |
| 295 | - | succ n ih => | |
| 296 | - refine beginStep ?_ (by | |
| 297 | - simpa [Nat.add_assoc, Nat.add_comm 1] using ih { w with temp := w.temp + 1, next := w.next + 1 }) | |
| 298 | - certificate_step | |
| 299 | - | |
| 300 | -private def putBackRun (i : Fin (k + 1)) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : | |
| 301 | - Run tm sep reject k (checkpoint (.putBack i) { w with temp := n }) | |
| 302 | - (checkpoint (.mul i) { w with base := w.base + n, temp := 0 }) := by | |
| 303 | - induction n generalizing w with | |
| 304 | - | zero => apply oneStep; certificate_step | |
| 305 | - | succ n ih => | |
| 306 | - refine beginStep ?_ (by | |
| 307 | - simpa [Nat.add_assoc, Nat.add_comm 1] using ih { w with base := w.base + 1 }) | |
| 308 | - certificate_step | |
| 309 | - | |
| 310 | -private def mulRun (i : Fin (k + 1)) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : | |
| 311 | - Run tm sep reject k (checkpoint (.mul i) { w with count := n, temp := 0 }) | |
| 312 | - (checkpoint (.transfer i) { w with count := 0, temp := 0, next := w.next + n * w.base }) := by | |
| 313 | - induction n generalizing w with | |
| 314 | - | zero => apply oneStep; certificate_step | |
| 315 | - | succ n ih => | |
| 316 | - have copied := copyRun (sep := sep) (reject := reject) i w.base { w with count := n, temp := 0 } | |
| 317 | - have returned := putBackRun (sep := sep) (reject := reject) i w.base | |
| 318 | - { w with base := 0, count := n, next := w.next + w.base } | |
| 319 | - simp only [Nat.zero_add] at copied returned | |
| 320 | - have remaining := ih { w with next := w.next + w.base } | |
| 321 | - have rest := EvalsTo.trans _ _ _ _ copied (EvalsTo.trans _ _ _ _ returned remaining) | |
| 322 | - refine beginStep ?_ (by | |
| 323 | - simpa [Nat.succ_mul, Nat.add_assoc, Nat.add_left_comm, Nat.add_comm] using rest) | |
| 324 | - certificate_step | |
| 325 | - | |
| 326 | -private def transferRun (i : Fin (k + 1)) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : | |
| 327 | - Run tm sep reject k (checkpoint (.transfer i) { w with next := n }) | |
| 328 | - (checkpoint (.power (prev i)) { w with next := 0, count := w.count + n }) := by | |
| 329 | - induction n generalizing w with | |
| 330 | - | zero => apply oneStep; certificate_step | |
| 331 | - | succ n ih => | |
| 332 | - refine beginStep ?_ (by | |
| 333 | - simpa [Nat.add_assoc, Nat.add_comm 1] using ih { w with count := w.count + 1 }) | |
| 334 | - certificate_step | |
| 335 | - | |
| 336 | -private def powerRun (n : ℕ) (i : Fin (k + 1)) (hi : i.val = n) (w : Work (tm.Γ tm.k₀)) : | |
| 337 | - Run tm sep reject k (checkpoint (.power i) { w with temp := 0, next := 0 }) | |
| 338 | - (checkpoint .clearBase { w with temp := 0, next := 0, count := w.count * w.base ^ n }) := by | |
| 339 | - induction n generalizing i w with | |
| 340 | - | zero => apply oneStep; certificate_step | |
| 341 | - | succ n ih => | |
| 342 | - have hn : i.val ≠ 0 := by omega | |
| 343 | - have hp : (prev i).val = n := by simp [prev, hi] | |
| 344 | - have multiplied := mulRun (sep := sep) (reject := reject) i w.count { w with next := 0 } | |
| 345 | - have transferred := transferRun (sep := sep) (reject := reject) i (w.count * w.base) | |
| 346 | - { w with count := 0, temp := 0 } | |
| 347 | - simp only [Nat.zero_add] at multiplied transferred | |
| 348 | - have remaining := ih (prev i) hp { w with count := w.count * w.base } | |
| 349 | - have rest := EvalsTo.trans _ _ _ _ multiplied (EvalsTo.trans _ _ _ _ transferred remaining) | |
| 350 | - refine beginStep ?_ (by simpa [pow_succ', Nat.mul_assoc] using rest) | |
| 351 | - certificate_step | |
| 352 | - | |
| 353 | -private def clearBaseRun (n : ℕ) (w : Work (tm.Γ tm.k₀)) : | |
| 354 | - Run tm sep reject k (checkpoint .clearBase { w with base := n }) | |
| 355 | - (checkpoint (.compare true) { w with base := 0 }) := by | |
| 356 | - induction n with | |
| 357 | - | zero => apply oneStep; certificate_step | |
| 358 | - | succ n ih => exact beginStep (by certificate_step) ih | |
| 359 | - | |
| 360 | -private def drainRun (ok : Bool) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : | |
| 361 | - Run tm sep reject k (checkpoint (.drain ok) { w with count := n }) | |
| 362 | - (checkpoint (.restore ok) { w with count := 0 }) := by | |
| 363 | - induction n with | |
| 364 | - | zero => apply oneStep; certificate_step | |
| 365 | - | succ n ih => exact beginStep (by certificate_step) ih | |
| 366 | - | |
| 367 | -private def compareRun (m n : ℕ) (ok : Bool) (w : Work (tm.Γ tm.k₀)) : | |
| 368 | - Run tm sep reject k (checkpoint (.compare ok) { w with cert := m, count := n }) | |
| 369 | - (checkpoint (.restore (ok && decide (m ≤ n))) { w with cert := 0, count := 0 }) := by | |
| 370 | - induction m generalizing n ok with | |
| 371 | - | zero => | |
| 372 | - refine beginStep ?_ (by simpa using drainRun ok n { w with cert := 0 }) | |
| 373 | - certificate_step | |
| 374 | - | succ m ih => | |
| 375 | - cases n with | |
| 376 | - | zero => | |
| 377 | - refine beginStep ?_ (by simpa using ih 0 false) | |
| 378 | - certificate_step | |
| 379 | - | succ n => | |
| 380 | - refine beginStep ?_ (by simpa using ih n ok) | |
| 381 | - certificate_step | |
| 382 | - | |
| 383 | -private def restoreRun (saved inp : List (tm.Γ tm.k₀)) : | |
| 384 | - Run tm sep reject k (checkpoint (.restore true) { input := inp, saved := saved }) | |
| 385 | - (liftCfg (initList tm (saved.reverse ++ inp))) := by | |
| 386 | - induction saved generalizing inp with | |
| 387 | - | nil => apply oneStep; certificate_step | |
| 388 | - | cons a xs ih => | |
| 389 | - refine beginStep ?_ (by simpa [List.reverse_cons, List.append_assoc] using ih (a :: inp)) | |
| 390 | - certificate_step | |
| 391 | - | |
| 392 | -private def rejectRun (saved : List (tm.Γ tm.k₀)) : | |
| 393 | - Run tm sep reject k (checkpoint (.restore false) { saved := saved }) | |
| 394 | - (haltList (guarded tm sep reject k) [reject]) := by | |
| 395 | - induction saved with | |
| 396 | - | nil => apply oneStep; certificate_step | |
| 397 | - | cons a xs ih => exact beginStep (by certificate_step) ih | |
| 398 | - | |
| 399 | -private theorem initial (inp : List (tm.Γ tm.k₀)) : | |
| 400 | - initList (guarded tm sep reject k) inp = checkpoint (.scan false) { input := inp } := by | |
| 401 | - simp only [initList, checkpoint, guarded] | |
| 402 | - congr | |
| 403 | - funext j | |
| 404 | - cases j with | |
| 405 | - | inl j => by_cases h : j = tm.k₀ <;> simp [h] | |
| 406 | - | inr j => cases j <;> rfl | |
| 407 | - | |
| 408 | -private theorem final (out : List (tm.Γ tm.k₁)) : | |
| 409 | - liftCfg (k := k) (haltList tm out) = haltList (guarded tm sep reject k) out := by | |
| 410 | - simp only [liftCfg, haltList, guarded, Option.map_none] | |
| 411 | - congr | |
| 412 | - funext j | |
| 413 | - cases j with | |
| 414 | - | inl j => by_cases h : j = tm.k₁ <;> simp [liftStacks, h] | |
| 415 | - | inr j => rfl | |
| 416 | - | |
| 417 | -/-- The old verifier's answer on certificates meeting the length bound, and false | |
| 418 | -otherwise. Length is measured on the left input's encoding and on the right word. -/ | |
| 419 | -def check {α Γ : Type} (ea : FinEncoding α) (k : ℕ) (verifier : α × List Γ → Bool) | |
| 420 | - (p : α × List Γ) : Bool := | |
| 421 | - if p.2.length ≤ (ea.encode p.1).length ^ k then verifier p else false | |
| 422 | - | |
| 423 | -/-- Adding Cook's certificate length bound preserves computable checking. -/ | |
| 424 | -def checked {α Γ : Type} [Fintype Γ] {ea : FinEncoding α} | |
| 425 | - {verifier : α × List Γ → Bool} | |
| 426 | - (h : TM2Computable (pair_encoding ea (fin_encoding_string Γ)).encode | |
| 427 | - finEncodingBoolBool.encode verifier) (k : ℕ) : | |
| 428 | - TM2Computable (pair_encoding ea (fin_encoding_string Γ)).encode | |
| 429 | - finEncodingBoolBool.encode (check ea k verifier) where | |
| 430 | - tm := guarded h.tm (h.inputAlphabet.symm (Sum.inr none)) (h.outputAlphabet.symm false) k | |
| 431 | - inputAlphabet := h.inputAlphabet | |
| 432 | - outputAlphabet := h.outputAlphabet | |
| 433 | - outputsFun p := by | |
| 434 | - let sep := h.inputAlphabet.symm (Sum.inr none) | |
| 435 | - let reject := h.outputAlphabet.symm false | |
| 436 | - let xs := (ea.encode p.1).map (fun a => h.inputAlphabet.symm (Sum.inl a)) | |
| 437 | - let ys := p.2.map (fun b => h.inputAlphabet.symm (Sum.inr (some b))) | |
| 438 | - have inputEq : | |
| 439 | - List.map h.inputAlphabet.invFun ((pair_encoding ea (fin_encoding_string Γ)).encode p) = | |
| 440 | - xs ++ sep :: ys := by | |
| 441 | - rw [pair_encoding.encode_eq, List.map_append, List.map_cons, List.map_map, List.map_map] | |
| 442 | - rfl | |
| 443 | - have noSep : ∀ x ∈ xs, x ≠ sep := by | |
| 444 | - intro x hx eq | |
| 445 | - obtain ⟨a, _, rfl⟩ := List.mem_map.mp hx | |
| 446 | - have bad := h.inputAlphabet.symm.injective eq | |
| 447 | - cases bad | |
| 448 | - have scan := EvalsTo.trans _ _ _ _ | |
| 449 | - (scanLeft (reject := reject) (k := k) xs ys [] 0 noSep) | |
| 450 | - (scanRight (sep := sep) (reject := reject) (k := k) | |
| 451 | - ys (sep :: (xs.reverse ++ [])) (0 + xs.length) 0) | |
| 452 | - let saved := ys.reverse ++ sep :: (xs.reverse ++ []) | |
| 453 | - have hs : saved.reverse = xs ++ sep :: ys := by | |
| 454 | - simp [saved, List.reverse_append, List.reverse_cons, List.append_assoc] | |
| 455 | - let n := 0 + xs.length | |
| 456 | - let m := 0 + ys.length | |
| 457 | - let cap := 1 * n ^ k | |
| 458 | - have powered := powerRun (sep := sep) (reject := reject) k ⟨k, by omega⟩ rfl | |
| 459 | - { saved := saved, base := n, count := 1, cert := m } | |
| 460 | - have cleared := clearBaseRun (sep := sep) (reject := reject) (k := k) n | |
| 461 | - { saved := saved, count := cap, cert := m } | |
| 462 | - have compared := compareRun (sep := sep) (reject := reject) (k := k) m cap true | |
| 463 | - { saved := saved } | |
| 464 | - have prefixRun := EvalsTo.trans _ _ _ _ scan (EvalsTo.trans _ _ _ _ powered | |
| 465 | - (EvalsTo.trans _ _ _ _ cleared compared)) | |
| 466 | - have lengths : (m ≤ cap) ↔ | |
| 467 | - p.2.length ≤ (ea.encode p.1).length ^ k := by simp [xs, ys, m, cap, n] | |
| 468 | - change Run h.tm sep reject k | |
| 469 | - (initList (guarded h.tm sep reject k) | |
| 470 | - (List.map h.inputAlphabet.invFun ((pair_encoding ea (fin_encoding_string Γ)).encode p))) | |
| 471 | - (haltList (guarded h.tm sep reject k) | |
| 472 | - (List.map h.outputAlphabet.invFun (finEncodingBoolBool.encode (check ea k verifier p)))) | |
| 473 | - rw [inputEq, initial] | |
| 474 | - by_cases bound : p.2.length ≤ (ea.encode p.1).length ^ k | |
| 475 | - · have yes := lengths.mpr bound | |
| 476 | - simp only [yes, decide_true, Bool.and_self] at prefixRun | |
| 477 | - have original := h.outputsFun p | |
| 478 | - rw [inputEq] at original | |
| 479 | - have restored := restoreRun (sep := sep) (reject := reject) (k := k) saved [] | |
| 480 | - rw [List.append_nil, hs] at restored | |
| 481 | - rw [check, if_pos bound, ← final] | |
| 482 | - exact EvalsTo.trans _ _ _ _ prefixRun (EvalsTo.trans _ _ _ _ restored (liftRun original)) | |
| 483 | - · have no := lengths.not.mpr bound | |
| 484 | - simp only [no, decide_false, Bool.and_false] at prefixRun | |
| 485 | - rw [check, if_neg bound] | |
| 486 | - exact EvalsTo.trans _ _ _ _ prefixRun (rejectRun saved) | |
| 487 | - | |
| 488 | 273 | end |
| 489 | 274 | |
| 490 | 275 | end Millennium.Machines.CertificateBound |