KEEL
The systems language agents write and a solver proves before it runs.
- Compiles to C
- Proofs in milliseconds
- Parallelism without data races
If it compiles, it cannot crash.
Keel checks every addition, array index, division, loop and recursive call before compiling. When it can't prove one is safe, it rejects the program and shows the inputs that break it.
fn midpoint(a: i64, b: i64) -> i64 {
return (a + b) / 2;
}
$ keel check midpoint.kl
error[V-OVERFLOW]: '+' may overflow i64
counterexample: a = 9223372036854775807, b = 1
fn midpoint(a: i64, b: i64) -> i64
ensures 2 * result <= a + b
&& a + b <= (2 * result) + 1
{ // halve each side, then add the carry }
$ keel check midpoint.kl
keel: 19 obligations proved in 2 functions
The ensures line is checked in exact arithmetic, so it holds for every possible input, including the ones no test would try. Proven code needs no runtime checks, so the compiled program carries none.
Split the work. The compiler proves the halves don't touch.
let mid: i64 = len(xs) / 2;
par {
msort(&mut xs[0..mid], &mut tmp[0..mid]);
msort(&mut xs[mid..len(xs)], &mut tmp[mid..len(xs)]);
}
merge(&mut xs, &mut tmp, mid);
$ keel check msort.kl
error[V-DISJOINT]: borrows of 'tmp' across
par branches may overlap (data race)
counterexample: len(xs) = 4097, mid = 2048
Threads are only started while a core is free. There are no locks or atomics to write, and a data race cannot compile.
As fast as Rust, measured.
Counting primes below 20 million, same algorithm in both languages, on a 15-core Apple M-series machine:
Parallel mergesort of 10 million integers takes 0.12 s in Keel, the same as Rust with rayon. Checking a file takes 0.01 to 1 s and gives the same verdict every time, so an agent can re-check after every edit.
Proofs an agent cannot fake.
The easy way to pass a proof is to weaken what it claims. Keel rejects that.
| Shortcut | Result |
|---|---|
| Preconditions that contradict each other | V-VACUOUS |
| A branch or loop that can never run | V-DEAD |
| A lemma whose conclusion does not follow | V-ENSURES, with a counterexample |
| A definition that proves itself in a circle | V-TERMINATION |
assume, unsafe, sorry | Not in the language |
A proof shows the code does what its requires and ensures lines say. It cannot show those lines say what you meant, so read them. They are a few lines, not the whole program.
Find a program the checker should have rejected.
The whole promise rests on the verifier being right, so we want outsiders attacking it. If keel check accepts a program that then overflows, reads out of bounds, divides by zero, races, hangs, or breaks its own ensures, that is a soundness bug and the most useful report you can send.
Small
One
.klfile, as short as you can make it.Accepted
The
keel checkoutput showing it proved.Wrong
What it did at run time instead.
Every fix ships with your program as a regression test, and reporters are credited in the hall of fame. Attack ideas, the rules, and the report form are in BREAK_IT.md.
Designed for the reader that is not human.
One way to write it
One loop, one conditional, no shadowing, no overloading, no implicit conversions.
No operator precedence
a + b * cis an error. Writea + (b * c)and nothing is misread.Errors as data
Stable codes,
--jsonoutput, a counterexample and a hint for every failure.Learned from one page
Haiku, Sonnet and Opus each wrote six verified programs from a one-page primer, first try.
In our evals, every model passed all hidden tests in both Keel and Rust. The Keel solutions also carried 120 to 151 machine-checked facts each; the Rust ones carried none.
Build it, then point your agent at it.
brew install z3
git clone https://github.com/swperb/keel
cd keel && cargo build --release
./target/release/keel run examples/primes.kl
When writing Keel:
- learn it from evals/PRIMER.md
- run `keel check --json` after every edit
- fix failures using the counterexample
- never weaken a requires clause to pass