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

Normalize the measured time bounds and expose the loop configurations
Browse filesChecks
build Program · lake build, no sorry
Details
challenge Program · statement files and build config
Details
+26 −23 Problems/PVersusNP/Examples/Palindrome.lean View file
| @@ -78,41 +78,42 @@ def cfg (l : Option Phase) (v : Register α) (inp scratch backwards : List α) | ||
| 78 | 78 | | .backwards => backwards.map some |
| 79 | 79 | | .output => out |
| 80 | 80 | |
| 81 | -private def Go (phase : Phase) (v : Register α) (inp s r : List α) (out : List Bool) := | |
| 81 | +/-- A running configuration at one of the three loop labels. -/ | |
| 82 | +abbrev atPhase (phase : Phase) (v : Register α) (inp s r : List α) (out : List Bool) := | |
| 82 | 83 | cfg (some phase) v inp s r out |
| 83 | 84 | |
| 84 | 85 | macro "pal_step" : tactic => |
| 85 | - `(tactic| (simp [Go, FinTM2.step, TM2.step, TM2.stepAux, machine, cfg, putInput, *] | |
| 86 | + `(tactic| (simp [atPhase, FinTM2.step, TM2.step, TM2.stepAux, machine, cfg, putInput, *] | |
| 86 | 87 | try (congr <;> first | rfl | (funext k; cases k <;> rfl) | (simp; done)))) |
| 87 | 88 | |
| 88 | 89 | theorem copy_cons (v : Register α) (a : α) (inp s r : List α) (out : List Bool) : |
| 89 | - (machine α).step (Go .copy v (a :: inp) s r out) = | |
| 90 | - some (Go .copy (some a, v.2) inp (a :: s) (a :: r) out) := by | |
| 90 | + (machine α).step (atPhase .copy v (a :: inp) s r out) = | |
| 91 | + some (atPhase .copy (some a, v.2) inp (a :: s) (a :: r) out) := by | |
| 91 | 92 | pal_step |
| 92 | 93 | |
| 93 | 94 | theorem copy_nil (v : Register α) (s r : List α) (out : List Bool) : |
| 94 | - (machine α).step (Go .copy v [] s r out) = | |
| 95 | - some (Go .restore (none, v.2) [] s r out) := by | |
| 95 | + (machine α).step (atPhase .copy v [] s r out) = | |
| 96 | + some (atPhase .restore (none, v.2) [] s r out) := by | |
| 96 | 97 | pal_step |
| 97 | 98 | |
| 98 | 99 | theorem restore_cons (v : Register α) (a : α) (inp s r : List α) (out : List Bool) : |
| 99 | - (machine α).step (Go .restore v inp (a :: s) r out) = | |
| 100 | - some (Go .restore (some a, v.2) (a :: inp) s r out) := by | |
| 100 | + (machine α).step (atPhase .restore v inp (a :: s) r out) = | |
| 101 | + some (atPhase .restore (some a, v.2) (a :: inp) s r out) := by | |
| 101 | 102 | have hex : Nonempty α := ⟨a⟩ |
| 102 | 103 | pal_step |
| 103 | 104 | |
| 104 | 105 | theorem restore_nil (v : Register α) (inp r : List α) (out : List Bool) : |
| 105 | - (machine α).step (Go .restore v inp [] r out) = | |
| 106 | - some (Go .compare (none, v.2) inp [] r out) := by | |
| 106 | + (machine α).step (atPhase .restore v inp [] r out) = | |
| 107 | + some (atPhase .compare (none, v.2) inp [] r out) := by | |
| 107 | 108 | pal_step |
| 108 | 109 | |
| 109 | 110 | theorem compare_cons (v : Register α) (a b : α) (xs ys : List α) (out : List Bool) : |
| 110 | - (machine α).step (Go .compare v (a :: xs) [] (b :: ys) out) = | |
| 111 | - some (Go .compare (none, v.2 && decide (a = b)) xs [] ys out) := by | |
| 111 | + (machine α).step (atPhase .compare v (a :: xs) [] (b :: ys) out) = | |
| 112 | + some (atPhase .compare (none, v.2 && decide (a = b)) xs [] ys out) := by | |
| 112 | 113 | pal_step |
| 113 | 114 | |
| 114 | 115 | theorem compare_nil (v : Register α) (out : List Bool) : |
| 115 | - (machine α).step (Go .compare v [] [] [] out) = | |
| 116 | + (machine α).step (atPhase .compare v [] [] [] out) = | |
| 116 | 117 | some (cfg none (none, true) ([] : List α) [] [] (v.2 :: out)) := by |
| 117 | 118 | pal_step |
| 118 | 119 | |
| @@ -127,12 +128,12 @@ private def follow {s t u : (machine α).Cfg} {m n : ℕ} | ||
| 127 | 128 | (a : EvalsToInTime (machine α).step s (some t) m) |
| 128 | 129 | (b : EvalsToInTime (machine α).step t (some u) n) : |
| 129 | 130 | EvalsToInTime (machine α).step s (some u) (m + n) := |
| 130 | - EvalsToInTime.trans _ m n _ _ _ a b | |
| 131 | + (Nat.add_comm n m) ▸ EvalsToInTime.trans _ m n _ _ _ a b | |
| 131 | 132 | |
| 132 | 133 | /-- After consuming the input, each work stack has acquired its reverse. -/ |
| 133 | 134 | def copyRun (inp : List α) (v : Register α) (s r : List α) (out : List Bool) : |
| 134 | - EvalsToInTime (machine α).step (Go .copy v inp s r out) | |
| 135 | - (some (Go .restore (none, v.2) [] (inp.reverse ++ s) (inp.reverse ++ r) out)) | |
| 135 | + EvalsToInTime (machine α).step (atPhase .copy v inp s r out) | |
| 136 | + (some (atPhase .restore (none, v.2) [] (inp.reverse ++ s) (inp.reverse ++ r) out)) | |
| 136 | 137 | (inp.length + 1) := by |
| 137 | 138 | induction inp generalizing v s r with |
| 138 | 139 | | nil => simpa using oneStep (copy_nil v s r out) |
| @@ -143,8 +144,8 @@ def copyRun (inp : List α) (v : Register α) (s r : List α) (out : List Bool) | ||
| 143 | 144 | |
| 144 | 145 | /-- Drain the scratch stack back onto the input, preserving the reversed copy. -/ |
| 145 | 146 | def restoreRun (s : List α) (v : Register α) (inp r : List α) (out : List Bool) : |
| 146 | - EvalsToInTime (machine α).step (Go .restore v inp s r out) | |
| 147 | - (some (Go .compare (none, v.2) (s.reverse ++ inp) [] r out)) | |
| 147 | + EvalsToInTime (machine α).step (atPhase .restore v inp s r out) | |
| 148 | + (some (atPhase .compare (none, v.2) (s.reverse ++ inp) [] r out)) | |
| 148 | 149 | (s.length + 1) := by |
| 149 | 150 | induction s generalizing v inp with |
| 150 | 151 | | nil => simpa using oneStep (restore_nil v inp r out) |
| @@ -156,7 +157,7 @@ def restoreRun (s : List α) (v : Register α) (inp r : List α) (out : List Boo | ||
| 156 | 157 | /-- Drain equally long stacks together. An earlier mismatch remains recorded. -/ |
| 157 | 158 | def compareRun (xs ys : List α) (same : xs.length = ys.length) |
| 158 | 159 | (v : Register α) (out : List Bool) : |
| 159 | - EvalsToInTime (machine α).step (Go .compare v xs [] ys out) | |
| 160 | + EvalsToInTime (machine α).step (atPhase .compare v xs [] ys out) | |
| 160 | 161 | (some (cfg none (none, true) ([] : List α) [] [] |
| 161 | 162 | ((v.2 && decide (xs = ys)) :: out))) (xs.length + 1) := by |
| 162 | 163 | induction xs generalizing ys v with |
| @@ -170,14 +171,14 @@ def compareRun (xs ys : List α) (same : xs.length = ys.length) | ||
| 170 | 171 | | cons b ys => |
| 171 | 172 | have agree : ((v.2 && decide (a = b)) && decide (xs = ys)) = |
| 172 | 173 | (v.2 && decide (a :: xs = b :: ys)) := by |
| 173 | - by_cases hab : a = b <;> simp [hab, Bool.and_assoc] | |
| 174 | + by_cases hab : a = b <;> simp [hab] | |
| 174 | 175 | have h := follow (oneStep (compare_cons v a b xs ys out)) |
| 175 | 176 | (ih ys (by simpa using same) (none, v.2 && decide (a = b))) |
| 176 | 177 | simpa only [agree, List.length_cons, Nat.add_comm 1] using h |
| 177 | 178 | |
| 178 | 179 | theorem init_eq (inp : List α) : |
| 179 | - initList (machine α) inp = Go .copy (none, true) inp [] [] [] := by | |
| 180 | - unfold Go initList cfg | |
| 180 | + initList (machine α) inp = atPhase .copy (none, true) inp [] [] [] := by | |
| 181 | + unfold atPhase initList cfg | |
| 181 | 182 | congr |
| 182 | 183 | funext k |
| 183 | 184 | cases k <;> rfl |
| @@ -200,7 +201,9 @@ def run (inp : List α) : | ||
| 200 | 201 | simp only [List.append_nil, List.reverse_reverse, List.length_reverse, Bool.true_and] at hc hr he |
| 201 | 202 | rw [init_eq, halt_eq] |
| 202 | 203 | have h := follow (follow hc hr) he |
| 203 | - convert h using 1 <;> omega | |
| 204 | + rw [show 3 * inp.length + 3 = | |
| 205 | + (inp.length + 1) + (inp.length + 1) + (inp.length + 1) by omega] | |
| 206 | + simpa using h | |
| 204 | 207 | |
| 205 | 208 | /-- The polynomial-time witness for comparison with the reversed input. -/ |
| 206 | 209 | noncomputable def decider (α : Type) [Fintype α] [DecidableEq α] : |