Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Trees as a denumerable, primcodable type: Cantor coding, projections, tree recursion and list indexing

Definition
MIPRE_Foundations_Cost_Codable

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

computabilitycost-modelmipre-formalizationself-interpreter

This bundle connects the ambient data type to Mathlib's computability theory, by making Data a Primcodable type and providing the first-order operations on trees from which the machine step on data is built.

MIPRE.Cost.Data.encodeNat codes a tree as a natural number through Cantor pairing: nil↦0\mathtt{nil} \mapsto 0nil↦0 and cons(a,b)↦⟨encodeNat a,encodeNat b⟩+1\mathtt{cons}(a, b) \mapsto \langle \mathrm{encodeNat}\ a, \mathrm{encodeNat}\ b \rangle + 1cons(a,b)↦⟨encodeNat a,encodeNat b⟩+1, where ⟨⋅,⋅⟩\langle \cdot, \cdot \rangle⟨⋅,⋅⟩ is Mathlib's pairing Nat.pair. MIPRE.Cost.Data.decodeNat is its inverse, defined by well-founded recursion on the number: 0↦nil0 \mapsto \mathtt{nil}0↦nil, and n+1↦consn + 1 \mapsto \mathtt{cons}n+1↦cons of the decodings of the two components of nnn. MIPRE.Cost.Data.equivNat is the resulting bijection Data≃N\mathrm{Data} \simeq \mathbb{N}Data≃N, and MIPRE.Cost.Data.instDenumerable the Denumerable Data instance it induces, from which Mathlib supplies Encodable and Primcodable instances.

MIPRE.Cost.Data.left and MIPRE.Cost.Data.right are the two projections of a node, both returning nil\mathtt{nil}nil on nil\mathtt{nil}nil. MIPRE.Cost.Data.recD base step\mathit{base}\ \mathit{step}base step is structural recursion on trees with a first-order result in a type σ\sigmaσ:

recD(nil)=base,recD(cons(a,b))=step a b (recD a) (recD b).\mathrm{recD}(\mathtt{nil}) = \mathit{base}, \qquad \mathrm{recD}(\mathtt{cons}(a, b)) = \mathit{step}\ a\ b\ (\mathrm{recD}\ a)\ (\mathrm{recD}\ b).recD(nil)=base,recD(cons(a,b))=step a b (recD a) (recD b).

MIPRE.Cost.Data.unaryToNat reads a unary numeral: it is the length of the right spine of a tree, so that unaryToNat(ofNat n)=n\mathrm{unaryToNat}(\mathrm{ofNat}\ n) = nunaryToNat(ofNat n)=n. MIPRE.Cost.Data.getList d n=left(rightn(d))d\ n = \mathrm{left}(\mathrm{right}^{n}(d))d n=left(rightn(d)) is the nnn-th element of a cons\mathtt{cons}cons-chain (nil\mathtt{nil}nil beyond its end), the operation by which the machine on data looks a variable up in an encoded environment.

The lemmas proved in the bundle are the recursion equations of encodeNat and decodeNat, the two inverse laws decodeNat(encodeNat d)=d\mathrm{decodeNat}(\mathrm{encodeNat}\ d) = ddecodeNat(encodeNat d)=d and encodeNat(decodeNat n)=n\mathrm{encodeNat}(\mathrm{decodeNat}\ n) = nencodeNat(decodeNat n)=n, the equations of the projections and of recD on the two constructors, and the equations of unaryToNat, including unaryToNat(ofNat n)=n\mathrm{unaryToNat}(\mathrm{ofNat}\ n) = nunaryToNat(ofNat n)=n.

Formalization Note. The Cantor coding serves only Mathlib's computability interface, to state that the machine step is primitive recursive and evaluation partial recursive; it plays no role in the cost model, whose sizes are those of the tree encoding, precisely because iterated Nat.pair distorts lengths exponentially. Everything the model manipulates is Data, programs included through Prog.toData, so no separate Primcodable instance for programs is needed. The step function of the machine on data is written with left, right, cons, unaryToNat and getList only, so that its primitive recursiveness follows from that of these operations (Data.primrec_recD and its companions, uploaded separately).

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/Codable.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.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

/-!
# `Data` as a primcodable type

`Data` is denumerable through Cantor pairing (`Data.encodeNat`/`Data.decodeNat`), hence
`Primcodable`, with primitive recursive constructor, projections, a tree-recursion
principle (`Data.primrec_recD`), unary numerals, list indexing and the decoders of bit
strings. This is the interface between the ambient model and Mathlib's computability
(`Cost/Partrec.lean`): everything the model manipulates is `Data` — programs included,
through `Prog.toData` — so no separate encoding of `Prog` is needed.
-/

namespace MIPRE.Cost

namespace Data

/-! ## `Data ≃ ℕ` -/

/-- `Data` as natural numbers through Cantor pairing: `nil ↦ 0`, `cons a b ↦ pair a b + 1`. -/
def encodeNat : Data → ℕ
  | nil => 0
  | cons a b => Nat.pair (encodeNat a) (encodeNat b) + 1

