Bend 2 vs. Lean 4 and Formal Proof Frameworks: Executable Test Engine, Differential Reference Oracle, and Proof Speed Compared
Why teams use Lean 4 for foundational metatheory and independent kernel receipts, while deploying Bend 2 for 0.38 s AI-agent proof loops, parallel execution, and dual-target (native + JS) differential test oracles.
Executive Summary
Bend 2 and Lean 4 have a symbiotic
relationship. Bend 2’s affine dependent type theory
(BendTT) is mechanically verified in 20,981
lines of Lean 4 (bend2/bendtt.lean) with zero
sorry or admit tactics (bendtt.lean
repository). That mechanization proves Church-Rosser confluence,
subject reduction, progress, normalization, and logical consistency.
Bend 2’s --verdict flag also exports proof obligations
directly to the Lean 4 kernel (Tripartite
Verification Stack).
When engineering teams verify application state machines or enforce invariant laws on AI coding agents, choosing between Lean 4 and Bend 2 has major consequences. That same choice governs how teams build differential test oracles for TypeScript, Rust, or Go services:
- 50.5x Faster Proof-Checking Feedback Loop: On 3,200
generic proof instantiations measured on an Apple M4 Max,
bend PROOF.bendcompletes in 0.38 s, compared to 6.0 s in Rocq and 19.2 s in Lean 4 (Kevin Ngo benchmark). - Multi-Core CPU and GPU Execution: Across all 16
Apple M4 Max runtime workloads in our ecosystem benchmark dataset, Bend 2
(
PAR-CPU16-core andPAR-GPUMetal) outperforms Lean 4’s compiled C backend by 6.4x to 219.8x. - Dual-Target Differential Test Engine (
native+bunJS): A single proved.bendspecification compiles to both a multi-core native binary (bend SMOKE.bend -o smoke) and a zero-dependency JavaScript module (bend SMOKE.bend -o smoke.js). Teams run differential conformance checks directly inside TypeScript/Bun/Node test suites without maintaining a separate model translator (Bend 2 Decision Guide).
1. Proof-Checking Speed and AI Coding Agent Ergonomics
When human engineers or AI coding agents synthesize code under formal constraints, verification latency determines whether formal methods can run inside the inner TDD loop.
Empirical Proof-Checking Benchmark (3,200 Generic Instantiations, Apple M4 Max)
| Formal Verification System | Verification Wall-Clock Time | Relative Latency vs. Bend 2 | Inner-Loop Suitability for AI Agents |
|---|---|---|---|
Bend 2.0.38 (bend PROOF.bend) (source) |
0.38 s | 1.0x (Baseline) | Runs on every file save and agent tool call. |
| Rocq (formerly Coq) (source) | 6.00 s | 15.8x slower | Usable in pre-commit hooks; noticeable pause in agent loops. |
Lean 4 (lake build) (source) |
19.20 s | 50.5x slower | Requires incremental .olean caching; heavy tactic
proofs stall agent iteration. |
| Agda (source) | > 300.00 s | > 789x slower | Timed out past 5 minutes on generic instantiation stress test. |
| Isabelle/HOL (source) | > 300.00 s | > 789x slower | Timed out past 5 minutes on generic instantiation stress test. |
Why Is Bend 2’s Proof Checker 50x Faster on Equational Laws?
Lean 4’s elaborator and tactic framework (simp,
omega, rw, typeclass resolution over Mathlib
hierarchies) solve classical mathematics, Quotient types, typeclasses,
and arbitrary metaprogramming.
Bend 2 restricts its surface to structural induction, pattern
matching, and beta-equality normalization ({==} and
rewrite) over total, affine-typed terms (Proof-Blocked
AI study):
# 1. Domain implementation (settlement.bend)
type Ratio is Data:
FourToOne{}
ThreeToOne{}
TwoToOne{}
def required_input(ratio: Ratio) -> Nat:
match ratio:
case FourToOne{}: 4n
case ThreeToOne{}: 3n
case TwoToOne{}: 2n
# 2. Human-locked invariant law (LAWS.bend)
def Law_PositiveRate(r: Ratio) -> Type:
all r: Ratio, Nat.is_ge(required_input(r), 2n) == True{}
# 3. Machine-checked inductive proof (PROOF.bend)
def proof_positive_rate(r: Ratio) -> Nat.is_ge(required_input(r), 2n) == True{}:
match r:
case FourToOne{}: {==}
case ThreeToOne{}: {==}
case TwoToOne{}: {==}
Splitting r with match reduces every branch
by pure computation to True{} == True{}. The
{==} reflexivity primitive discharges each branch in
microseconds without tactic search or typeclass backtracking.
2. Using Bend 2 as a Formal Test Engine Instead of (or Alongside) Lean 4
Many engineering teams assume that using a formal proof language requires rewriting their production backend in that language. In practice, one of the most effective architectures uses Bend 2 purely as a formal test engine and differential reference oracle for an existing TypeScript, Rust, or Python application.
How the Bend 2 Test Engine Architecture Works
Consider a complex state-machine domain such as a multi-phase
strategy rules engine, an order-book settlement kernel
(pareto-frontier), or an OAuth2/PKCE state machine
(oauth2-fsm in bend2-packages):
Keep the Production Service in TypeScript or Rust: Your existing WebSocket server, database handlers, and UI clients remain in TypeScript or Rust.
Author the Normative Rules and Laws in Bend 2:
actions.bend,lifecycle.bend, andtopology.bendencode the pure transition rules.LAWS.bendpins invariant contracts (conservation of resources, turn-phase admission, and explicit rejection of underflow/overflow).PROOF.benddischarges every law in 0.38 s viabend PROOF.bend(benchmark).
Compile
SMOKE.bendto Both Native CPU and JavaScript (.js):# Verify all inductive proofs first bend formal/bend2/domain/PROOF.bend # Compile the smoke & oracle suite to both native binary and JS bend formal/bend2/domain/SMOKE.bend -o "$OUT/smoke" bend formal/bend2/domain/SMOKE.bend -o "$OUT/smoke.js" # Verify deterministic parity across 1-thread CPU, 4-thread CPU, and Bun JS "$OUT/smoke" +RTS --gpu off --threads 1 -RTS > "$OUT/cpu-1.txt" "$OUT/smoke" +RTS --gpu off --threads 4 -RTS > "$OUT/cpu-4.txt" bun "$OUT/smoke.js" > "$OUT/js.txt" diff -u formal/bend2/domain/SMOKE.expected "$OUT/cpu-4.txt" diff -u formal/bend2/domain/SMOKE.expected "$OUT/js.txt"Run Differential Trace Comparison (
compare.bend) Against Production: The Bend 2 oracle ingests state-transition rows emitted by the production TypeScript/Rust test suite (before_state,action,observed_decision,observed_after_state) and outputs a machine-readable JSON verdict:{"version":1,"verdict":"conformant","modelDecision":"granted"}{"version":1,"verdict":"divergent","modelDecision":"denied","cause":"decision"}{"version":1,"verdict":"divergent","modelDecision":"granted","cause":"state"}
Why Teams Prefer This Over a Standalone Lean 4, Goose, or TLA+ Test Harness
| Requirement in a Production Test Engine | Bend 2 (LAWS.bend + bend -o .js) |
Lean 4 (lake build + Fixtures) |
MIT PDOS Goose (Go ->
Rocq/Lean) |
TLA+ / Alloy |
|---|---|---|---|---|
Runs in-process inside bun test / Node /
Vitest |
Yes (bend -o oracle.js emits
zero-dependency JS) |
No (requires FFI or external subprocess CLI piping) | No (lifts restricted Go subset to Rocq/Iris or Lean) | No (JVM/TLC model checker only) |
| Parallel multi-core / GPU state-space sweeps | Yes (+RTS --threads 16 or GPU
warps) |
Manual Task spawning on CPU only |
Go runtime CPU goroutines only | Multi-threaded TLC (CPU only, high RAM) |
| Proof feedback time on rule edits (source) | 0.38 s | 19.2 s | 6.0 s+ (Rocq / Perennial
/ Iris separation logic) |
Seconds to minutes (state explosion) |
| Independent small-kernel verification | Yes (via bend --verdict exporting to
Lean 4) |
Native (Lean 4 kernel +
lean4checker) |
Native (Rocq or Lean 4 kernel over
GooseLang) |
TLAPS (separate proof obligations) |
Open-source projects in our ecosystem directory already use
this exact pattern, including Dicklesworthstone/beads_bend
and Dicklesworthstone/toon_bend
(368 machine-checked laws verified across 4 parallel lanes). Bend 2 lets
you maintain one executable specification that serves as both your
formal proof artifact and your differential
test runner.
3. Lessons from Anthropic’s 13-Million-Line Lean Formalization, 180-Million-Line Rocq Study, and Proof-Blocked AI
In late August and September 2026, Anthropic announced that Claude
had completed the first machine-checked formalization of
Fermat’s Last Theorem (FLT) in Lean 4 (anthropics/fermats-last-theorem).
The repository spans 13 million lines of Lean 4 and
mechanically verifies 29,500 intermediate lemmas on top
of Mathlib 4 (Anthropic
FLT research). On October 22, 2026, Anthropic and Theorem also
published “LLMs
are making formal software verification feasible at a much larger
scale”, verifying 239 OSS-Fuzz targets in
180 million lines of Rocq. Read our standalone analysis
in Anthropic’s
180-Million-Line Formal Verification Milestone: Why Bend 2 Was Invented
for Exactly This.
Our verification series on gerryburde.com (Part
1: Machine-Checked Proofs and Part
3: Bend 2 and Proof-Blocked AI) analyzed Anthropic’s Lean milestone.
It revealed both the breakthrough and the
operational bottleneck of using Lean 4 with AI coding
agents:
- The Breakthrough (The Micro-Kernel Loop): Frontier LLMs hallucinate subtle edge cases when prompted in natural language, but excel when paired with a deterministic proof kernel that rejects invalid steps.
- The Bottleneck (13 Million Lines of Verbosity &
19.2 sTactic Latency): Anthropic’s 13 million lines of Lean 4 reflect machine tactic verbosity and deep Mathlib dependency chains. When an AI coding agent edits ordinary application code (such as a state machine, parser, or ledger), waiting19.2 sperlake buildcheck and debugging fragile stateful tactic scripts (simp,omega,rw) breaks inner-loop velocity (Kevin Ngo benchmark). - Specification Risk Relocated (Why Laws Must Bind):
Both Anthropic’s FLT documentation and our Bend
2 Proof-Blocked AI experiments demonstrate the foundational rule of
formal methods. A formal proof guarantees only that the encoded
conclusion follows from the encoded assumptions. In our Bend 2 sort
experiment, a law stating only
is_sorted(sort(xs)) == True{}allowed an AI agent to return an empty list[]and pass"All terms check."Teams must write the law pair (is_sortedandis_permutation). They must also guardLAWS.bendwith fail-closed CI checks that reject@unsafeand mutation-test laws against known-bad code (Proof-Blocked AI field guide).
4. Bend 2 vs. MIT PDOS Goose, Verus, Kani, and Dafny
How does Bend 2 compare against other code-verification frameworks
like MIT PDOS Goose
(github.com/tchajed/goose), Verus,
Kani, and Dafny?
- MIT PDOS Goose (
GotoGooseLangin Rocq/Perennial/Iris or Lean 4): Goose translates a restricted subset of Go (structs, pointers, locks,fork) intoGooseLangmodels (MIT PDOS Goose). Researchers use it to prove crash safety and concurrent separation logic invariants in Rocq (Perennial/Iris) or Lean 4. Goose is purpose-built for verifying stateful POSIX filesystems, write-ahead logs, and lock-based storage servers in Go. However, Goose proofs require heavy interactive separation-logic engineering (6.0 sto minutes per build) and cannot compile algorithms to GPUs. - Verus & Dafny (SMT-Backed Verification): Offload proof obligations to external Z3/cvc5 SMT solvers. When an SMT query hits quantifier instantiation limits (“spurious timeout”), AI coding agents get stuck without a constructive counterexample.
- Bend 2 (
LAWS.bend+PROOF.bend): Eliminates the translation gap because you prove the exact.bendfile you compile. Unlike Dafny or Verus, proofs check deterministically by beta-reduction in 0.38 s with zero SMT solver timeouts (benchmark dataset).
5. Runtime Performance: All 16 Apple M4 Max Benchmarks (Bend 2 vs. Lean 4)
Lean 4 includes a functional-to-C compiler with reference-counting
reuse (FBIP, Functional But In-Place). On purely
sequential, single-core recursive code (SEQ-CPU), Lean 4
and sequential Bend 2 are often within 1.1x to 2x of each other (ecosystem benchmark dataset).
However, Lean 4 does not automatically parallelize recursive tree reductions across CPU cores or compile functional code to GPU warps. Across all 16 benchmarks in our ecosystem benchmark dataset on an Apple M4 Max:
| Benchmark (dataset) | Lean 4 (1c C) | Bend SEQ-CPU (1c) | Bend PAR-CPU (16c) | Bend PAR-GPU (Metal) | PAR-CPU vs. Lean 4 | PAR-GPU vs. Lean 4 |
|---|---|---|---|---|---|---|
| Conway’s Game of Life (source) | 13.849 s | 7.803 s | 0.647 s | 0.063 s | 21.4x faster | 219.8x faster |
| N-Body Gravity (source) | 5.387 s | 5.758 s | 0.516 s | 0.059 s | 10.4x faster | 91.3x faster |
| Merkle Tree Build (source) | 5.986 s | 4.222 s | 0.422 s | 0.071 s | 14.2x faster | 84.3x faster |
| Tree Bitonic Sorter (source) | 38.123 s | 6.043 s | 0.799 s | 0.450 s | 47.7x faster | 84.7x faster |
| Mandelbrot Fractal (source) | 4.575 s | 4.473 s | 0.395 s | 0.057 s | 11.6x faster | 80.3x faster |
| Procedural Terrain (source) | 8.039 s | 2.225 s | 0.197 s | 0.106 s | 40.8x faster | 75.8x faster |
| 3D Raytracing Engine (source) | 10.825 s | 4.616 s | 0.396 s | 0.151 s | 27.3x faster | 71.7x faster |
| Breadth-First Search (source) | 9.274 s | 3.918 s | 0.343 s | 0.207 s | 27.0x faster | 44.8x faster |
| Tree Matrix Multiply (source) | 7.758 s | 2.064 s | 0.226 s | 0.231 s | 34.3x faster | 33.6x faster |
| Levenshtein Distance (source) | 4.799 s | 2.559 s | 0.236 s | 0.144 s | 20.3x faster | 33.3x faster |
| K-Means Clustering (source) | 5.686 s | 2.069 s | 0.268 s | 0.191 s | 21.2x faster | 29.8x faster |
| Parallel Radix Sorter (source) | 6.500 s | 4.083 s | 0.445 s | 0.314 s | 14.6x faster | 20.7x faster |
| Regex Parallel Lexer (source) | 4.487 s | 2.144 s | 0.198 s | 1.075 s | 22.7x faster | 4.2x faster |
| N-Queens Solver (source) | 7.704 s | 5.406 s | 0.455 s | 0.933 s | 16.9x faster | 8.3x faster |
| Symbolic Regression (source) | 2.457 s | 3.014 s | 0.266 s | 0.529 s | 9.2x faster | 4.6x faster |
| Concurrent HAMT Map (source) | 1.526 s | 2.741 s | 0.238 s | 0.521 s | 6.4x faster | 2.9x faster |
Notice the two honest nuances in the table above (ecosystem benchmark dataset): 1. On
1-core sequential execution (SEQ-CPU),
Lean 4’s FBIP allocator beats sequential Bend 2 on pointer-heavy
hashmap (1.526 s vs 2.741 s),
symreg (2.457 s vs 3.014 s), and
nbody (5.387 s vs 5.758 s). 2.
Once Bend 2 enables 16-core PAR-CPU or
PAR-GPU, it beats Lean 4 across all 16
benchmarks. Speedups range from 6.4x to 47.7x
on multi-core CPU and up to 219.8x on GPU (dataset).
6. Where Lean 4 Still Wins (and Why We Use Both)
Bend 2 does not replace Lean 4 in every domain. Choose Lean 4 directly when:
- You Need Classical Mathematics or Mathlib: Bend 2 is built for constructive, structural induction over algebraic data types and finite machine integers/trees. If your proof requires real analysis, measure theory, algebraic geometry, or number-theoretic lemmas from Mathlib (like Anthropic’s 13-million-line Fermat’s Last Theorem formalization), Lean 4 is unmatched (Anthropic FLT repository).
- You Are Proving Programming Language Metatheory:
HigherOrderCO proved Bend 2’s own type soundness and Church-Rosser
confluence (
bend2/bendtt.lean) in Lean 4 (bendtt.lean formalization). - You Require an Independent Kernel Audit Receipt: In
high-assurance release gates, we run
bend PROOF.bendfor fast 0.38 s developer/agent feedback during implementation. At release closeout, we invokebend --verdict(or standalone Lean 4 receipt verification checking#print axioms) in CI so the final proof obligation is certified by the Lean 4 kernel (Tripartite Verification Stack).
7. Decision Matrix: When to Choose Bend 2 vs. Lean 4, Goose, or Cedar
| Engineering Scenario | Recommended Choice | Rationale |
|---|---|---|
| Executable test engine & differential oracle for a TypeScript/Rust/Go app | Bend 2 | Compiles the proved spec to both native CPU and .js
(bun), checking laws in 0.38 s (benchmark). |
| Guardrailing AI coding agents with human-written invariants | Bend 2 (LAWS.bend /
PROOF.bend) |
50.5x faster proof checking (0.38 s vs
19.2 s) keeps agents in flow without tactic hallucination
(Proof-Blocked
AI). |
| Parallel tree, graph, simulation, or BVH compute with formal guarantees | Bend 2 (PAR-CPU /
PAR-GPU) |
6.4x to 219.8x faster runtime execution than Lean 4 across all 16 M4 Max benchmarks (ecosystem benchmark dataset). |
| Deep mathematical formalization or cryptographic number theory | Lean 4 + Mathlib | Rich tactic library (omega, linarith,
ring) and over 1.5 million lines of formalized
mathematics. |
| Final release-gate kernel attestation | Both (Bend 2 +
Lean 4 --verdict) |
Fast inner-loop iteration in Bend 2 backed by Lean 4 kernel verification at release closeout. |