Metakit / @doeixd/metakit

Node

namespace · packages/metakit/src/node.ts:12

export * as Node from './node.js'

The seven kinds of node every MetaKit expression is made of, and the closed set of 50 primitive operators (CoreOpId).

Every reader's table (sqlOps, abstractOps, smtOps, opProperties) is typed by CoreOpId, and coreOps (from @doeixd/metakit/build) holds the operator descriptors under the same type, so adding a primitive fails to compile until every reader implements it. Derived helpers (min, max, abs, clamp, between, isNotNull, implies) are plain TypeScript functions over If and the primitives and need no table entry.

Members

Node.AnyOp

type · packages/metakit/src/nodeOps.ts:266

type AnyOp = Op | ForeignOp

An operator a Call may name: a core one, which carries its evaluator, or a foreign one.

Node.apply

function · packages/metakit/src/nodeOps.ts:346

function apply(op: Op, args: ReadonlyArray<unknown>): unknown

op applied to argument values under its null rule; an absent value is null.

Node.applying

function · packages/metakit/src/nodeOps.ts:350

function applying(
  op: AnyOp,
  args: ReadonlyArray<unknown>,
  run: (values: ReadonlyArray<unknown>) => unknown,
): unknown

run over argument values under op's null rule; an absent value is null.

Node.Binder

interface · packages/metakit/src/nodeTree.ts:17

interface Binder {
  readonly slot: symbol
}

The identity a fold or a list write gives the element it binds. A bound Path names it, so no field name of the environment is reserved and nothing is renamed when terms compose; a reader holds the element under slot, which no path a Schema field spells can reach.

Node.binder

function · packages/metakit/src/nodeTree.ts:22

function binder(): Binder

A new binder, distinct from every other.

Node.Call

interface · packages/metakit/src/nodeTree.ts:36

interface Call {
  readonly _tag: 'Call'
  readonly op: AnyOp
  readonly args: ReadonlyArray<Node>
}

An operator applied to arguments.

Node.ClaimSide

type · packages/metakit/src/nodeOps.ts:182

type ClaimSide = 'left' | 'right' | 'both'

Side an identity or absorbing element applies on.

Node.CoreOpId

type · packages/metakit/src/nodeOps.ts:7

type CoreOpId =
  | 'bool.and'
  | 'bool.or'
  | 'bool.not'
  | 'eq.is'
  | 'eq.in'
  | 'eq.equals'
  | 'eq.among'
  | 'null.isNull'
  | 'null.coalesce'
  | 'str.lowerAscii'
  | 'str.upperAscii'
  | 'str.trim'
  | 'str.concat'
  | 'str.lt'
  | 'str.lte'
  | 'str.gt'
  | 'str.gte'
  | 'ord.lt'
  | 'ord.lte'
  | 'ord.gt'
  | 'ord.gte'
  | 'num.add'
  | 'num.sub'
  | 'num.mul'
  | 'num.neg'
  | 'num.mulScaled'
  | 'num.div'
  | 'num.rem'
  | 'time.addDays'
  | 'time.addMonths'
  | 'time.daysBetween'
  | 'time.startOfMonth'
  | 'time.overlaps'
  | 'str.len'
  | 'str.startsWith'
  | 'str.endsWith'
  | 'str.contains'
  | 'arr.count'
  | 'arr.isEmpty'
  | 'arr.has'
  | 'arr.append'
  | 'arr.include'
  | 'arr.exclude'
  | 'arr.move'
  | 'arr.at'
  | 'arr.with'
  | 'set.union'
  | 'set.intersect'
  | 'set.difference'
  | 'rec.make'

The closed set of 50 primitive operators.

Node.element

function · packages/metakit/src/nodeTree.ts:227

function element(env: unknown, binder: Binder): unknown

The element binder holds in env, which a bound path reads; throws where the environment holds none, as for a reference outside every fold that binds it (a malformed term).

Node.escaped

function · packages/metakit/src/nodeTree.ts:231

function escaped(): Error

The error of a bound read outside every fold or write that binds it.

Node.failed

function · packages/metakit/src/nodeOps.ts:338

function failed(op: ForeignOp, error: unknown): Refusal

