Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Finite stack machine and column sweep functions

Definition
CookPvsNP_StackModel

by arexychen · Oct 2, 2026 · Mathlib 0df444a (Lean v4.33.1)

polynomial-timeturing-machines

A source instruction reads the finite tuple of stack tops, updates finite control, and keeps, pushes, or pops each stack. The accompanying column functions describe the right and left sweeps. These are definitions, without any assumed compiler theorem.

Definition code
import Definitions.Def_CookPvsNP_defs

set_option autoImplicit false
namespace CookPvsNP

/-- One constant-cost operation on a stack. -/
inductive StackAct (A : Type) where
  | keep
  | push (a : A)
  | pop
  deriving DecidableEq, Fintype

def StackAct.apply {A : Type} : StackAct A → List A → List A
  | .keep, s => s
  | .push a, s => a :: s
  | .pop, s => s.tail

/-- Fixed finite control and a fixed finite family of stacks. A step reads only their tops. -/
structure StackMachine (K A Q : Type) where
  initial : Q
  done : Q → Bool
  next : Q → (K → Option A) → Q × (K → StackAct A)

structure StackCfg (K A Q : Type) where
  state : Q
  store : K → List A

def StackMachine.step {K A Q : Type} (P : StackMachine K A Q)
    (c : StackCfg K A Q) : StackCfg K A Q :=
  if P.done c.state then c else
    let d := P.next c.state (fun k => (c.store k).head?)
    ⟨d.1, fun k => (d.2 k).apply (c.store k)⟩

abbrev StackCol (K A : Type) := K → Option A

def stackZero {K A : Type} : StackCol K A := fun _ => none

/-- A column encoding with explicit blank padding; the width is shared by all stacks. -/
def stackRows {K A : Type} (s : K → List A) (n : ℕ) : List (StackCol K A) :=
  (List.range n).map fun i k => (s k)[i]?

def stackPushCarry {K A : Type} (a : K → StackAct A) : StackCol K A := fun k =>
  match a k with | .push x => some x | _ => none

def stackPushCell {K A : Type} (a : K → StackAct A)
    (carry x : StackCol K A) : StackCol K A := fun k =>
  match a k with | .push _ => carry k | _ => x k

def stackPushNext {K A : Type} (a : K → StackAct A)
    (x : StackCol K A) : StackCol K A := fun k =>
  match a k with | .push _ => x k | _ => none

def stackPopCell {K A : Type} (a : K → StackAct A)
    (carry x : StackCol K A) : StackCol K A := fun k =>
  match a k with | .pop => carry k | _ => x k

def stackPopNext {K A : Type} (a : K → StackAct A)
    (x : StackCol K A) : StackCol K A := fun k =>
  match a k with | .pop => x k | _ => none

/-- The right sweep writes one extra column when it reaches the blank tape. -/
def stackForward {K A : Type} (a : K → StackAct A) :
    StackCol K A → List (StackCol K A) → List (StackCol K A)
  | c, [] => [c]
  | c, x :: xs => stackPushCell a c x :: stackForward a (stackPushNext a x) xs

/-- The left sweep, specified in left-to-right column order. -/
def stackBackward {K A : Type} (a : K → StackAct A) :
    List (StackCol K A) → List (StackCol K A)
  | [] => []
  | x :: xs => stackPopCell a (xs.headD stackZero) x :: stackBackward a xs

def stackRight {K A : Type} (a : K → StackAct A) :
    StackCol K A → List (StackCol K A) → StackCol K A × List (StackCol K A)
  | c, [] => (c, [])
  | c, x :: xs =>
    let z := stackRight a (stackPushNext a x) xs
    (z.1, stackPushCell a c x :: z.2)

def stackLeft {K A : Type} (a : K → StackAct A) :
    StackCol K A → List (StackCol K A) → StackCol K A × List (StackCol K A)
  | c, [] => (c, [])
  | c, x :: xs =>
    let z := stackLeft a x xs
    (z.1, stackPopCell a c x :: z.2)

end CookPvsNP
Source
New auxiliary formalization for the ResourceScheduling Q2 reduction. The target is the unchanged CookPvsNP one-tape machine model, following Cook, The P versus NP problem, Clay Mathematics Institute (2000), Appendix. This explicit finite-column stack compiler and its simulation lemmas are new contributions, not numbered claims from Cook or the scheduling source paper.

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me