Lynx logo

LYNX

Write proofs about Erlang/Elixir programs using Lean.

You write laws in Erlang/Elixir, which are automatically translated to Lean, and you write proofs in Lean. The laws and proofs are assembled together and verified by Lean's kernel.

This project uses technique known as mixed embedding: your Erlang/Elixir functions become Lean functions (shallow embedding), which proofs reason about directly, while processes are modeled explicitly as a transition system over their states (deep embedding).

If there are no bugs in the translation, in the model of the Erlang runtime, or in Lean's kernel, then the proofs are correct. Given the early nature of this project, you should expect bugs and limitations (for example, we do not yet implement all of Erlang's built-in functions). Please file bug reports whenever you run into them!

See the documentation for the Lynx module to get started or ask your coding agent to run mix help Lynx.

Important

A quick word on formal verification

Formal verification is not a silver bullet and it cannot prove your software is generally bug free! Instead, projects like Lynx allow you to prove that certain properties of a given program hold for all specified inputs. Proofs can be automated by coding agents, but defining the underlying properties and valid inputs is very likely to benefit from developer guidance.

See the "Getting started with proofs" section below.

Features

Examples

Elixir

Use Lynx with ExUnit to run proofs alongside ordinary tests. Save this example as test/laws/sum_test.exs. It defines a recursive sum and proves that the empty list sums to zero and that, for integer lists, summing each list and adding the results equals summing their concatenation:

defmodule SumProofsTest do
  use ExUnit.Case, async: true
  use Lynx.Case

  def sum([]), do: 0
  def sum([x | xs]), do: x + sum(xs)

  laws "sum" do
    law sum_empty,
      expects: sum([]) == 0,
      proof: ~LEAN"""
      simp [«sum_empty:ensures/0», «sum/1», Erlang.erlang.«==/2»]
      """

    law sum_append(l, r),
      requires: is_integer_list(l) and is_integer_list(r),
      expects: sum(l) + sum(r) == sum(l ++ r),
      proof: ~LEAN"""
      have sums (input : Lynx.Term)
          (valid : «is_integer_list/1» input = .ok Lynx.Term.true) :
          ∃ value, «sum/1» input = .ok (.integer value) ∧
            ∀ right total, «sum/1» right = .ok (.integer total) →
              ∃ joined, Erlang.erlang.«++/2» input right = .ok joined ∧
                «sum/1» joined = .ok (.integer (value + total)) := by
        induction input using «sum/1».induct with
        | case1 => exact ⟨0, rfl, fun right total returned => ⟨right, rfl, by simpa using returned⟩⟩
        | case2 head tail ih =>
          cases head <;> simp [«is_integer_list/1», Erlang.erlang.«is_integer/1»] at valid
          rename_i value
          obtain ⟨subtotal, returned, append⟩ := ih valid
          refine ⟨value + subtotal, by simp [«sum/1», returned], ?_⟩
          intro right total rightReturned
          obtain ⟨joined, appended, combined⟩ := append right total rightReturned
          exact ⟨.cons (.integer value) joined, by simp [Erlang.erlang.«++/2», appended],
            by simp [«sum/1», combined, Int.add_assoc]⟩
        | case3 input notNil notCons => simp_all [«is_integer_list/1»]
      have validity : «is_integer_list/1» l = .ok Lynx.Term.true ∧
          «is_integer_list/1» r = .ok Lynx.Term.true := by
        simpa [«sum_append:requires/2»] using requires
      obtain ⟨leftSum, leftReturned, append⟩ := sums l validity.1
      obtain ⟨rightSum, rightReturned, _⟩ := sums r validity.2
      obtain ⟨joined, appended, combined⟩ := append r rightSum rightReturned
      simp [«sum_append:ensures/2», leftReturned, rightReturned, appended, combined,
        Erlang.erlang.«==/2»]
      """
  end

  defp is_integer_list([h | t]), do: is_integer(h) and is_integer_list(t)
  defp is_integer_list([]), do: true
  defp is_integer_list(_), do: false
end

requires: states the input assumptions and expects: must evaluate to true under those assumptions. Each laws block registers one ExUnit test and Lynx verifies all its laws together. The ~LEAN sigils contain the proofs checked by Lean. Laws also remain callable from ordinary tests, which can be used for fuzzing and validation.

