Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The translation of Mathlib's `ToPartrec` codes into the ambient model produces closed programs

Proved
MIPRE.Cost.Prog.ofCode_wellScoped

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). A program is closed (WellScoped 1) if it reads no variable other than its input, variable 000, every variable of the program being bound in its binding context.

Statement. For every code ccc,

ofCode(c) is well-scoped in one variable,\mathrm{ofCode}(c) \text{ is well-scoped in one variable,}ofCode(c) is well-scoped in one variable,

that is, the translation of any code is a closed program.

Closedness is the hypothesis under which calls of translated subprograms can be lifted into larger environments (callVar_eval, Eval.append_of_wellScoped) and restricted back (callVar_runs_rev, Eval.of_append_of_wellScoped); it is therefore a premise of both correctness directions ofCode_sound and ofCode_complete, and of the s-m-n transfer in the compiler exists_compile. It serves the halting form of the compression lemmas (blueprint lem:halting-form).

Formalization Note: WellScoped n p is the syntactic predicate "every variable of ppp is below nnn in its binding context", with elim adding two binders and let_ one and loop requiring a nonempty environment; the translation uses the calling convention callVar i q =let var i in q= \mathtt{let}\ \mathtt{var}\ i\ \mathtt{in}\ q=let var i in q of the closure library, which keeps callees closed.

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_wellScoped : ∀ c : Code, (ofCode c).WellScoped 1 := by sorry
Source
https://github.com/vidick/MIPRE-formalization/blob/70cbf1c52e3d5e1dff1162fdf952c9a9f4bd99ed/MIPRE/Foundations/Cost/FromPartrec.lean#L82-L95; 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