Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Finite stack comparator for canonical binary naturals

Definition
TwoAgentSched_TotalTotal_StackCompareDigits

by kmehltretter82 · Nov 1, 2026 · Mathlib 0df444a (Lean v4.33.1)

binary-encodingcomplexity-theorystack-programtwo-agent-scheduling

A finite three-stack program for comparing two most-significant-bit-first binary digit strings. It scans paired symbols, maintaining a comparison-state stack, and then uses unmatched input lengths to decide which string is shorter. A separator marker represents equality, zero represents less, and one represents greater.

On arbitrary symbols the program is still a finite stack program; a separate theorem states its total step bound. Its numerical correctness is stated for canonical binary expansions, for which comparing lengths before equal-length lexicographic order is valid. The program supports the sorting component of the scheduling reduction.

Definition code
import Mathlib
import Definitions.Def_CookPvsNP_StackProgram
import Definitions.Def_ProjSchedTW_Complexity_Encoding

set_option autoImplicit false
open CookPvsNP ProjSchedTW.Complexity

namespace TwoAgentSched.TotalTotal.StackCompareDigits

/-- Left digits, right digits, and a nonempty stack of comparison states.
    The state symbols are separator = equal, zero = less, one = greater. -/
def store (left right states : List BSym) : Fin 3 → List BSym :=
  fun k => if k = 0 then left else if k = 1 then right else states

def digitSym (b : Bool) : BSym := if b then .one else .zero

def nextState (state x y : BSym) : BSym :=
  if state = .sep then
    if x = .zero ∧ y = .one then .zero
    else if x = .one ∧ y = .zero then .one
    else .sep
  else state

def bothNonempty (h : Fin 3 → Option BSym) : Bool :=
  (h 0).isSome && (h 1).isSome

def compareAct (h : Fin 3 → Option BSym) (k : Fin 3) : StackAct BSym :=
  if k = 0 ∨ k = 1 then .pop
  else .push (nextState ((h 2).getD .sep) ((h 0).getD .sep) ((h 1).getD .sep))

/-- A finite program for equal-length MSB-first digit comparison. -/
def compareProg : StackProg (Fin 3) BSym := .loop bothNonempty (.act compareAct)

def leftShort (h : Fin 3 → Option BSym) : Bool :=
  !(h 0).isSome && (h 1).isSome
def rightShort (h : Fin 3 → Option BSym) : Bool :=
  (h 0).isSome && !(h 1).isSome
def markAct (a : BSym) (_h : Fin 3 → Option BSym) (k : Fin 3) : StackAct BSym :=
  if k = 2 then .push a else .keep
def keepAct (_h : Fin 3 → Option BSym) (_k : Fin 3) : StackAct BSym := .keep

/-- At the end of the pair scan, unequal lengths override the lexicographic
    marker; equal lengths leave the previous state unchanged. -/
def finishLength : StackProg (Fin 3) BSym :=
  .branch leftShort (.act (markAct .zero))
    (.branch rightShort (.act (markAct .one)) (.act keepAct))

/-- Pairwise bit comparison followed by its length check. -/
def compareFullProg : StackProg (Fin 3) BSym := .seq compareProg finishLength


end TwoAgentSched.TotalTotal.StackCompareDigits
Source
Supporting implementation for the formalization of Agnetis, Mirchandani, Pacciarelli and Pacifici, Scheduling Problems with Two Competing Agents, Operations Research 52(2), 2004, Theorem 9.2, pp. 237–238. This comparator is an additional machine component, not a claim in the 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