Run the example or all law groups with:

mix test test/laws/sum_test.exs
mix test --only laws

Erlang

Lynx can also verify laws written in Erlang.

Each -law attribute names ordinary predicate functions. A separate binary -proof attribute must immediately follow its law:

-module(sum_proofs).
-export([sum/1, sum_empty_ensures/0,
         sum_append_requires/2, sum_append_ensures/2]).

-law #{name => {sum_empty, []}, ensures => sum_empty_ensures}.
-proof ~"""
(SAME PROOF AS ABOVE)
""".

-law #{name => {sum_append, [l, r]},
       requires => sum_append_requires,
       ensures => sum_append_ensures}.
-proof ~"""
(SAME PROOF AS ABOVE)
""".

sum([]) -> 0;
sum([X | Xs]) -> X + sum(Xs).

sum_empty_ensures() -> sum([]) == 0.
sum_append_requires(L, R) -> is_integer_list(L) andalso is_integer_list(R).
sum_append_ensures(L, R) -> sum(L) + sum(R) == sum(L ++ R).

is_integer_list([H | T]) -> is_integer(H) andalso is_integer_list(T);
is_integer_list([]) -> true;
is_integer_list(_) -> false.

The proof placeholders refer to the proofs above, with the generated Elixir predicate names replaced by their Erlang equivalents.

The argument names in name become Lean theorem parameters. The predicate helpers receive those arguments in order and remain callable from Erlang. ensures is required, while requires may be omitted for an unconditional law. Then call Lynx.Laws.verify([:sum_proofs]) to validate the proofs.

Installation

Install Elixir 1.18+ and Elan, Lean's toolchain manager, with lake available on PATH.

Now add Lynx to your application's mix.exs dependencies:

defp deps do
  [
    {:lynx, "~> 0.2", only: :test}
  ]
end

Then add a lean-toolchain file to your project's root with the same Lean version as Lynx's, so Elan builds and runs Lynx with it:

leanprover/lean4:v4.36.0

If you are using Mix.install/2, Mix builds dependencies outside the current working directory, so you must explicitly set the Elan toolchain:

Mix.install([{:lynx, "~> 0.2"}],
  system_env: [{"ELAN_TOOLCHAIN", String.trim(File.read!("lean-toolchain"))}]
)

For agentic usage, you can ask your agent to run mix help Lynx and get all instructions to get started.

Getting started with proofs

A law states a property, expects:, that must hold for every input satisfying its assumptions, requires:. Several kinds of properties are straightforward to state and often catch real bugs. Here are some examples:

Note that a property does not necessarily imply the absence of bugs. For example, proving that a JSON encoder roundtrips for some terms, such as JSON.decode(JSON.encode!(term)) == {:ok, term}, does not prove that it emits JSON according to its specification!

Similarly, the algebraic law written above for sum also holds for length, so a sum implementation that simply calls length would be accepted, even though it is incorrect!

It is important to state each law with care, as it states both the required inputs and expected outcomes. Assumptions that are too strong make a law hold vacuously, while missing assumptions make it false, and therefore unprovable, so review requires: as carefully as expects:.

Finally, because Lynx translates your Erlang/Elixir program to Lean, it relies on whole program analysis. It supports both sequential and concurrent features of the Erlang runtime, such as the process dictionary and spawning processes, but it cannot prove anything that involves external systems, running either over the network (sockets) or I/O (ports).

Using it with coding agents

The growing interest in formally verifying our programs is largely driven by AI and coding agents. Lynx embraces this trend, and we recommend the following approach:

In other words, humans are responsible for specifying what correctness means, while coding agents can take care of proving it.

Disclaimer

The Elixir code, the translation layer, and the overall design of the Erlang runtime in Lean were done with full human design and review. Proofs, which are validated by Lean's kernel, and the deserialization layer in Lean were produced by coding agents with limited review.

Contributing

The project requires Elixir 1.18+ and Lean 4.36.0+, which includes Lake. With those installed, fetch dependencies and compile the project with:

mix deps.get
mix compile

Run tests with:

mix test          # Elixir tests
mix test.lean     # Lean tests only
mix test.all      # Elixir + Lean

Before committing:

mix precommit

The Lean source code can be found in the Lean directory. You can run lake commands from that directory whenever working with Lean directly.