{"slug": "show-hn-formal-agentic-property-checker-for-code-backed-by-lean-4", "title": "Show HN: Formal – agentic property checker for code, backed by Lean 4", "summary": "A Show HN project called Formal released an agentic property checker for code that is backed by the Lean 4 proof assistant and does not call a language model itself, requiring no API key. Formal checks Lean specs in which each property is stated as def <name> : Prop, verifies that functions match their source code through a code: doc-comment line and an inputs list, and searches for counterexamples with Plausible, returning refuted properties with the failing variable values. It also explores every reachable state of a state machine up to a given depth, reporting violations with a shortest trace, and supplies a Formal.Temporal library with always, eventually, leadsTo and fair operators for liveness properties.", "body_md": "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.\n\nFig. 01\n\n01 / Specs\n\nA Lean spec\n\nTwo 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.\n\nA 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.\n\n```\nnamespace Ring\n\n/-- code: ring.py wrap -/\ndef wrap (input : Int × Int) : Int := input.1 % input.2\n\ndef wrap.inputs : List (Int × Int) :=\n  [(7, 3), (-7, 3), (0, 3), (7, -3), (-7, -3)]\n\ndef in_range : Prop :=\n  ∀ index size : Int, 0 < size → 0 ≤ wrap (index, size) ∧ wrap (index, size) < size\n\nend Ring\nbash\n$ formal check Ring.lean --answers answers.json\n✓ Ring.in_range  verified\n✓ Ring.wrap  matches ring.py wrap\n1 of 1 properties hold, 1 of 1 functions match the code\n```\n\nFig. 02\n\n02 / Counterexamples\n\nA false property comes back refuted\n\nWhen 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.\n\n```\nnamespace Lists\n\ndef reverse_keeps : Prop :=\n  ∀ l : List Nat, l.reverse = l\n\nend Lists\n✗ Lists.reverse_keeps  refuted: l := [0, 1]\n  [1, 0] = [0, 1] does not hold\n0 of 1 properties hold, 1 refuted\n```\n\nFig. 03\n\n03 / State machines\n\nEvery reachable state, up to a depth\n\nA spec can describe a state machine. It defines init : State, step : State → Action → State and at least one invariant in its namespace.\n\nWhen 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.\n\n``` python\nimport Formal.Temporal\n\nnamespace DieHard\nstructure State where\n  big : Nat\n  small : Nat\ninductive Action where\n  | fillSmall\n  | fillBig\n  | emptySmall\n  | emptyBig\n  | smallToBig\n  | bigToSmall\ndef init : State := ⟨0, 0⟩\ndef step (s : State) : Action → State\n  | .fillSmall => { s with small := 3 }\n  | .fillBig => { s with big := 5 }\n  | .emptySmall => { s with small := 0 }\n  | .emptyBig => { s with big := 0 }\n  | .smallToBig => let b := if s.big + s.small < 5 then s.big + s.small else 5; ⟨b, s.small - (b - s.big)⟩\n  | .bigToSmall => let m := if s.big + s.small < 3 then s.big + s.small else 3; ⟨s.big - (m - s.small), m⟩\ndef typeOK (s : State) : Prop := s.small ≤ 3 ∧ s.big ≤ 5\ndef solved : Prop := Temporal.reachable init step (fun s => s.big = 4)\n\nend DieHard\n✓ DieHard.solved  verified by formal's own tactics\n  reached by [DieHard.Action.fillBig, DieHard.Action.bigToSmall, DieHard.Action.emptySmall, DieHard.Action.bigToSmall, DieHard.Action.fillBig, DieHard.Action.bigToSmall]\n✓ DieHard.typeOK.always  verified by formal's own tactics\n  held in every state up to depth 10 (16 states)\n2 of 2 properties hold\n✓ BuggyCircularBuffer.in_range.always  verified by formal's own tactics\n✗ BuggyCircularBuffer.safe.always  refuted: actions := [BuggyCircularBuffer.Action.put 0, BuggyCircularBuffer.Action.put 0, BuggyCircularBuffer.Action.put 0]\n  3 ≤ 2 does not hold\n1 of 2 properties hold, 1 refuted\n```\n\nFig. 04\n\n04 / Temporal\n\nSomething good eventually happens\n\nLiveness 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:\n\nTemporal.always init step p: p holds in every state the machine reaches.\n\nTemporal.eventually init step assumed p: every run that satisfies assumed reaches a state where p holds.\n\nTemporal.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.\n\nTemporal.fair a: the run takes the action a again and again.\n\nOn the states the bounded walk reaches, a violation is a loop that takes every fair action and never reaches q.\n\n``` python\nimport Formal.Temporal\n\nnamespace Live\n\ninductive Action where\n  | inc\n  | reset\n\nstructure State where\n  count : Nat\n\ndef init : State := ⟨0⟩\n\ndef step (s : State) : Action → State\n  | .inc => ⟨min (s.count + 1) 2⟩\n  | .reset => ⟨0⟩\n\ndef a_fair_reset_empties_it : Prop :=\n  Temporal.leadsTo init step (Temporal.fair Action.reset)\n    (fun s => 0 < s.count) (fun s => s.count = 0)\n\ndef unfair : Prop :=\n  Temporal.leadsTo init step (fun _ => True)\n    (fun s => 0 < s.count) (fun s => s.count = 0)\n\nend Live\ntheorem Live.a_fair_reset_empties_it.holds : Live.a_fair_reset_empties_it :=\n  Temporal.leadsTo_of_fair_step Live.Action.reset (fun _ h => h)\n    ⟨_, Temporal.fair_constant Live.Action.reset⟩ Temporal.always_true\n    (fun _ _ _ hp _ => by cases ‹Live.Action› <;> simp_all [Live.step] <;> omega)\n    (fun _ _ _ _ => rfl)\n✓ Live.a_fair_reset_empties_it  verified\n  held in every state up to depth 10 (3 states)\n✗ Live.unfair  refuted: actions := [Live.Action.inc, Live.Action.inc], then forever := [Live.Action.inc]\n  on this fair run, what the property promises never comes\n1 of 2 properties hold, 1 refuted\n```\n\nFig. 05\n\n05 / Induction\n\nInvariants without a proof\n\nformal 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.\n\nAn 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 ✓.\n\ntests/fixtures/machines/ holds protocols from other tools, with what formal finds on each one.\n\nMachine\n\nProved\n\nRefuted\n\nTwoPhaseCommit\n\nagreement, committed_all_yes, aborted_flag\n\nRicartAgrawala\n\nmutex, one_way, all_replied\n\nRicartAgrawalaEarly\n\none_way\n\nmutex, all_replied\n\nDecentralizedLock\n\nmutex\n\nDieHard\n\nsolved, typeOK\n\nBuggyCircularBuffer\n\nin_range\n\nsafe\n\nFig. 06\n\n06 / Verified\n\nWhat “verified” means\n\nLean'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.\n\nIt 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.\n\nIt refuses proofs that can run code. Code that runs during a check could print a fake verdict.\n\nIt audits the axiomatic dependencies of every declaration. A declared axiom, a sorry or native_decide fails the check at the line of that declaration.\n\nIt checks every declaration with the kernel again.\n\nIt 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.\n\n07 / In CI\n\nThe GitHub Action\n\nThe 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.\n\n```\n- uses: yamafaktory/formal@<tag>\n  with:\n    specs: spec/*.lean\n```\n\n08 / Agents\n\nTell your agent about it\n\nformal 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:\n\n```\ngh skill install yamafaktory/formal formal --agent <agent>\n```\n\nFor an agent that does not read skills, formal guide prints the same skill.", "url": "https://wpnews.pro/news/show-hn-formal-agentic-property-checker-for-code-backed-by-lean-4", "canonical_source": "https://yamafaktory.github.io/formal/", "published_at": "2026-12-04 15:01:21+00:00", "updated_at": "2026-12-04 15:17:41.723582+00:00", "lang": "en", "topics": ["ai-agents", "developer-tools", "ai-tools"], "entities": ["Formal", "Lean 4", "Plausible", "Formal.Temporal"], "also_reported_by": [], "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.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/show-hn-formal-agentic-property-checker-for-code-backed-by-lean-4", "markdown": "https://wpnews.pro/news/show-hn-formal-agentic-property-checker-for-code-backed-by-lean-4.md", "text": "https://wpnews.pro/news/show-hn-formal-agentic-property-checker-for-code-backed-by-lean-4.txt", "jsonld": "https://wpnews.pro/news/show-hn-formal-agentic-property-checker-for-code-backed-by-lean-4.jsonld"}}