A permission check#
A check with no state is a pure function. Its laws say what it must never allow, given what about the request.
With gdp-ts, guard from gdp-wired/gdp
then holds every call site to the proven check
(how).
import {bool, fun, oneOf, proveDecision} from 'gdp-wired'
const canEdit = fun('can edit')
.inputs({role: oneOf('owner', 'editor', 'viewer'), locked: bool})
.returns(({role, locked}) => ({allow: role.is('owner').or(role.is('editor').and(locked.not()))}))
const viewers = canEdit.law('a viewer never edits',
({role}) => role.is('viewer'), out => out.allow.not())
const lockedDocs = canEdit.law('only the owner edits a locked document',
({role, locked}) => [locked, role.is('owner').not()], out => out.allow.not())
proveDecision(viewers) // {proven: true, checked: true}
proveDecision(lockedDocs) // {proven: true, checked: true}
canEdit.run({role: 'editor', locked: true}) // {allow: false}
An order with line items#
A request may carry a list. Folds run over it (a total, a taxable part), and a lemma about the folds is proven by induction over the lines when the rule is built, then used by every law.
import {bool, nat, proveRule, record, rule, when} from 'gdp-wired'
const Line = record({amount: nat, taxable: bool})
const invoices = rule('invoices')
.params({budget: nat})
.carriesList('Lines', Line, {
total: {from: 0, step: (acc, l) => acc.plus(l.amount)},
taxed: {from: 0, step: (acc, l) => acc.plus(when(l.taxable, l.amount, 0))},
}, {'the taxable part is within the total': ({taxed, total}) => taxed.atMost(total)})
.state({spent: 0, tax: 0})
.flags(({Lines, budget, spent}) => ({fits: spent.plus(Lines.fold.total).atMost(budget)}))
.step(({Lines, spent, tax, fits}) => ({
spent: when(fits, spent.plus(Lines.fold.total), spent),
tax: when(fits, tax.plus(Lines.fold.taxed), tax),
}))
const withinBudget = invoices.law('nothing is spent past the budget',
({spent, budget}) => spent.atMost(budget))
const taxWithin = invoices.law('the tax recorded is within what is spent',
({tax, spent}) => tax.atMost(spent))
proveRule(invoices, [withinBudget, taxWithin]) // {proven: true, checked: true}
More shapes, each in the README: sliding-window rate limits over a log of requests, sums over every
key of a map (M.sum()), laws over up to 64 steps in a row, “every ticket issued is eventually served”
(under weak or strong fairness), an implementation that behaves like a simpler spec, functions defined by a
recurrence with lemmas you state, and rules read from your own TypeScript.
What it proves ·
Writing laws