-
Find the rule your code already has#
Somewhere in your app is a function like this one: admit a request while the spend is under a budget, each request capped. It is short, and it is exactly the kind of code that breaks in production.
// What the app does today let spent = 0 function admit(cost: number, budget: number, maxPerRequest: number) { if (spent < budget) spent += Math.min(cost, maxPerRequest) } -
Write it as a rule#
The same logic, with its parts named.
natis a natural number (0, 1, 2, …);when(c, a, b)isc ? a : b; terms have methods instead of operators (s.lt(b)iss < b). Names are typed: a misspelled one is a TypeScript error.import {min, nat, rule, when} from 'gdp-wired' const budget = rule('budget') .params({b: nat, m: nat}) // fixed for the run: the budget, the cap per request .inputs({c: nat}) // what each request brings: its cost .state({s: 0}) // what is kept between requests: the spend, from 0 .step(({b, m, c, s}) => ({s: when(s.lt(b), s.plus(min(c, m)), s)})) -
Say what must always be true#
A law reads the params and the state. This one says that no sequence of requests, of any costs, ever pushes the spend past the budget plus one request's cap.
const bounded = budget.law('spend stays within b + m', ({s, b, m}) => s.atMost(b.plus(m))) -
Prove it#
import {proveRule} from 'gdp-wired' proveRule(budget, [bounded]) // {proven: true, checked: true}That is the whole proof. Under the hood: it holds at the start, and every step from a state where it holds leads to a state where it holds (induction over steps). The prover finds the case analysis; the kernel checks it. Require both
provenandchecked. -
Break it on purpose#
Forget the cap, the bug this law exists to catch, and the proof is refused. A search from the start then turns the refusal into a real run.
import {search} from 'gdp-wired/machine' const uncapped = budget.step(({b, c, s}) => ({s: when(s.lt(b), s.plus(c), s)})) proveRule(uncapped, [bounded]) // {proven: false, goal: 'spend stays within b + m @step', stuck: 'sat', witness: {...}} search(uncapped.machine, bounded) // {params: {b: 1, m: 0}, trace: [{c: 2}]}Read the run: with a budget of 1 and a cap of 0, one request of cost 2 takes the spend to 2, past 1 + 0. Keep this broken variant in your tests next to the real rule; it is the cheapest regression test you will write.
-
Use it in your app#
Ship the rule that was proven:
compiled()is plain generated JavaScript.encodeParamsandencodeInputturn your values into what the rule reads;decodereads the state back.const shipped = budget.compiled() const params = budget.encodeParams({b: 100, m: 30}) let st = shipped.init(params) for (const c of [50, 40, 70, 20]) st = shipped.step(params, st, budget.encodeInput({c})) budget.decode(st) // {s: 110}Or keep your own code, and hold it to the rule.
compareruns both on drawn and edge inputs, field by field:budget.compare({ init: () => ({s: 0}), step: ({b, m}, state, {c}) => ({s: state.s < b ? state.s + Math.min(c, m) : state.s}), }) // {checked: 2000} // Drop the cap in your code, and compare names the first difference: // {checked: 1, failure: 'budget, params {"b":57,"m":26}, after {"c":90}: // s is 26 here, 90 in the code it describes'} -
Put it in your tests#
Three assertions per rule: the laws are proven and checked, the broken variant is refused, and the builders agree with themselves on concrete runs (
crossCheck).import {expect, test} from 'vitest' // or bun:test test('the budget keeps its law', () => { expect(proveRule(budget, [bounded])).toMatchObject({proven: true, checked: true}) expect(proveRule(uncapped, [bounded]).proven).toBe(false) expect(budget.crossCheck([bounded]).failure).toBeUndefined() })
GuideStart here
Your first proof, in seven steps.
Every block below is real code: it is kept in the repository's
examples/guide.ts,
which the test suite runs, and each result shown is asserted there.