Limits per key: per account, per day#
Maps keep a number per key. A flag is a condition decided once per step. forall(k => …) makes a
law hold at every key at once.
import {bool, forall, nat, proveRule, rule} from 'gdp-wired'
const caps = rule('caps')
.params({d: nat})
.inputs({acct: nat, day: nat, ok: bool})
.maps({Acct: 0, Day: 0})
.flags(({ok, acct, day, d, Acct, Day}) => ({
granted: ok.and(Acct.at(acct).lt(1), Day.at(day).lt(d)),
}))
.step(({acct, day, granted, Acct, Day}) => ({
Acct: Acct.bump(acct, granted),
Day: Day.bump(day, granted),
}))
const dayCap = caps.law('no day passes its cap', ({Day, d}) => forall(k => Day.at(k).atMost(d)))
const once = caps.law('no account is granted twice', ({Acct}) => forall(k => Acct.at(k).atMost(1)))
proveRule(caps, [dayCap, once]) // {proven: true, checked: true}
Machine integers: wrap, saturate or trap#
When the code's numbers are fixed-width (a u8, an i64), say so, and say what overflow
does: 'WRAP', 'SATURATE' or 'TRAP'. Division names its rounding:
'FLOOR', 'TRUNCATING' or 'EUCLIDEAN'. Widths are 8, 16, 32 and 64, signed or
not, and Lean checks them as its own UIntN, IntN and BitVec.
import {fixedInt, proveSteps, rule} from 'gdp-wired'
const u8 = fixedInt(8, false)
const meter = (mode: 'SATURATE' | 'WRAP') => rule('meter')
.inputs({n: u8})
.state({total: u8.from(0)})
.step(({n, total}) => ({total: total.plus(n, mode)}))
for (const mode of ['SATURATE', 'WRAP'] as const) {
const r = meter(mode)
const grows = r.stepLaw('adding never lowers the total', ({total}, after) => after.total.atLeast(total))
proveSteps(r, [], [grows])
}
// SATURATE: {proven: true, checked: true}
// WRAP: {proven: false, goal: 'adding never lowers the total @step law', stuck: 'sat',
// witness: {total$0: 174, n$0: 108}} 174 + 108 wraps to 26
New in 0.51.0; the output above is from that release, not from examples/guide.ts.