{"slug": "zean-write-zig-prove-it-in-lean", "title": "Zean: Write Zig, prove it in Lean", "summary": "Okcontract released Zean, an open-source toolkit that translates supported Zig functions into executable Lean models so developers can formally prove memory contracts and other properties. Zean works from Zig's analyzed intermediate representation (AIR), with an instrumented compiler capturing supported functions and a translator emitting Lean program definitions, while specifications and proofs live in separate handwritten modules; the included copy example proves that, under stated preconditions, the translated model avoids invalid accesses and uninitialized reads, copies source contents into the destination, and preserves source and caller memory outside the destination. The release cites Microsoft's FORGE Lab report of November 9 on memory-safety bugs in Rust's standard library, including cases where safe Rust could trigger undefined behavior, and pitches Zig's explicit memory control plus proofs on selected components as an alternative for developers considering Rust for memory safety.", "body_md": "Today we’re introducing **Zean = Zig + Lean**, an open-source toolkit for translating supported Zig functions into executable Lean models and proving their behavior.\n\nWith it, you can code your implementation in Zig, then use Lean to make its memory contracts explicit and check them mathematically. Or other properties more specific to your software.\n\nWe want to explore a different workflow for systems programming:\n\n- Keep design iteration in Zig.\n- Once a component settles, state what its callers must provide, prove what the component guarantees, and check those proofs again as the implementation evolves.\n\nFor developers considering Rust primarily for memory safety, this offers another direction to investigate: Zig’s explicit control over memory, combined with proofs about selected components.\n\nOn November 9, Microsoft’s FORGE Lab described [memory-safety bugs in Rust](https://www.microsoft.com/en-us/security/blog/2026/11/09/forge-lab-uses-agentic-tools-to-identify-memory-safety-issues-in-rust/)’s standard library, including cases where entirely safe Rust could trigger undefined behavior. The report concerns mistakes in unsafe implementations beneath safe APIs, including assumptions about panics, caller-provided behavior, and operating-system state.\n\nRust’s ownership system provides broad protections during ordinary development. Its safe abstractions also depend on unsafe implementations honoring their contracts. Rust’s [own documentation](https://doc.rust-lang.org/book/ch20-01-unsafe-rust.html) describes this boundary and the programmer’s responsibility for maintaining it.\n\nZean provides these contracts through explicit specifications and proofs. That involves more work per verified component, but it also lets us express properties specific to the algorithm: which memory may change, which contents must remain intact, and what a successful result means. At a time where developers use agentic assistance, these larger proofs are tractable!\n\nConsider the copy loop included in the release:\n\n``` js\ninline fn copyWord(destination: *u32, source: *const u32) void {\n    destination.* = source.*;\n}\n\npub export fn copy(destination: [*]u32, source: [*]const u32, length: usize) void {\n    var index: usize = 0;\n    while (index < length) : (index += 1) {\n        copyWord(&destination[index], &source[index]);\n    }\n}\n```\n\nIts pointers leave important obligations with the caller. The source must contain enough initialized words. The destination must provide enough writable storage. For this copy’s source-preservation contract, the ranges must be disjoint.\n\nIn Lean, those requirements become explicit assumptions. The example proves that, under those assumptions, the translated model:\n\n- Avoids invalid accesses and uninitialized reads throughout execution.\n- Copies the source contents into the destination on successful completion.\n- Preserves the source and caller memory outside the destination.\n\nModeled allocation exhaustion remains an explicit permitted failure. Termination would be a separate property.\n\nThese statements cover every input satisfying the contract. They go beyond checking a collection of example buffers and the caller still has to establish the preconditions.\n\nThe [copy tutorial](https://github.com/okcontract/zean/blob/main/examples/copy/README.md) includes the implementation, generated model, contracts, proofs, and deliberately false claims that must fail verification.\n\nZean works from Zig’s analyzed intermediate representation, AIR. An instrumented compiler captures supported functions, and the translator emits executable Lean program definitions. Specifications and proofs live in separate handwritten modules.\n\nThe verification library derives obligations for memory access, initialization, permissions, arithmetic, control flow, and supported calls. Developers supply contracts, loop invariants, and any remaining mathematical arguments. Lean checks the resulting proofs. Unsupported operations reject and unresolved proof obligations remain obligations.\n\nWe believe this separation is central to the development experience we want. You can explore an implementation before investing in its complete proof. When the design stabilizes, verification can expose assumptions that tests and code review missed and sometimes send you back to improve the implementation.\n\nAfter a source change, you recapture the function, regenerate its model, and replay the proofs. Changing a loop condition from `< length` to `<= length`, for example, introduces an extra boundary access. The original contract cannot justify that access in general.\n\nThe intended benefit is more freedom during design, followed by more verification about the component you intend to rely on (compared to Rust for instance). Faster and better development are both goals even if writing and maintaining proofs remains real engineering work.\n\nThis first release includes memory-safety examples derived from Ziglings, buffer updates, copy loops, helper calls, and a captured specialization of Zig’s `std.mem.replaceScalar`. It supports restricted AIR fragments, with explicit target and layout constraints. It's not yet complete!\n\nIt's important to note that Lean checks the translated model under its stated assumptions: The capture pipeline, Zig frontend, and native backend remain trust boundaries. That means these proofs do not establish correctness of the final native binary or the whole compiler. Read more in the [architecture guide](https://github.com/okcontract/zean/blob/main/docs/architecture.md).\n\nTo try the included Ziglings demonstration, install Zig **0.17.1** and **elan**, then run:\n\n```\ngit clone https://github.com/okcontract/zean.git\ncd zean\nelan toolchain install leanprover/lean4:v4.35.0\nzig build ziglings\n```\n\nZean is released under Apache 2.0 at [**GitHub**](https://github.com/okcontract/zean). If you have a small Zig application whose safety currently depends on a comment you would like to turn into a proof, we’d like to see it.", "url": "https://wpnews.pro/news/zean-write-zig-prove-it-in-lean", "canonical_source": "https://okcontract.com/blog/introducing-zean-write-zig-and-prove-it-in-lean/", "published_at": "2026-11-16 16:09:56+00:00", "updated_at": "2026-11-16 16:25:37.199373+00:00", "lang": "en", "topics": ["developer-tools", "ai-tools", "ai-agents"], "entities": ["Zean", "Zig", "Lean", "Okcontract", "Microsoft", "FORGE Lab", "Rust"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/zean-write-zig-prove-it-in-lean", "markdown": "https://wpnews.pro/news/zean-write-zig-prove-it-in-lean.md", "text": "https://wpnews.pro/news/zean-write-zig-prove-it-in-lean.txt", "jsonld": "https://wpnews.pro/news/zean-write-zig-prove-it-in-lean.jsonld"}}