Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Translating Mathlib's partial recursive programs into the ambient model

Definition
MIPRE_Foundations_Cost_FromPartrec

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

computabilitycost-modelmipre-formalizationundecidability

This bundle translates Mathlib's model of partial recursive functions, the language Turing.ToPartrec.Code on lists of natural numbers, into the ambient model: the bridge along which the halting problem of Nat.Partrec.Code is compiled into the model.

MIPRE.Cost.encL vvv encodes a list vvv of natural numbers as the cons\mathtt{cons}cons-chain of the unary numerals of its elements: encL([ ])=nil\mathrm{encL}([\,]) = \mathtt{nil}encL([])=nil and encL(n::v)=cons(ofNat n,encL v)\mathrm{encL}(n :: v) = \mathtt{cons}(\mathrm{ofNat}\ n, \mathrm{encL}\ v)encL(n::v)=cons(ofNat n,encL v).

MIPRE.Cost.Prog.fixBody pfp_fpf​ is the body of the translation of fix f\mathtt{fix}\ ffix f: run the translation pfp_fpf​ of fff on the state, then dispatch on the head of the result, stopping with the tail if the head is 000 and continuing with the tail otherwise (an empty result stops with the empty list). MIPRE.Cost.Prog.consTail assembles headI(f(v))::fs(v)\mathrm{headI}(f(v)) :: fs(v)headI(f(v))::fs(v) in the translation of cons f fs\mathtt{cons}\ f\ fscons f fs, in an environment holding fs(v)fs(v)fs(v), f(v)f(v)f(v) and vvv. MIPRE.Cost.Prog.ofCode is the translation itself, construct by construct: zero′\mathtt{zero'}zero′ (prepend 000) becomes cons(nil,var 0)\mathtt{cons}(\mathtt{nil}, \mathtt{var}\ 0)cons(nil,var 0); succ\mathtt{succ}succ (the singleton of the successor of the head) and tail\mathtt{tail}tail become elim\mathtt{elim}elims on the input; cons f fs\mathtt{cons}\ f\ fscons f fs and comp f g\mathtt{comp}\ f\ gcomp f g become let\mathtt{let}let bindings of the translated pieces; case f g\mathtt{case}\ f\ gcase f g, which runs fff on the tail when the head is 000 and ggg on n::tailn :: \mathrm{tail}n::tail when the head is n+1n + 1n+1, becomes a nested elim\mathtt{elim}elim on the head; and fix f\mathtt{fix}\ ffix f becomes loop (fixBody (ofCode f))\mathtt{loop}\ (\mathrm{fixBody}\ (\mathrm{ofCode}\ f))loop (fixBody (ofCode f)). MIPRE.Cost.Prog.fixFlag www is the value of the dispatch of fixBody on a result www of fff: cons(nil,nil)\mathtt{cons}(\mathtt{nil}, \mathtt{nil})cons(nil,nil) on the empty list, the stop signal cons(nil,encL t)\mathtt{cons}(\mathtt{nil}, \mathrm{encL}\ t)cons(nil,encL t) on 0::t0 :: t0::t, and the continue signal cons(cons(nil,nil),encL t)\mathtt{cons}(\mathtt{cons}(\mathtt{nil}, \mathtt{nil}), \mathrm{encL}\ t)cons(cons(nil,nil),encL t) on (n+1)::t(n + 1) :: t(n+1)::t.

MIPRE.Cost.univPart nnn is the universal partial function on code numbers,

n⟼φcn(0),cn=the Nat.Partrec.Code with Go¨del number n,n \longmapsto \varphi_{c_n}(0), \qquad c_n = \text{the } \texttt{Nat.Partrec.Code} \text{ with Gödel number } n,n⟼φcn​​(0),cn​=the Nat.Partrec.Code with Go¨del number n,

undefined when nnn decodes to no code; it is the function that the halting-problem bridge implements.

The lemmas proved in the bundle are the two equations of encL, on the empty list and on n::vn :: vn::v.

Formalization Note. The translation is correct in both directions, ofCode_sound (a run of the translated program yields a value of Mathlib's Code.eval) and ofCode_complete (a value of Code.eval yields a run), both uploaded separately; fix is translated as a loop, and Mathlib's headI convention (head 000 on the empty list) is matched by elim returning nil=ofNat 0\mathtt{nil} = \mathrm{ofNat}\ 0nil=ofNat 0 on nil\mathtt{nil}nil. The resulting bridge exists_compile is a map from Nat.Partrec.Code to programs, computable on descriptions, such that the compiled program halts on the empty input if and only if the code halts on 000: it hardcodes the code number into the translation of a ToPartrec.Code for univPart. Only computability is transferred, not time: ToPartrec.Code cannot rebuild lists in polynomial time, which is why it is not the ambient model.

Definition code
/-
Copyright (c) 2026 Thomas Vidick. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Thomas Vidick
-/
/- Generated for Prove2Me from vidick/MIPRE-formalization@70cbf1c, MIPRE/Foundations/Cost/FromPartrec.lean (definition bundle: declarations the uploaded statements rest on);
   produced mechanically by prove2me/tools/gen.py, never edited by hand. -/
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

/-!
# From Mathlib's partial recursive functions into the ambient model

Mathlib's `Turing.ToPartrec.Code` is a small first-order language on lists of naturals
(`zero'`, `succ`, `tail`, `cons`, `comp`, `case`, `fix`) in which every partial recursive
function is computable (`Turing.ToPartrec.Code.exists_code`). It translates construct by
construct into the ambient language (`Prog.ofCode`), with lists of naturals encoded as
`cons`-chains of unary numerals (`encL`) and `fix` as a `loop`; `ofCode_sound` and
`ofCode_complete` are the two directions of the correctness of the translation.

`exists_compile` is the resulting bridge for the halting problem: a map `compile` from
`Nat.Partrec.Code` to programs, computable on descriptions, such that `compile pc` halts on
the empty input iff `pc` does on `0`: `compile pc` hardcodes the code number of `pc` into
the translation of a `ToPartrec.Code` for the universal partial function `n ↦ (ofNat n).eval 0`.
-/

namespace MIPRE.Cost

open Turing.ToPartrec (Code)

/-- Lists of naturals as data: a `cons`-chain of unary numerals. -/
def encL (v : List ℕ) : Data := Data.list (v.map Data.ofNat)

@[simp] theorem encL_nil : encL [] = .nil := rfl

@[simp] theorem encL_cons (n : ℕ) (v : List ℕ) : encL (n :: v) = .cons (.ofNat n) (encL v) := rfl

namespace Prog

/-- The body of the translation of `fix f`: run `f`, then dispatch on the head of the
result (stop with the tail if it is `0`, continue with the tail otherwise). -/
def fixBody (pf : Prog) : Prog :=
  .let_ (callVar 0 pf)
    (.elim 0 (.cons .nil .nil) (.elim 0 (.cons .nil (.var 1)) (.cons (.cons .nil .nil) (.var 3))))

/-- The `headI :: tail` assembly of the translation of `cons f fs`, in an environment
`[fs v, f v, v]`. -/
def consTail : Prog := .cons (.elim 1 .nil (.var 0)) (.var 0)

/-- Translation of `ToPartrec.Code` into the ambient language. -/
def ofCode : Code → Prog
  | .zero' => .cons .nil (.var 0)
  | .succ => .elim 0 (.cons (.cons .nil .nil) .nil) (.cons (.cons .nil (.var 0)) .nil)
  | .tail => .elim 0 .nil (.var 1)
  | .cons f fs => .let_ (ofCode f) (.let_ (callVar 1 (ofCode fs)) consTail)
  | .comp f g => .let_ (ofCode g) (ofCode f)
  | .case f g =>
    .elim 0 (callVar 0 (ofCode f))
      (.elim 0 (callVar 1 (ofCode f)) (.let_ (.cons (.var 1) (.var 3)) (callVar 0 (ofCode g))))
  | .fix f => .loop (fixBody (ofCode f))

/-! ## Forward runs of the pieces -/

/-- The result of the dispatch of `fixBody` on the value `fv` of `f`. -/
def fixFlag : List ℕ → Data
  | [] => .cons .nil .nil
  | 0 :: t => .cons .nil (encL t)
  | (_ + 1) :: t => .cons (.cons .nil .nil) (encL t)

/-! ## Soundness -/

/-! ## Completeness -/

end Prog

/-! ## The halting-problem bridge -/

/-- The universal partial function on code numbers: `n ↦ (decode n).eval 0`. -/
noncomputable def univPart (n : ℕ) : Part ℕ :=
  ((Encodable.decode n : Option Nat.Partrec.Code) : Part Nat.Partrec.Code).bind fun c => c.eval 0

end MIPRE.Cost
Source
https://github.com/vidick/MIPRE-formalization/blob/70cbf1c52e3d5e1dff1162fdf952c9a9f4bd99ed/MIPRE/Foundations/Cost/FromPartrec.lean

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