Proving a Meow Function with Lean 4: From a Contract to a Finished Proof
meow verify proves a contract with a solver, automatically, for the integers
and bools it understands. When a function is out of its reach, or the property
is more than a contract can say, write the function out as Lean 4
and prove it there.
This guide takes one function from a Meow file to a finished proof. Whisker is
the same with whisker for meow, pure fn for trill meow, and requires /
ensures / result for beg / vow / prey; the Lean file it writes says
Whisker where this one says Meow, in its first comment and its namespace.
1. Export
# abs.nyan
trill meow abs(x int) int
beg x > 0 - 9223372036854775807
vow prey >= 0
{
sniff (x < 0) { bring 0 - x }
bring x
}
meow export lean abs.nyan -o Abs.lean
Without -o the Lean goes to standard output. What comes out:
namespace Meow
def abs (x : Int64) : Int64 :=
if x < 0 then
0 - x
else
x
theorem abs_ensures_1 (x : Int64) (h1 : x > 0 - 9223372036854775807) : abs x >= 0 := by sorry
end Meow
Every trill function becomes a def, each after the functions it calls. Every
vow becomes a theorem: the beg clauses are its hypotheses (h1, h2, …) and
the vow is its conclusion. A function that cannot be written (a loop, a litter,
a basket, a parameter with no type) is reported on standard error as
skipped: f (reason), and so is any function that calls it. What is not trill
is left out too, and said so: a function that is not is reported as
skipped: f (not pure), and a method (a function in a groom block, which is
never pure) as skipped: T.m (a method). The lines come in the order the
declarations appear in the file; the export fails only when there is nothing to
write at all, and then the lines are printed first.
2. Replace each sorry with a proof
A sorry is a promise to prove it later; Lean accepts the file with warnings
until you do. Put the proof in the same file. (A second file that imports this
one can state the theorem again under a name of its own, after open Meow
(open Whisker for a Whisker program), but the exported theorem keeps its
sorry.) For a fixed-width integer the tool to reach for is bv_decide (it turns
the statement into a bit-vector problem for a SAT solver) or omega for
arithmetic over unbounded Int. bv_decide lives in Std, so add its import
as the first line of the file (an import has to come before everything else,
namespace Meow included):
import Std.Tactic.BVDecide
theorem abs_ensures_1 (x : Int64) (h1 : x > 0 - 9223372036854775807) : abs x >= 0 := by
unfold abs
bv_decide
A theorem about a recursive function usually goes by induction, and Lean also
has to be shown that the recursion ends: the export writes an ordinary def and
leaves that to you, with a note at the top of the file. Use termination_by and
decreasing_by.
3. Where Lean and Meow differ
- Overflow.
Int64and the other fixed-width types in Lean wrap around, as Meow’s integers do, so a statement about them is a statement about Meow’s.meow verifyfound the case that breaksabswithout itsbeg; Lean needs the sameh1. - Division by zero. Dividing by zero is a Furball in Meow. In Lean,
x / 0is0. A proof about a function that divides does not cover a zero divisor, so add a hypothesis (beg d != 0) and prove that callers satisfy it. The exported file says so at the top whenever it contains a division. - Contracts at run time. A contract is also checked every time the function runs. The theorem says it holds for every input; the check catches the day it does not.
- What is not written. Floats and strings are written as
FloatandStringbut the operators on them are the least tested part of the export: read the result before relying on it.
Where to go next
meow verifyfor a proof with no Lean at all.meow testtries each contract on generated inputs.- The contracts section of the spec .