The refusal of a call whose implementation threw, with what it threw.

Node.Fold

interface · packages/metakit/src/nodeTree.ts:138

interface Fold {
  readonly id: RowsFold
  readonly evaluate: (
    rows: Rows,
    source: unknown,
    env: unknown,
    at: (node: Node, env: unknown) => unknown,
    foreign?: ForeignImplementations,
  ) => unknown
  readonly judge: <T>(
    rows: Rows,
    partialEnv: unknown,
    reader: RowsReader<T>,
    lookup: (segments: ReadonlyArray<string>) => Option.Option<unknown>,
    foreign?: ForeignImplementations,
  ) => T
}

The question a Rows node asks, as a Call holds its Op: its name, by which every static reader's table is keyed, and the reference reading of it, concrete evaluation and partial judgement. A page carries only the folds it builds.

Node.foreignAllowlist

function · packages/metakit/src/nodeOps.ts:293

function foreignAllowlist(
  ops: ReadonlyArray<Pick<ForeignOp, 'id' | 'version'>>,
): ReadonlySet<string>

The names of ops, as isForeignAllowed tests them.

Node.ForeignClaims

interface · packages/metakit/src/nodeOps.ts:188

interface ForeignClaims {
  readonly identity?: {
    readonly value: unknown
    readonly side: ClaimSide
  }
  readonly absorbing?: {
    readonly value: unknown
    readonly result: unknown
    readonly side: ClaimSide
  }
  readonly idempotent?: true
  readonly commutative?: true
  readonly associative?:
    | true
    | {
        readonly unlessRefused: true
      }
  readonly invertible?: {
    readonly inverse: ForeignOp | 'self'
  }
  readonly monotone?: ReadonlyArray<MonotoneDirection>
  readonly total?: true
}

Algebraic claims carried on a ForeignOp descriptor.

Node.ForeignDefinition

interface · packages/metakit/src/nodeOps.ts:243

interface ForeignDefinition {
  readonly params: ReadonlyArray<readonly [name: string, schema: Schema.Top]>
  readonly result: Schema.Top
  readonly where: ReadonlyArray<Node>
  readonly obligations: ReadonlyArray<{
    readonly when: Option.Option<Node>
    readonly expr: Node
    readonly schema: Option.Option<Schema.Top>
    readonly ensures: ReadonlyArray<Node>
  }>
  readonly body: Node
  readonly ensures: ReadonlyArray<Node>
}

A derived operation's function, carried on its descriptor so every call reaches it. Its definition hash (Artifact.operationFingerprint) covers all but the ensures parts, which its contract hash adds (Artifact.operationHashes).

Node.ForeignImplementation

interface · packages/metakit/src/nodeTree.ts:64

interface ForeignImplementation {
  readonly run: (
    args: ReadonlyArray<unknown>,
    foreign?: ForeignImplementations,
  ) => unknown
}

One implementation of a foreign operation.

Node.ForeignImplementations

type · packages/metakit/src/nodeTree.ts:76

type ForeignImplementations = Readonly<Record<string, ForeignImplementation>>

The implementations a reader is given, by id@version (Node.foreignName), so two versions of one operation each have theirs.

Node.foreignName

function · packages/metakit/src/nodeOps.ts:289

function foreignName(op: Pick<ForeignOp, 'id' | 'version'>): string

A foreign operation's name in every reader's table of implementations: id@version, so two versions of one operation are two entries.

Node.ForeignOp

interface · packages/metakit/src/nodeOps.ts:216

interface ForeignOp {
  readonly _tag: 'Foreign'
  readonly id: string
  readonly version: number
  readonly returns: ValueKind
  readonly nulls: 'strict' | 'own'
  readonly args?: ReadonlyArray<Schema.Top>
  readonly result?: Schema.Top
  readonly checkArgs?: ReadonlyArray<ForeignSchemaCheck>
  readonly checkResult?: ForeignSchemaCheck
  readonly claims: ForeignClaims
  readonly definition?: ForeignDefinition
}

An operation an application defines, outside the closed set: carried by its id and version, with no evaluator, since a page and a server may hold different implementations of it or none. Each reader takes the implementations from its caller, and refuses a call it holds none for.

