Lynx.Case (Lynx v0.2.0)

Copy Markdown View Source

Define callable laws alongside ExUnit tests.

Add use Lynx.Case after use ExUnit.Case, then group laws to verify together:

use ExUnit.Case, async: true
use Lynx.Case

laws "identity" do
  law identity(value), requires: value, expects: value,
    proof: ~LEAN"exact requires"
end

Each group registers one ExUnit test of type :laws. Run groups with mix test --only laws.

Set LYNX_DEBUG=lean to print each translated module's reported verification time in milliseconds, status, and whether it was cached, as well as the elaboration time of any law. The translated .lean files are also written by module path to tmp/<test module>/<group>/ for inspection. Set LYNX_DEBUG=olean to also write the compiled .olean files of every verified module next to its source, so Lean tools such as profilers can load the translated modules by adding the directory to LEAN_PATH.

Set LYNX_CACHE=0 to disable the verification cache.

Lean's warnings and information messages are printed with their locations whenever a group verifies, including from cached results.

Set LYNX_EXPORT=dir to write each group's elaborated declarations to dir/<test module>/<group>.ndjson for independent kernel checks, as described in Lynx. Exporting bypasses the verification cache.

You can also set Lean's own environment variables and they are forwarded to Lean accordingly. For example, lean's elaboration runs in parallel and you can control the concurrency by setting LEAN_NUM_THREADS=n. Note law groups may verify concurrently (according to the ExUnit async configuration), each in its own process with its own threads.

Summary

Functions

Leaves a function untranslated in this laws group.

Declares a function that calls by name may reach in this laws group.

Defines a callable law and includes it in the current laws group.

Registers a group of laws as one ExUnit test.

Loads the Lean modules in a directory after the translated definitions of this laws group.

Loads the Lean modules in a directory before the translated definitions of this laws group.

Attaches a Lean termination suffix to a translated function for this laws group.

Functions

cutoff(target)

(macro)

Leaves a function untranslated in this laws group.

cutoff &Float.invalid_precision_message/1

The function's body becomes Lynx.Except.cutoff, which raises Lynx.Exception.exhausted: not a return value or an Erlang exception, and no handler catches it. A successful-return proof must establish that the cutoff is never reached. This supports proofs whose unreachable error paths depend on libraries outside the model but it makes no claim about executing those paths. The function is embedded as a pure function (see the embedding levels in Lynx).

Functions built into Lean cannot be cut off.

dynamic(target)

(macro)

Declares a function that calls by name may reach in this laws group.

dynamic &:erlang.apply/2

Calls whose module or function is computed, such as apply(mod, fun, args) or mod.fun(x), and computed references, such as &mod.fun/1, are resolved when the program runs. Lean cannot load code at that point, so they reach only the declared functions and those the program captures by name, such as &Module.function/arity. Calls to other functions raise Lynx.Exception.exhausted, like a cutoff, so a successful-return proof must establish that they are not reached. Declared functions are translated with the program.

law(call, opts)

(macro)

Defines a callable law and includes it in the current laws group.

The must be explicitly declared in a laws/2 block.

Options

  • :requires (optional) — the precondition expression. Defaults to true.
  • :expects — the expression to prove.
  • :reaches — instead of :expects, an expression over concurrent code that some run of Lynx's model makes true (Lynx.reachable). See "Existential quantification of concurrent programs" in Lynx.
  • :proof (optional) — a Lynx.Lean value. Defaults to rfl.

Executable definition

Besides defining a law, law/2 also defines a public function that you can invoke passing Elixir values. Elixir will then validate said values against the given :requires, the given :expects, and return true when both predicates succeed, otherwise it will raise. This is useful to provide counter examples.

laws(description, opts \\ [], list)

(macro)

Registers a group of laws as one ExUnit test.

The test translates the laws and their callees, verifies their proofs, and fails if they do not pass.

