Prove2Me
Navigate
DiscoverCollectionsFormalpediaBlogsUsersMomentumMy Missions+
Prove2Me
⌕
Log in
← Formalpedia

Item-language compiler correctness with cost preservation

Proved
NLogN.compile_correct

by xuanji · Oct 12, 2026 · Mathlib 0df444a (Lean v4.33.1)

integer-multiplicationschonhage-strassen-tmturing-machines

Let iprog be an item-language program with PPP procedures over SSS item stacks, and let prog be a stack-machine program over S+4S+4S+4 stacks whose procedures are the compilations of those of iprog (prog j=compile(iprog j)\mathrm{prog}\ j = \mathrm{compile}(\mathrm{iprog}\ j)prog j=compile(iprog j) for all jjj). Suppose the item statement ppp executes (big-step semantics IExec, with input word III) from item state sss to s′s's′ with cost ccc. Then

  1. the compiled statement runs on the encoded states within the same cost:
Runs I prog (compile p) (encI s) (encI s′) c;\mathrm{Runs}\ I\ \mathrm{prog}\ (\mathrm{compile}\ p)\ (\mathrm{encI}\ s)\ (\mathrm{encI}\ s')\ c;Runs I prog (compile p) (encI s) (encI s′) c;
  1. if moreover p=loop i f bp = \mathrm{loop}\ i\ f\ bp=loop i f b, then 24≤c24\le c24≤c and the inner loop of its compilation runs from encF i f s\mathrm{encF}\ i\ f\ sencF i f s (the encoding of sss with the loop flag for the test fff on stack iii pushed on the scratch stack s3s_3s3​) to encI s′\mathrm{encI}\ s'encI s′ with a 000 flag pushed on s3s_3s3​, within cost c−24c-24c−24.

Part 2 is the strengthened induction hypothesis needed for loops. The item-level costs (e.g. +25+25+25 for a conditional, +30+30+30 per loop test) are chosen to dominate the compiled code, so all cost bounds proved in the item language transfer verbatim to the stack machine and hence, via multipliesAt_of_exec, to the Turing machine.

Formalization Note. Item states IState S have item stacks G, input pointer ip and output out; encI s writes each item stack GiG_iGi​ as the concatenation of item encodings on stack iii and leaves the four extra scratch stacks (sq,s1,s2,s3s_q, s_1, s_2, s_3sq​,s1​,s2​,s3​ = indices S,…,S+3S,\dots,S+3S,…,S+3) empty. Runs asserts an exact final state and an upper bound on the cost; IExec carries the exact item-level cost.

Preamble
-- Schönhage–Strassen multitape-TM development (NLogN), generated by skeleton subtraction.

import Definitions.Def_IntMul_SS_Defs2

set_option autoImplicit false
set_option relaxedAutoImplicit false
set_option maxHeartbeats 1000000

open NLogN
open IntMul
variable {S P : ℕ}
variable {I : List Sym} {prog : Fin P → Stmt (S + 4) P}
Formal statement
theorem NLogN.compile_correct {iprog : Fin P → IStmt S P} (hprog : ∀ j, prog j = compile (iprog j))
    {p : IStmt S P} {s s' : IState S} {c : ℕ} (h : IExec I iprog p s s' c) :
    Runs I prog (compile p) (encI s) (encI s') c ∧
      ∀ i f b, p = .loop i f b →
        Runs I prog (inner i f b) (encF i f s) ((encI s').push (s3 S) 0) (c - 24) ∧ 24 ≤ c := by sorry
Source
A. Schönhage, V. Strassen, Schnelle Multiplikation großer Zahlen, Computing 7 (1971), 281–292 (main theorem: O(n log n log log n) multitape TM multiplication). Lemma `NLogN.compile_correct` of the Lean development NLogN/IComp.lean (lines 159–206), written for the prove2.me integer-multiplication campaign.

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 paper
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me