# Show HN: Formal – agentic property checker for code, backed by Lean 4

> Source: <https://yamafaktory.github.io/formal/>
> Published: 2026-12-04 15:01:21+00:00

**Key takeaway:** Formal verifies Lean 4 properties against source code and bounded state-machine exploration without calling a model, returning refuted properties with counterexample values and shortest violation traces.

formal does not call a model. It needs no API key and has no opinion about which model you use. The agent that already reads your code writes the Lean. formal does everything that needs no model.

Fig. 01

01 / Specs

A Lean spec

Two proofs can model the same function differently. A Lean spec holds one model for all of them. It defines the model inside one namespace, and states each property as def <name> : Prop.

A Lean spec can also hold the code to its model. A function names the code it describes in a code: line of its doc comment, and def <name>.inputs lists the inputs.

```
namespace Ring

/-- code: ring.py wrap -/
def wrap (input : Int × Int) : Int := input.1 % input.2

def wrap.inputs : List (Int × Int) :=
  [(7, 3), (-7, 3), (0, 3), (7, -3), (-7, -3)]

def in_range : Prop :=
  ∀ index size : Int, 0 < size → 0 ≤ wrap (index, size) ∧ wrap (index, size) < size

end Ring
bash
$ formal check Ring.lean --answers answers.json
✓ Ring.in_range  verified
✓ Ring.wrap  matches ring.py wrap
1 of 1 properties hold, 1 of 1 functions match the code
```

Fig. 02

02 / Counterexamples

A false property comes back refuted

When the session opens, formal also searches each property for a counterexample with Plausible. A false property comes back under refuted, with the value of each variable and what does not hold.

```
namespace Lists

def reverse_keeps : Prop :=
  ∀ l : List Nat, l.reverse = l

end Lists
✗ Lists.reverse_keeps  refuted: l := [0, 1]
  [1, 0] = [0, 1] does not hold
0 of 1 properties hold, 1 refuted
```

Fig. 03

03 / State machines

Every reachable state, up to a depth

A spec can describe a state machine. It defines init : State, step : State → Action → State and at least one invariant in its namespace.

When the actions are known, formal also explores every reachable state up to a depth. formal visits each state once, so a violation comes back under refuted with a shortest trace.

``` python
import Formal.Temporal

namespace DieHard
structure State where
  big : Nat
  small : Nat
inductive Action where
  | fillSmall
  | fillBig
  | emptySmall
  | emptyBig
  | smallToBig
  | bigToSmall
def init : State := ⟨0, 0⟩
def step (s : State) : Action → State
  | .fillSmall => { s with small := 3 }
  | .fillBig => { s with big := 5 }
  | .emptySmall => { s with small := 0 }
  | .emptyBig => { s with big := 0 }
  | .smallToBig => let b := if s.big + s.small < 5 then s.big + s.small else 5; ⟨b, s.small - (b - s.big)⟩
  | .bigToSmall => let m := if s.big + s.small < 3 then s.big + s.small else 3; ⟨s.big - (m - s.small), m⟩
def typeOK (s : State) : Prop := s.small ≤ 3 ∧ s.big ≤ 5
def solved : Prop := Temporal.reachable init step (fun s => s.big = 4)

end DieHard
✓ DieHard.solved  verified by formal's own tactics
  reached by [DieHard.Action.fillBig, DieHard.Action.bigToSmall, DieHard.Action.emptySmall, DieHard.Action.bigToSmall, DieHard.Action.fillBig, DieHard.Action.bigToSmall]
✓ DieHard.typeOK.always  verified by formal's own tactics
  held in every state up to depth 10 (16 states)
2 of 2 properties hold
✓ BuggyCircularBuffer.in_range.always  verified by formal's own tactics
✗ BuggyCircularBuffer.safe.always  refuted: actions := [BuggyCircularBuffer.Action.put 0, BuggyCircularBuffer.Action.put 0, BuggyCircularBuffer.Action.put 0]
  3 ≤ 2 does not hold
1 of 2 properties hold, 1 refuted
```

Fig. 04

04 / Temporal

Something good eventually happens

