Skip to content

Latest commit

 

History

History

samples

README.md

Samples

Programs built with leanfran, each small enough to read in one sitting. Every directory is a Clojure project that depends on leanfran through :local/root, with its own README and a make test that the repository's make check runs. Run the samples inside nix develop of the repository root.

leanfran is used in three ways, and the samples follow them.

1. Write functions in Lean and call them from Clojure

deflean holds Lean inside a Clojure file. leanfran --lang clojure sync checks the definitions and their proofs and writes the code to _lean/, which is committed, and the functions become Clojure functions of the namespace. make sync regenerates the code of every sample.

Sample What it shows
todo-cli deflean in S-expressions: structures and theorems about the list operations, behind a Clojure CLI
word-count deflean with :imports (Std.HashMap) and Lean source written as a string next to S-expressions, for do notation

2. Compile a Lean project to a Clojure namespace

leanfran --lang clojure compiles the modules of a lake project to a Clojure namespace, which you commit and load without Lean.

Sample What it shows
roman-numerals an ordinary lake project compiled to a committed Clojure namespace, which runs without Lean

3. Write functions in Clojure and verify them in Lean

leanfran.typed/defn gives a Clojure function a Lean signature, and its marks :refines, :requires and :ensures name the functions of deflean blocks that specify it. The proofs are in lean/Proofs/. (clojure f) in a deflean block turns the function into a Lean model to prove theorems about. The ways of relating an implementation to its specification each have a sample:

How the implementation relates to the specification Sample
The specification runs as it is use 1: a deflean definition is the program
A faster implementation equals the specification on every input, and the code of the specification calls it: :refines verified-clojure (sum-to-fast)
A faster implementation equals the specification under an assumption: :refines and :requires sorted-search
An ordinary defn satisfies a property of its result: lt/verify with :ensures verified-clojure (insertion sort is sorted)
Theorems about the model of the implementation inventory (how operations change a map)

For a single inline Bool postcondition that can refer to the result as %, see the inline postconditions example. Run clojure -X leanfran.tools/check :require-proved true from a sample project to check its saved sources.

The samples also cover the Clojure that such functions are written in:

Sample What it shows
inventory maps ((Map K V)), update and get with defaults, when returning nil, and destructuring
generic-search a polymorphic function, a function with two arities, and rest arguments

The signatures also serve Clojure tools without any proof:

Sample What it shows
typed-schemas malli schemas, test.check generators, and clj-kondo types derived from the Lean signatures

Where to start

Start with todo-cli for deflean, then verified-clojure for verifying Clojure functions.