Node.ForeignOperation

interface · packages/metakit/src/nodeOps.ts:301

interface ForeignOperation {
  readonly descriptor: ForeignOp
}

An operation as Operation.define returns it, named by the descriptor it holds: what a table of [operation, value] pairs names each entry by.

Node.ForeignSchemaCheck

interface · packages/metakit/src/nodeOps.ts:205

interface ForeignSchemaCheck {
  readonly is: (value: unknown) => boolean
  readonly describe: string
}

A precompiled Schema guard carried on a ForeignOp for runtime domain enforcement.

Node.foreignTable

function · packages/metakit/src/nodeOps.ts:309

function foreignTable<V>(
  pairs: ReadonlyArray<readonly [ForeignOperation, V]>,
  table: string,
): ReadonlyMap<string, V>

The entries of a list of [operation, value] pairs by id@version. An operation listed twice throws, naming table.

Node.held

function · packages/metakit/src/nodeTree.ts:217

function held(env: unknown, binder: Binder): Option.Option<unknown>

The element a reader holds in env for binder, where it holds one.

Node.holdsNul

function · packages/metakit/src/nodeOps.ts:119

function holdsNul(values: ReadonlyArray<unknown>): boolean

Whether one of values (or a nested array candidate) is text holding a NUL character.

Node.If

interface · packages/metakit/src/nodeTree.ts:43

interface If {
  readonly _tag: 'If'
  readonly cond: Node
  readonly then: Node
  readonly otherwise: Node
}

A two-way choice on a boolean; a null condition gives null.

Node.illTyped

function · packages/metakit/src/nodeOps.ts:97

function illTyped(at: string, expected: string, value: unknown): Refusal

The refusal of an operand that is not of the operator's type.

Node.isForeign

function · packages/metakit/src/nodeOps.ts:269

function isForeign(op: AnyOp): op is ForeignOp

Whether op is a foreign operation, which carries no evaluator.

Node.isForeignAllowed

function · packages/metakit/src/nodeOps.ts:328

function isForeignAllowed(
  name: string,
  allowlist: ReadonlySet<string> | undefined,
): boolean

Whether a foreign operation's id@version name is permitted by allowlist. Matches exact id@version names; undefined permits every operation.

Node.isRefusal

function · packages/metakit/src/nodeOps.ts:94

function isRefusal(value: unknown): value is Refusal

Node.key

function · packages/metakit/src/nodeKey.ts:56

function key(node: Node): string

A string equal for two nodes exactly when they are the same term: the key by which partial evaluation removes duplicates and the checkers name terms. Two opaque nodes are one term only when they share their function.

A bound read is written as its distance, ^n, the folds between it and the one that binds it, so two folds that differ only in their binder have one key wherever they sit; a read whose binder is outside node is ^#id, a value of this process, as an opaque node's key is.

Node.keyWithin

function · packages/metakit/src/nodeKey.ts:70

function keyWithin(node: Node, scope: ReadonlyArray<Binder>): string

node's key as a part of a term whose folds around it bind scope, innermost last.

Node.Lit

interface · packages/metakit/src/nodeTree.ts:6

interface Lit {
  readonly _tag: 'Lit'
  readonly value: unknown
}

A constant.

Node.mapPaths

function · packages/metakit/src/nodePaths.ts:99

function mapPaths(node: Node, path: (node: PathNode) => Node): Node

Every read of the environment in node passed through path; a bound read is left as it is. path is asked once per Path node object, and a subterm node shares is shared in the result.

Node.mapRows

function · packages/metakit/src/nodeTree.ts:186

function mapRows(
  rows: Rows,
  source: (node: Node) => Node,
  body: (node: Node) => Node,
): Rows

rows with its source passed through source and each node of its body through body.

Node.Match

interface · packages/metakit/src/nodeTree.ts:56

interface Match {
  readonly _tag: 'Match'
  readonly on: Node
  readonly cases: ReadonlyArray<readonly [value: string, then: Node]>
  readonly otherwise: Option.Option<Node>
}

A choice on a value among literal cases. With no otherwise, the cases cover every value the Schema of on allows and a null on gives null; with one, it takes null and every value no case names. A subject holding a NUL is refused (unportable).

