KEEL

The systems language agents write and a solver proves before it runs.

  • Compiles to C
  • Proofs in milliseconds
  • Parallelism without data races
Proofs

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.

First tryrejected
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
Fixedproved in 0.01 s
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.

Parallelism

Split the work. The compiler proves the halves don't touch.

msort.klsortedness proven
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);
One slice offrejected
$ 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.

Speed

As fast as Rust, measured.

Counting primes below 20 million, same algorithm in both languages, on a 15-core Apple M-series machine:

  • Rust, one thread2.72 s
  • Rust, scoped threads0.20 s
  • Keel, par0.23 s

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.

No shortcuts

Proofs an agent cannot fake.

The easy way to pass a proof is to weaken what it claims. Keel rejects that.

ShortcutResult
Preconditions that contradict each otherV-VACUOUS
A branch or loop that can never runV-DEAD
A lemma whose conclusion does not followV-ENSURES, with a counterexample
A definition that proves itself in a circleV-TERMINATION
assume, unsafe, sorryNot 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.

Break it

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 .kl file, as short as you can make it.

  • Accepted

    The keel check output 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.

Agents

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 * c is an error. Write a + (b * c) and nothing is misread.

  • Errors as data

    Stable codes, --json output, 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.

Get started

Build it, then point your agent at it.

Terminal
brew install z3
git clone https://github.com/swperb/keel
cd keel && cargo build --release
./target/release/keel run examples/primes.kl
AGENTS.md
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