gdp-wired
Start here · First proof in 7 steps

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.

On this page
  1. 1. Find the rule
  2. 2. Write it as a rule
  3. 3. State the law
  4. 4. Prove it
  5. 5. Break it on purpose
  6. 6. Use it in your app
  7. 7. Put it in your tests
  1. 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.

    ts
    // What the app does today
    let spent = 0
    function admit(cost: number, budget: number, maxPerRequest: number) {
      if (spent < budget) spent += Math.min(cost, maxPerRequest)
    }
  2. Write it as a rule#

    The same logic, with its parts named. nat is a natural number (0, 1, 2, …); when(c, a, b) is c ? a : b; terms have methods instead of operators (s.lt(b) is s < b). Names are typed: a misspelled one is a TypeScript error.

    ts
    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)}))
  3. 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.

    ts
    const bounded = budget.law('spend stays within b + m', ({s, b, m}) => s.atMost(b.plus(m)))
  4. Prove it#

    ts
    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 proven and checked.

  5. 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.

    ts · broken variant
    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.

  6. Use it in your app#

    Ship the rule that was proven: compiled() is plain generated JavaScript. encodeParams and encodeInput turn your values into what the rule reads; decode reads the state back.

    ts
    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. compare runs both on drawn and edge inputs, field by field:

    ts
    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'}
  7. 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).

    ts
    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()
    })