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: substitute dependent stack indices in guard steps
Browse filesChecks
build Program · lake build, no sorry
Details
challenge Program · statement files and build config
Details
+144 −1 Problems/PVersusNP/Machines/CertificateBound.lean View file
| @@ -253,7 +253,7 @@ 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 | - simp_all [initList, haltList, Function.update_of_ne] | |
| 256 | + (try subst j) <;> simp_all [initList, haltList, Function.update_of_ne] | |
| 257 | 257 | | inr j => cases j <;> simp_all [Function.update_of_ne]))) |
| 258 | 258 | |
| 259 | 259 | private def scanRight (ys saved : List (tm.Γ tm.k₀)) (base cert : ℕ) : |
| @@ -269,6 +269,149 @@ private def scanRight (ys saved : List (tm.Γ tm.k₀)) (base cert : ℕ) : | ||
| 269 | 269 | ih (y :: saved) (cert + 1)) |
| 270 | 270 | certificate_step |
| 271 | 271 | |
| 272 | +private def scanLeft (xs ys saved : List (tm.Γ tm.k₀)) (base : ℕ) | |
| 273 | + (noSep : ∀ x ∈ xs, x ≠ sep) : | |
| 274 | + Run tm sep reject k | |
| 275 | + (checkpoint (.scan false) { input := xs ++ sep :: ys, saved := saved, base := base }) | |
| 276 | + (checkpoint (.scan true) | |
| 277 | + { input := ys, saved := sep :: (xs.reverse ++ saved), base := base + xs.length, count := 1 }) := by | |
| 278 | + induction xs generalizing saved base with | |
| 279 | + | nil => apply oneStep; certificate_step | |
| 280 | + | cons x xs ih => | |
| 281 | + have hx : x ≠ sep := noSep x (by simp) | |
| 282 | + refine beginStep ?_ (by | |
| 283 | + simpa [List.reverse_cons, List.append_assoc, Nat.add_assoc, Nat.add_comm 1] using | |
| 284 | + ih (x :: saved) (base + 1) (fun z hz => noSep z (by simp [hz]))) | |
| 285 | + certificate_step | |
| 286 | + | |
| 287 | +private def copyRun (i : Fin (k + 1)) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : | |
| 288 | + Run tm sep reject k (checkpoint (.copy i) { w with base := n }) | |
| 289 | + (checkpoint (.putBack i) { w with base := 0, temp := w.temp + n, next := w.next + n }) := by | |
| 290 | + induction n generalizing w with | |
| 291 | + | zero => apply oneStep; certificate_step | |
| 292 | + | succ n ih => | |
| 293 | + refine beginStep ?_ (by | |
| 294 | + simpa [Nat.add_assoc, Nat.add_comm 1] using ih { w with temp := w.temp + 1, next := w.next + 1 }) | |
| 295 | + certificate_step | |
| 296 | + | |
| 297 | +private def putBackRun (i : Fin (k + 1)) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : | |
| 298 | + Run tm sep reject k (checkpoint (.putBack i) { w with temp := n }) | |
| 299 | + (checkpoint (.mul i) { w with base := w.base + n, temp := 0 }) := by | |
| 300 | + induction n generalizing w with | |
| 301 | + | zero => apply oneStep; certificate_step | |
| 302 | + | succ n ih => | |
| 303 | + refine beginStep ?_ (by | |
| 304 | + simpa [Nat.add_assoc, Nat.add_comm 1] using ih { w with base := w.base + 1 }) | |
| 305 | + certificate_step | |
| 306 | + | |
| 307 | +private def mulRun (i : Fin (k + 1)) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : | |
| 308 | + Run tm sep reject k (checkpoint (.mul i) { w with count := n, temp := 0 }) | |
| 309 | + (checkpoint (.transfer i) { w with count := 0, temp := 0, next := w.next + n * w.base }) := by | |
| 310 | + induction n generalizing w with | |
| 311 | + | zero => apply oneStep; certificate_step | |
| 312 | + | succ n ih => | |
| 313 | + have copied := copyRun (sep := sep) (reject := reject) i w.base { w with count := n, temp := 0 } | |
| 314 | + have returned := putBackRun (sep := sep) (reject := reject) i w.base | |
| 315 | + { w with base := 0, count := n, next := w.next + w.base } | |
| 316 | + simp only [Nat.zero_add] at copied returned | |
| 317 | + have remaining := ih { w with next := w.next + w.base } | |
| 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 | |
| 322 | + | |
| 323 | +private def transferRun (i : Fin (k + 1)) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : | |
| 324 | + Run tm sep reject k (checkpoint (.transfer i) { w with next := n }) | |
| 325 | + (checkpoint (.power (prev i)) { w with next := 0, count := w.count + n }) := by | |
| 326 | + induction n generalizing w with | |
| 327 | + | zero => apply oneStep; certificate_step | |
| 328 | + | succ n ih => | |
| 329 | + refine beginStep ?_ (by | |
| 330 | + simpa [Nat.add_assoc, Nat.add_comm 1] using ih { w with count := w.count + 1 }) | |
| 331 | + certificate_step | |
| 332 | + | |
| 333 | +private def powerRun (n : ℕ) (i : Fin (k + 1)) (hi : i.val = n) (w : Work (tm.Γ tm.k₀)) : | |
| 334 | + Run tm sep reject k (checkpoint (.power i) { w with temp := 0, next := 0 }) | |
| 335 | + (checkpoint .clearBase { w with temp := 0, next := 0, count := w.count * w.base ^ n }) := by | |
| 336 | + induction n generalizing i w with | |
| 337 | + | zero => apply oneStep; certificate_step | |
| 338 | + | succ n ih => | |
| 339 | + have hn : i.val ≠ 0 := by omega | |
| 340 | + have hp : (prev i).val = n := by simp [prev, hi] | |
| 341 | + have multiplied := mulRun (sep := sep) (reject := reject) i w.count { w with next := 0 } | |
| 342 | + have transferred := transferRun (sep := sep) (reject := reject) i (w.count * w.base) | |
| 343 | + { w with count := 0, temp := 0 } | |
| 344 | + simp only [Nat.zero_add] at multiplied transferred | |
| 345 | + have remaining := ih (prev i) hp { w with count := w.count * w.base } | |
| 346 | + have rest := EvalsTo.trans _ _ _ _ multiplied (EvalsTo.trans _ _ _ _ transferred remaining) | |
| 347 | + refine beginStep ?_ (by simpa [pow_succ', Nat.mul_assoc] using rest) | |
| 348 | + certificate_step | |
| 349 | + | |
| 350 | +private def clearBaseRun (n : ℕ) (w : Work (tm.Γ tm.k₀)) : | |
| 351 | + Run tm sep reject k (checkpoint .clearBase { w with base := n }) | |
| 352 | + (checkpoint (.compare true) { w with base := 0 }) := by | |
| 353 | + induction n with | |
| 354 | + | zero => apply oneStep; certificate_step | |
| 355 | + | succ n ih => exact beginStep (by certificate_step) ih | |
| 356 | + | |
| 357 | +private def drainRun (ok : Bool) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : | |
| 358 | + Run tm sep reject k (checkpoint (.drain ok) { w with count := n }) | |
| 359 | + (checkpoint (.restore ok) { w with count := 0 }) := by | |
| 360 | + induction n with | |
| 361 | + | zero => apply oneStep; certificate_step | |
| 362 | + | succ n ih => exact beginStep (by certificate_step) ih | |
| 363 | + | |
| 364 | +private def compareRun (m n : ℕ) (ok : Bool) (w : Work (tm.Γ tm.k₀)) : | |
| 365 | + Run tm sep reject k (checkpoint (.compare ok) { w with cert := m, count := n }) | |
| 366 | + (checkpoint (.restore (ok && decide (m ≤ n))) { w with cert := 0, count := 0 }) := by | |
| 367 | + induction m generalizing n ok with | |
| 368 | + | zero => | |
| 369 | + refine beginStep ?_ (by simpa using drainRun ok n { w with cert := 0 }) | |
| 370 | + certificate_step | |
| 371 | + | succ m ih => | |
| 372 | + cases n with | |
| 373 | + | zero => | |
| 374 | + refine beginStep ?_ (by simpa using ih 0 false) | |
| 375 | + certificate_step | |
| 376 | + | succ n => | |
| 377 | + refine beginStep ?_ (by simpa using ih n ok) | |
| 378 | + certificate_step | |
| 379 | + | |
| 380 | +private def restoreRun (saved inp : List (tm.Γ tm.k₀)) : | |
| 381 | + Run tm sep reject k (checkpoint (.restore true) { input := inp, saved := saved }) | |
| 382 | + (liftCfg (initList tm (saved.reverse ++ inp))) := by | |
| 383 | + induction saved generalizing inp with | |
| 384 | + | nil => apply oneStep; certificate_step | |
| 385 | + | cons a xs ih => | |
| 386 | + refine beginStep ?_ (by simpa [List.reverse_cons, List.append_assoc] using ih (a :: inp)) | |
| 387 | + certificate_step | |
| 388 | + | |
| 389 | +private def rejectRun (saved : List (tm.Γ tm.k₀)) : | |
| 390 | + Run tm sep reject k (checkpoint (.restore false) { saved := saved }) | |
| 391 | + (haltList (guarded tm sep reject k) [reject]) := by | |
| 392 | + induction saved with | |
| 393 | + | nil => apply oneStep; certificate_step | |
| 394 | + | cons a xs ih => exact beginStep (by certificate_step) ih | |
| 395 | + | |
| 396 | +private theorem initial (inp : List (tm.Γ tm.k₀)) : | |
| 397 | + initList (guarded tm sep reject k) inp = checkpoint (.scan false) { input := inp } := by | |
| 398 | + simp only [initList, checkpoint, guarded] | |
| 399 | + congr | |
| 400 | + funext j | |
| 401 | + cases j with | |
| 402 | + | inl j => by_cases h : j = tm.k₀ <;> simp [h] | |
| 403 | + | inr j => cases j <;> rfl | |
| 404 | + | |
| 405 | +private theorem final (out : List (tm.Γ tm.k₁)) : | |
| 406 | + liftCfg (k := k) (haltList tm out) = haltList (guarded tm sep reject k) out := by | |
| 407 | + simp only [liftCfg, haltList, guarded, Option.map_none] | |
| 408 | + congr | |
| 409 | + funext j | |
| 410 | + cases j with | |
| 411 | + | inl j => by_cases h : j = tm.k₁ <;> simp [liftStacks, h] | |
| 412 | + | inr j => rfl | |
| 413 | + | |
| 414 | + | |
| 272 | 415 | end |
| 273 | 416 | |
| 274 | 417 | end Millennium.Machines.CertificateBound |