Node.MonotoneDirection

type · packages/metakit/src/nodeOps.ts:185

type MonotoneDirection = 'up' | 'down' | 'none'

Monotonicity direction for one argument of a custom operation.

Node.Node

type · packages/metakit/src/nodeTree.ts:177

type Node = Lit | PathNode | Call | If | Match | Opaque | Rows

Node.Op

interface · packages/metakit/src/nodeOps.ts:159

interface Op<Id extends CoreOpId = CoreOpId> {
  readonly _tag?: undefined
  readonly id: Id
  readonly returns: ValueKind | 'operand'
  readonly nulls: 'strict' | 'own'
  readonly refuses?: (value: unknown) => Refusal | undefined
  readonly evaluate: (args: ReadonlyArray<unknown>) => unknown
}

An operator's descriptor: its id and its in-memory evaluator. A Call holds the descriptor itself, so a page that uses one operator ships only that operator's evaluator. What it keeps is in opProperties.

Node.Opaque

interface · packages/metakit/src/nodeTree.ts:83

interface Opaque {
  readonly _tag: 'Opaque'
  readonly name: string
  readonly reads: Option.Option<ReadonlyArray<ReadonlyArray<string>>>
  readonly run: (env: unknown, foreign: ForeignImplementations) => unknown
}

Arbitrary code no reader can see into. It declares what it reads (none: everything), runs in evaluation with the caller's foreign implementations, and leaves every static reader undecided.

Node.OpId

type · packages/metakit/src/nodeOps.ts:59

type OpId = CoreOpId

Node.PathNode

interface · packages/metakit/src/nodeTree.ts:28

interface PathNode {
  readonly _tag: 'Path'
  readonly segments: ReadonlyArray<string>
  readonly ast: Option.Option<SchemaAST.AST>
  readonly bound?: Binder | undefined
}

A read at segments, typed by the Schema it was built from: of the environment, or, where bound is set, of the element that binder holds.

Node.prefix

function · packages/metakit/src/nodePaths.ts:57

function prefix(node: Node, segments: ReadonlyArray<string>): Node

Prefix every read of the environment (and Opaque read/env) inside node with segments. A subterm node shares is shared in the result.

Node.Refusal

class · packages/metakit/src/nodeOps.ts:76

class Refusal extends Data.TaggedError('Refusal')<{
  readonly reason:
    | 'unsafe-integer'
    | 'non-finite'
    | 'ill-typed'
    | 'unmatched'
    | 'unportable'
    | 'unsupported'
    | 'failed'
  readonly at: string
  readonly detail: string
}> {}

Evaluation declined to answer: an integer result left the safe range (unsafe-integer), a number was NaN or infinite (non-finite), an operand was not of the operator's type (ill-typed), a Match met a value none of its cases names (unmatched), a text, equality or Match operation met text holding a NUL character, which Postgres cannot store (unportable), a foreign operation had no implementation (unsupported), or the one given threw (failed). A refusal is not a value: it decides before null, and an operator given a refused operand is refused, as a database refuses a statement it cannot run before any operator answers. Only what needs an operand conditionally passes one by: null.coalesce answers its first operand where it has a value, If and Match evaluate only the branch taken, and an any (every) fold is true (false) once one element settles it, as SQL's EXISTS is. A predicate that is refused does not hold.

Node.refusedForEquality

function · packages/metakit/src/nodeOps.ts:140

function refusedForEquality(at: string, value: unknown): Refusal | undefined

The refusal of an operand an equality refuses whatever the others hold: text holding a NUL, or NaN; candidates are looked into.

Node.relatedSegment

function · packages/metakit/src/nodeTree.ts:122

function relatedSegment(relation: string): string

The segment under a row at which the rows pointing at it through relation are held.

Node.RESERVED_PREFIXES

const · packages/metakit/src/nodeOps.ts:272

const RESERVED_PREFIXES: readonly [
  'bool.',
  'eq.',
  'null.',
  'ord.',
  'num.',
  'time.',
  'str.',
  'arr.',
  'set.',
  'rows.',
]

The prefixes of the core operators' ids, which a foreign operation may not take.

