Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Structural recursion on binary-tree data is primitive recursive

Proved
MIPRE.Cost.Data.primrec_recD

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

computabilitycost-modelmipre-formalization

The data of the ambient cost model are binary trees Data, generated by nil and cons. The type Data is in bijection with N\mathbb{N}N through Cantor pairing: encodeNat(nil)=0\mathrm{encodeNat}(\texttt{nil}) = 0encodeNat(nil)=0 and encodeNat(cons a b)=⟨encodeNat(a),encodeNat(b)⟩+1\mathrm{encodeNat}(\texttt{cons}\,a\,b) = \langle \mathrm{encodeNat}(a), \mathrm{encodeNat}(b)\rangle + 1encodeNat(consab)=⟨encodeNat(a),encodeNat(b)⟩+1 (with ⟨⋅,⋅⟩\langle\cdot,\cdot\rangle⟨⋅,⋅⟩ Mathlib's Nat.pair), which makes Data a Primcodable type in the sense of Mathlib's computability library. Structural recursion on trees is the operator

recD(base,step):Data→σ,recD(nil)=base,recD(cons a b)=step(a,b,recD(a),recD(b)).\mathrm{recD}(\mathrm{base}, \mathrm{step}) : \texttt{Data} \to \sigma,\qquad \mathrm{recD}(\texttt{nil}) = \mathrm{base},\qquad \mathrm{recD}(\texttt{cons}\,a\,b) = \mathrm{step}\bigl(a, b, \mathrm{recD}(a), \mathrm{recD}(b)\bigr).recD(base,step):Data→σ,recD(nil)=base,recD(consab)=step(a,b,recD(a),recD(b)).

Let σ\sigmaσ be a primcodable type with a default element, let base∈σ\mathrm{base} \in \sigmabase∈σ, and let step:Data→Data→σ→σ→σ\mathrm{step} : \texttt{Data} \to \texttt{Data} \to \sigma \to \sigma \to \sigmastep:Data→Data→σ→σ→σ be primitive recursive as a function of the quadruple, i.e. the map ((a,b),(u,v))↦step(a,b,u,v)((a,b),(u,v)) \mapsto \mathrm{step}(a,b,u,v)((a,b),(u,v))↦step(a,b,u,v) on (Data×Data)×(σ×σ)(\texttt{Data} \times \texttt{Data}) \times (\sigma \times \sigma)(Data×Data)×(σ×σ) satisfies Mathlib's Primrec. Then

Primrec(recD(base,step)).\mathrm{Primrec}\bigl(\mathrm{recD}(\mathrm{base}, \mathrm{step})\bigr).Primrec(recD(base,step)).

This is the recursion principle through which the first-order functions on Data used by the formalization are shown primitive recursive: the reading of unary numerals (unaryToNat), the decoding of lists and hence of bit strings (primrec_decode_bitStr), and the step function of the evaluation machine. In the import closure of the recursive compression lemma it is what lets the halting-problem forms of the two compression lemmas (recursive_compression_halting, compressibility_criterion_halting) decode the output of the polynomial-time reduction into a Mathlib-Computable function on Nat.Partrec.Code.

Formalization Note. MIPRE.Cost.Data.recD is the recursor restricted to first-order results; the hypothesis hstep is Primrec of the uncurried step on (Data × Data) × σ × σ, and σ needs [Primcodable σ] [Inhabited σ] (the default element is used for out-of-range list lookups in the proof). The Primcodable Data instance is the one derived from Data.equivNat (Denumerable Data); it is deliberately distinct from the size-faithful SizedEncoding of the cost model, which encodes into Data itself, and it is used only at this interface with Mathlib's computability.

Preamble
import Mathlib.Algebra.Polynomial.Eval.Defs
import Mathlib.Computability.Primrec.List
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 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_Codable

open MIPRE
open MIPRE.Cost
open MIPRE.Cost.Data
Formal statement
theorem MIPRE.Cost.Data.primrec_recD {σ : Type*} [Primcodable σ] [Inhabited σ] (base : σ)
    (step : Data → Data → σ → σ → σ)
    (hstep : Primrec fun p : (Data × Data) × σ × σ => step p.1.1 p.1.2 p.2.1 p.2.2) :
    Primrec (recD base step) := by sorry
Source
https://github.com/vidick/MIPRE-formalization/blob/70cbf1c52e3d5e1dff1162fdf952c9a9f4bd99ed/MIPRE/Foundations/Cost/Codable.lean#L162-L214; internal lemma of the formalization of blueprint lem:halting-form (the primitive recursion principle behind the decoding of bit strings used to restate the reductions against Mathlib's halting problem)

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