Documentation

Realizability.Realizability

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 #

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.

Instances For

    The assembly's codes fit in sworkBits bits (objects ⊆ {0,1}^{sworkBits}).

    Equations
    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
      Instances For

        The identity is realized, by Code.id.

        theorem Realizability.realizes_comp {A B C : Assembly} {g : B.carrier → C.carrier} {f : A.carrier → B.carrier} (hg : Realizes g) (hf : Realizes f) :

        Realized functions compose, with realizer Code.comp (= "apply f then g").

        @[instance_reducible]

        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
        Instances For

          tableFn l is primitive recursive (a finite if-cascade over a fixed list).

          theorem Realizability.tableFn_eval {k v : ℕ} (l : List (ℕ × ℕ)) :
          (k, v) ∈ l → (∀ (k' v' : ℕ), (k', v') ∈ l → k' = k → v' = v) → tableFn l k = v

          Evaluation of tableFn at a key present in the list, when the list is functional at that key (every pair sharing the key shares the value) — so the first-match result is the intended value.

          Every function between finite assemblies is realized — a morphism of the realizability category. Finiteness makes every total function a morphism (the realizer is an explicit code).

          Products of assemblies (realized projections and pairing) #

          Product assembly: carrier A.carrier × B.carrier, encoded by Nat.pair of the codes.

          Equations
          • One or more equations did not get rendered due to their size.
          Instances For

            The first projection is realized, by Code.left.

            The second projection is realized, by Code.right.

            theorem Realizability.pair_realizes {A B C : Assembly} {f : C.carrier → A.carrier} {g : C.carrier → B.carrier} (hf : Realizes f) (hg : Realizes g) :
            Realizes fun (c : C.carrier) => (f c, g c)

            Pairing of realized maps is realized, by Code.pair.

            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 #

            @[reducible, inline]

            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
            Instances For
              theorem Realizability.exists_code_tracks_succ (n : ℕ) :
              ∃ (c : Nat.Partrec.Code), ∀ (x : ZMod (n + 1)), c.eval x.val = Part.some (x + 1).val

              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.

              theorem Realizability.counter_step_realizes (n : ℕ) :
              Realizes fun (x : ZMod (n + 1)) => x + 1

              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).