Node.Rows

interface · packages/metakit/src/nodeTree.ts:164

interface Rows {
  readonly _tag: 'Rows'
  readonly fold: Fold
  readonly over: RowsOver
  readonly where: Option.Option<Node>
  readonly value: Option.Option<Node>
  readonly set: ReadonlyArray<readonly [field: string, value: Node]>
  readonly op?: ForeignOp | undefined
  readonly binder: Binder
}

One question asked of each element of a list, or each related row: whether any or every one passes where, how many do, the sum, least or greatest of value over those that do, the first that does (find), the ones that do (keep), or each that does changed by set (patch). where, value and set read the element through paths bound to binder; an element passes only where where is true.

Node.rowsBody

function · packages/metakit/src/nodeTree.ts:210

function rowsBody(rows: Rows): ReadonlyArray<Node>

The nodes of a Rows body: its test, its value and what set writes.

Node.RowsFold

type · packages/metakit/src/nodeTree.ts:91

type RowsFold =
  | 'any'
  | 'every'
  | 'count'
  | 'sum'
  | 'min'
  | 'max'
  | 'reduce'
  | 'find'
  | 'keep'
  | 'patch'

The question a Rows node asks of each element.

Node.RowsOver

const · packages/metakit/src/nodeTree.ts:119

const RowsOver: Data.TaggedEnum.Constructor<
  | { readonly _tag: 'List'; readonly list: Node }
  | {
      readonly _tag: 'Related'
      readonly of: Node
      readonly relation: string
      readonly cardinality?: 'one' | 'optional' | 'many'
    }
>

What a Rows node asks its question of: the elements of a list the environment holds (List), or the rows of another kind pointing at the row of reads through relation (Related), held under that row at the segment <relation> as { rows, whole }, where whole says whether these are all of them.

Node.RowsReader

interface · packages/metakit/src/nodeTree.ts:125

interface RowsReader<T> {
  readonly judge: (node: Node, env: unknown) => T
  readonly known: (judged: T) => Option.Option<unknown>
  readonly value: (value: unknown) => T
  readonly kept: (rows: Rows, source: T, outer: unknown) => T
}

How a reader evaluates a body, shows a value, and keeps a Rows node it cannot decide.

Node.rowsSource

function · packages/metakit/src/nodeTree.ts:182

function rowsSource(over: RowsOver): Node

The node a Rows node asks its question of: the list, or the row the related rows point at.

Node.substitutePaths

function · packages/metakit/src/nodePaths.ts:103

function substitutePaths(
  node: Node,
  replace: (segments: ReadonlyArray<string>) => Option.Option<Node>,
): Node

Substitute matching Path nodes using replace, asked once per Path node object; sharing is kept.

Node.textIdentity

function · packages/metakit/src/nodeOps.ts:115

function textIdentity(value: unknown): unknown

A value as every reader compares it: text as a database holds it (wellFormed), so '\uD800' and '�' are one text to equality, a Match, the checker and the solver alike; any other value as it is.

Node.unbound

function · packages/metakit/src/nodeTree.ts:235

function unbound(node: Node): Option.Option<Binder>

The binder of a read in node that no fold or write inside node holds: none where node is closed.

Node.unordered

function · packages/metakit/src/nodeOps.ts:133

function unordered(at: string): Refusal

The refusal of NaN by an operation that orders or compares numbers, as the databases part over it.

Node.unportable

function · packages/metakit/src/nodeOps.ts:125

function unportable(at: string): Refusal

The refusal of text holding a NUL character, which every text, equality and Match operation gives.

Node.unsupported

function · packages/metakit/src/nodeOps.ts:334

function unsupported(op: ForeignOp, detail?: string): Refusal

The refusal of a call of an operation the reader holds no implementation of.

Node.ValueKind

type · packages/metakit/src/nodeOps.ts:152

type ValueKind = 'number' | 'boolean' | 'text'

What an operator's result is, which decides the properties that may return an operand of it.

Node.wellFormed

function · packages/metakit/src/nodeOps.ts:108

function wellFormed(text: string): string

text as SQLite's and Postgres's drivers hold it: each lone surrogate as U+FFFD.