A Lean 4 library focused on proving the correctness of a new O(1) implementation of the Luby sequence based on segments, along with supporting lemmas on natural numbers and binary representations, and notes/papers about the construction.
The Luby sequence was introduced in: Michael Luby, Alistair Sinclair, and David Zuckerman. “Optimal Speedup of Las Vegas Algorithms.” Information Processing Letters, 47(4): 173–180, 1993. It is sometimes used as a restart schedule in randomized algorithms (notably SAT solvers). This repository provides a Lean 4 library you can build locally.
- Formal definitions of zero-based and one-based variants of the Luby sequence
- A segment-based O(1) implementation (
SegmentSequence,SegmentedState) with correctness proofs - Supporting lemmas on binary sizes and trailing zeros
- Proofs connecting the segment-based implementation to the recursive definition
- Accompanying notes in Typst describing the definitions and complexity intuition
If you just want to browse the code, start with the top-level module:
LubySequence.lean(imports the core modules)
-
LubySequence.Size- Supporting lemmas about the binary size (
Nat.size) of natural numbers.
- Supporting lemmas about the binary size (
-
LubySequence.TrailingZeros- Defines
trailing_zerosand proves its key properties.
- Defines
-
LubySequence.Basic- Defines the Luby sequence and supporting functions.
- Includes lemmas relating natural numbers to their binary representation (e.g., using
Nat.size). - Introduces “envelope” utilities used by other modules.
-
LubySequence.SegmentSequence- Defines
Segmentand the O(1) segment-based computation of Luby values. - Proves segment boundary properties and monotonicity.
- Defines
-
LubySequence.SegmentedState- Defines
SegmentedState, a stateful O(1) iterator over the Luby sequence. - Proves correctness of the iterator with respect to the recursive definition.
- Defines
-
LubySequence.Equivalence- Proves that the segment-based implementation agrees with the recursive definition.
- Contains completed proofs connecting segment indices to envelope depths.
-
LubySequence(root)- Umbrella module that imports
Basic,SegmentSequence,SegmentedState, andEquivalence.
- Umbrella module that imports
This is a Lean 4 project managed with Lake and using mathlib.
- Lean toolchain managed by elan (recommended)
- Lake (comes with Lean toolchain)
- Internet access on first build to fetch mathlib
The project pins the Lean version via lean-toolchain and depends on:
- mathlib
v4.28.0-rc1(as configured inlakefile.toml)
- Install Lean via elan: https://leanprover-community.github.io/get_started.html
- Clone this repository
- From the project root, run:
lake update
lake build
This will fetch dependencies (mathlib) and compile the library.
This repo includes a flake.nix that provides a development shell with:
- elan + Lean + Lake
- typst + tinymist (for Typst docs)
To enter the dev shell:
- With flakes:
nix develop - If you use direnv (there’s a
.envrcin the repo), allow it in the project directory to auto-enter the shell.
Once in the shell, build as usual:
lake update
lake build
-
Typst documents in repo root:
memo.typandpresen.typ, with prebuilt PDFs (memo.pdf,presen.pdf) included.- Build with:
typst compile memo.typ typst compile presen.typ - The Nix dev shell includes
typstand thetinymistLSP for editor support.
-
A
graph.dotis included for visualizations. If you have Graphviz installed:dot -Tpdf graph.dot -o graph.pdfTo regenerate the graph from the current source files, run:
python3 parse_theorems.pyEach node in the graph represents a theorem, lemma, or definition and is colored by its source file. Directed edges indicate proof dependencies.
- Built on Lean 4 and mathlib.