Expand description
§datalog-structural-model
Typed Datalog rule programs, validation, and stratification analysis.
datalog-structural-model is the finite rule-program layer between a rule authoring or
compiler front-end and an evaluator. It models predicates, variables, terms,
atoms, rules, filters, bindings, and aggregates, then validates safety and
computes rule strata over complete tuple patterns for programs with negation.
It does not read input data, execute rules, retain a fact store, or collect
solutions. Evaluation engines can consume a validated Program while keeping
facts and derived results in their own asynchronous, backpressured, and
spill-capable data paths.
§A small rule program
use datalog_structural_model::{
Atom,
ConstantSymbol,
Literal,
PredicateSymbol,
Program,
Rule,
Term,
VariableSymbol,
};
let person = PredicateSymbol::new("person");
let ancestor = PredicateSymbol::new("ancestor");
let x = VariableSymbol::new("x");
let y = VariableSymbol::new("y");
let z = VariableSymbol::new("z");
let parent_xy = Atom {
predicate: PredicateSymbol::new("parent"),
terms: vec![Term::Variable(x.clone()), Term::Variable(y.clone())],
};
let ancestor_yz = Atom {
predicate: ancestor.clone(),
terms: vec![Term::Variable(y), Term::Variable(z.clone())],
};
let ancestor_xz = Atom {
predicate: ancestor,
terms: vec![Term::Variable(x), Term::Variable(z)],
};
let program = Program {
rules: vec![Rule::new(
ancestor_xz,
vec![Literal::Positive(parent_xy), Literal::Positive(ancestor_yz)],
)],
};
program.validate().expect("the rule is safe and stratifiable");
assert_eq!(person.as_str(), "person");
assert_eq!(ConstantSymbol::new("example").as_str(), "example");The rule model is deliberately separate from execution. A caller may build a program in memory because a rule program is a finite description, but the facts matched by that program are not part of this crate and must not be materialized here.
§Validation and stratification
Program::validate checks range restriction, variable safety, supported
expression structure, and the absence of cycles through negation. A valid
program can be passed to a forward or backward evaluator. Program::stratify
exposes the calculated strata when an evaluator needs to schedule them.
The dependency graph uses strongly connected components and preserves the distinction between positive and negative dependencies. A recursive positive cycle is valid; a cycle that contains a negated dependency is rejected.
§Error locations and codes
Every validation error that concerns a location names it with typed
positions: a RuleIndex, a RuleSite (head or body), a LiteralPath (the
body literal, then one step per enclosing aggregate), and a TermPath (an
AtomSlot or ExpressionTermIndex, then one argument slot per nested triple
term or Skolem call). A cycle through negation or aggregation reports an
UnstratifiableCycle: the rules and predicates of the component plus the
offending StrictDependency — producer rule, consumer rule, consumer literal,
and whether the edge is negation or aggregation.
Each error variant has a stable kebab-case ErrorCode (error.code(), also
available as CODE_* associated constants), such as unsafe-negation or
unstratifiable-cycle. Codes are pinned by a census test and do not change
when a variant is renamed.
use datalog_structural_model::{
Atom, Literal, LiteralIndex, LiteralPath, PredicateSymbol, Program,
ProgramValidationError, Rule, RuleIndex, Term, VariableSymbol,
};
let x = Term::Variable(VariableSymbol::new("x"));
let y = Term::Variable(VariableSymbol::new("y"));
let atom = |p: &str, terms: Vec<Term>| Atom { predicate: PredicateSymbol::new(p), terms };
let error = Program::try_new(vec![Rule::new(
atom("lonely", vec![x.clone()]),
vec![
Literal::Positive(atom("person", vec![x.clone()])),
Literal::Negative(atom("knows", vec![x, y])),
],
)])
.unwrap_err();
assert_eq!(error.code().as_str(), "unsafe-negation");
let ProgramValidationError::UnsafeNegation { rule, literal, .. } = error else {
unreachable!()
};
assert_eq!(rule, RuleIndex::new(0));
assert_eq!(literal, LiteralPath::top(LiteralIndex::new(1)));§Serialization and checked loading
All public semantic data supports Serde directly, including symbols, nested terms, expressions, aggregates, rules, programs, profiles and diagnostics. JSON and Postcard are exercised; the model does not use a JSON intermediate. Symbols serialize as exact text. Cached hashes and allocation identity are rebuilt through the symbol constructors and never appear in serialized data.
Program::try_new and Rule::try_new validate checked construction. Their
Serde inputs use those same paths: unsafe variables, invalid Skolem placement,
excessive term nesting and cycles through negation fail on input. Standalone
Term and GroundTerm decoding checks context-free structure; rule position
and variable binding still require program validation. Raw public fields remain
available for authoring invalid source descriptions and reporting their errors;
those descriptions cannot bypass checked loading into executable programs.
Unknown semantic fields and enum variants are rejected.
use datalog_structural_model::{
Atom, ConstantSymbol, DatalogProfile, PredicateSymbol, Program, Rule, Term,
};
let atom = Atom {
predicate: PredicateSymbol::new("person"),
terms: vec![Term::Constant(ConstantSymbol::new("alice"))],
};
let program = Program::try_new(vec![Rule::positive(atom, vec![])])?;
let profile = DatalogProfile::positive();
program.validate_for(&profile)?;
let bytes = postcard::to_allocvec(&program)?;
let loaded: Program = postcard::from_bytes(&bytes)?;
loaded.validate_for(&profile)?;
assert_eq!(program, loaded);Serialized forms are pinned by exact JSON and Postcard golden files under
tests/golden; regenerate them deliberately with
UPDATE_GOLDEN=1 cargo test --test golden_corpus.
Stratification results serialize as reports. Recompute stratification from the
validated program when scheduling execution; loading a report does not certify
an executable plan. Serde encodings are neither canonical hashes nor RDF concrete
syntaxes. Callers own decoding resource budgets; this crate owns structural and
program-safety validation. The supported triple-term nesting depth remains
MAX_TRIPLE_TERM_NESTING.
§Triple-term adapter conventions
Term::TripleTerm contains a generic structured Atom. The RDF reasoning
catalogue’s convention uses a fixed urn:rdf-reasoning:triple relation with
three ordered operands [subject, predicate, object], recursively. The RDF
predicate can be a bound variable because it is an operand, distinct from the
fixed Datalog relation symbol. A two-operand atom with its RDF predicate in the
relation symbol is a different structure; the model does not translate it.
Checked construction and nested JSON/Postcard preserve all three operands and
reject unbound variables, including a nested predicate variable. Programs need
the explicit TripleTerms capability. These checks do not implement term
construction or unification, validate RDF term domains/scope, or certify an
entailment regime. Adapters own those contracts. Display text is not identity.
§Explicit Datalog language profiles
DatalogProfile::positive() describes positive flat Datalog without extension
operators. DatalogProfile::stratified() adds stratified negation. Custom profiles
pair an explicit DatalogCapabilities set with an equality validation regime.
Program::required_capabilities() inspects the entire program, including nested
bindings, filters, triple terms, Skolem terms and aggregate inner bodies. Filter,
binding and aggregate requirements identify exact operations, rather than broad
“supports expressions” flags. Program::validate_for rejects unsupported
features with a typed missing-capability set, and retains the existing rejection
of negation/aggregation under equality rewriting.
A capability declaration describes a contract, not proof of engine support.
DatalogCapabilities::all_model_features() lists what the IR can represent;
an engine must independently declare and test what it implements. Forward and
backward execution remain separate from the language profile.
RDFS, OWL 2 RL/RDF, Horst and vendor-compatible entailment presets belong in RDF reasoning rule packages. They can consume this model without adding OWL or backend dependencies to the generic Datalog layer. This crate does not yet provide those RDF rules or enable new inference modes in Python packages.
§Scope
This crate contains rule-program data structures and pure validation only. It does not provide a parser or CST for a Datalog source language, and it does not define an execution runtime. Those are separate layers so that parsers can stream source tokens and evaluators can stream facts and results without turning this IR crate into an in-memory data engine.
§License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option. Typed Datalog rule programs and stratification analysis.
Structs§
- Aggregate
Body - An aggregation sub-query inside a rule body.
- Atom
- Datalog atom:
predicate(term_1, ..., term_n). - Atom
Slot - Position of a term in an atom, triple term, or Skolem argument list.
- Constant
Symbol - Constant symbol used in Datalog terms.
- Datalog
Capabilities - Language feature set. This describes a contract; it does not certify an engine.
- Datalog
Profile - Explicit language features and equality regime selected for a program. This is neither an RDF entailment profile nor an execution strategy.
- Error
Code - Stable machine-readable identifier of an error variant.
- Expression
Term Index - Position of a top-level term inside a filter or BIND expression, in left-to-right source order.
- Ground
Atom - Ground form of an
Atom— the value form with no variables anywhere in the argument tree. Used insideGroundTerm::TripleTermso quoted-triple facts can be stored alongside flat facts in a fact set (TT1). - Literal
Index - Position of a literal in a rule body or an aggregate’s inner body.
- Literal
Path - Path from a rule body to one literal: the outer body position, then one inner-body position per enclosing aggregate.
- Predicate
Symbol - Predicate symbol in a Datalog atom.
- Program
- Datalog program as an ordered set of rules.
- Rule
- Datalog rule:
head :- body_1, ..., body_n. - Rule
Index - Position of a rule in
Program::rules. - Stratification
- Rule-level stratification of a finite Datalog program.
- Stratum
- A set of rules evaluated together to a fixed point.
- Strict
Dependency - A producer-to-consumer dependency that requires a strictly earlier producer stratum.
- Term
Path - Path to a term: its root, then one argument slot per nested triple term or Skolem call descended into.
- Unstratifiable
Cycle - A strongly connected rule component containing a strict dependency.
- Unsupported
Capabilities - Features required by the program and absent from the selected profile.
- Variable
Symbol - Variable symbol used in Datalog terms.
Enums§
- Aggregator
- SQL/SPARQL aggregator vocabulary supported in rule bodies.
- Bind
Expr - Expression carried by a
Literal::Bindbody literal. - Binding
Kind - An operation represented by
BindExpr, independent of evaluator support. - Builtin
Filter - Built-in filter expression.
- Datalog
Feature - A language feature required by a rule program. Positive flat atoms require no extension capabilities.
- Equality
Mode - Equality-reasoning mode under which a Datalog program is validated.
- Filter
Kind - An operation represented by
BuiltinFilter, independent of evaluator support. - Ground
Term - Ground term — the value form of a
Termwith all variables substituted. Used as the cell type inside tuples so an evaluator can store both flat constant-only facts and quoted-triple facts (TT1). - Literal
- Datalog body literal — a positive or negated atom, a built-in
filter expression, or a built-in
BIND(expr AS ?var)form. - Profile
Check Error - An invalid program or an explicitly unsupported language feature.
- Program
Validation Error - Program validation failures. Every failure tied to a location names the rule and, where narrower, the literal and term inside it.
- Rule
Site - The part of a rule a location falls in.
- Stratification
Error - Stratification failure.
- Strict
Dependency Kind - Why a dependency must cross strata.
- Term
- Datalog term (
variable,constant,quoted-triple term, orSKOLEM(name, args...)function call). - Term
Root - Where the outermost term of a
TermPathsits. - Term
Shape Error - Invalid term structure, independent of variable binding or rule position.
Constants§
- MAX_
TRIPLE_ TERM_ NESTING - Maximum allowed nesting depth for
Term::TripleTermvariants. Programs whose triple-term nesting exceeds this limit are rejected atProgram::validatetime.