LeanToolkit
This is a prototype implementation of an inductive type composition framework in Lean. More information can be found at the "Modular Composition of Inductive Types Using Lean Meta-programming" paper.
Directory Structure
LeanToolkit/
|- LeanToolkit/ ...... The implementation of the type and function composition framework
|- Test/ ............. Test cases
|- CaseStudy/ ........ The case study outlined in the paper
Quick Start
An installation of Lean 4 (including elan and lake) is required.
The library is built with Lean 4 v4.33.1 (see lean-toolchain).
To build the project:
lake build
And to run the test cases and the case study:
lake test
lake build CaseStudy builds all the case study files (including CaseStudy/K3.lean,
which the test driver cannot import next to CaseStudy/CaseStudy.lean).
Each of the test cases uses the #print command to display the result of the test case, and the #guard_msgs command to compare it against the expected result.
Usage
The library's files are modules, so it can be imported from module and non-module
files alike.
import LeanToolkit
/-- LTL formulas: the propositional connectives and the temporal operators. -/
inductive Formula := Prop'.Formula |+ Temp.Formula
deriving DecidableEq
fnsum Sat {P : Type} (σ : Nat → P → Prop) : Nat → Formula P → Prop := Prop'.Sat |+ Temp.Sat
inductive T sig? := A |+ B | extra… deriving …sums the inductive typesAandB(which may take type parameters and indices), keeping their constructors' doc comments and field names, and generatesSubType A Tcoercions andCoeDepcoercions back from each constructor. A sum can itself be a constituent of another sum (e.g.Prop'.Formula |+ Temporal.FormulawithTemporal.Formula := Past.Formula |+ Future.Formula). Constituents may have reflexive fields (functions into the type itself, e.g.args : Fin n → Formula P), which the coercions map pointwise, and may be universe-polymorphic; the constituents must then have the same type, universes included. Declaration modifiers (a doc comment, attributes, visibility) go onT.fnsum f binders* : type := f₁ |+ f₂ alts*definesfby matching on its first explicit argument whose type is a sum (the discriminant, at any position). The arguments of eachfᵢmust be some of those off, in the same order, with the same types (up to the sum) and explicitness, and its result type that off: e.g. a propositionalSat (L : S → Set P) : S → Prop'.Formula P → Propcomposes into a CTLSat (R : S → S → Prop) (L : S → Set P) : S → Formula P → Prop, and the arguments thatfᵢdoes not take are passed along unchanged in its recursive calls. An implicit argument offᵢthatfdoes not have is solved by unification: e.g. a propositionalSat {S} (L : S → Set P) : S → …composes into an LTLSat (σ : Nat → Set P) : Nat → …withS := Nat. The alternativesalts(| pattern => rhs, in which the arguments named inbindersare in scope) come first; the cases of the constructors they do not cover entirely (with a pattern whose arguments are all variables or_) are then generated from thefᵢ, so an alternative with nested patterns (where features interact) takes precedence over the generated case of its constructor. For a definitionfᵢ, its own cases are inlined from its unfolding equation (fᵢ.eq_def), with the recursive calls redirected tof; in a module, it must be@[expose]d to be composed from another module. A theoremfᵢ(no unfolding equation) is called in the cases of the non-recursive constructors; the recursive ones must be given inalts. A function intoPropis declared as a theorem. (fnsumreplaces the formerfnandfnRec.)
The sums and composed functions are recorded in persistent environment extensions, so
a sum declared in one file can be composed over in another. The commands add the
tokens fnsum, |+, enum, datalog, :- and back_chaining; in a module, a private import LeanToolkit keeps
them from leaking to the module's importers (the SubType class lives in
LeanToolkit.Common, which can be imported publicly).
Enumerations
LeanToolkit.Enum (also imported by LeanToolkit) provides the enum command for
finite enumeration types:
import LeanToolkit.Enum
/-- The colours of a traffic light. -/
enum Colour where
| red
| green
| yellow
deriving Repr
declares Colour as inductive would (every constructor must be a constant), with
DecidableEq Colour and LeanToolkit.Enumerable Colour: the list of its values, in
declaration order, with which ∀ c : Colour, … and ∃ c : Colour, … are decidable.
Further classes can be derived after deriving; a private type gets a private instance.
Enumerable only needs Lean's core library; with Mathlib, one instance
[Enumerable α] [DecidableEq α] : FinEnum α := FinEnum.ofNodupList Enumerable.all Enumerable.complete Enumerable.nodup gives FinEnum (hence Fintype and Finite)
for every enumeration.
Datalog
LeanToolkit.Datalog (also imported by LeanToolkit) provides the datalog command,
from LDatalog (without its Mathlib examples):
import LeanToolkit.Datalog
datalog {
/-- The edges of a graph. -/
relation edge(x : String, y : String).
relation path(x : String, y : String).
/-- Reachability in a graph `E`, a parameter. -/
relation Reach[E : String → String → Prop](x : String, y : String).
f1: edge("a", "b").
f2: edge("b", "c").
r1: path(x, y) :- edge(x, y).
r2: path(x, z) :- path(x, y), edge(y, z).
here: Reach(E, x, x).
step: Reach(E, x, z) :- (E x y), Reach(E, y, z).
q0: path("a", "c")?
q1: path("b", m?)?
}
relation R[p₁ : P₁, …](x₁ : T₁, …).declaresR (p₁ : P₁) … : T₁ → … → Prop, an inductive predicate with the parameterspᵢand one constructor per fact or rule whose head isR, named by its label (path.r1): the least relation closed under its rules, with an induction principle; no axioms are added. A parameter can also be any binder ([{α : Type}, M : DFA α σ]). An atom onRgives the explicit parameters first, as themselves:R(p₁, …, t₁, …). With declared parameters (even[]) these are exactly the parameters; without, Lean may promote an argument that is the same variable everywhere to a parameter.- The variables of a clause become implicit arguments of its constructor, in order of
first occurrence. A label can bind them explicitly instead, to fix their order, types
or binder kinds:
post {e s e' s'}: Fwd(e', s') :- …,refl (x): Eq(x, x).. - A premise is an atom
R(t₁, …)or a side condition(p); relations depending on later ones are mutual; relations and clauses can have doc comments. q: R(t₁, …)?provesR t₁ …by backward chaining (solve_by_elim) over the constructors ofRand of the relations its constructors' premises mention (transitively; relations declared with plaininductiveincluded), the earlier queries, and the lemmas listed inuse l₁, … ., as the theoremq; identifiers ending in?are unknowns solved by the search.LeanToolkit.BackChainprovidesback_chaining l₁ … lₙ, the search as a tactic, closing side goals withsimp.
Tests: Test/datalog.lean.