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 | |
| 3 | /-! |
| 4 | # Sanity tests for the P versus NP statement |
| 5 | |
| 6 | Positive facts that must keep holding under the corrected definition of `NP` |
| 7 | (`Millennium.InNondeterministicPolynomialTime`: certificates are finite strings over a finite |
| 8 | alphabet, and the checking relation must be decided on *every* pair `w#y`): |
| 9 | |
| 10 | * a concrete language is in `P` via Mathlib's identity machine (`trueLanguage_in_P`); |
| 11 | * the empty language over `Bool`-strings is in `P` and in `NP`, witnessed by an explicit two-stack |
| 12 | machine that drains its input and outputs `false` (`emptyLanguage_in_P`, `emptyLanguage_in_NP`); |
| 13 | so the corrected `NP` is satisfiable with string certificates and a real checking machine; |
| 14 | * `P ⊆ NP` still follows from the composition-closure hypothesis (`P_subset_NP`); |
| 15 | * the main equivalence theorems use only the standard axioms (checked with `#guard_msgs`). |
| 16 | |
| 17 | Everything here is `sorry`-free and is built by `lake build` (library `Tests`). |
| 18 | -/ |
| 19 | |
| 20 | namespace Tests.PVersusNP |
| 21 | |
| 22 | open Millennium Turing Computability StateTransition |
| 23 | |
| 24 | /-! ### A language in `P` from Mathlib's identity machine -/ |
| 25 | |
| 26 | /-- `{true} ⊆ Bool` is decided by the identity machine in one step. -/ |
| 27 | theorem trueLanguage_in_P : InPolynomialTime finEncodingBoolBool (fun b : Bool => b = true) := |
| 28 | ⟨id, idComputableInPolyTime finEncodingBoolBool.encode, fun _ => Iff.rfl⟩ |
| 29 | |
| 30 | /-! ### An explicit linear-time machine deciding the empty language -/ |
| 31 | |
| 32 | /-- Stack alphabets: the input stack (`true`) carries `Γ₀`, the output stack (`false`) carries |
| 33 | `Bool`. Reducible so that `simp` can see `StkΓ Γ₀ true = Γ₀` in the step lemmas. -/ |
| 34 | abbrev StkΓ (Γ₀ : Type) : Bool → Type |
| 35 | | true => Γ₀ |
| 36 | | false => Bool |
| 37 | |
| 38 | /-- Pop the input stack until it is empty, then push `false` on the output stack, reset the state |
| 39 | and halt. -/ |
| 40 | def constFalseMachine (Γ₀ : Type) [Fintype Γ₀] : FinTM2 where |
| 41 | K := Bool |
| 42 | k₀ := true |
| 43 | k₁ := false |
| 44 | Γ := StkΓ Γ₀ |
| 45 | Λ := Unit |
| 46 | main := () |
| 47 | σ := Bool |
| 48 | initialState := true |
| 49 | Γk₀Fin := inferInstanceAs (Fintype Γ₀) |
| 50 | m _ := |
| 51 | TM2.Stmt.pop true (fun _ x => x.isSome) |
| 52 | (TM2.Stmt.branch (fun v => v) (TM2.Stmt.goto fun _ => ()) |
| 53 | (TM2.Stmt.push false (fun _ => false) (TM2.Stmt.load (fun _ => true) TM2.Stmt.halt))) |
| 54 | |
| 55 | variable (Γ₀ : Type) [Fintype Γ₀] |
| 56 | |
| 57 | /-- Explicit configurations of `constFalseMachine`. -/ |
| 58 | def cfg (l : Option Unit) (v : Bool) (inp : List Γ₀) (out : List Bool) : |
| 59 | (constFalseMachine Γ₀).Cfg where |
| 60 | l := l |
| 61 | var := v |
| 62 | stk |
| 63 | | true => inp |
| 64 | | false => out |
| 65 | |
| 66 | /-- Unfold the machine and compare configurations componentwise. -/ |
| 67 | macro "step_tac" : tactic => |
| 68 | `(tactic| (simp [FinTM2.step, TM2.step, TM2.stepAux, constFalseMachine, cfg] |
| 69 | try (congr <;> first | rfl | (funext k; cases k <;> rfl) | (simp; done)))) |
| 70 | |
| 71 | theorem step_cons (v : Bool) (a : Γ₀) (inp : List Γ₀) (out : List Bool) : |
| 72 | (constFalseMachine Γ₀).step (cfg Γ₀ (some ()) v (a :: inp) out) = |
| 73 | some (cfg Γ₀ (some ()) true inp out) := by |
| 74 | step_tac |
| 75 | |
| 76 | theorem step_nil (v : Bool) (out : List Bool) : |
| 77 | (constFalseMachine Γ₀).step (cfg Γ₀ (some ()) v [] out) = |
| 78 | some (cfg Γ₀ none true [] (false :: out)) := by |
| 79 | step_tac |
| 80 | |
| 81 | /-- Weaken a time bound. -/ |
| 82 | def EvalsToInTime.mono {σ : Type} {f : σ → Option σ} {a : σ} {b : Option σ} {m m' : ℕ} |
| 83 | (h : EvalsToInTime f a b m) (hm : m ≤ m') : EvalsToInTime f a b m' := |
| 84 | ⟨h.toEvalsTo, le_trans h.steps_le_m hm⟩ |
| 85 | |
| 86 | /-- One step. -/ |
| 87 | def oneStep {a b : (constFalseMachine Γ₀).Cfg} (h : (constFalseMachine Γ₀).step a = some b) : |
| 88 | EvalsToInTime (constFalseMachine Γ₀).step a (some b) 1 := |
| 89 | { steps := 1 |
| 90 | evals_in_steps := by simpa [flip, Option.bind] using h |
| 91 | steps_le_m := le_rfl } |
| 92 | |
| 93 | /-- Draining the input takes one step per symbol; the final step writes the output. -/ |
| 94 | def run (inp : List Γ₀) (v : Bool) (out : List Bool) : |
| 95 | EvalsToInTime (constFalseMachine Γ₀).step (cfg Γ₀ (some ()) v inp out) |
| 96 | (some (cfg Γ₀ none true [] (false :: out))) (inp.length + 1) := by |
| 97 | induction inp generalizing v with |
| 98 | | nil => simpa using oneStep Γ₀ (step_nil Γ₀ v out) |
| 99 | | cons a t ih => |
| 100 | have h₁ := oneStep Γ₀ (step_cons Γ₀ v a t out) |
| 101 | have h₂ := ih true |
| 102 | have h := EvalsToInTime.trans (constFalseMachine Γ₀).step 1 (t.length + 1) _ _ _ h₁ h₂ |
| 103 | exact EvalsToInTime.mono h (by simp) |
| 104 | |
| 105 | theorem initList_eq (inp : List Γ₀) : |
| 106 | initList (constFalseMachine Γ₀) inp = cfg Γ₀ (some ()) true inp [] := by |
| 107 | unfold initList cfg |
| 108 | congr |
| 109 | funext k |
| 110 | cases k <;> rfl |
| 111 | |
| 112 | theorem haltList_eq (out : List Bool) : |
| 113 | haltList (constFalseMachine Γ₀) out = cfg Γ₀ none true [] out := by |
| 114 | unfold haltList cfg |
| 115 | congr |
| 116 | funext k |
| 117 | cases k <;> rfl |
| 118 | |
| 119 | /-- The constant-`false` machine, packaged as a polynomial-time computation of `fun _ => false` |
| 120 | for any input encoding. -/ |
| 121 | noncomputable def constFalseDecider {α : Type} (ea : FinEncoding α) : |
| 122 | TM2ComputableInPolyTime ea.encode finEncodingBoolBool.encode (fun _ : α => false) where |
| 123 | tm := constFalseMachine ea.Γ |
| 124 | inputAlphabet := Equiv.refl _ |
| 125 | outputAlphabet := Equiv.refl _ |
| 126 | time := Polynomial.X + 1 |
| 127 | outputsFun a := by |
| 128 | show EvalsToInTime (constFalseMachine ea.Γ).step |
| 129 | (initList (constFalseMachine ea.Γ) (List.map id (ea.encode a))) |
| 130 | (some (haltList (constFalseMachine ea.Γ) (List.map id [false]))) |
| 131 | ((Polynomial.X + 1 : Polynomial ℕ).eval (ea.encode a).length) |
| 132 | rw [List.map_id, List.map_id, initList_eq, haltList_eq] |
| 133 | exact EvalsToInTime.mono (run ea.Γ (ea.encode a) true []) (by simp) |
| 134 | |
| 135 | /-- The empty language over `Bool`-strings is in `P`. -/ |
| 136 | theorem emptyLanguage_in_P : |
| 137 | InPolynomialTime (fin_encoding_string Bool) (fun _ : List Bool => False) := |
| 138 | ⟨fun _ => false, constFalseDecider _, fun _ => by simp⟩ |
| 139 | |
| 140 | /-- The empty language over `Bool`-strings is in `NP`: certificates are `Bool`-strings and the |
| 141 | constant-`False` checking relation is decided by the constant-`false` machine on every `w#y`. -/ |
| 142 | theorem emptyLanguage_in_NP : |
| 143 | InNondeterministicPolynomialTime (fin_encoding_string Bool) (fun _ : List Bool => False) := |
| 144 | ⟨Bool, inferInstance, fun _ _ => False, 0, |
| 145 | ⟨fun _ => false, constFalseDecider _, fun _ => by simp⟩, fun _ => by simp⟩ |
| 146 | |
| 147 | /-! ### Structural facts that must survive -/ |
| 148 | |
| 149 | /-- `P ⊆ NP` still follows from composition closure of the machine model. -/ |
| 150 | theorem P_subset_NP (hComp : ClayPVersusNP.Support.PolynomialTimeComputableComposition) |
| 151 | (alphabet : Type) [Fintype alphabet] [Nontrivial alphabet] (L : Language (List alphabet)) |
| 152 | (hP : InPolynomialTime (fin_encoding_string alphabet) L) : |
| 153 | InNondeterministicPolynomialTime (fin_encoding_string alphabet) L := |
| 154 | PolynomialTimeContainedInNondeterministicPolynomialTime.of_turing_machine_composition hComp |
| 155 | alphabet L hP |
| 156 | |
| 157 | /-- The Clay verifier wording still coincides with `NP`. -/ |
| 158 | example (alphabet : Type) [Fintype alphabet] (L : Language (List alphabet)) : |
| 159 | ClayVerifiableLanguage alphabet L ↔ |
| 160 | InNondeterministicPolynomialTime (fin_encoding_string alphabet) L := |
| 161 | ClayVerifiableLanguage.iff_in_nondeterministic_polynomial_time alphabet L |
| 162 | |
| 163 | /-- `NegativeBranch` is literally the negation of the positive target. -/ |
| 164 | example : ClayPVersusNP.Formulations.NegativeBranch ↔ ¬ ClayPVersusNP := Iff.rfl |
| 165 | |
| 166 | /-! ### Axiom checks |
| 167 | |
| 168 | These fail the build if any of the listed theorems ever picks up `sorryAx`. -/ |
| 169 | |
| 170 | /-- info: 'Millennium.ClayVerifiableLanguage.iff_in_nondeterministic_polynomial_time' depends on axioms: [propext, Classical.choice, Quot.sound] -/ |
| 171 | #guard_msgs (whitespace := lax) in |
| 172 | #print axioms ClayVerifiableLanguage.iff_in_nondeterministic_polynomial_time |
| 173 | |
| 174 | /-- info: 'Millennium.ClayPVersusNP.Formulations.ClassEquality.iff_hard_direction' depends on axioms: [propext, Classical.choice, Quot.sound] -/ |
| 175 | #guard_msgs (whitespace := lax) in |
| 176 | #print axioms ClayPVersusNP.Formulations.ClassEquality.iff_hard_direction |
| 177 | |
| 178 | /-- info: 'Millennium.ClayPVersusNP.iff_checkable' depends on axioms: [propext, Classical.choice, Quot.sound] -/ |
| 179 | #guard_msgs (whitespace := lax) in |
| 180 | #print axioms ClayPVersusNP.iff_checkable |
| 181 | |
| 182 | /-- info: 'Millennium.ClayPVersusNP.Consequences.NondeterministicPolynomialTimeCompleteInPolynomialTime.of_turing_machine_composition' depends on axioms: [propext, Classical.choice, Quot.sound] -/ |
| 183 | #guard_msgs (whitespace := lax) in |
| 184 | #print axioms ClayPVersusNP.Consequences.NondeterministicPolynomialTimeCompleteInPolynomialTime.of_turing_machine_composition |
| 185 | |
| 186 | /-- info: 'Tests.PVersusNP.emptyLanguage_in_NP' depends on axioms: [propext, Classical.choice, Quot.sound] -/ |
| 187 | #guard_msgs (whitespace := lax) in |
| 188 | #print axioms emptyLanguage_in_NP |
| 189 | |
| 190 | /-- info: 'Tests.PVersusNP.emptyLanguage_in_P' depends on axioms: [propext, Classical.choice, Quot.sound] -/ |
| 191 | #guard_msgs (whitespace := lax) in |
| 192 | #print axioms emptyLanguage_in_P |
| 193 | |
| 194 | end Tests.PVersusNP |