← All papers
First page of CrossCheck I: Cost Bounds of Imperative Programs as Constrained Optimization, with a Sound Cost Logic and a Verified Certificate Checker

CrossCheck I: Cost Bounds of Imperative Programs as Constrained Optimization, with a Sound Cost Logic and a Verified Certificate Checker

Luca Dall'Olio

cs.LO Nov 23, 2026 · v1 cs.PL
Mechanizes the trace semantics, sound cost logic, and integer-arithmetic certificate checker in about 11,000 lines of Lean 4 over mathlib, with no sorry.
Static analysis of imperative programs often relies on iterative fixed-point approximations or concrete path exploration. We show that structured imperative control flow—while loops, linear recursion and branching recursion—can be mapped into constrained optimization problems over the free monoid of execution traces and the free algebra of computation trees. Ordering traces and trees by an intensional order-by-composition ($\sqsubseteq$), halting is characterized as finding the minimal trace or tree that satisfies the exit condition, and we prove that this characterization coincides with the standard big-step semantics. Exact state recurrences are undecidable in general, so we relax them into cost envelopes, using ranking potentials and the tropical $(\max,+)$ reading of the timing schema, and obtain sound upper bounds on execution cost. The bounds compose over whole programs in a cost logic for a small language with nondeterministic actions, procedures, recursion and assumed contracts, which we prove sound for terminating and non-terminating executions. A linear instance of the logic has certificates—programs annotated with invariants, potentials and Farkas multipliers—and a checker that validates them with integer arithmetic only; an accepted certificate bounds the cost of every execution. All results, the checker and the worked examples are machine-checked in Lean 4. A companion paper describes a Clang plugin that produces such certificates and its evaluation.

Static cost analysis of imperative programs usually relies on fixed-point approximation or path exploration. These approaches do not directly give closed-form, sound bounds on execution cost, especially for recursion and branching recursion.

Loops and linear or branching recursion are characterized as minimization problems over the free monoid of traces and the free algebra of computation trees, ordered by composition, and shown equivalent to big-step semantics. Exact recurrences are relaxed into cost envelopes using ranking potentials and the tropical (max,+) timing schema. This yields a cost logic for a small language with nondeterminism, procedures, recursion and contracts. A linear instance uses certificates (invariants, potentials, Farkas multipliers) validated by an integer-only checker, and everything is mechanized in Lean 4 over mathlib.

The cost logic is proved sound for terminating and non-terminating executions, and an accepted certificate bounds the cost of every execution. The Lean development has about 11,000 lines and 499 theorems, uses only the standard axioms and no sorry, and includes machine-checked worked examples.