{"slug": "show-hn-zean-transpiles-zig-to-lean", "title": "Show HN: Zean Transpiles Zig to Lean", "summary": "OKContract released Zean, an open-source tool that captures Zig's analyzed intermediate representation (AIR) and emits Lean program definitions so developers can write specifications and proofs about Zig functions, with memory-safety obligations generated via Lean 4.35's Std.WP and vcgen. The initial release, built with AI assistance mostly using GPT-6 Astra over two months and extracted from formal verification work on the Zig-written Solidity compiler oksolc, requires Zig 0.17.1 and elan, and its model is incomplete with translation restricted to supported operations. Tutorial examples include Ziglings exercises 038 and 054 plus an atomic-publication dispatch-table proof showing a reader can use either entry once its acquire load sees the writer's release store.", "body_md": "**Translate Zig functions into executable Lean models and prove their behavior.**\n\nZean captures Zig's analyzed intermediate representation (AIR) and emits Lean program definitions. You write specifications and proofs in separate files, then regenerate the model when the Zig code changes. Our model is currently incomplete and translation is restricted to supported operations.\n\nFor memory safety, a reusable verification library generates proof obligations\nfrom the translated program using Lean 4.35's `Std.WP` and `vcgen`. You supply\ncontracts and loop invariants, then discharge the remaining obligations in Lean.\n\nNote: This initial release of this project was written with AI assistance mostly\nusing GPT-6 Astra over the course of two months. It is extracted from our formal\nverification work for [oksolc](https://github.com/okcontract/oksolc), a Solidity\ncompiler written in Zig.\n\nWith **Zig 0.17.1** and **elan** installed, run from the repository root:\n\n```\nelan toolchain install leanprover/lean4:v4.35.0\nzig build ziglings\n```\n\nThe [tutorial](https://github.com/okcontract/zean/blob/main/examples/ziglings/README.md) follows two familiar Ziglings lessons:\n\n| Exercise | Checked property of the derived runtime kernel | \n|---|---|\n| 038: initialize both characters | Reading either gold slot is safe after both writes, without assuming initial contents | \n| 054: give a many-pointer a length | Reads stay within the caller's initialized byte region and preserve memory | \n\nYou supply the caller contracts; reusable verification rules discharge the memory obligations. Broken variants demonstrate an uninitialized read and an invented slice extent. The command checks the proofs, requires false claims to fail, and runs the two officially corrected upstream exercises on Zig 0.17.1.\n\nThe formal proofs cover the captured runtime kernels described in the tutorial. The original exercises, including their printing code, receive separate native compatibility checks. Retained captures let you run the demo without building an instrumented compiler.\n\nWhen can one thread safely read a dispatch table that another thread is filling\nin? The writer sets a flag with a release store after it fills both entries.\nThe Lean proof shows that a reader can use either entry once its acquire load\nsees that store. Run `zig build atomic-publication` to check the proof and see\nwhat goes wrong if the writer sets the flag too soon.\n[Dispatch-table example](https://github.com/okcontract/zean/blob/main/examples/atomic_publication/README.md)\n\nContinue with the [buffer update](https://github.com/okcontract/zean/blob/main/examples/buffer/README.md), or follow the\n[copy-loop tutorial](https://github.com/okcontract/zean/blob/main/examples/copy/README.md) for initialization, framing and\nhelper contracts. The [verification guide](https://github.com/okcontract/zean/blob/main/docs/verification.md) explains how\nto apply the same machinery to another supported function.\nThe [manifest-based client](https://github.com/okcontract/zean/blob/main/examples/client/README.md) captures and verifies\nyour own function without adding a new tutorial command or changing the verifier.\nIts [caller/helper variant](https://github.com/okcontract/zean/blob/main/examples/client/calls/README.md) uses the explicit\ncall-stack interpreter and verifies a caller from a separately checked helper\ncontract, with readable generated function tables.\nThe [byte-scanning client](https://github.com/okcontract/zean/blob/main/examples/client/scan/README.md) adds a loop invariant,\nearly returns, and a helper call inside the loop.\nFor runtime helper calls, the [call-chain example](https://github.com/okcontract/zean/blob/main/examples/calls/README.md)\nshows a caller reusing checked callee contracts across actual runtime calls.\nThe [standard-library example](https://github.com/okcontract/zean/blob/main/examples/stdlib_replace/README.md) goes beyond\nthe tutorials: it verifies an unchanged `std.mem.replaceScalar(u32)` specialization,\nwith a separately checked AIR-to-model correspondence certificate.\nThe [compatibility probes](https://github.com/okcontract/zean/blob/main/examples/compatibility/README.md) also show examples\nthat still reject, so these results should not be read as general Zig support.\n\nStart with a counter that advances without overflowing:\n\n```\npub export fn advance(value: u8) u8 {\n    return if (value < 255) value + 1 else 255;\n}\n```\n\nThe [handwritten proof](https://github.com/okcontract/zean/blob/main/lean/Compiler/Tutorial/Counter/V1/Proofs.lean) establishes\nthat, for every valid byte input `x`, the generated model returns\n`min(x + 1, 255)` successfully. A second theorem proves that the result never\ndecreases and never exceeds 255.\n\n```\nzig build counter-run\nzig build counter\n```\n\n`counter-run` runs the working Zig examples. `counter` checks their native\nresults, generated-source identities, Lean proofs, and expected proof failures.\nThe example includes generated models from real native AIR captures, so these\ncommands need no instrumented compiler.\n\nThe [counter tutorial](https://github.com/okcontract/zean/blob/main/examples/counter/README.md) follows the complete lifecycle:\n\n1. Write the byte counter and prove its behavior.\n2. Change the step to two and see an incomplete edit overflow at input 254.\n3. Fix the guard, regenerate the model, and update the proof.\n4. Apply the same reasoning to a `u32` counter, proving the contract for all\n2³² inputs without enumerating them.\n\nThe [buffer tutorial](https://github.com/okcontract/zean/blob/main/examples/buffer/README.md) takes the next step: prove that\na Zig function updates exactly one element of a writable buffer and preserves\nsurrounding memory. Its Lean proofs cover arbitrary valid caller heaps, buffer\ncontents and 64-bit indices under explicit storage contracts.\n\n```\nzig build buffer-run\nzig build buffer\n```\n\nAn off-by-one edit demonstrates both an invalid access and a write outside a smaller logical buffer that still lies inside allocated memory. A repaired version restores the contract. Like the counter, it includes real captured models, separate handwritten proofs, and automated fresh capture and proof replay. These are memory-safety proofs for the supported models; caller storage assumptions and source/compiler correspondence remain explicit.\n\nThe [copy example](https://github.com/okcontract/zean/blob/main/examples/copy/README.md) uses the same verifier for a loop\nover a readonly source and an initially uninitialized destination. Its contract\nproves memory safety at every execution prefix; successful completion copies\nthe contents and preserves the source and surrounding memory. Run `zig build copy`\nto check the proofs, native controls and three intentionally false proof clients.\n\n```\nZig source → captured AIR → generated Lean model → handwritten Lean proofs\n```\n\nLean checks proofs about execution in the selected model, including its explicit failure outcomes. The counter's successful-return theorem rules out arithmetic faults for every input in its stated domain.\n\nSource admission, the Zig frontend, capture instrumentation, and the native\nbackend remain trusted boundaries. Model proofs and matching native object\nbytes do not establish whole-compiler correctness. See\n[architecture and trust](https://github.com/okcontract/zean/blob/main/docs/architecture.md) for the exact boundaries.\n\nTo generate a new model, first build an instrumented Zig compiler using the\n[toolchain guide](https://github.com/okcontract/zean/blob/main/docs/toolchain.md). An ordinary Zig installation can run the\nexamples and tests; producing AIR records requires the instrumented compiler.\nNative capture tooling supports macOS and Linux.\n\nFor example, capture and translate the original counter:\n\n```\nzig build capture -- \\\n  --toolchain /path/to/instrumented-build/report.json \\\n  --source examples/counter/v1/counter.zig \\\n  --root counter.advance \\\n  --output /tmp/counter-capture\n\nzig build run -- translate \\\n  --capture /tmp/counter-capture/report.json \\\n  --root counter.advance \\\n  --profile scalar \\\n  --namespace MyCounter.Generated \\\n  --output /tmp/counter-model\n```\n\nThe output bundle contains:\n\n| File | Purpose | \n|---|---|\n| `Generated.lean` | Executable program data; regenerated by the translator | \n| `manifest.json` | Source, AIR, capture, producer and model identities, plus verification scope | \n\nTranslation validates every selected root before publishing the bundle. Use a\nnew output directory for each run, and keep captures and compiler builds in a\nseparate working directory. Repeat `--root` to select more functions; their\nfinal name components must be unique.\n\nFor `scalar`, `memory` and `loops`, add `--certify /tmp/certificate-evidence` to check\nAIR-to-model correspondence in Lean before publication. The generated theorem\ncompares an independent Lean interpretation of the captured data with the emitted\nprogram. This checks the lowering step; source capture, JSON lifting and native\ncode generation remain trusted. See the [admission specification](https://github.com/okcontract/zean/blob/main/docs/admission.md).\n\nBuild the reusable model library and check the generated module:\n\n```\nzig build lean-framework\nelan run leanprover/lean4:v4.35.0 lake env lean /tmp/counter-model/Generated.lean\n```\n\nAdd your specification and proofs in separate Lean files. The tutorial shows how to organize those files and update them after a source change.\n\n| Translation profile | Supported operations | \n|---|---|\n| `scalar` | Selected integer/Boolean operations, casts and structured control | \n| `memory` | Typed pointers, allocation, loads/stores and retained slice guards | \n| `loops` | Memory operations with resumable loops | \n| `aggregate` | Complete aggregate declarations, fields and aggregate operations | \n| `word-bytes` | Restricted initialized `u32` byte views with checked provenance | \n| `word-byte-loops` | Word-byte access with resumable loops | \n| `calls` | Finite memory programs with an admitted direct-call closure | \n| `call-loops` | Looping callers with finite, separately verified callees | \n| `nested-calls` | Acyclic calls to looping callees, with safety over all finite observation budgets | \n| `recursive-calls` | Direct recursion over a checked call table, including mutual calls | \n\nThese profiles describe admitted AIR, rather than general Zig language support.\nStandalone translation currently admits ReleaseFast AIR. Memory profiles\nhave additional target and contract restrictions; see the\n[supported routes](https://github.com/okcontract/zean/blob/main/docs/architecture.md#supported-routes).\n\nThe [native tooling guide](https://github.com/okcontract/zean/blob/main/docs/native-tooling.md) describes capture, source-owner\nvalidation, certified translation, and proof replay. The repository includes\nthe inputs needed for its portable examples and regression checks. New captures\nrequire an instrumented build of the public, pinned Zig release.\n\nVersions are pinned in [.zigversion](https://github.com/okcontract/zean/blob/main/.zigversion) and\n[lean-toolchain](https://github.com/okcontract/zean/blob/main/lean-toolchain). The build uses elan by default; select an\nexisting matching Lake executable with `-Dlake=/path/to/lake`.\n\n| Command | What it does | \n|---|---|\n| `zig build` | Build and install the native CLI | \n| `zig build test` | Run native translation, admission, process, and tutorial regressions | \n| `zig build lean-core` | Check the core verification API and its dependencies | \n| `zig build lean-framework` | Check reusable semantics, optional verification domains and correspondence | \n| `zig build lean-examples` | Check tutorial and component proof clients | \n| `zig build lean-regressions` | Check retained captures, standard-library cases, controls and baselines | \n| `zig build lean` | Check the full Lean library and admission controls with two workers | \n| `zig build fmt-check` | Check Zig formatting | \n| `zig build check -Doptimize=ReleaseFast` | Run portable checks, including compilation of newly emitted Lean | \n| `zig build counter` | Check the complete counter lifecycle | \n| `zig build buffer` | Check buffer safety, update behavior and off-by-one proof failures | \n| `zig build copy` | Check copy-loop safety, helper contracts and initialization/bounds/aliasing controls | \n| `zig build calls` | Check a captured call chain through reusable callee contracts | \n| `zig build stdlib-replace` | Check an unchanged standard-library loop and its initialization/permission controls | \n| `zig build wp-proofs` | Check generic verification rules against interpreter and fault controls | \n| `zig build benchmark -- --output /tmp/proof-cost` | Measure fresh proof elaboration with warm library dependencies | \n\nTo recapture the tutorial and replay its proofs against fresh definitions:\n\n```\nzig build counter-regenerate -- \\\n  --toolchain /path/to/instrumented-build/report.json \\\n  --output /tmp/counter-evidence \\\n  --check\n```\n\nThis also produces an evidence archive containing source and proof inputs,\nAIR, native objects, manifests and process receipts. The\n[validation guide](https://github.com/okcontract/zean/blob/main/docs/validation.md) documents the full `release-check` gate,\nincluding fresh memory and slice audits. Portable CI runs native Debug tests\nand ReleaseFast checks. See [contributing](https://github.com/okcontract/zean/blob/main/CONTRIBUTING.md) for fixture and proof\nconventions.\n\n- [Counter tutorial](https://github.com/okcontract/zean/blob/main/examples/counter/README.md) : program, capture, proof and update.\n- [Buffer tutorial](https://github.com/okcontract/zean/blob/main/examples/buffer/README.md) : memory safety, surrounding-memory preservation and a broken update.\n- [Copy tutorial](https://github.com/okcontract/zean/blob/main/examples/copy/README.md) : loop invariants, initialization and compositional inline-helper contracts.\n- [Verification guide](https://github.com/okcontract/zean/blob/main/docs/verification.md) : contracts and automation for other supported programs.\n- [Architecture](https://github.com/okcontract/zean/blob/main/docs/architecture.md) : translation routes, models and trust boundaries.\n- [Toolchain guide](https://github.com/okcontract/zean/blob/main/docs/toolchain.md) : build the instrumented compiler and capture AIR.\n- [Native tooling](https://github.com/okcontract/zean/blob/main/docs/native-tooling.md) : capture, source ownership, translation and proof replay.\n- [Validation](https://github.com/okcontract/zean/blob/main/docs/validation.md) : regression coverage, release checks and recorded evidence.\n\nImplementation lives in [`src/`](https://github.com/okcontract/zean/blob/main/src), models and proofs in [` lean/`](https://github.com/okcontract/zean/blob/main/lean),\nand regression fixtures in [`test/`](https://github.com/okcontract/zean/blob/main/test). Generated programs are kept\nseparate from handwritten proofs.\n\nLicensed under [Apache 2.0](https://github.com/okcontract/zean/blob/main/LICENSE). See [NOTICE](https://github.com/okcontract/zean/blob/main/NOTICE) for attribution.", "url": "https://wpnews.pro/news/show-hn-zean-transpiles-zig-to-lean", "canonical_source": "https://github.com/okcontract/zean", "published_at": "2026-11-23 13:29:55+00:00", "updated_at": "2026-11-23 13:44:36.521786+00:00", "lang": "en", "topics": ["developer-tools", "ai-tools"], "entities": ["OKContract", "Zean", "Zig", "Lean", "Lean 4.35", "GPT-6 Astra", "oksolc", "elan"], "also_reported_by": [], "takeaway": "Zean translates supported Zig AIR into Lean models and uses Lean 4.35's Std.WP and vcgen to generate memory-safety proof obligations, letting developers supply contracts and loop invariants and discharge the rest in Lean.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/show-hn-zean-transpiles-zig-to-lean", "markdown": "https://wpnews.pro/news/show-hn-zean-transpiles-zig-to-lean.md", "text": "https://wpnews.pro/news/show-hn-zean-transpiles-zig-to-lean.txt", "jsonld": "https://wpnews.pro/news/show-hn-zean-transpiles-zig-to-lean.jsonld"}}