Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The body of the translated `fix` loop dispatches on the head of the output of fff as the loop flag

Proved
MIPRE.Cost.Prog.fixBody_runs

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 constructors zero′\mathtt{zero'}zero′, succ\mathtt{succ}succ, tail\mathtt{tail}tail, cons f fs\mathtt{cons}\,f\,fsconsffs, comp f g\mathtt{comp}\,f\,gcompfg, case f g\mathtt{case}\,f\,gcasefg and fix f\mathtt{fix}\,ffixf, in which every partial recursive function is computable. The translation ofCode:Code→Prog\mathrm{ofCode} : \mathrm{Code} \to \mathrm{Prog}ofCode:Code→Prog (Prog.ofCode) maps it construct by construct into the language of 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), zero′\mathtt{zero'}zero′ prepends nil\mathtt{nil}nil, succ\mathtt{succ}succ and tail\mathtt{tail}tail are elims on the input, cons\mathtt{cons}cons and comp\mathtt{comp}comp are let_ binders with calls of the translated subprograms, case\mathtt{case}case is a double elim on the head of the input, and fix\mathtt{fix}fix is a loop whose body runs the translated fff and dispatches on the head of its result (fixBody). Write p(x)⇓trp(x) \Downarrow_t rp(x)⇓t​r for a run of the program ppp on the input xxx with result rrr at cost ttt, and ρ⊢p⇓sr\rho \vdash p \Downarrow_s rρ⊢p⇓s​r for a run in the environment ρ\rhoρ (Eval). For a program pfpfpf, the loop body is

fixBody(pf)=let (callVar 0 pf) in elim 0 (cons(nil,nil)) (elim 0 (cons(nil,var 1)) (cons(cons(nil,nil),var 3))),\mathrm{fixBody}(pf) = \mathtt{let}\ (\mathtt{callVar}\ 0\ pf)\ \mathtt{in}\ \mathtt{elim}\ 0\ \bigl(\mathtt{cons}(\mathtt{nil},\mathtt{nil})\bigr)\ \Bigl(\mathtt{elim}\ 0\ \bigl(\mathtt{cons}(\mathtt{nil}, \mathtt{var}\ 1)\bigr)\ \bigl(\mathtt{cons}(\mathtt{cons}(\mathtt{nil},\mathtt{nil}), \mathtt{var}\ 3)\bigr)\Bigr),fixBody(pf)=let (callVar 0 pf) in elim 0 (cons(nil,nil)) (elim 0 (cons(nil,var 1)) (cons(cons(nil,nil),var 3))),

which runs pfpfpf on the loop state and inspects the head of the result. The loop protocol of the model is that a body value cons(nil,w)\mathtt{cons}(\mathtt{nil}, w)cons(nil,w) stops the loop with result www, while cons(cons(⋅,⋅),w)\mathtt{cons}(\mathtt{cons}(\cdot,\cdot), w)cons(cons(⋅,⋅),w) continues with the state www. The intended body value is (fixFlag)

fixFlag([])=cons(nil,nil),fixFlag(0::w)=cons(nil,encL(w)),fixFlag((n+1)::w)=cons(cons(nil,nil),encL(w)),\mathrm{fixFlag}([]) = \mathtt{cons}(\mathtt{nil}, \mathtt{nil}),\qquad \mathrm{fixFlag}(0 :: w) = \mathtt{cons}(\mathtt{nil}, \mathrm{encL}(w)),\qquad \mathrm{fixFlag}((n+1) :: w) = \mathtt{cons}(\mathtt{cons}(\mathtt{nil},\mathtt{nil}), \mathrm{encL}(w)),fixFlag([])=cons(nil,nil),fixFlag(0::w)=cons(nil,encL(w)),fixFlag((n+1)::w)=cons(cons(nil,nil),encL(w)),

mirroring Mathlib's semantics of fix f\mathtt{fix}\,ffixf: stop with [][][] if fff returns [][][], stop with www if it returns 0::w0 :: w0::w, and iterate from www if it returns (n+1)::w(n+1) :: w(n+1)::w.

Statement. Let pfpfpf be a closed program and v,fvv, fvv,fv lists of naturals such that pf(encL(v))⇓tencL(fv)pf(\mathrm{encL}(v)) \Downarrow_t \mathrm{encL}(fv)pf(encL(v))⇓t​encL(fv) for some ttt. Then there is a cost sss with

[encL(v)]⊢fixBody(pf)⇓sfixFlag(fv).[\mathrm{encL}(v)] \vdash \mathrm{fixBody}(pf) \Downarrow_s \mathrm{fixFlag}(fv) .[encL(v)]⊢fixBody(pf)⇓s​fixFlag(fv).

This is the single-iteration lemma for the fix case of both correctness directions of the translation: ofCode_sound uses it to build the run of the loop, and ofCode_complete uses it, with determinism, to show that any run of the body returns a fixFlag value. It serves blueprint lem:halting-form through exists_compile.

Formalization Note: No cost is tracked (sss is existential): the translation from Mathlib's codes is a computability-only bridge. The encoding encL\mathrm{encL}encL uses unary numerals, so that the head test is a single elim; the environment is the singleton [encL(v)][\mathrm{encL}(v)][encL(v)], the loop state being variable 000.

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.fixBody_runs {pf : Prog} (hpf : pf.WellScoped 1) (v fv : List ℕ) {t : ℕ}
    (h : pf.Runs (encL v) (encL fv) t) :
    ∃ s, Eval [encL v] (fixBody pf) (fixFlag fv) s := by sorry
Source
https://github.com/vidick/MIPRE-formalization/blob/70cbf1c52e3d5e1dff1162fdf952c9a9f4bd99ed/MIPRE/Foundations/Cost/FromPartrec.lean#L105-L118; 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