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

| 1 | import Problems.PVersusNP.Millennium |
| 2 | import Problems.PVersusNP.Machines.WordStacks |
| 3 | |
| 4 | /-! |
| 5 | # A linear-time palindrome decider |
| 6 | |
| 7 | Copy the input into two reversed work stacks. Restore one copy and compare it |
| 8 | with the other. Each loop uses the timed programs in `Machines.WordStacks`. |
| 9 | -/ |
| 10 | |
| 11 | namespace Millennium.Examples.Palindrome |
| 12 | |
| 13 | noncomputable section |
| 14 | |
| 15 | open Turing Computability StateTransition |
| 16 | open Millennium.Machines.WordStacks |
| 17 | |
| 18 | -- Keep the example's original names available to its callers. |
| 19 | export Millennium.Machines.WordStacks |
| 20 | (Stack Phase Alphabet Register machine cfg atPhase copy_cons copy_nil restore_cons |
| 21 | restore_nil compare_cons compare_nil copyRun restoreRun compareRun init_eq halt_eq) |
| 22 | |
| 23 | variable {α : Type} [Fintype α] [DecidableEq α] |
| 24 | |
| 25 | /-- Each phase takes at most one step per symbol and one final transition. -/ |
| 26 | def run (inp : List α) : |
| 27 | EvalsToInTime (machine α).step (initList (machine α) inp) |
| 28 | (some (haltList (machine α) [decide (inp = inp.reverse)])) |
| 29 | (3 * inp.length + 3) := by |
| 30 | have hc := copyRun inp (none, true) [] [] [] |
| 31 | have hr := restoreRun inp.reverse (none, true) [] inp.reverse [] |
| 32 | have he := compareRun inp inp.reverse (by simp) (none, true) [] |
| 33 | simp only [List.append_nil, List.reverse_reverse, List.length_reverse, Bool.true_and] at hc hr he |
| 34 | rw [init_eq, halt_eq] |
| 35 | have h := follow (follow hc hr) he |
| 36 | rw [show 3 * inp.length + 3 = |
| 37 | (inp.length + 1) + (inp.length + 1) + (inp.length + 1) by omega] |
| 38 | convert h using 1 |
| 39 | congr 5 |
| 40 | |
| 41 | /-- The polynomial-time witness for comparison with the reversed input. -/ |
| 42 | noncomputable def decider (α : Type) [Fintype α] [DecidableEq α] : |
| 43 | TM2ComputableInPolyTime (fin_encoding_string α).encode finEncodingBoolBool.encode |
| 44 | (fun inp => decide (inp = inp.reverse)) where |
| 45 | tm := machine α |
| 46 | inputAlphabet := Equiv.refl _ |
| 47 | outputAlphabet := Equiv.refl _ |
| 48 | time := 3 * Polynomial.X + 3 |
| 49 | outputsFun inp := by |
| 50 | show EvalsToInTime (machine α).step (initList (machine α) (List.map id inp)) |
| 51 | (some (haltList (machine α) (List.map id [decide (inp = inp.reverse)]))) |
| 52 | ((3 * Polynomial.X + 3 : Polynomial ℕ).eval inp.length) |
| 53 | simpa using run inp |
| 54 | |
| 55 | /-- Over every finite alphabet, the language of words equal to their reversal is in P. -/ |
| 56 | theorem palindromes_in_P (α : Type) [Fintype α] : |
| 57 | InPolynomialTime (fin_encoding_string α) (fun inp => inp = inp.reverse) := by |
| 58 | classical |
| 59 | exact ⟨_, decider α, fun _ => by simp⟩ |
| 60 | |
| 61 | #print axioms palindromes_in_P |
| 62 | |
| 63 | end |
| 64 | |
| 65 | end Millennium.Examples.Palindrome |