TheoremDrivenDevelopment
AI can generate code faster than humans can review it. AI-assisted review helps, but it can miss requirements that were never written down. Each change must still respect those requirements, even when they were established years earlier. Reviewing every generated change risks nullifying the productivity gains, while accepting changes without review risks breaking behavior we depend on. We need to record these requirements and have the compiler enforce them.
Tests catch regressions in specific inputs and call sequences. Property based testing generates more of these cases, but a passing run generally doesn't cover every possible execution. Expected results can become stale when requirements change, and large suites can be slow to run.
I've been working on this in Lim. Its current contract checker lets us require evidence that a condition has been checked before a value reaches a function. It also tracks when mutation makes that evidence unusable. Try the working examples in the handbook or browser playground.
Validate once, enforce at calls
A condition is an ordinary pure Boolean function. Another function can name that condition as a requirement. Here's the mechanism in Rust-like notation:
fn below_100(value: u64) -> bool {
value < 100
}
fn submit(value: u64) -> u64
requires below_100(value);
if_valid below_100(input) {
submit(input);
}
Lim spells these forms #requires and #if-valid. The branch evaluates the predicate at runtime. In its successful arm, the compiler knows that this exact predicate was checked for this value, so the call is allowed. Remove the validation and the call is rejected, even for submit(42). The checker doesn't unfold the predicate to discover that 42 is below 100.
This makes the requirement part of the API. A future change cannot introduce a call without supplying the required evidence. The compiler performs the tedious, repetitive work of checking how that evidence is used.
Validation belongs to the value we checked
Checking a request and then changing it must not authorize sending the altered request. Lim tracks the storage a predicate reads. An unrelated field can change without discarding the fact, but changing a dependency requires new evidence:
if_valid below_100(request.count) {
request.tag = 1;
submit(request.count); // Allowed: count is unchanged.
request.count = 150;
submit(request.count); // Rejected: evidence no longer applies.
}
The same checks apply across helper calls using their declared contracts and analyzed mutations. Moving a write into another function doesn't hide it from the checker. We can record relationships such as validation before use without repeatedly tracing every call ourselves.
Compose conditions without multiplying types
Rust already carries some conditions through types. A NonZeroU64 supplies a nonzero divisor, and a String supplies valid UTF-8. These are useful abstractions. But a number that must also be even and a Fibonacci number needs further wrappers or a combined type.
Named predicates let us write additional requirements directly. They can also declare prerequisites: if small-positive requires positive, a function requiring small-positive inherits both requirements. Callers establish both facts; we declare the dependency once.
With #where, conditions can constrain stored fields, parameters and results without changing their runtime representation. Mutation through an exclusive borrow must restore fixed storage constraints before returning the owner to general use. Temporary facts can instead become unavailable after mutation.
What is checked, and what is promised?
Lim enforces requirements and evidence use at compile time. It does not prove that every function implements its promises. #ensures supplies a postcondition that later code may rely on; #preserves carries a declared fact through a call when it held beforehand. Their truth remains the programmer's responsibility. #assume is another explicit trust point.
Instrumented builds using --check-contracts audit postconditions and preservation on the inputs we exercise. Those checks can catch a false promise, but a passing run is still testing. The validation branch remains executable in production; evidence tracking doesn't require a runtime proof object.
The connection to theorems
In mathematics, a theorem is a statement established by a proof. Two even numbers can be written as 2a and 2b; their sum is 2(a + b), so it is even. That argument covers every pair. A proof that an operation preserves a condition would give us a stronger foundation than testing its promise.
Lean demonstrates that distinction. Anthropic's formalization of Fermat's Last Theorem supplies a proof behind a short statement. Using their final check, we can write:
import Theorems.Thm_fermat_last_theorem
example : ∀ (n : ℕ), n ≥ 3 → ∀ (a b c : ℕ),
a > 0 → b > 0 → c > 0 → a^n + b^n ≠ c^n := fermat_last_theorem
The term after := supplies the proof; Lean's kernel checks that it establishes the statement. Lim's current checker does not provide that mathematical proof machinery. It enforces the use of declared evidence and contracts.
That is still useful for the original problem. We can record a requirement once and prevent later changes from silently bypassing it, while keeping the promises that need review visible. The Lim contract tutorial is a place to try this with the working compiler.