DEV Community

Cover image for Zean 0.2.0 generates Lean models inside your own Zig project
Thiru
Thiru

Posted on

Zean 0.2.0 generates Lean models inside your own Zig project

Zean turns Zig's analyzed intermediate representation (AIR) into executable Lean models for supported functions. You state what a function should do and prove it in Lean against the generated program. The memory verifier uses Lean's Std.WP and vcgen to generate the proof obligations.

Zean 0.2.0 adds zean [directory], which writes the Lean modules into your own project. You commit them next to your Zig code and check the proofs with Lake, locally and in CI.

Running Zean on a project

A client is a manifest that tells Zean what to capture and what to prove. This is the client example from the repo, for one function:

export fn put(pointer: *u32, value: u32) void {
    pointer.* = value;
}
Enter fullscreen mode Exit fullscreen mode
{
  "schema": "zean.client.v1",
  "source": "cell.zig",
  "sources": [],
  "roots": ["cell.put"],
  "profile": "memory",
  "generatedModule": "Cell.Generated",
  "libraries": ["Compiler.ZigAir.Verify.Words"],
  "proofs": [{"module": "Cell.Proofs", "path": "Proofs.lean"}],
  "theorems": ["Cell.safe"]
}
Enter fullscreen mode Exit fullscreen mode

The manifest names the Zig source, the root function, the translation profile and the Lean module to generate. It also points to the handwritten proof and the theorem to check. Here Cell.safe states that every finite execution prefix of put avoids memory faults for any valid, writable caller region of at least one word.

From the Zean checkout, generate the example's model with:

zean examples/client
Enter fullscreen mode Exit fullscreen mode

In your own project, run zean from the project root or pass its path. Zean writes the model to lean/Cell/Generated.lean and lists it in zean.outputs.json. Commit both with the Zig source and the proof. Zean only replaces files that carry its autogenerated header.

One client goes in client.json at the project root. For several, add a zean.json that lists them in run order. It can also set the Lean source directory.

Checking proofs with Lake

Put leanprover/lean4:v4.35.0 in lean-toolchain. Move the proof to lean/Cell/Proofs.lean and set its path in client.json to match, so Lake finds it under the lean source directory. Then add Zean's Lean library as a Lake dependency. With Zean checked out next to your project:

import Lake
open Lake DSL

package cell_verification where
  leanOptions := #[⟨`warningAsError, true⟩]

require zean from "../zean"

@[default_target] lean_lib Cell where
  srcDir := "lean"
Enter fullscreen mode Exit fullscreen mode

In lean/Cell.lean, import the proof and audit the theorem:

import Cell.Proofs
import Compiler.ZigAir.ProofAudit

#audit_proof "Cell.safe" from ["Cell.Proofs"]
Enter fullscreen mode Exit fullscreen mode

Then run LEAN_NUM_THREADS=2 lake build. It checks the generated correspondence theorems and your proof, along with the axioms the proof depends on.

CI

zean --check reports generated files that are missing or stale, and obsolete ones from modules you dropped. To capture the same way on every machine, pin the Zig target and CPU in zean.json:

"captureTarget": {"triple": "aarch64-macos.15.0...15.0", "cpu": "baseline"}
Enter fullscreen mode Exit fullscreen mode

Without it, Zean captures for the host it runs on.

Recursion, generics and concurrency

The recursive-calls profile certifies direct self-recursion and mutual recursion. Zean also certifies calls to explicit type specializations and to concrete runtime anytype instances. Methods on types returned by type factories are now matched to their source declarations, nested factories included.

A new example covers concurrent code. publish fills two byte slots, then sets a flag to 1 with a release store. readEntry reads a slot once its acquire load sees 1. With one writer publishing once, the Lean proof shows that a reader who sees the release gets exactly the requested byte. A second proof shows the Lean model matches the captured AIR.

The repo includes counterexamples too. Moving the release store between the two writes can expose an uninitialized slot, and weakening either the load or the store to .monotonic loses the guarantee.

What a proof covers

Zean rejects AIR outside the selected profile. Lean checks each theorem for the admitted model and its stated assumptions. Source validity, native resource bounds and compiler correctness need separate evidence.

Try it

Zean 0.2.0 is on main. The demo needs Zig 0.17.1 and the pinned Lean toolchain:

git clone https://github.com/okcontract/zean
cd zean
elan toolchain install leanprover/lean4:v4.35.0
zig build ziglings
Enter fullscreen mode Exit fullscreen mode

It checks the official fixes for Ziglings 038 and 054 natively, then checks initialization and bounds properties of the derived kernels in Lean. The demo uses retained captures.

Running Zean on your own project needs the standalone executable, which bundles an instrumented Zig compiler. You build that compiler from a clean Zig 0.17.1 checkout against LLVM and LLD 22.x, using CMake with Ninja, on macOS or Linux. The toolchain guide has the steps. On an Apple silicon MacBook Air with LLVM and LLD 22 from Homebrew, that build took about six minutes. Then:

zig build standalone -Doptimize=safe -Dtoolchain-report=/path/to/toolchain/report.json
export PATH="$PWD/zig-out/bin:$PATH"
zean /path/to/project
Enter fullscreen mode Exit fullscreen mode

The client example in the repo captures AArch64 macOS AIR.

Repo: github.com/okcontract/zean

Changelog: CHANGELOG.md

Client setup: examples/client

Top comments (1)

Collapse
 
maria_smith_1cde45f6a07f5 profile image
Maria Smith •

Generating executable Lean models alongside implementation files makes kernel auditing way more practical. In Smithian Fold Theory (SFT V4), I pushed Lean 4.35 in a pure-constructive direction across 9,000+ files and 55,000+ theorems with literally ZERO axioms—no Classical.choice, propext, or Quot.sound—deriving 21 physical constants including α⁻¹ = 503846395469/3676744786 (+0.0086σ) checkable directly via #print axioms. Curious if Zean's vcgen obligations stay constructive without Classical.choice too (GitHub: MettaMazza/SFT-v4-LEAN-VERIFIED, Zenodo DOI 10.5281/zenodo.24320778).