Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

A program halts with a value if and only if the evaluation machine reaches a final configuration with that value

Proved
MIPRE.Cost.Machine.halts_iff

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

computabilitycost-modelmipre-formalizationself-interpreteruniversal-machine

The evaluation machine (Machine) is a small-step, CEK-style machine for the ambient language of the cost model. A configuration ⟨κ,ρ,k⟩\langle \kappa, \rho, k \rangle⟨κ,ρ,k⟩ (Cfg) consists of a control κ\kappaκ, which is either ev p\mathtt{ev}\,pevp ("evaluate the program ppp") or ret v\mathtt{ret}\,vretv ("return the value vvv"), an environment ρ\rhoρ (a list of data, variable 000 first) and a stack kkk of continuation frames: cons1(t,ρ)\mathtt{cons1}(t, \rho)cons1(t,ρ) (the head of a cons\mathtt{cons}cons is being evaluated; evaluate ttt in ρ\rhoρ next), cons2(a)\mathtt{cons2}(a)cons2(a) (the head value is aaa; return cons(a,⋅)\mathtt{cons}(a, \cdot)cons(a,⋅) next), let1(b,ρ)\mathtt{let1}(b, \rho)let1(b,ρ) and loop1(b,ρ)\mathtt{loop1}(b, \rho)loop1(b,ρ). The step function step\mathrm{step}step (step) is total and deterministic, and it leaves final configurations ⟨ret v,ρ,[]⟩\langle \mathtt{ret}\,v, \rho, [] \rangle⟨retv,ρ,[]⟩ unchanged. Each configuration is charged a cost stepCost\mathrm{stepCost}stepCost: a configuration ⟨ev p,ρ,k⟩\langle \mathtt{ev}\,p, \rho, k\rangle⟨evp,ρ,k⟩ is charged the unit of the evaluation rule for the head constructor of ppp — size(ρ(i))+1\mathrm{size}(\rho(i)) + 1size(ρ(i))+1 for var i\mathtt{var}\ ivar i, 111 for nil\mathtt{nil}nil, size(d)\mathrm{size}(d)size(d) for const d\mathtt{const}\ dconst d, 111 for cons\mathtt{cons}cons, elim\mathtt{elim}elim and let\mathtt{let}let, and 000 for loop\mathtt{loop}loop, whose unit is charged instead when the value of the body is dispatched at a loop1\mathtt{loop1}loop1 frame; all other returns cost 000. Write cost(c,N)=∑i<NstepCost(stepi(c))\mathrm{cost}(c, N) = \sum_{i < N} \mathrm{stepCost}(\mathrm{step}^i(c))cost(c,N)=∑i<N​stepCost(stepi(c)) (costSum) for the cost accumulated over NNN steps, and Steps(c,N,c′,t)\mathrm{Steps}(c, N, c', t)Steps(c,N,c′,t) (Steps) for "stepN(c)=c′\mathrm{step}^N(c) = c'stepN(c)=c′ and cost(c,N)=t\mathrm{cost}(c, N) = tcost(c,N)=t". The big-step semantics ρ⊢p⇓tr\rho \vdash p \Downarrow_t rρ⊢p⇓t​r (Eval) is the timed evaluation relation that the machine implements: ppp in the environment ρ\rhoρ halts with result rrr at cost ttt. For a program ppp and an input xxx, write p(x)⇓trp(x) \Downarrow_t rp(x)⇓t​r (p.Runs x r t) for [x]⊢p⇓tr[x] \vdash p \Downarrow_t r[x]⊢p⇓t​r.

Statement. For every program ppp and data x,rx, rx,r:

(∃t, p(x)⇓tr)  ⟺  (∃N,e, stepN⟨ev p,[x],[]⟩=⟨ret r,e,[]⟩).\bigl(\exists t,\ p(x) \Downarrow_t r\bigr) \iff \bigl(\exists N, e,\ \mathrm{step}^{N}\langle \mathtt{ev}\,p, [x], [] \rangle = \langle \mathtt{ret}\,r, e, [] \rangle\bigr).(∃t, p(x)⇓t​r)⟺(∃N,e, stepN⟨evp,[x],[]⟩=⟨retr,e,[]⟩).

This combines the two directions of the derivation–machine correspondence at the initial configuration of a program on its input. It is the form consumed by the soundness of the self-interpreter (univProg_halts_of, blueprint lem:universal-tm) and by the proof that evaluation is partial recursive (mem_evalData_iff), which transports it along the encoding of configurations as data.

Formalization Note: The left side is the halting predicate of the big-step semantics with an unspecified cost, and the right side mentions only the step function iterated on Cfg, with the environment eee of the final configuration existential; costs play no role in this statement.

Preamble
import Mathlib.Algebra.Polynomial.Eval.Defs
import Mathlib.Data.List.Basic
import Mathlib.Data.Nat.Bits
import Mathlib.Data.Nat.Notation
import Mathlib.Data.Nat.Size
import Mathlib.Logic.Function.Iterate
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_Machine

open MIPRE
open MIPRE.Cost
open MIPRE.Cost.Machine
Formal statement
theorem MIPRE.Cost.Machine.halts_iff (p : Prog) (x r : Data) :
    (∃ t, p.Runs x r t) ↔ ∃ N e, step^[N] ⟨.ev p, [x], []⟩ = ⟨.ret r, e, []⟩ := by sorry
Source
https://github.com/vidick/MIPRE-formalization/blob/70cbf1c52e3d5e1dff1162fdf952c9a9f4bd99ed/MIPRE/Foundations/Cost/Machine.lean#L331-L346; internal lemma of the formalization of blueprint lem:universal-tm (via the self-interpreter)

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