The API
Vex has three packages. @mark1russell7/vex has the kernel and the builder, and it has no runtime dependencies. @mark1russell7/vex-domains has the domains of the site. @mark1russell7/vex-testkit has the property-test tools.
The builder
Section titled “The builder”import { space, vex } from "@mark1russell7/vex";import { NumDomain, Vec2Domain } from "@mark1russell7/vex-domains";
const root = vex(Vec2Domain, NumDomain).over(space.record({ A, B, C }));const nearest = root.from("position").others((e) => e._.subtract("position")._.length()).min();
nearest.at("A"); // : Optional<number>nearest.result("A"); // : Result<number>nearest.explain("A"); // : Tracenearest.all(); // : Traversal<"A" | "B" | "C", number>nearest.program; // : Exprnearest.at("A"); // => some(5)vex(...domains) gives the entry point for a list of domains. The interpreter finds the op of a value in the first domain that accepts the value. vex(...).withOptions({ fns, extensions }) adds free functions and extension handlers. A chain applies a free function with call:
const withHalf = vex(NumDomain).withOptions({ fns: { half: (n: number) => n / 2 } }).over(space.record({ A }));
withHalf.from("weight").call("half").at("A"); // => some(1)withHalf.from("weight").call("half").at("A"); // : Optional<number>The root
Section titled “The root”| Member | What it gives |
|---|---|
from(field) |
A chain whose value is the field at the focus. The types accept only the fields of the records. |
start(value) |
A chain whose value is value. A string is a field reference. |
of(key, field) |
An argument: the field of the record at key |
ofPath<T>(key, path) |
An argument: a dotted path at key, with the type T that you give |
field<T>(path) |
An argument: a dotted path at the focus |
lit(value) |
An argument: a literal. Use it for a string or an array, because a bare string is a field reference. |
rec(fields) |
An argument: a record of argument values |
ext<T>(kind, data) |
An argument: the node ext(kind, data). The extension handler of kind gives its value. |
sheet() |
A sheet over the space: named columns of formulas. Refer to the next section. |
space |
The space of the root |
A chain
Section titled “A chain”| Member | What it does |
|---|---|
._.op(...args) |
Applies an op of the domain of the current value. The types list only the declared ops, with their parameter types. |
from(field) |
Starts a new value: the field at the current address |
to(key), index(i), offset(...d) |
Moves the address of the later field references and axes. to and index also take a chain, for a key or a position from the data. |
other() |
Moves to the other key of a pair. The types offer it only for a space with two keys. |
origin() |
Moves back to the origin |
parent() |
Moves to the parent of the focus, in a tree space |
others(body, { where }) |
The others axis. The body starts with the current value, and its field names read at each target. |
neighbors(n, body, { where }) |
The neighbors axis of a grid. n is 4 or 8. |
children, ancestors, descendants, siblings (each with (body, { where })) |
The axes of a tree space |
each(body, { where }) |
The all axis inside the expression |
with(binds, body) |
Binds names. The body gets a typed argument for each name. |
functions(fns, body) |
Binds functions of root.fn (FN.BIND). The body gets a typed call of each function. |
fork(branches) |
Evaluates each branch with the current value, and gives a record |
ifError(fallback) |
Gives fallback when the chain gives an error |
if(then, otherwise) |
For a boolean value: gives then or otherwise. Only the selected branch evaluates. |
iterate(limit, init, step, result, { until }) |
A loop of at most limit steps. limit is a number, dataLimit(axis) or a list chain. step and result get a chain for each name of init. |
fix(name, join, init, step) |
A column with a value for each row (BUILD.FIX). step gets a chain of the last round at the focus. join is or, and, max or min, and it gives the kind of the chain. |
call(name, ...args) |
Applies a free function of withOptions to the current value. The types accept only the functions whose first parameter accepts the value. |
at(key), value(key), result(key), explain(key), all() |
Evaluates the chain |
program |
The expression of the chain |
The moves before an axis are the address of the axis. For example, parent().children(body) evaluates body at each child of the parent:
const org = vex(NumDomain).over(space.tree({ ceo: { pay: 300 }, cto: { pay: 200 }, cfo: { pay: 190 } }, { cto: "ceo", cfo: "ceo" }));
org.start(0).parent().children((c) => c.from("pay")).sum().at("cto"); // => some(390)An axis gives a list chain. result(key), at(key) and explain(key) give the list at an origin: one item for each target, with its key and its result. A list chain has the reductions count, sum, mean, min, max, any, all, none, values, keys, first and reduce(op). sum, mean, min and max take only numbers, and reduce("max") takes the values of a domain, for example vectors. fold(init, step, result) is a loop over the items, in order. Each reduction takes { strict: true }. By default, a reduction skips error items. keys() gives the keys of the items with a value, and errors() gives the error of each item that failed.
A list chain also has the ops on its order: sortBy(), take(n) and nth(i). Its method each(body) goes over the items of the list, in their order. The body reads the fields of the record of each item.
const rows = vex(NumDomain).over(space.array([{ w: 3, n: 30 }, { w: 1, n: 10 }, { w: 2, n: 20 }]));const byW = rows.start(0).each((c) => c.from("w")).sortBy();
byW.each((t) => t.from("n")).values().at("0"); // => some([10, 20, 30])byW.nth(0).at("0"); // => some(1)The body of each of a list chain also gets a chain of the item and a chain of its index, from 0 (BUILD.LIST.ITEM). In fold, the step gets the index after the item. The builder writes the field as of the IR only for the names that the body or the step reads.
const rows = vex(NumDomain).over(space.array([{ w: 3 }, { w: 1 }, { w: 2 }]));const byW = rows.start(0).each((c) => c.from("w")).sortBy();
byW.each((_c, w, i) => w._.multiply(10)._.add(i)).values().at("0"); // => some([10, 21, 32])byW.each((_c, w) => w).fold({ s: 0 }, (x, w, i) => ({ s: x.s._.add(w._.multiply(i)) }), (x) => x.s).at("0"); // => some(8)A list chain also builds lists: drop(n), setNth(i, value), append(other), without(other) and flat(). The argument other is a list chain. The body of an axis can give a list chain. Then the value of each item is a list, and flat() joins these lists in order.
const rows = vex(NumDomain).over(space.array([{ n: 30 }, { n: 10 }, { n: 20 }]));const ns = rows.start(0).each((c) => c.from("n"));
ns.drop(1).append(ns.take(1)).values().at("0"); // => some([10, 20, 30])ns.without(ns.take(2)).keys().at("0"); // => some(["2"])rows.start(0).each((c) => c.window(0, 1, (t) => t.from("n"))).flat().values().at("0"); // => some([30, 10, 10, 20, 20])In iterate and fold, a name whose first value is a list chain is a list chain in each step (BUILD.LIST.LOOP). Its next value must be a list chain too. root.list(items) makes a list literal, and get(key) of a list chain gives the value at a key. The key is a key of the space or a chain of a text.
const rows = vex(NumDomain).over(space.array([{ n: 30 }, { n: 10 }, { n: 20 }]));const ns = rows.start(0).each((c) => c.from("n"));// A stack: each step removes the top item.const rest = rows.start(0).iterate(2, { stack: ns }, ({ stack }) => ({ stack: stack.drop(1) }), ({ stack }) => stack);
rest.keys().at("0"); // => some(["2"])ns.get("1").at("0"); // => some(10)rows.list([["2", 5]]).append(ns).get("2").at("0"); // => some(5)rows.list().keys().at("0"); // => some([])fix repeats its step until no row changes, and its value is the row of the focus (FIX.ROUNDS). The chain lastRound exists only in the step, so the types have no read outside a column. An axis moves the focus of the read. Thus lastRound goes inside the body of the axis.
const graph = vex(NumDomain, BoolDomain, TextDomain).over(space.graph({ a: { id: "a" }, b: { id: "b" }, c: { id: "c" } }, { a: ["b"], b: ["a"], c: ["a"] }));const isSource = graph.from("id")._.eq(graph.start(0).origin().from("id"));const reach = graph.start(0).fix("reach", "or", isSource, (lastRound) => graph.start(0).predecessors(() => lastRound).any());
reach.at("a"); // => some(true)root.fn(kinds, body) makes a function, and functions(fns, body) of a chain binds it (FN.BIND). Each kind is "number", "string", "boolean" or "any", and it gives the type of the chain of a parameter. The body of a function evaluates at the focus of each call.
const rows = vex(NumDomain).over(space.array([{ w: 3, n: 30 }, { w: 1, n: 10 }]));const scaled = rows.fn(["number"], (x) => x._.multiply(rows.start(0).from("w")));
rows.from("n").functions({ scaled }, (c, call) => call.scaled(c)).at("1"); // => some(10)The traversal
Section titled “The traversal”all() evaluates the chain at each key, and gives a traversal.
| Member | What it gives |
|---|---|
items |
The key and the result of each item |
get(key) |
The result at one key, as an Optional |
values(), keys(), errors() |
The values, the keys and the errors of the items |
count(), sum(), mean(), min(), max() |
A number, as an Optional |
any(), all(), none() |
A boolean, as an Optional |
reduce(op) |
The left fold with a domain op |
map(f) |
A new traversal. If f throws, the item gets the error #CALC!. |
fold(monoid, f) |
The fold of the values with a monoid |
strict() |
A traversal whose reductions give the first error |
Sheets
Section titled “Sheets”root.sheet() gives an empty sheet. A sheet is immutable: each call of column gives a new sheet.
| Member | What it does |
|---|---|
column(name, formula) |
Adds a column. The formula gets a root with cell, and gives a chain. |
declare<T>() |
Gives the types of columns before their formulas. A formula can then read the column itself or a later column with its type. |
r.cell(column, at) |
In a formula: an argument whose value is the column at the focus, at the key at, or at the address at, which can have computed moves |
result(k, column), at(k, column), explain(k, column) |
Evaluates one cell |
table() |
Evaluates each cell in key order, and gives one row for each key |
columns |
The program of each column, as JSON data |
A cell on a cycle of cell references gives #CYCLE!. Refer to sheets and cycles.
Spaces
Section titled “Spaces”space.record({ A: a, B: b }); // keys "A" and "B"space.array([a, b, c]); // keys "0", "1" and "2"space.grid([[a, b], [c, d]]); // keys "0,0", "0,1", "1,0" and "1,1"space.tree({ r: a, x: b, y: c }, { x: "r", y: "r" }); // keys "r", "x" and "y": x and y are the children of rspace.graph({ a, b, c }, { a: ["b"], b: ["c"], c: ["a"] }); // keys "a", "b" and "c", with edges in a cycleIn a tree space, offset(d) with one number moves among the siblings in key order. For example, offset(-1) goes to the previous sibling. The roots are the siblings of a root.
A space copies its input. Nothing changes a space after it is made.
Domains
Section titled “Domains”import { defineDomain, type Domain, type OpTable } from "@mark1russell7/vex";
export const MoneyDomain: Domain<"Money", Money, OpTable<Money, "add" | "times", "add">> = defineDomain({ name: "Money", is: (u: unknown): u is Money => u instanceof Money, fromScalar: (n: number): Money => new Money(n), valid: (m: Money): boolean => Number.isFinite(m.cents), ops: { add: { laws: ["commutative", "associative"], identity: () => Money.zero, liftScalar: true, params: ["domain"] }, times: { params: ["number"] }, },});| Field | Meaning |
|---|---|
name |
The name of the domain |
is |
The runtime test for the values. It must not accept a value of another kind. |
ops |
The ops. Each op can declare laws, identity, liftScalar, params, returns and fn. |
primitive |
"number", "string" or "boolean", if is accepts each value of that type and no other value |
methods |
With "all", each method of a value is an op. The default is "declared". |
fromScalar |
The lift from a number to a value |
valid |
A check of the values that ops give. A value that fails it gives #NUM!. |
show |
A short text for a value |
encode, decode |
The JSON form of a value, for literals in a serialized program |
An op without fn calls the method of the value with the same name. An op with fn calls the function: use it for values without methods, for example numbers.
The interpreter and its folds
Section titled “The interpreter and its folds”| Function | What it gives |
|---|---|
evaluate(expr, { space, origin, domains, fns, extensions, vars }) |
A Result. It does not throw. expr can also be a document. |
compile(expr, { domains, fns, extensions }) |
A program with run({ space, origin, vars }) and explain. One program runs at each origin of each space. evaluate uses it too. |
explain(expr, options) |
A trace: one event for each node, with the focus, the reads and the result |
deps(expr) |
Each field read of the expression, with its address and the axes around it |
serialize(expr, domains), parse(text, domains) |
The JSON text of an expression, and the expression of a JSON text. The text can also be a document. |
toDocument(expr, domains), fromDocument(doc, domains), isDocument(value) |
The document of an expression in coeval, the expression of a document, and the test for a document. Refer to documents. |
kernelPlan(source, { domains, fns, extensions }) |
The reference tier: a plan of the kernel of coeval. run({ space, origin, vars, tables }) gives a result of Vex, and evaluate gives the result of the kernel. reference reads the document with the reference interpreter of the kernel, which compiles nothing. agree(options, tier) compares the tiers of the kernel and your tier (EVAL.AGREE). |
VEX_WORLD, vexMachine(options), vexChecker(), vexDefinitions(options), vexSelf(space, origin, tables) |
The contracts of the kinds of Vex as data, the machine of the kernel that evaluates the documents of Vex, and the machine of the checks, without the code of the evaluators. Also the definitions of the formula kit for the reference interpreter of the kernel, and the position that is the root self of an evaluation. |
toVexError(error), toKernelError(error), toVexResult(result) |
The conversions between the errors of the kernel and the errors of Vex |
monotoneError(expr, options) |
The reason that the check of fix refuses a program, or undefined (FIX.MONOTONE) |
The IR
Section titled “The IR”lit, ref, app, let_, v, fn, call, rec, each, fold, iterate, fix, last and ext make expressions. key, index, other, offset, origin and parent make moves. axes.all, axes.others, axes.other, axes.neighbors(n), axes.children, axes.ancestors, axes.descendants, axes.siblings, axes.reverse(axis), axes.list(expr) and axes.where(axis, test) make axes. dataLimit(axis) makes a limit of iterate from the size of the data. Refer to the IR.
Results
Section titled “Results”Result<T> is { ok: true, value } or { ok: false, error }. Optional<T> is { tag: "some", value } or { tag: "none" }. It is the Optional of the family, from @mark1russell7/optional, so the values of render and Vex are the same type. The helpers are ok, fail, some, none, isSome, isNone, toOptional, getOr, mapResult and chainResult. An error has a code, a kind, a message and the path of its node. Refer to the error codes.
The test kit
Section titled “The test kit”| Function | What it does |
|---|---|
checkLaws(domain, { arb, eq, numRuns }) |
Tests each declared law with fast-check, and gives one result for each law |
assertLaws(domain, options) |
Throws an error that lists each failed law |
arbSpace(value), arbExpr(options), arbOrigin(space) |
Arbitraries for random spaces and programs |
referenceEvaluate(expr, options) |
A short reference interpreter, for differential tests |
fromCore, fromReference |
Comparable forms of the two outcomes |
A domain author checks the laws of a new domain in one line:
expect(checkLaws(MoneyDomain, { arb: fc.integer().map((n) => new Money(n)) }).filter((r) => !r.ok)).toEqual([]);