Metakit / @doeixd/metakit
What the example did
Expr.refinecompiles a symbolic expression into a real EffectSchemafilter and attaches its AST toSchemaASTannotations, soSchemadecode, JSON Schema, andArbitrarykeep working while static readers inspect the rule.Pathas a subtype ofExprlets you readstate.balanceinside expressions whileTransition.setrequires aPathas its write target.- Environment scoping (
Env) separates validity from authority at compile time:Account's invariant cannot reference aprincipalbecauseprincipalis not in itsEnv, while a guard built withExpr.in({ row: Account, principal: Principal }, ...)can. - Partial evaluation (
Expr.residualize,Expr.judge,Transition.can) folds known environment slices into a three-valuedVerdict(True,False, orUnknownwith a reason), keeping missing (Unknown) fields separate from runtimenull. - Escape hatch (
Expr.opaque) wraps arbitrary JavaScript with optional declaredreads; it runs inExpr.evaland degrades soundly toUnknownin static checkers.Transition.opaquedoes 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 assupplied.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)(alsoevery,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 andExpr.reads(which namestodos.*.done) see it. A list is written by a named way:Transition.insert(unless an element has the key),remove,removeWhere,patch,patchWhere,append, andincludeandexcludeover 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
commutesfrom@doeixd/metakit/pairs. It returnsTruewhen both orders give the same state, emitted records, and refusals. If it returnsUnknown, usewalkPairto test both orders for transitions in a machine. The same entry exportsidempotentto 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 asforeignand userewritePolicyto choose which declared laws these checks can use.