Overview
A Datalog program in Zodd consists of facts, rules, and optional
queries.
Lines starting with % are treated as comments and are ignored.
The words not, count, sum, min, and max
are reserved keywords.
Basic Elements
- Constants: integers (such as
68) or double-quoted strings (such as
"alice"). Bare lowercase identifiers (such as alice) are not valid
constants.
- Variables: identifiers starting with an uppercase letter or an underscore (such as
X, Y, or the wildcard _).
- Facts: assertions of the form
predicate(constants). representing base
data.
edge(1, 2).
likes("alice", "tea").
- Rules: statements of the form
head :- body., where the body is a
comma-separated list of subgoals (predicates applied to variables or constants) and optional
comparison filters.
path(X, Y) :- edge(X, Y).
path(X, Z) :- path(X, Y), edge(Y, Z).
- Queries: requests of the form
?- predicate(arguments).. When a query
is present, the engine returns only matching tuples; otherwise, all derived relations are returned.
?- path(1, X).
Negation
Negated subgoals use the not keyword. To ensure rule safety, every variable in a negated
subgoal must also appear in at least one positive subgoal within the same rule body.
allowed(R, P) :- has_perm(R, P), not denied(R, P).
Comparison Filters
Zodd supports the comparison operators <, <=, >, >=,
=, and !=. Every variable in a comparison must be bound by a positive subgoal
in the rule body, and wildcards are not allowed. Ordered comparisons compare integers; a string operand
will fail the comparison.
hop(X, Y) :- edge(X, Y, W), W < 60.
reach(X, Z) :- reach(X, Y), hop(Y, Z), X != Z.
Aggregates
Zodd supports aggregation functions in the heads of rules. The supported functions are count,
sum, min, and max. The argument to an aggregation function must
be a single variable.
out_deg(N, count(M)) :- hop(N, M).
fanout(P, count(D)) :- needs(P, D).