@tola/datalog

Evaluate stratified Datalog queries over site relations, facts, and derivation rules.

Import

typst
#import "@tola/datalog:0.0.0": *

Declare each relation (including empty ones) with relation, supply ground rows with .atom(...), derive new tuples with rule, and query the resulting database.

  • Range restriction: every variable used in a rule head, select, or negate(...) must also appear in at least one positive literal in the same rule or query.
  • Stratified negation: a relation may appear inside negate(...) only when its definition does not depend (directly or indirectly) on the head relation.
  • Deterministic ordering: query results are deduplicated and sorted column-by-column. Distinct types compare in fixed order: none, bool, int, finite float, decimal, str, bytes, datetime (numeric types such as 1 and 1.0 stay distinct).

Exports

relation

typc
let relation(
  name: str,
  arity: int,
) = datalog-relation;

Declare a named relation with a fixed number of columns.

Call .atom(...) on the returned relation to build facts or rule/query literals.

Positional parameters

  • name str required: Non-empty, case-sensitive relation name.
  • arity int required: Non-negative integer column count.

atom

r is the value returned by relation(...).

typc
r.atom(
  ..terms: any,
) -> any

Construct a fact row or query pattern matching this relation’s arity.

Rest parameters

  • ..terms any: One constant or var(...) per column; omit for zero-arity relations. Represent Typst content or functions by string IDs.

var

typc
let var(
  name: str,
) = any;

Create a logical variable scoped to a rule or query.

Positional parameters

  • name str required: Non-empty, case-sensitive variable name. Equal names within one rule or query refer to the same variable.

rule

typc
let rule(
  head: any,
  literal: any,
  ..rest: any,
) = any;

Derive head whenever every body literal holds.

Positional parameters

  • head any required: Derived atom produced when all conditions match.
  • literal any required: First body condition (a positive atom or negate(...)).

Rest parameters

  • ..rest any: Zero or more additional body conditions.

negate

typc
let negate(
  atom: any,
) = any;

Succeed when no row in the relation matches atom.

Positional parameters

  • atom any required: Atom to test for absence.

database

typc
let database(
  relations: array,
  facts: array = (),
  rules: array = (),
) = datalog-database;

Build a Datalog database from declared relations, ground facts, and rules.

Call db.query(select: (...), where: (...)) on the returned database. An empty select returns ((),) when the query holds and () otherwise; an empty where always holds.

Positional parameters

  • relations array required: Array of unique relation(...) declarations, including relations with no initial facts.

Named parameters

  • facts array default: (): Ground fact rows (constants only). Defaults to ().
  • rules array default: (): Derivation rules built with rule(...). Defaults to ().

query

d is the value returned by database(...).

typc
d.query(
  select: array,
  where: array,
) -> array

Return sorted, deduplicated result rows matching all conditions in where.

Named parameters

  • select array required: Array of var(...) values defining output columns (variables may repeat).
  • where array required: Array of positive atoms and negate(...) conditions that must all hold (() matches unconditionally).

Find reachable pages while excluding hidden ones

typst
#import "@tola/datalog:0.0.0": database, negate, relation, rule, var

#let edge = relation("edge", 2)
#let reach = relation("reach", 2)
#let hidden = relation("hidden", 1)
#let (x, y, z) = (var("x"), var("y"), var("z"))

#let db = database(
  (edge, reach, hidden),
  facts: (edge.atom("a", "b"), edge.atom("b", "c"), hidden.atom("b")),
  rules: (
    rule(reach.atom(x, y), edge.atom(x, y)),
    rule(reach.atom(x, z), reach.atom(x, y), edge.atom(y, z)),
  ),
)

#let visible = db.query(
  select: (z,),
  where: (reach.atom("a", z), negate(hidden.atom(z))),
)
#document("index.html")[Reachable: #visible.map(row => row.first()).join(", ")]

Search this site

Use the arrow keys to move through results.

Type to search.