DM: Chapter 7
Datalog with Equality: Syntax and Semantics
Definition of Syntax Extensions: Datalog is extended by allowing equality atoms within the rule bodies. These atoms can take two forms:
Expressions of the form , where both and are variables.
Expressions of the form , where is a variable and is a constant.
Safety Requirement: To ensure safety, all variables present in the equality atoms must also occur in the non-equality body atoms of the rule.
Example of Datalog with Equality: Finding nodes in a graph that are part of both a directed triangle and a directed rectangle:
.
.
.
Semantics and Least Fixpoint (LFP): The standard LFP semantics can be reused with a slight extension to the definition of rule satisfaction by a mapping.
Consistent Mappings: A mapping or homomorphism satisfies a rule only if it is consistent with the equalities. Specifically, must hold if is an atom in the rule body.
Existence of LFP: The LFP semantics is always defined. Repeatedly applying the immediate-consequence operator () will always lead to a fixpoint, using the same logic as Datalog without equalities.
Expressive Power comparison:
Proposition: Every query expressible in standard Datalog is expressible in Datalog with equalities (trivial proof).
Equivalence: Every query expressible in Datalog with equalities is also expressible in standard Datalog.
Proof Strategy for Eliminating Equalities:
If a query is unsatisfiable, replace it with a non-satisfiable program without equalities.
For , substitute all occurrences of variable in the rule with constant .
For , substitute all occurrences of variable in the rule with variable .
If an equality results in (two different constants), remove the entire rule.
Equalities of the form are redundant and can be removed.
Corollary: Datalog and Datalog with equalities possess identical expressive power.
Datalog with Non-Equality
Definition: Datalog can be extended with non-equality atoms in the rule bodies.
Expressions of the form (variables ).
Expressions of the form (variable , constant ).
Safety: As with equalities, all variables in non-equality atoms must appear in regular body atoms.
Example: Find nodes in a directed triangle where all three nodes are distinct:
.
Semantics: The mapping must satisfy if is a body atom. The LFP semantics remains defined as the immediate-consequence operator is still inflationary.
Expressivity Gap: Datalog with non-equalities is strictly more expressive than Datalog.
Proof: Datalog queries are preserved under homomorphisms. However, queries with non-equalities are not necessarily preserved.
Counter-example instance: Let graph and . There is a homomorphism . In , node is in a triangle of distinct nodes, so . In , node is only in a self-loop, so . Since the result is not preserved, the query cannot be expressed in standard Datalog.
Datalog with Negation (Datalog ⁻)
Definition: Allows the use of the negation operator () over atoms in rule bodies.
Safety Requirement: Every variable appearing in a negated atom must also appear in at least one non-negated (positive) atom in the same rule body.
Example Program:
.
.
Fixpoint Semantics (): The operator over database produces:
All facts originally in .
Facts produced by a rule where a valuation maps positive atoms to facts in and negated atoms to facts not in .
Termination: The process always terminates on a finite database because is inflationary (the set of facts only grows) and no new constants are introduced (the domain is limited to ).
Semi-Positive Datalog⁻
Definition: A Datalog⁻ program is semi-positive if negation is applied only to EDB (Extensional Database) atoms (i.e., predicates that only appear in the input and never in the head of a rule).
Examples:
"Reachability over complement graph" is semi-positive.
"Non-reachability" (negation of an IDB) is not semi-positive.
Properties:
Semi-positive programs have a unique minimal model, and the fixpoint operator correctly identifies it.
Expressivity: Strictly more expressive than Datalog. While Datalog is monotone, semi-positive Datalog is not (e.g., adding an edge to a complement graph might remove a result).
Construction to Positive Datalog: Any semi-positive program and database over schema can be converted to an equivalent positive Datalog program and database :
Create a copy for every relation in .
Replace in with .
Populate with all tuples in the domain that are not in .
Result: .
Semi-Monotonicity: A query is semi-monotone if provided that the new facts in involve at least one constant not in the original domain .
Intuition: In a graph, a monotone query result remains if we add edges. A semi-monotone result remains if we add edges, as long as we don't add edges between nodes that already existed in the original graph.
Stratified Datalog
Definition: A stratification is a sequence of programs that partitions program .
Rules for Stratification (Given mapping from IDB predicates to strata indices ):
All rules defining an IDB predicate must belong to the same stratum .
If IDB depends on a positive atom , then .
If IDB depends on a negated atom , then .
Semantics: Stratified Datalog evaluates each stratum sequentially using fixpoint semantics. The results of lower strata () are treated as EDB facts for higher strata ().
Key Proposition: A program may have multiple valid stratifications, but they all yield the same final semantic result.
Logic of Stratification Example (Non-Reachability):
Stratum 1: Compute (positive recursion).
Stratum 2: Compute (negation of result from Stratum 1).
Comparison of Model-Theoretic and LFP Semantics
Discrepancy: Unlike standard Datalog, for Datalog with negation, the LFP and model-theoretic semantics do not coincide.
Counter-example: Let database and program :
.
.
Evaluation:
LFP result (): .
Minimal Models: and .
Since contains both and , it is not a minimal model. The model-theoretic semantics is ambiguous (multiple minimal models), whereas LFP is deterministic but not minimal.