Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Soundness of the `ToPartrec` translation: ofCode(c)\mathrm{ofCode}(c)ofCode(c) computes every value of ccc on encL(v)\mathrm{encL}(v)encL(v)

Proved
MIPRE.Cost.Prog.ofCode_sound

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

computabilitycost-modelmipre-formalizationundecidability

Mathlib's Turing.ToPartrec.Code is a small first-order language on lists of natural numbers, with the partial semantics c.eval:List N→Part(List N)c.\mathrm{eval} : \mathrm{List}\,\mathbb{N} \to \mathrm{Part}(\mathrm{List}\,\mathbb{N})c.eval:ListN→Part(ListN): zero′(v)=0::v\mathtt{zero'}(v) = 0 :: vzero′(v)=0::v; succ(v)=[head(v)+1]\mathtt{succ}(v) = [\mathrm{head}(v) + 1]succ(v)=[head(v)+1], the head of the empty list being 000; tail(v)=tail(v)\mathtt{tail}(v) = \mathrm{tail}(v)tail(v)=tail(v); (cons f fs)(v)=head(f(v))::fs(v)(\mathtt{cons}\,f\,fs)(v) = \mathrm{head}(f(v)) :: fs(v)(consffs)(v)=head(f(v))::fs(v); (comp f g)(v)=f(g(v))(\mathtt{comp}\,f\,g)(v) = f(g(v))(compfg)(v)=f(g(v)); (case f g)([])=f([])(\mathtt{case}\,f\,g)([]) = f([])(casefg)([])=f([]), (case f g)(0::v)=f(v)(\mathtt{case}\,f\,g)(0 :: v) = f(v)(casefg)(0::v)=f(v), (case f g)((n+1)::v)=g(n::v)(\mathtt{case}\,f\,g)((n+1) :: v) = g(n :: v)(casefg)((n+1)::v)=g(n::v); and (fix f)(v)(\mathtt{fix}\,f)(v)(fixf)(v) is the least fixed point (PFun.fix) that returns tail(f(v))\mathrm{tail}(f(v))tail(f(v)) if head(f(v))=0\mathrm{head}(f(v)) = 0head(f(v))=0 and recurses on tail(f(v))\mathrm{tail}(f(v))tail(f(v)) otherwise. Write w∈c.eval(v)w \in c.\mathrm{eval}(v)w∈c.eval(v) for "ccc on vvv is defined, with value www". In the ambient cost model, lists of naturals are encoded as cons\mathtt{cons}cons-chains of unary numerals, encL(v)\mathrm{encL}(v)encL(v) (encL; the numeral kˉ\bar kkˉ is the list of kkk copies of nil\mathtt{nil}nil), and p(x)⇓trp(x) \Downarrow_t rp(x)⇓t​r (p.Runs x r t) denotes a run of the program ppp on the input xxx with result rrr at cost ttt. The translation ofCode\mathrm{ofCode}ofCode (Prog.ofCode) maps each construct to a program of the model — elims on the input for the primitives and for case\mathtt{case}case, let_ binders with calls of the translated subprograms for cons\mathtt{cons}cons and comp\mathtt{comp}comp, and a loop for fix\mathtt{fix}fix.

Statement. For every code ccc and lists of naturals v,wv, wv,w,

w∈c.eval(v)  ⟹  ∃t, ofCode(c)(encL(v))⇓tencL(w).w \in c.\mathrm{eval}(v) \;\Longrightarrow\; \exists t,\ \mathrm{ofCode}(c)\bigl(\mathrm{encL}(v)\bigr) \Downarrow_t \mathrm{encL}(w) .w∈c.eval(v)⟹∃t, ofCode(c)(encL(v))⇓t​encL(w).

This is one direction of the correctness of the translation; with ofCode_complete it gives that ofCode(c)\mathrm{ofCode}(c)ofCode(c) halts on encL(v)\mathrm{encL}(v)encL(v) exactly when c.eval(v)c.\mathrm{eval}(v)c.eval(v) is defined (ofCode_halts_iff), which the compiler exists_compile uses to transport the halting problem of Mathlib's Nat.Partrec.Code into the model, for the halting form of the compression lemmas (blueprint lem:halting-form).

Formalization Note: The statement is computability-only: the cost ttt is existential and no bound is claimed — the module docstring of the cost model explains why Mathlib's ToPartrec.Code cannot serve as a polynomial-time model. Lists of naturals are encoded with unary numerals, unlike the binary encoding of N\mathbb{N}N used elsewhere in the development, because the translation mirrors succ\mathtt{succ}succ and the head tests of case\mathtt{case}case and fix\mathtt{fix}fix by single elims.

Preamble
import Mathlib.Algebra.Polynomial.Eval.Defs
import Mathlib.Computability.PartrecCode
import Mathlib.Computability.Primrec.List
import Mathlib.Computability.TuringMachine.Config
import Mathlib.Data.List.Basic
import Mathlib.Data.Nat.Bits
import Mathlib.Data.Nat.Notation
import Mathlib.Data.Nat.Size
import Mathlib.Logic.Denumerable
import Mathlib.Logic.Function.Iterate
import Mathlib.Tactic.Linarith
import Mathlib.Tactic.Ring
import Definitions.Def_MIPRE_Foundations_Cost_Basic
import Definitions.Def_MIPRE_Foundations_Cost_Encoding
import Definitions.Def_MIPRE_Foundations_Cost_PolyTime
import Definitions.Def_MIPRE_Foundations_Cost_Closure
import Definitions.Def_MIPRE_Foundations_Cost_Loops
import Definitions.Def_MIPRE_Foundations_Cost_Unary
import Definitions.Def_MIPRE_Foundations_Cost_Toolkit
import Definitions.Def_MIPRE_Foundations_Cost_Machine
import Definitions.Def_MIPRE_Foundations_Cost_Codable
import Definitions.Def_MIPRE_Foundations_Cost_MachineData
import Definitions.Def_MIPRE_Foundations_Cost_Interpreter
import Definitions.Def_MIPRE_Foundations_Cost_MachineBound
import Definitions.Def_MIPRE_Foundations_Cost_Universal
import Definitions.Def_MIPRE_Foundations_Cost_Kleene
import Definitions.Def_MIPRE_Foundations_Cost_FromPartrec

open MIPRE
open MIPRE.Cost
open MIPRE.Cost.Prog
open Turing.ToPartrec (Code)
Formal statement
theorem MIPRE.Cost.Prog.ofCode_sound : ∀ (c : Code) (v w : List ℕ), w ∈ c.eval v →
    ∃ t, (ofCode c).Runs (encL v) (encL w) t := by sorry
Source
https://github.com/vidick/MIPRE-formalization/blob/70cbf1c52e3d5e1dff1162fdf952c9a9f4bd99ed/MIPRE/Foundations/Cost/FromPartrec.lean#L139-L230; internal lemma of the formalization of blueprint lem:halting-form (the bridge from Mathlib's partial recursive functions into the model)

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