🧮 Formal: a Lean 4 checker for the properties and proofs your coding agent writes
In brief
- 🧮 Formal by yamafaktory checks properties of code that your own coding agent states and proves in Lean 4.
- 🔑 It calls no model and needs no API key.
- 🔍 It can search counterexamples, explore state machines and flag when the code no longer matches its spec.
- 📦 Install script for Linux and macOS, or build with Cargo; also a GitHub Action.
[Formal](https://yamafaktory.github.io/formal/) is a command-line tool and local server by developer yamafaktory. The coding agent you already use reads your code, picks the pure functions, writes the property as a Lean 4 theorem and a proof; Formal has Lean check it and answers verified, refuted or rejected with Lean's first error. According to the project page it calls no model, needs no API key and does not care which model you use. It can also compare the Lean model with the real code on sample inputs, search for counterexamples and explore state machines. It is not a proof that your source is correct, only that a stated model satisfies a stated property.
🛠️ Install and run. The project page gives an install script for Linux and macOS (x86_64 or arm64) that checks a SHA-256 and places the binary in `~/.local/bin`; on other platforms you build it with `cargo install --git https://github.com/yamafaktory/formal formal-cli`. `formal setup` then installs elan, the pinned Lean version and Plausible. Specs that import Mathlib need a further step that downloads several GB of prebuilt modules. `formal serve --background` starts a local server on port 1337; the agent talks to it over HTTP, or you run `formal check` on a spec file.
📐 What a spec looks like. A Lean file defines a model of a function inside a namespace, states each property as a `Prop`, and names the real code in a `code:` line of the doc comment. A list of inputs lets Formal run the real function and compare its answers with the model. In the project's example, the obvious Lean model of Python's `%` differs from the code for a negative divisor, so a proof about it would hold and say nothing about the Python file; the fix is a floor modulo. This is why the page says the agent's three judgements (what is a pure function, which property, how to formalize it) come before any Lean run, and code with side effects such as database or HTTP calls is out of scope.
🔍 Refuting and exploring. When a session opens, Formal searches each property for a counterexample and reports the values that break it. A spec can instead describe a state machine with `init`, `step` and at least one invariant; Formal then explores every reachable state to a depth, visiting each once, and returns a shortest trace for a violation. Its examples include a water-jug puzzle and a buggy circular buffer. Liveness properties use operators such as `always`, `eventually` and `leadsTo` with fairness assumptions; the page shows an unfair run refuted by a loop that never reaches the goal. Invariants that are not inductive on their own get helper invariants that Formal learns; a wrong guess costs time, never a false check mark. The repository includes protocols such as TwoPhaseCommit and Ricart-Agrawala as fixtures.
🛡️ What a result means. The page is open about limits: Lean's kernel accepting a proof is strong evidence against logical errors, relative to the standard axioms, but the theorem concerns a model, and nothing certifies the model is faithful to the code. Formal refuses proofs that can run code, fails a check on declared axioms, `sorry` or `native_decide`, re-checks declarations with the kernel and re-checks every passing proof in a second Lean that runs none of its code.
🤖 Agents and CI. The repository ships an agent skill (`gh skill install yamafaktory/formal formal --agent <agent>`; `formal guide` prints the same text) and a GitHub Action that runs `formal check` on specs and fails when a property is refuted, a proof no longer holds or the code no longer matches its spec. Price and licence are not stated on the project page we read; check the repository before relying on it.