Incremental verification, counterexample triage, and verified native/SIMD/GPU/TPU compilation for Lean 4.
turbo-lean combines an incremental verification oracle (TurboLean) and a formally verified native compiler (LeanNative) on top of lean-sys.
- Sub-2ms Incremental Proof Verification & Counterexample Triage (
TurboLean):- Re-check edited theorems in milliseconds via warm
ZygoteWorkercheckpoints, content-addressed.olean/shard caching, and topological dependency cones instead of rebuilding whole Lake packages. - Reject false conjectures in under a millisecond via CPU SIMD / GPU fast-check and classify counterexamples (
CandidateBug | SpecTooStrong | ReferenceDiscrepancy) through in-process C-ABI gates. - Block reward hacking on untrusted proofs: enforce in-kernel transitive axiom checks (
{propext, Quot.sound, Classical.choice}), rejectsorryAx/native_decide/trustCompiler/unsafe/@[extern]/@[implemented_by]bypasses, pin theorem statements from kernel declarations, select kernel engines fail-closed (--kernel auto|standard|con-leche|both,--base-isolated), and emit BLAKE3 provenance receipts (schema_version = 2, with optional Ed25519 signatures).
- Re-check edited theorems in milliseconds via warm
- Verified Native & Accelerator Compilation (
LeanNative):- Lower pure Lean 4 (
Lean.Compiler.LCNF) throughLNIRto standalone native CPU/SIMD code, GPU kernels, TPU programs, and zero-copy C99/Rust FFI without sacrificing Lean's soundness guarantees. - Keep every heuristic optimizer, hardware plugin (
@[simd],@[gpu],@[tpu],@[superopt]), and stochastic/autotuned rewrite strictly untrusted behind kernel refinement proofs or symbolic translation validation. - Pair pure compute kernels with
lean-sys(0 import Lean,libleanrt.a-only) when compiled Lean 4 binaries need OS interaction (POSIX I/O, WAL/CAS storage, shared memory, sandboxing, HTTP/JSON-RPC).
- Lower pure Lean 4 (
- North-Star Roadmap (
roadmap/GOALS.md):- Verified In-Memory Kernel (
G1): Vendorleanprover/con-leche(+lean4leancorner cases) over zero-copy mmap'd.oleanshards for sub-millisecond Lean-in-Lean receipt replay. - Elaboration De-Trusting (
G2): Surface-ASTHeaderPinround-tripping and distilled CAS kernel proof-term replay (LNIR_EXPR_V1) that bypassesLean.Elabon warm builds. - Broader
LCNF& Upstream Target Semantics (G3–G4): ExpandLCNFlowering to general recursion, closures, and signedInt*; link upstreamopencompl/lean-mlir,leanprover/LNSym, andopencompl/sail-riscv-lean; and unify production text emission directly over the proved Target-IR AST pretty-printers. - Ecosystem Differential Fuzzing (
G5): Scale 3-way differential conformance across the upstream Lean 4 run suite, real Lake packages, and continuousLCNFfuzzing.
- Verified In-Memory Kernel (
Registered soundness and refinement theorems in lean/LeanNative/Proofs/AxiomAudit.lean (#verify_lean_native_axioms), alongside CI kernel replay across lean/TurboLean/Spec.lean and lean/LeanNative/Spec.lean, are checked in the Lean 4 kernel against standard axioms [propext, Quot.sound, Classical.choice] with zero sorryAx, Lean.ofReduceBool, Lean.ofReduceNat, or Lean.trustCompiler:
- Frontend Lowering (
Lean.Compiler.LCNF -> LNIR): Semantic refinement (lowerRealLCNFCode_sound,lowerRealLCNFDecl_sound) against the in-repoRealLCNFSemanticsevaluator over upstreamLean.Compiler.LCNFdeclarations for the supported scalar, flat-tuple, and bounded Stage-1D constructor/ADT subset, plus retained LCNF proof facts (ProofRetention.lean) discharging bounds and exclusivity checks. - Middle-End Optimizations & Ownership/RC (
LNIR -> LNIR): Machine-checkedCodeRefinesproofs across algebraic peepholes and equality saturation (EqSat.lean), structured loop scheduling andparForkernel fusion (LoopSchedule.lean,KernelFusionProofs.lean), closure defunctionalization and ADT/slice lowering (ClosureADTProofs.lean,SliceViewProofs.lean), unboxed regions and Perceus/COW reference-counting elision (UnboxedRegion.lean,OwnershipRC.lean,HeapObjAbiProofs.lean), and zero-alloc stackless async state machines (AsyncStateMachine.lean). - Translation Validation & Certifying Superoptimization (
SymbolicTV&@[superopt]): Unconditional structuralCodeRefinessoundness (symbolicCodeRefines_soundinSymbolicTVRefinesSoundness.lean) plus fuel-bounded symbolic execution (SymbolicTVSoundness.lean), alongside kernel-checked cost-guided and stochastic superoptimization (SuperoptSoundness.lean,StochasticCert.lean,CostAutotune.lean). - Backend AST Lowering & Host-Device ABI Contracts: Semantic preservation for each supported
LNIRfragment into four typed Target-IR ASTs (lowerToLLVM_sound,lowerToPTX_sound,lowerToMetal_sound,lowerToStableHLO_sound), verified C99 ABI /HeapObjlayout, multi-threaded RC/COW, and zero-copy host-device dispatch contracts (validateHybridDispatch_sound,execZeroCopySliceIoHostCall_ok_sound,HeapObjAbiProofs.lean,HeapObjMtAbiProofs.lean), and tight non-vacuous C-ABI decision gates inlean/TurboLean/Spec.leanandlean/LeanNative/Spec.lean.
(Note: lean/TurboLean/ConLeche.lean and lean/TurboLean/ElabAudit.lean prove CIC typechecking and statement-pinning soundness over a self-contained model; wiring official leanprover/con-leche to real .olean files remains G1/G2.)
LeanNative formalizes four typed Target-IR ASTs with executable operational semantics in pure Lean 4, paired with verified C99 ABI / HeapObj and host-device dispatch contracts:
- LLVM IR (
LLVMAst) — Scalar, control-flow, memory, and generic LLVM vector IR (with text codegen targetingx86_64AVX2/AVX-512 andaarch64NEON), modeling exactBitVecarithmetic,poison/nsw/nuwpropagation, anddisjointSlices-backednoaliasnon-interference (lean/LeanNative/Backend/LLVMAst/). - NVIDIA CUDA PTX (
PTXAst) — SIMT GPU kernels (sm_80/sm_90), modeling warp/thread execution, shared/global memory race traps, per-barrier generation epochs (bar.syncwith early-retthreads), and optionalpo/rf/co/swweak-memory consistency (lean/LeanNative/Backend/PTXAst/). - Apple Metal 3.0 MSL (
MetalAst) — Apple Silicon GPU kernels, modeling threadgroup execution, barriers, and disjoint buffer-offset bounds (lean/LeanNative/Backend/MetalAst/). - Google TPU StableHLO (
StableHLOAst) — Shaped 1D/scalarStableHLOtensor programs (lean/LeanNative/Backend/StableHLOAst/; companion JAXPallasemission lives inMLIRGPU.lean). - C99 ABI,
HeapObj& Hybrid Dispatch Contracts (CAbiEmit/Hybrid) — Verified C99structlayouts,lean.hHeapObjheader/RC conservation (docs/HEAPOBJ_CONTRACT.md), and zero-copy host-device slice launch/marshaling contracts (lean/LeanNative/Backend/Hybrid.lean,lean/LeanNative/Driver/CAbiEmit.lean).
Covered PTX kernels, Metal scalar/device functions and wrappers, and i64 StableHLO map/zip/saxpy/dot operations now share the typed AST printers with production emission. Parsers cover scalar PTX operands/addresses/instructions/statements/declarations/kernels, Metal scalar expressions/types/attributes/parameters/statements/blocks/kernels, and the 22-operation covered StableHLO fragment (constant, 12 binary arithmetic/bitwise/shift ops, compare, select, convert, reshape, broadcastInDim, dotGeneral, slice, returnOp) plus FuncOp with SSA TypeEnv threading. Kernel-checked roundtrips (parse ∘ pp = some) and canonical printer injectivity are proved across LLVM types (ppType_injective), covered canonical PTX operands/addresses/instructions/statements/kernels (parsePTXKernel_roundtrip, ppPTXKernel_injective_on_canonical), Metal canonical expressions/types/attributes/parameters/statements/blocks/kernels (parseMSLKernel_roundtrip, ppMSLKernel_injective_on_canonical), and covered canonical StableHLO operations/bodies/functions (parseStableHLOOp_roundtrip, ppStableHLOOp_injective_on_canonical, parseFuncOp_roundtrip, ppFuncOp_injective_on_canonical), alongside lowerer canonicality and roundtrip corollaries (PTXLowerProofs.lowerKind_canonical/lowerKind_roundtrip, MetalLowerProofs.lowerKind_canonical/lowerKind_roundtrip, MLIRGPU.lowerStableHLODeclFunc_canonical/lowerStableHLODeclFunc_roundtrip, and lowerToStableHLO_canonical/lowerToStableHLO_roundtrip) under explicit lexical identifier-validity premises. The axiom audit contains 835 unique theorems (830 LeanNative.* plus 5 ConLeche/TurboLean.ElabAudit). Uncovered AST constructors (StableHLOOp.dynamicSlice/reduce, non-scalar PTX atomic reductions such as dotProduct, and general LLVM instruction/function parser roundtrips) and remaining standalone string-emission helpers (LLVM.lean separate from optional LLVMEmitVerify, plus uncovered wrapper/helper paths in MLIRGPU.lean, Metal.lean, Hybrid.lean, and CoreCLI.lean) remain open, and these Target-IR models are authored in-repo rather than wired to upstream lean-mlir/LNSym. Trusting compiled output relies on four explicit layers:
- In-Kernel Refinement Proofs (
Proven): For each supportedLNIRfragment and theorem precondition,LNIR -> Target-IR ASTlowering and Hybrid dispatch validation are proved in the Lean 4 kernel to preserve modeled semantics without introducing modeledpoison, out-of-bounds slice access, barrier divergence, or shared-memory races. - Anti-Cheat Shadow-Semantics Gate (
Checked in CI):scripts/check_shadow_semantics.lean(docs/SHADOW_SEMANTICS.md, empty allowlist) checks the elaborated constant graph for forbiddenLNIR-evaluator dependencies and verbatim/alpha-equivalent syntactic clones. - Differential Conformance, Unwrapped
libleanrt.aSanitizer Stress & Emitter Mutation Checks (Tested in CI): The roadmap workflow runs TierC1scalar/tuple/state-machine programs, provisioned TierC2multi-module Lake package fixtures (verification/c2-lake-packages), and TierC4LCNFfuzz cases 3-way across (a) stock Lean 4, (b)evalCode, and (c) native LLVM binaries compiled with ASan (plus UBSan on the C driver/link runtime), alongsidescripts/run_heapobj_abi_sanitizers.pylinking unwrappedlibleanrt.a+libInit.a(LEAN_MIMALLOCactive) +CAbiEmitheaders/IR under ASan/LSan/UBSan and TSan (lean_mark_mt4-thread stress + positive memory/race traps), while Stage-1DHeapObjRC-conservation (live_objects == 0) runs 2-way against stock Lean (sincecompile-llvmrejects heap-ADT ops fail-closed untilG3.2). The impact planner selects 16 seeds for narrowly classified changes, 50 for backend/runtime/unknown inputs, and 64 for main pushes or manual runs; standalone conformance defaults to 32. Docs-only PRs skip compilation only when non-documentation Git-tree digests also agree. Fast inventory, planner, and mutation-runner tests still run. Build caches use the input digest and never substitute for verification. In-kernel mutant kill proofs (GPUEmitterMutantProofs.lean,LLVMEmitVerify.lean) andemitter_mutants.py --unit-testsgate code PRs; the latter also checks that active M1–M10 each apply exactly once. Full mutation campaigns run on push/manual dispatch. Multi-seed Rust parity indocs/BENCH_RUST_PARITY.mdis benchmark evidence; external ecosystemC2packages (Batteries,SciLean,ArkLib), fullSEALEDC3campaigns, and GPU/TPU hardware qualification remain open underG3.3/G5. - Explicit Vendor Toolchain TCB (
Trusted,Target IR -> Silicon): Downstream vendor compilers and runtimes (clang/LLVM,ptxas/CUDA, Apple's Metal compiler, XLA/Pallas, the C compiler, Lean 4.35.0's kernel/lean.hruntime, OS loader, and hardware) form the Trusted Computing Base;turbo-leandoes not verify binary machine code below Target IR.
# 1. Incremental Build, Theorem Check & Deterministic Eval
turbo-lean build [target...] [--seed-shards] [--project-dir <dir>]
turbo-lean check <file-or-dir> [--decl <id>] [--kernel auto|standard|con-leche|both] [--jobs <N>] [--max-memory-mb <MB>]
turbo-lean eval <file.lean> [--lean-path <paths>] [--timeout-ms <ms>] [--verify-determinism]
# 2. Agent Loop, Fast-Check (with Counterexample Triage) & Spec Audit
turbo-lean agent-step <file.lean> [--decl <id>] [--max-memory-mb <MB>]
turbo-lean fast-check <file.lean|kernel.cu> [--decl <id>] [--backend auto|cpu|gpu|tpu]
turbo-lean audit-spec <file.lean>
# 3. Native & Accelerator Compilation (`lean-native` for `.lean`; `lean-native-core` for LNIR wire/JSON)
(cd lean && lake exe lean-native compile-llvm --input <input.lean> -o <out.ll>)
(cd lean && lake exe lean-native compile-ptx|compile-metal|compile-mlir --input <input.lean> -o <out>)
(cd lean && lake exe lean-native emit-cabi-bindings --input <input.lean> --emit-header <out.h>)
(cd lean && lake exe lean-native-core self-test && lake exe turbo-lean-native self-test)
# 4. PR/CI Verification, Cryptographic Receipts & Portable CAS (.tlpack)
turbo-lean pr-verify [--base <git-ref>] [--mode untrusted|trusted] [--kernel auto|standard|con-leche|both] [--receipt <out.json>] [--signing-key <32-byte-hex>]
turbo-lean ci-verify [--base <git-ref>] [--mode untrusted|trusted] [--kernel auto|standard|con-leche|both] [--receipt <out.json>] [--signing-key <32-byte-hex>]
turbo-lean verify-receipt <receipt.json> [--verify-pubkey <32-byte-hex>] [--require-signature] # pin expected signer via --verify-pubkey; require any signature via --require-signature
turbo-lean export -o cache.tlpack && turbo-lean import cache.tlpack- Toolchain & Option Pinning: Verdict-producing commands resolve
lean,lake, andleancstrictly from the installed toolchain (PATHrestricted to the toolchain plus/usr/bin:/bin). Lake compiler flags are validated against a static allowlist (warn.sorry = true,debug.skipKernelTC = false; disalloweddebug.*/internal.*/weak.<disallowed>options are rejected, and source/lakefile scanners rejectrun_elab/run_cmd/run_metabypasses). Non-proof edits automatically re-verify reverse-dependent modules (bwrap-isolated in--mode untrusted). - Configurable Axiom &
@[csimp]Policy (turbo-lean.toml): Projects usingbv_decideornative_decidecan allow auxiliary axioms via[axioms] allow = ["*._native.bv_decide.ax_*"](resolved from--baseinpr-verify; neversorryAxorLean.trustCompiler) and[csimp] allow = [...]. - Worktree & Telemetry Controls: Pass
--no-auto-worktree-link(or[worktree] auto_link = false) to keep worktree build artifacts isolated without CAS hardlinking. Telemetry is local-only by default (remote = false) and can be disabled completely with--no-telemetry,TURBO_LEAN_TELEMETRY=off, orturbo-lean telemetry disable.
# (a) Untrusted PR verification (`pull_request`) with statement pinning & receipt output
- name: Incremental Lean 4 PR Verification
id: verify
uses: Th0rgal/turbo-lean@main
with:
project-dir: "."
command: "pr-verify"
mode: "untrusted"
base-ref: "origin/main"
disallow-statement-changes: "true"
receipt-path: "receipt.json"
# (b) Main-branch CI verification (`push`) with portable .tlpack CAS & GDSF GC
- name: Lean 4 CI Verification & Portable CAS Seed
id: ci
uses: Th0rgal/turbo-lean@main
with:
project-dir: "."
command: "ci-verify"
mode: "trusted"
seed-shards: "true"
cache-key-prefix: "turbo-lean-cas"
max-reclaimable-bytes: "2147483648"
receipt-path: "ci-receipt.json"
# (c) Optional remote prebuild fetch (outputs: status, interface-hash, action-key, theorem-shards, failing-module, artifact-sha256)
- name: Fetch Remote Prebuild Artifact
id: prebuild
uses: Th0rgal/turbo-lean@main
with:
project-dir: "."
command: "remote-prebuild"
remote-prebuild-url: ${{ vars.TURBO_LEAN_PREBUILD_URL }}
remote-prebuild-output: "prebuild.export.zst"
remote-prebuild-format: "export.zst"
remote-prebuild-timeout-secs: "300"- Native Execution Parity (
docs/BENCH_RUST_PARITY.md): Across the canonicalLNIRkernel suite on Zen 5, optimizedLeanNativeachieves 0.875× Rust execution time (1.14× throughput geomean, with bit-exact checksum parity across the reported seed set; algorithm-sensitive 256-bit EVM wins offset remaining array-kernel gaps). Reproduce withpython3 scripts/bench_rust_parity.py --trials 7. - Incremental Proof Verification (Reported Reference Measurements): Warm single-theorem checks on
lfglabs-dev/verity(Uint256.add_comm, warmZygoteWorker) reported2.80 ms(1,280×vs.3,587.5 mscoldlake env lean), cachedpr-verify --base HEADacrossverity(9,275 shards) reported23.89 ms, in-process C-ABI counterexample triage measured~3 nsvs.40 µsJSON-RPC IPC, and 1M-state 256-bit GPUfast-check(CuDenoteon NVIDIA GB10) reported1.41B states/s(0.71 ms).
External tactic accelerators, counterexample finders, and LNIR_WIRE_V1 superoptimizers run out-of-process over single-line JSONL stdin/stdout behind SpeculativeGate (untrusted_accelerator_kernel_replay_sound, untrusted_opt_plugin_tv_gate_sound)—no Lean 4 compiler build is needed to author or validate a plugin:
python3 sdk/accelerator-template/validate.py
TURBO_LEAN_ACCELERATORS_CONFIG=sdk/accelerator-template/accelerators.example.json turbo-lean acceleratorsSee sdk/accelerator-template/ for the zero-dependency starter kit and roadmap/OPEN_CHALLENGES.md for Track A (zero-TCB plugins) and Track B (G1–G5 core verification) challenges.
lean/TurboLean/— Incremental verification specs, C-ABI policy gates (PolicyGates.lean,Spec.lean), statement hashing, in-kernel axiom inspector, and warmZygoteWorker.lean/LeanNative/— VerifiedLNIRcompiler:LCNFbridge (IR/,Frontend/), verified optimization & superoptimization passes (Transform/), operational semantics (Semantics/), Target-IR ASTs & emitters (Backend/), and machine-checked soundness proofs (Proofs/).crates/turbo-lean/— Rust CLI, MCP server, DAG module scheduler,bwrapsandboxed PR verifier, CAS/Zygote orchestrator, and C-ABI bridge.sdk/accelerator-template/&plugins/— Zero-TCB JSONL-stdio accelerator &LNIRsuperoptimizer starter kit, offline validator (validate.py), and domain plugin examples.roadmap/— North-Star goals (GOALS.md), open contributor challenges (OPEN_CHALLENGES.md), cheat-proof theorem contracts (VALIDATION_CONTRACTS.md), 3-way differential conformance (CONFORMANCE_SUITE.md), and Maxwell oracle pointer (MAXWELL_STRATEGIES.md).