The realizability layer: finite assemblies over a universal machine #
Objects are finite sets encoded as natural numbers (codes), and every morphism carries a code that is itself a value: morphisms are realized by codes for an actual universal machine.
Why this is novel infrastructure #
Realizability categories — assemblies together with their categorical structure — are absent
from Mathlib. Lean 4 does have a standalone development of partial combinatory algebras (Bauer,
andrejbauer/partial-combinatory-algebras, 2024), but no assembly category built over one. The
nearest mechanized prior art in any prover is the Coq formalization of combinatory algebras and
their realizability semantics accompanying Cohen, Grunfeld, Kirst and Miquey (2025), together
with UniMath's Coq Cartesian-closed category of partial equivalence relations (a realizability
category in the sense of van Oosten 2008) — both in other foundations, not portable to Lean.
What Mathlib does ship is the full
substrate of Kleene's first partial combinatory algebra — Nat.Partrec.Code with a universal
eval, the s-m-n theorem (curry/eval_curry), identity (Code.id/eval_id), composition
(Code.comp, eval (comp cf cg) n = eval cg n >>= eval cf), and exists_code — but never
assembles it into a realizability structure. We build that layer here, directly on
Nat.Partrec.Code: realizers are genuine codes (Denumerable Code, so each realizer is a value),
the evaluator is Mathlib's verified universal eval, and no bespoke universal machine is
introduced.
What this establishes #
Assembly: a finite carrier with an injective encodingenc : carrier → ℕinto codes (the encoding of an object).Assembly.FitsIn sworkBits: all codes< 2^{sworkBits}.Realizes f: the realizes predicate — a codertracks (realizes)f, i.e.eval r (enc x) = enc (f x)for allx. The realizer is a value (aCode).realizes_id,realizes_comp: identity (Code.id) and composition (Code.comp) are realized — so realized functions are closed under the categorical operations.instance : Category AssemblywithHom A B := { f // Realizes f }: the realizability category of finite assemblies — a genuineCategoryTheory.Category, morphisms being exactly the code-realizable functions (the realizer existentially witnessed).
Boundary #
Objects here are finite assemblies with a single-valued encoding (each element is its code), the
reading of objects as subsets of {0,1}^{sworkBits}. The Cartesian-closed structure
of this realizability category (exponentials as codes realizing morphisms — combinatory completeness
via k/s) is the next increment; this file establishes the category and the realizers of functions.
A finite assembly: a finite carrier together with an injective encoding of its elements as codes (numbers) — an object of the realizability category.
- carrier : Type
the underlying finite set of elements.
the encoding: each element gets a code
- enc_inj : Function.Injective self.enc
the encoding is faithful (distinct elements, distinct codes)
Instances For
A function f is realized by a code if some code r tracks it — the
universal evaluator applied to r and an element's code returns the code of the image. The realizer
r is itself a value (a Code).
Equations
- Realizability.Realizes f = ∃ (r : Nat.Partrec.Code), ∀ (x : A.carrier), r.eval (A.enc x) = Part.some (B.enc (f x))
Instances For
The realizability category of finite assemblies: morphisms are exactly the code-realizable
functions (the realizer existentially witnessed). A genuine
CategoryTheory.Category.
Equations
- One or more equations did not get rendered due to their size.
Every function between finite assemblies is a morphism (the finiteness fact) #
Because the objects are finite, every morphism is a total function that halts on every input, so
no "computation budget" clause is needed. We machine-check exactly that converse-free fact:
every function between finite assemblies is realized by a code, so it is a
genuine morphism of the realizability category — not merely a total set-function. The realizer is a
code doing finite table lookup over the (injectively encoded) domain, built by folding Primrec.ite
over Finset.univ and extracting a code via Nat.Partrec.Code.exists_code.
Finite table lookup at the ℕ level: the value paired with the first matching key, else 0.
The realizer-defining function behind realizes_of_finite.
Equations
- Realizability.tableFn [] x✝ = 0
- Realizability.tableFn ((k, v) :: rest) x✝ = if x✝ = k then v else Realizability.tableFn rest x✝
Instances For
Products of assemblies (realized projections and pairing) #
The first projection is realized, by Code.left.
The second projection is realized, by Code.right.
Combinatory completeness: the evaluation and currying realizers (toward the exponential) #
The realizability exponential [A ⇒ B] needs two computational ingredients — a code that
applies a realizer to an argument (evaluation), and one that specializes a realizer in its first
argument (currying / s-m-n). Both are provided here over Nat.Partrec.Code. These are the
universal-property content: realizer codes can themselves be applied and built.
(Packaging [A ⇒ B] as an assembly object additionally needs the modest/relational form of
Assembly — each morphism realized by many codes, "under extensional equality" — a
generalization of the single-encoding Assembly here; see the module note.)
The universal evaluator as a code (the evaluation realizer): a single code E that applies
an encoded realizer to an argument — E · ⟨⌜r⌝, a⟩ = r · a. Built from Mathlib's universal eval
(Code.eval_part : Partrec₂ eval) via exists_code.
The currying realizer (s-m-n): specializing a realizer r of a two-argument map in its
first argument c yields a realizer curry r c of the one-argument residual —
(curry r c) · a = r · ⟨c, a⟩. This is Mathlib's eval_curry, the form used to build
[A ⇒ B] (a realizer for a ↦ g(c,a) from one for g).
The modular-counter instance: the successor is realized #
The modular-counter assembly (the carrier ZMod (n+1), encoded by ZMod.val).
Reducible so the carrier's Add/One/ZMod.val instances are visible at use sites.
Equations
- Realizability.counterAssembly n = { carrier := ZMod (n + 1), fin := ZMod.fintype (n + 1), enc := ZMod.val, enc_inj := ⋯ }
Instances For
The core fact on the explicit carrier ZMod (n+1): a code tracks the modular successor on
ZMod.val-encodings. Obtained from the computable m ↦ (m+1) % (n+1) via Mathlib's
exists_code.
The counter's successor is realized (concretely): the modular successor
step : x ↦ x + 1 on ZMod (n+1) is a morphism of the
realizability category, its realizer a value (a Nat.Partrec.Code). Packaged from
exists_code_tracks_succ by defeq (counterAssembly's carrier is ZMod (n+1), enc is
ZMod.val).