Metakit / @doeixd/metakit

What the example did

  • Expr.refine compiles a symbolic expression into a real Effect Schema filter and attaches its AST to SchemaAST annotations, so Schema decode, JSON Schema, and Arbitrary keep working while static readers inspect the rule.
  • Path as a subtype of Expr lets you read state.balance inside expressions while Transition.set requires a Path as its write target.
  • Environment scoping (Env) separates validity from authority at compile time: Account's invariant cannot reference a principal because principal is not in its Env, while a guard built with Expr.in({ row: Account, principal: Principal }, ...) can.
  • Partial evaluation (Expr.residualize, Expr.judge, Transition.can) folds known environment slices into a three-valued Verdict (True, False, or Unknown with a reason), keeping missing (Unknown) fields separate from runtime null.
  • Escape hatch (Expr.opaque) wraps arbitrary JavaScript with optional declared reads; it runs in Expr.eval and degrades soundly to Unknown in static checkers. Transition.opaque does the same for a whole step, declared by the paths it reads and writes.
  • Supplied values are what a transition cannot make: Transition.make({ …, supplied: { now: Schema.Int } }) reads the time as supplied.now, and whoever runs the step passes it once (Transition.run(t, state, input, { now })), so the step stays a function of its arguments and a proof holds for every time.
  • Lists are read by asking a question of each element: Arr.any(state.todos, todo => todo.done) (also every, count, sum, min, max, find, keep). You write the test as an arrow over a path for the element; what is kept is the expression it returns, so SQL, the checker and Expr.reads (which names todos.*.done) see it. A list is written by a named way: Transition.insert (unless an element has the key), remove, removeWhere, patch, patchWhere, append, and include and exclude over a sorted set. A key no element has gives back the state it was given.
  • Commands as data: emits: ({ state }) => [Transition.emit('Saved', { id: state.id }, { when: … })] lists the records a step returns beside its state, read over the state before it; your runtime turns each into a Command.
  • Whether two transitions swap order. Import commutes from @doeixd/metakit/pairs. It returns True when both orders give the same state, emitted records, and refusals. If it returns Unknown, use walkPair to test both orders for transitions in a machine. The same entry exports idempotent to check whether repeating a transition on the result of the first run, with the same inputs and supplied values, leaves the same state and adds no records. For custom operations, pass implementations as foreign and use rewritePolicy to choose which declared laws these checks can use.