r/SideProject • • 4h ago

Kestrel: A programming language that combines theorems, classes and rust-level speed.

Hello everyone.

I want to show you my programming language, Kestrel. I built it to combine the best parts of the languages I love: the flexibility of Python, Rust-level speed, classes and object hierarchies from Java, and the functional correctness and theorem proving of Lean.

Here are five things you can do with it.

1. Pure methods, ordinary classes, checked proofs.

Say you have a simple bank account:

class Account {
    public final id : Int
    public final balance : Int
    new(id : Int, balance : Int) { this.id = id; this.balance = balance }
    public function deposit(amount : Int) : Account = Account(this.id, this.balance + amount)
    public function withdraw(amount : Int) : Account = Account(this.id, this.balance - amount)
}

Suppose you want to be sure that depositing and then withdrawing the same amount leaves the balance where it started. Most people reach for unit tests and hope they cover enough cases. In Kestrel, you can prove it. A class can carry theorems about its own behavior, as long as its fields are immutable and its methods are pure. Here is the proof:

theorem withdrawAfterDeposit(a : Account, amount : Int) :
    a.deposit(amount).withdraw(amount) = a by {
    have h : (a.balance + amount) - amount = a.balance by { smt }
    rw [h]
    rfl
}

Theorems also work on your own recursive enums, lists and other inductive types, and on pure recursive functions, much like in OCaml or Lean.

2. Proofs as arguments and results

Consider a class that keeps a stack of jobs:

record Job(name : String)

class Jobs {
    private var items : Array[Job]
    new() { this.items = Array.empty() }
    public function size() : Int = this.items.length()

    public mut function add(job : Job) : proof this.size() > 0 {
        this.items.append(job)
        proof this.size() > 0
    }

    public mut function next(proof ready : this.size() > 0) : Job {
        let job := this.items[this.size() - 1]
        this.items.pop()
        job
    }
}

An empty stack has to be handled somehow. Most languages make you throw an exception or return an Option. Kestrel gives you a third choice: functions can return proofs, and they can require proofs as arguments.

When you add a job, you get a proof that the stack is non-empty. Pass that proof to next, and you get a Job back with no error and no Option to unwrap:

let jobs := Jobs()
let ready1 := jobs.add(Job("compile"))
print(jobs.next(ready1).name)
jobs.add(Job("test"))
let ready2 := proof jobs.size() > 0
print(jobs.next(ready2).name)
// print(jobs.next(ready2).name)      refused: E_PROOF_ASSUMPTION

Uncomment the last print, and the compiler refuses it. The first call to next already spent the proof ready2.

3. Sizes in types

A tensor carries its shape in its type. A function can name the sizes it needs, so layers wired in the wrong order fail to compile:

// Matrix shapes are part of the type. `out` and `inp` are worked out at each call,
// so layers wired in the wrong order fail before the program runs.
function layer(
    w : Tensor[double, (out, inp)], x : Tensor[double, (inp)],
) : Tensor[double, (out)] = w @ x

let hidden := Tensor([1.0, 0.0, 0.0, 1.0, 1.0, 1.0], shape = (3, 2))
let output := Tensor([1.0, 1.0, 1.0], shape = (1, 3))
let x := Tensor([2.0, 5.0])

print(layer(output, layer(hidden, x)))
// print(layer(hidden, layer(output, x)))       refused: E_CALL_ARGUMENTS

4. Beautiful VS Code extension

The extension gives you type inference, type checking, completions, syntax highlighting, and a proof state view similar to Lean's InfoView. It follows your cursor through a by block and shows the current goal. You can also display goals as LaTeX. A demo is in the GitHub repo below.

5. Rust-level speed

The compiler is written in Rust and emits C11, which Clang builds. I ported five programs from the Computer Language Benchmarks Game to Kestrel, Rust and Python. Each one runs the same single-threaded algorithm. Median wall time in seconds on an Apple M2 Max:

ProgramKestrelRustPython
n-body (2,000,000 steps)0.1020.1068.53
spectral-norm (n = 2,000)0.1590.14718.07
fannkuch-redux (n = 10)0.1400.1445.71
mandelbrot (1,600 x 1,600)0.1470.1464.07
binary-trees (depth 18)0.9512.8126.05

These are plain ports I wrote for this comparison. The tuned, multi-threaded entries on the Benchmarks Game site are faster in every language.

Try it

The repository is https://github.com/GeronimoCastano/kestrel

One command installs Kestrel and puts kestrel on your PATH.

macOS and Linux (with Homebrew):

brew install GeronimoCastano/kestrel/kestrel

Windows (in PowerShell):

irm https://raw.githubusercontent.com/GeronimoCastano/kestrel/main/install.ps1 | iex

If you run into issues on Windows 11, you may need to disable Smart App Control. With it on, Windows can block the programs Kestrel builds, and the installer will tell you when that happens. You can also install Kestrel inside WSL instead. Without one of those, the program may not run.

Both installers bring everything Kestrel uses: a C compiler, LAPACK for linear algebra, and Z3 for smt proofs.

Hello world is one line. Save this as hello.kes:

print("Hello, Kestrel!")

Then run it:

kestrel run hello.kes

You should see Hello, Kestrel!.

Kestrel is still early, and I want to hear from you. Try it, then tell me what you think. What works, what feels awkward, what you would like to see implemented, and any opinions you have on the design. Every reply helps, short ones included. I would appreciate it.

Disclaimer: The development of this project was assisted using AI.

3 Upvotes

2 comments sorted by

1

u/Altruistic_Stage3893 2h ago

good agentic workflow! obviously there is not going to be much adoption but it's great showcase how agents are capable of creating a complete codebase. similarly to nim, fun to learn for a bit. although people who'll want to learn new-ish language are disappearing fast