Prove equations between programs and relations using Kleene algebra (KA) and Kleene algebra with tests (KAT). Built on Mathlib, this library provides proof-producing tactics, typed and untyped completeness theorems, relational and matrix models, and Hoare rules.
This is a Lean translation and extension of Damien Pous’s relation-algebra library for Rocq/Coq. It is not a feature-for-feature mirror; PORTING.md records the coverage and remaining differences.
OpenAI Codex was used to translate and extend Damien Pous’s relation-algebra library from Rocq/Coq to Lean 4.
User guide · Examples · Contributing · License
Install Lean and Lake, then run:
git clone https://github.com/focs-lab/lean-relational-algebra.git
cd lean-relational-algebra
lake exe cache get
lake buildThe project pins Lean 4.30.0 and Mathlib 4.30.0. The cache command downloads prebuilt Mathlib dependencies.
Save this as Demo.lean in the repository and check it with lake env lean Demo.lean:
import RelationAlgebra open scoped Computability SetRel KAT variable {α : Type*} -- Denesting: two equivalent ways to express iteration. example (R S : SetRel α α) : (R + S)∗ = R∗ * (S * R∗)∗ := by ka -- A guard and its negation cannot both hold. example (b : Set α) : (⌜b⌝ : SetRel α α) * ⌜bᶜ⌝ = 0 := by kat -- Whenever the loop terminates, its guard is false. example (b : Set α) (R : SetRel α α) : KAT.HoareTriple ⊤ (KAT.whileDo b R) bᶜ := by kat -- `hkat` also uses the hypotheses in the context. example (b : Set α) (R : SetRel α α) (h : ⌜b⌝ * R ≤ R * ⌜b⌝) : (⌜b⌝ : SetRel α α) * R∗ ≤ R∗ * ⌜b⌝ := by hkat -- `ra` adds converse, for any Kleene algebra with an involution. example (R S : SetRel α α) : star (R * S) = star S * star R := by ra
More examples: algebra and relations · using the tactics · typed goals · typed converse · Boolean and residual reasoning · Paterson’s flowchart equivalence.
Use as a dependency in another project
Use the same Lean toolchain (Lean 4.30.0) and add this entry to your lakefile.toml:
[[require]] name = "relation_algebra" git = "https://github.com/focs-lab/lean-relational-algebra.git" rev = "b01130a20cf812c06580ab50adb444cf0268793e"
This pins a reviewed revision that includes the license and attribution files. Commit
the generated lake-manifest.json to keep the resolved dependencies reproducible.
Run lake update, lake exe cache get, and lake build, then import RelationAlgebra to use
the library.
| Tactic | Goals | Operations understood |
|---|---|---|
ka |
Equalities and inequalities | 0, 1, +, *, ∗, ⁺ |
kat |
Equalities, inequalities, and KAT.HoareTriple |
KA operations, embedded Boolean tests, ifThenElse, whileDo |
kat on typed goals |
Equalities, inequalities, and TypedKAT.HoareTriple |
⊥, 𝟙, ⊔, ≫, ∗, ⁺, typed tests and guarded commands |
hkat |
Untyped or typed goals, using Hoare-style hypotheses from the context | The same operations as kat |
ra, ra_normalise, ra_simpl |
Untyped or typed equalities and inequalities | KA operations, converse, ⊓, ᶜ, ⊤, \, ⇨, and residuals |
ka proves identities in any Kleene algebra. kat adds Boolean tests and also supports
typed composition. hkat adds reasoning from hypotheses at one or several objects.
ra normalises expressions and checks structural inclusions.
Every successful tactic produces a kernel-checked proof. Algebraic completeness is proved
for KA and for typed and untyped KAT. Search completeness is not proved: ka, kat, and
hkat use fuel-bounded search, and ra is incomplete. A failed tactic does not show that a
goal is false. Try kat 5000 or hkat 10000 when the default fuel is insufficient.
ka, kat, and ra ignore hypotheses; hkat uses supported Hoare-style constraints.
Hoare triples express partial correctness, not termination. See the
guide for assumptions, supported hypotheses, and resource limits.
Use ordinary relations (SetRel, RelCat), rectangular matrices (Matrix.Mat),
computable finite relations (FinRelCat), relations on setoids (SetoidRelCat), or
languages of typed traces (TraceCat). The model guide
links to executable examples. The library also includes a free typed KAT with its
universal property.
Worked applications include IMP with named assignments, twelve compiler optimizations, and Paterson’s flowchart equivalence. Use the library map to find definitions and theorems.
Damien Pous and the contributors to relation-algebra provided the central algebraic organization, proof developments, and automation approach. Upstream also credits Christian Doczkal, Insa Stucke, and the Rocq development team; Doczkal developed the finite-relation model. Pous’s documentation is a useful companion. We also rely on the Lean and Mathlib contributors’ proof infrastructure and mathematics.
The translation and extensions are distributed under LGPL-3.0-or-later, following upstream. See LICENSE, the full LGPL and GPL texts, and the preserved attribution and modification notice. Dependencies retain their own licenses. Mathematical references identify the underlying papers.