Skip to content

Expression Engine

Built-in predicates and functions.

The expression engine evaluates the predicates and functions that appear inside Datalog :where clauses. It is a row-at-a-time evaluator over a tree of expressions built from variables, literals and supported predicates and functions.

There are two ways to use an expression in a clause:

  • Predicates — a boolean expression that filters rows.
    [(< ?age 30)]
  • Function binding — an expression whose result is bound to a new variable.
    [(+ ?x 1) ?y]
    [(upper ?name) ?upper-name]

Predicates return a boolean and are used as filters.

PredicateSyntaxArgument types
<(< a b)any comparable, same kind
<=(<= a b)any comparable, same kind
>(> a b)any comparable, same kind
>=(>= a b)any comparable, same kind
=(= a b)any
not=, !=(not= a b)any
not(not x)boolean

Functions return a value and are used in a function binding to introduce a new variable.

FunctionSyntaxArgument typesResult
+(+ a b)long/doublelong/double
-(- a b)long/doublelong/double
*(* a b)long/doublelong/double
/, quot(/ a b)long/doublelong/double
mod(mod a b)long/doublelong/double
abs(abs x)long/doublelong/double

Notes:

  • Integer arithmetic uses checked operations — overflow makes the row drop out.
  • Division or modulo by zero is treated as no result rather than an error.
  • Mixed long/double arithmetic promotes both sides to double.
FunctionSyntaxArgument typesResult
concat(concat a b)string, stringstring
upper(upper s)stringstring
lower(lower s)stringstring
strlen(strlen s)stringlong
str(str x)long, bigint, double, float, boolean, string, uuidstring

Notes:

  • str falls back to a debug-style representation for any argument type not listed above.
FunctionSyntaxArgument typeResult
year(year t)instantlong
month(month t)instantlong
  • Expressions evaluate against a row’s variable bindings. An unbound variable, a type mismatch, or an arithmetic failure (overflow, divide-by-zero) all produce “no result” — the row is excluded from a predicate and produces no binding from a function.
  • Predicate evaluation returns true only when the expression yields boolean true; anything else is treated as false.
  • Expressions can be nested freely, for example:
    [(not (< ?x 10))]
    [(+ (* ?x 2) 10) ?y]