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 types A and B (which may take type parameters and indices), keeping their constructors' doc comments and field names, and generates SubType A T coercions and CoeDep coercions back from each constructor. A sum can itself be a constituent of another sum (e.g. Prop'.Formula |+ Temporal.Formula with Temporal.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 on T.
  • fnsum f binders* : type := f₁ |+ f₂ alts* defines f by matching on its first explicit argument whose type is a sum (the discriminant, at any position). The arguments of each fᵢ must be some of those of f, in the same order, with the same types (up to the sum) and explicitness, and its result type that of f: e.g. a propositional Sat (L : S → Set P) : S → Prop'.Formula P → Prop composes into a CTL Sat (R : S → S → Prop) (L : S → Set P) : S → Formula P → Prop, and the arguments that fᵢ does not take are passed along unchanged in its recursive calls. An implicit argument of fᵢ that f does not have is solved by unification: e.g. a propositional Sat {S} (L : S → Set P) : S → … composes into an LTL Sat (σ : Nat → Set P) : Nat → … with S := Nat. The alternatives alts (| pattern => rhs, in which the arguments named in binders are 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 the fᵢ, so an alternative with nested patterns (where features interact) takes precedence over the generated case of its constructor. For a definition fᵢ, its own cases are inlined from its unfolding equation (fᵢ.eq_def), with the recursive calls redirected to f; in a module, it must be @[expose]d to be composed from another module. A theorem fᵢ (no unfolding equation) is called in the cases of the non-recursive constructors; the recursive ones must be given in alts. A function into Prop is declared as a theorem. (fnsum replaces the former fn and fnRec.)

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₁, …). declares R (p₁ : P₁) … : T₁ → … → Prop, an inductive predicate with the parameters pᵢ and one constructor per fact or rule whose head is R, 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 on R gives 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₁, …)? proves R t₁ … by backward chaining (solve_by_elim) over the constructors of R and of the relations its constructors' premises mention (transitively; relations declared with plain inductive included), the earlier queries, and the lemmas listed in use l₁, … ., as the theorem q; identifiers ending in ? are unknowns solved by the search.
  • LeanToolkit.BackChain provides back_chaining l₁ … lₙ, the search as a tactic, closing side goals with simp.

Tests: Test/datalog.lean.