Each group has the :laws test type and tag. Run only law groups with mix test --only laws.

Options

  • :options (optional) — a keyword list of Lean options, such as [maxHeartbeats: 1_000_000, maxRecDepth: 10_000], set at the top of every translated module of the group, including its laws. Names are passed to Lean as given. Proofs remain kernel-checked.

Example

laws "checked" do
  law checked(value),
    requires: value,
    expects: value,
    proof: ~LEAN"exact requires"
end

If verifying the laws fail, the error report includes the path to folder with all .lean files. Use this to debug and guide your proofs.

postlude(dir)

(macro)

Loads the Lean modules in a directory after the translated definitions of this laws group.

postlude "postlude"

As with prelude/1, each .lean file below the directory is a Lean module named by its path, which begins with its own header. Unlike preludes, postludes may import the modules translated for the group, such as Elixir.MyTest, and Erlang.program for its function table, so they can share lemmas about the translated code between the proofs of several laws:

module
public import Elixir.MyTest
@[expose] public section

namespace Elixir.MyTest

theorem double_integer (n : Int) : «double/1» (.integer n) = .ok (.integer (n + n)) := by
  simp [«double/1»]

end Elixir.MyTest

The laws import every postlude module. Namespaces are chosen by the source: the test module's namespace, such as Elixir.MyTest above, makes declarations available to law proofs without qualification. Modules that import none of each other verify independently, and changing one verifies again only the modules that import it.

Postludes do not translate anything themselves: the laws must reach every definition they mention. They do not leak into other groups, and their sources participate in verification cache keys. Proofs retain kernel checking and axiom audits.

prelude(dir)

(macro)

Loads the Lean modules in a directory before the translated definitions of this laws group.

prelude "prelude"

The path is relative to the directory of the calling file. Each .lean file below it is a Lean module named by its path below the directory, so prelude/Tactics.lean is the module Tactics and prelude/Bdd/Core.lean is Bdd.Core. Directory and file names must be Lean identifiers, and hidden files are skipped. Each module begins with its own header:

module
public import Lynx
public section

open Lean Elab Tactic
elab "finish" : tactic => do evalTactic (← `(tactic| rfl))

Modules import what they use, such as runtime modules like Lynx.Modules.Erlang.erlang and each other, and verify after the modules they import. Every translated module imports every prelude module before its proofs and termination suffixes are parsed. Definitions, lemmas, macros, and elaborators may be declared; namespaces are chosen by the source. Meta helper definitions must use Lean's meta modifier as usual.

Preludes verify before the translated modules, so they cannot import them. They do not leak into other groups, and their sources participate in verification cache keys. Proofs retain kernel checking and axiom audits.

termination_suffix(target, source, opts \\ [])

(macro)

Attaches a Lean termination suffix to a translated function for this laws group.

termination_suffix &:calendar.mod/2, ~LEAN"partial_fixpoint"

The source is a Lynx.Lean value. It is parsed as part of the function's declaration, after its body. The suffix does not change the function's embedding (see the embedding levels in Lynx). With partial_fixpoint, inputs on which the function diverges raise Lynx.Exception.exhausted. Raises if the target function is not translated by the group's laws.

Pass recur: true to also attach the suffix to the functions mutually recursive with the target that have no suffix of their own. Lean requires every function in a mutually recursive group to be a partial fixpoint once one of them is:

termination_suffix &Module.Types.Descr.unfold/1, ~LEAN"partial_fixpoint", recur: true

A function with partial_fixpoint also gets a fixpoint_induct principle, which proves properties of every successful return without a termination measure, as returns on divergent inputs never happen. For a pure function of n arguments, Lynx.Except.admissible_returnsN discharges its admissibility goal:

apply «mod/2».fixpoint_induct
  (motive := fun f => ∀ a b v, f a b = .ok v → post a b v)
· exact Lynx.Except.admissible_returns2 _
· intro f ih a b v returned
  ...