Liveness says that something good eventually happens. import Formal.Temporal gives a spec formal's temporal library. It defines the operators over a machine's init and step:

Temporal.always init step p: p holds in every state the machine reaches.

Temporal.eventually init step assumed p: every run that satisfies assumed reaches a state where p holds.

Temporal.leadsTo init step assumed p q: in every run that satisfies assumed, each state where p holds is followed by a state where q holds, or is one.

Temporal.fair a: the run takes the action a again and again.

On the states the bounded walk reaches, a violation is a loop that takes every fair action and never reaches q.

``` python
import Formal.Temporal

namespace Live

inductive Action where
  | inc
  | reset

structure State where
  count : Nat

def init : State := ⟨0⟩

def step (s : State) : Action → State
  | .inc => ⟨min (s.count + 1) 2⟩
  | .reset => ⟨0⟩

def a_fair_reset_empties_it : Prop :=
  Temporal.leadsTo init step (Temporal.fair Action.reset)
    (fun s => 0 < s.count) (fun s => s.count = 0)

def unfair : Prop :=
  Temporal.leadsTo init step (fun _ => True)
    (fun s => 0 < s.count) (fun s => s.count = 0)

end Live
theorem Live.a_fair_reset_empties_it.holds : Live.a_fair_reset_empties_it :=
  Temporal.leadsTo_of_fair_step Live.Action.reset (fun _ h => h)
    ⟨_, Temporal.fair_constant Live.Action.reset⟩ Temporal.always_true
    (fun _ _ _ hp _ => by cases ‹Live.Action› <;> simp_all [Live.step] <;> omega)
    (fun _ _ _ _ => rfl)
✓ Live.a_fair_reset_empties_it  verified
  held in every state up to depth 10 (3 states)
✗ Live.unfair  refuted: actions := [Live.Action.inc, Live.Action.inc], then forever := [Live.Action.inc]
  on this fair run, what the property promises never comes
1 of 2 properties hold, 1 refuted
```

Fig. 05

05 / Induction

Invariants without a proof

formal check proves the invariants of a machine on its own. It inducts on the list of actions, with all the invariants together, by case on the action. There is no solver to install, and Lean's kernel checks each proof.

An invariant that is not inductive needs others beside it. formal learns them. Lean then proves them together with the invariant, so a wrong guess can cost time, and never a wrong ✓.

tests/fixtures/machines/ holds protocols from other tools, with what formal finds on each one.

Machine

Proved

Refuted

TwoPhaseCommit

agreement, committed_all_yes, aborted_flag

RicartAgrawala

mutex, one_way, all_replied

RicartAgrawalaEarly

one_way

mutex, all_replied

DecentralizedLock

mutex

DieHard

solved, typeOK

BuggyCircularBuffer

in_range

safe

Fig. 06

06 / Verified

What “verified” means

Lean's kernel accepted a proof of a theorem derived from your code, relative to the axioms propext, Classical.choice and Quot.sound. That is strong evidence against logical errors, which models make and Lean catches.

It is not a proof that your source code is correct: the theorem concerns a Lean model of the code, and nothing certifies that the model is faithful.

It refuses proofs that can run code. Code that runs during a check could print a fake verdict.

It audits the axiomatic dependencies of every declaration. A declared axiom, a sorry or native_decide fails the check at the line of that declaration.

It checks every declaration with the kernel again.

It checks every passing proof again, in a Lean that runs none of its code. Code that runs in the first Lean can print any verdict, but it cannot make the second Lean accept a false proof.

07 / In CI

The GitHub Action

The repository is also a GitHub Action that runs formal check on each change. It fails when a property is refuted, when a proof no longer holds, or when the code no longer matches its spec.

```
- uses: yamafaktory/formal@<tag>
  with:
    specs: spec/*.lean
```

08 / Agents

Tell your agent about it

formal ships an agent skill: skills/formal/SKILL.md, in the open format that many agents read. Install it in your project with the GitHub CLI:

```
gh skill install yamafaktory/formal formal --agent <agent>
```

For an agent that does not read skills, formal guide prints the same skill.