/-- The inverse of `encodeNat`. -/
def decodeNat : ℕ → Data
  | 0 => nil
  | n + 1 => cons (decodeNat (Nat.unpair n).1) (decodeNat (Nat.unpair n).2)
decreasing_by
  all_goals
    try simp_wf
    first
    | exact Nat.lt_succ_of_le (Nat.unpair_left_le _)
    | exact Nat.lt_succ_of_le (Nat.unpair_right_le _)

@[simp] theorem encodeNat_nil : encodeNat nil = 0 := rfl

@[simp] theorem encodeNat_cons (a b : Data) :
    encodeNat (cons a b) = Nat.pair (encodeNat a) (encodeNat b) + 1 := rfl

@[simp] theorem decodeNat_zero : decodeNat 0 = nil := by rw [decodeNat]

@[simp] theorem decodeNat_succ (n : ℕ) :
    decodeNat (n + 1) = cons (decodeNat (Nat.unpair n).1) (decodeNat (Nat.unpair n).2) := by
  rw [decodeNat]

@[simp] theorem decodeNat_encodeNat : ∀ d : Data, decodeNat (encodeNat d) = d
  | nil => by simp
  | cons a b => by
    rw [encodeNat_cons, decodeNat_succ, Nat.unpair_pair]
    simp [decodeNat_encodeNat a, decodeNat_encodeNat b]

@[simp] theorem encodeNat_decodeNat (n : ℕ) : encodeNat (decodeNat n) = n := by
  induction n using Nat.strong_induction_on with
  | _ n ih =>
    cases n with
    | zero => simp
    | succ n =>
      rw [decodeNat_succ, encodeNat_cons, ih _ (Nat.lt_succ_of_le (Nat.unpair_left_le n)),
        ih _ (Nat.lt_succ_of_le (Nat.unpair_right_le n)), Nat.pair_unpair]

/-- `Data ≃ ℕ`. -/
def equivNat : Data ≃ ℕ where
  toFun := encodeNat
  invFun := decodeNat
  left_inv := decodeNat_encodeNat
  right_inv := encodeNat_decodeNat

instance : Denumerable Data := Denumerable.mk' equivNat

/-! ## Primitive recursive constructor and projections -/

/-- The head of a pair (`nil` on `nil`). -/
def left : Data → Data
  | nil => nil
  | cons a _ => a

/-- The tail of a pair (`nil` on `nil`). -/
def right : Data → Data
  | nil => nil
  | cons _ b => b

@[simp] theorem left_nil : left nil = nil := rfl
@[simp] theorem left_cons (a b : Data) : left (cons a b) = a := rfl
@[simp] theorem right_nil : right nil = nil := rfl
@[simp] theorem right_cons (a b : Data) : right (cons a b) = b := rfl

/-! ## Tree recursion -/

/-- Structural recursion on `Data` with a first-order result. -/
def recD {σ : Type*} (base : σ) (step : Data → Data → σ → σ → σ) : Data → σ
  | nil => base
  | cons a b => step a b (recD base step a) (recD base step b)

@[simp] theorem recD_nil {σ : Type*} (base : σ) (step : Data → Data → σ → σ → σ) :
    recD base step nil = base := rfl

@[simp] theorem recD_cons {σ : Type*} (base : σ) (step : Data → Data → σ → σ → σ)
    (a b : Data) : recD base step (cons a b) = step a b (recD base step a) (recD base step b) :=
  rfl

/-! ## Unary numerals and list indexing -/

/-- The length of the right spine: reads a unary numeral (`unaryToNat (ofNat n) = n`). -/
def unaryToNat : Data → ℕ := recD 0 fun _ _ _ rb => rb + 1

@[simp] theorem unaryToNat_nil : unaryToNat nil = 0 := rfl

@[simp] theorem unaryToNat_cons (a b : Data) : unaryToNat (cons a b) = unaryToNat b + 1 := rfl

@[simp] theorem unaryToNat_ofNat (n : ℕ) : unaryToNat (Data.ofNat n) = n := by
  induction n with
  | zero => rfl
  | succ n ih => simp [Data.ofNat, ih]

/-- The `n`-th element of a `cons`-chain (`nil` beyond the end). -/
def getList (d : Data) (n : ℕ) : Data := left (right^[n] d)

/-! ## Decoding bit strings -/

/-! ## Encoding naturals

The companion of `primrec_decode_bitStr` in the other direction. A natural is encoded as the
bit string of `Nat.bits`, so its encoding satisfies the halving recursion below, and strong
recursion on `ℕ` turns that into a primitive recursive definition. Every budgeted run of the
tabulation feeds the machine an `encode (n, …)`, so this is on the critical path of
`MIPRE.Halting.tab_computable`. -/

end Data

/-! ## Two small gaps in the ambient library -/

end MIPRE.Cost
Source
https://github.com/vidick/MIPRE-formalization/blob/70cbf1c52e3d5e1dff1162fdf952c9a9f4bd99ed/MIPRE/Foundations/Cost/Codable.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