DM: Chapter 6

Data and Information Management: Positive Datalog

  • Instructor: Bas Ketsman

  • Academic Year: 2025-2026

  • Topic: Adding recursion to query languages via Positive Datalog.

Agenda of Today

  • Introduction to Recursion: Adding recursive capabilities to standard query languages.

  • Datalog Syntax: Formal structure of Datalog rules and programs.

  • Model-Theoretic Semantics: Declarative logic-based interpretation of Datalog.

  • Datalog as a Query Language: Definition of queries and output.

  • Datalog Properties and Expressiveness: Comparison with other languages and limitations.

Datalog Syntax

  • Definition of a Datalog Rule:

    • A Datalog rule over a schema SS is an expression of the form:     R0(x):−R1(u1),…,Rn(un)R_0(x) :- R_1(u_1), \dots, R_n(u_n)

    • The Head: The expression R0(x)R_0(x).

    • The Body: The expression R1(u1),…,Rn(un)R_1(u_1), \dots, R_n(u_n).

    • Parameters:

      • The number of body atoms n≥1n \ge 1.

      • Each predicate RiR_i is a member of the schema SS.

      • The terms xx and uiu_i (for every ii) are tuples consisting of constants and variables.

  • Safety Requirement: Every variable that appears in the head tuple xx must also be mentioned in at least one body atom tuple uiu_i.

  • Datalog Program: A Datalog program over schema SS is a finite set of Datalog rules over SS.

  • Comparison Note: While Datalog has a similar flavor to Prolog, it is not the same (e.g., Datalog is purely declarative and does not use Prolog\'s search strategy).

Example: Reachability in Graphs

  • Scenario: Given a directed graph represented by an EdgeEdge relation, collect all nodes reachable from a specific source node ′s′'s'.

  • Rule 1 (Base Case): Reachable(x):−Edge(′s′,x).Reachable(x) :- Edge('s', x).

  • Rule 2 (Recursive Step): Reachable(x):−Reachable(y),Edge(y,x).Reachable(x) :- Reachable(y), Edge(y, x).

Extensional vs. Intensional Relations

  • Intensional Database Predicates (IDB):

    • A relation name is intensional if it occurs in the head of at least one rule.

    • These predicates are computed through definitions within the program.

    • Example: ReachableReachable in the reachability program.

  • Extensional Database Predicates (EDB):

    • A relation name is extensional if it does not occur in any rule head.

    • These predicates are defined through explicit enumeration within the database.

    • Example: EdgeEdge in the reachability program.

  • Schema Assumption: Similar to conjunctive queries (CQs), schemas are often not explicitly provided; it is assumed that databases DD consist of the extensional relations of the program PP.

Example: Points-To Analysis

  • Context: Based on the work of Smaragdakis and Balatsouras (2015).

  • EDB Conversion of C Instructions:

    • Instruction a = &b becomes AddressOf(a,b)AddressOf(a, b).

    • Instruction a = b becomes Assign(a,b)Assign(a, b).

    • Instruction a = *b becomes Load(a,b)Load(a, b).

    • Instruction *a = b becomes Store(a,b)Store(a, b).

  • IDB Relation: PointsTo(x,y)PointsTo(x, y) stores pairs of variables where xx may point to yy.

  • Rules for Analysis:

    1. PointsTo(x,y):−AddressOf(x,y).PointsTo(x, y) :- AddressOf(x, y).

    2. PointsTo(x,y):−Assign(x,z),PointsTo(z,y).PointsTo(x, y) :- Assign(x, z), PointsTo(z, y).

    3. PointsTo(x,w):−Load(x,z),PointsTo(z,y),PointsTo(y,w).PointsTo(x, w) :- Load(x, z), PointsTo(z, y), PointsTo(y, w).

    4. PointsTo(x,y):−Store(z,w),PointsTo(z,x),PointsTo(w,y).PointsTo(x, y) :- Store(z, w), PointsTo(z, x), PointsTo(w, y).

Datalog Semantics Overview

  • Datalog utilizes coinciding declarative and operational semantics:

    • Model-Theoretic Semantics: Declarative approach based on First-Order (FO) logic formulas.

    • Fixpoint Semantics: Operational approach based on the repeated application of a fixpoint operator.

    • Proof-Tree Semantics: Note that this is mentioned as out of scope for the current course.

Model-Theoretic Datalog Semantics

  • Interpretation: A Datalog rule ρ\rho of the form R0(x):−R1(u1),…,Rn(un)R_0(x) :- R_1(u_1), \dots, R_n(u_n) is interpreted as a First-Order sentence ϕρ\phi_{\rho}:     ∀x1…∀xm(R1(u1)∧⋯∧Rn(un)→R0(x))\forall x_1 \dots \forall x_m (R_1(u_1) \land \dots \land R_n(u_n) \rightarrow R_0(x))

  • FO Theory: A Datalog program PP defines a set of constraints called an FO theory: ϕρ∣ρ∈P{ \phi_{\rho} \mid \rho \in P }

  • Model Definition: A database over the extensional schema of PP (edb(P)edb(P)) is a model for PP if it satisfies all sentences in the FO theory.

  • Program Output: Given a Datalog program PP and database DD of edb(P)edb(P), the output P(D)P(D) is defined as the minimal model (with respect to set inclusion ⊆\subseteq) of PP that contains DD.

  • Validity Proof Hint:

    • A model of PP containing DD always exists (specifically, the database containing all possible facts over the Domain Dom(P,D)Dom(P, D)).

    • The intersection of all valid models is itself a model and is the unique minimal model.

Fixpoint Semantics: Immediate Consequences

  • Immediate Consequences of P over D:

    1. Include all facts currently in DD.

    2. Include facts R(a)R(a) for which there exists a rule ρ∈P\rho \in P and a valuation mapping all body atoms into existing facts in DD, results in the head mapping to R(a)R(a).

    • Operational Note: This computes the rule similarly to a Conjunctive Query (CQ).

  • Immediate Consequence Operator (TPT_P):

    • A function mapping databases DD over edb(P)∪idb(P)edb(P) \cup idb(P) to their immediate consequences.

    • TP1(D)=TP(D)T_P^1(D) = T_P(D).

    • TPi(D)=TP(TPi−1(D))T_P^i(D) = T_P(T_P^{i-1}(D)).

  • Fixpoint Definition: A database DD is a fixpoint for TPT_P if TP(D)=DT_P(D) = D.

  • Least Fixpoint (TP∞(D)T_P^{\infty}(D)): The first/least database that establishes a fixpoint for TPT_P and DD.

  • Definition of Output: The output P(D)P(D) is defined as TP∞(D)T_P^{\infty}(D).

Equivalence of Semantics

  • Proposition: For any Datalog program PP and database DD over edb(P)edb(P), the Fixpoint and Model-Theoretic semantics are equivalent because:

    • TPT_P is monotone.

    • There exists an integer kk such that the fixpoint is reached at TPk(D)T_P^k(D).

    • Every fixpoint of TPT_P containing DD is a model for PP and DD.

    • Every model for PP and DD contains the set produced by TP(D)T_P(D).

Datalog as a Query Language

  • Definition: A Datalog query qq over schema SS is a pair q=(P,R)q = (P, R).

    • PP is a Datalog program over schema S∪S′S \cup S'.

    • R∈idb(P)R \in idb(P).

    • edb(P)⊆Sedb(P) \subseteq S.

    • idb(P)⊆S′idb(P) \subseteq S'.

  • Query Output (q(D)q(D)): The set of tuples present in relation RR within the resulting database instance P(D)P(D).

Properties and Expressiveness

  • Preservation Properties: Datalog queries are preserved under homomorphisms and are monotone.

  • Expressive Limitations: Because of these properties, Datalog cannot express:

    • Inequalities.

    • Negative relational atoms.

    • Set difference.

  • Complexity: Naive evaluation of Datalog takes polynomial many steps relative to the size of the database.

Naive Datalog Evaluation Example

  • Database DD: Edge={(a,b),(b,c),(c,d),(d,e)}Edge = \{ (a, b), (b, c), (c, d), (d, e) \}, R=∅R = \emptyset.

  • Program Rules:

    • R(x,y):−Edge(x,y).R(x, y) :- Edge(x, y).

    • R(x,y):−R(x,z),R(z,y).R(x, y) :- R(x, z), R(z, y).

  • Rounds of Evaluation:

    • Round 1: D1=TP1(D)=D∪{(a,b),(b,c),(c,d),(d,e)}D_1 = T_P^1(D) = D \cup \{ (a, b), (b, c), (c, d), (d, e) \}.

    • Round 2: D2=TP2(D)=D1∪{(a,c),(b,d),(c,e)}D_2 = T_P^2(D) = D_1 \cup \{ (a, c), (b, d), (c, e) \}.

    • Round 3: D3=TP3(D)=D2∪{(a,d),(b,e),(a,e)}D_3 = T_P^3(D) = D_2 \cup \{ (a, d), (b, e), (a, e) \}.

    • Round 4: D4=TP4(D)=D3D_4 = T_P^4(D) = D_3. (Fixpoint reached).

  • Critique: Already discovered facts are derived repeatedly in every round, which is inefficient.

Semi-Naive Datalog Evaluation

  • Logic: Avoid redundant work by focusing on "new" facts found in the previous step.

  • Variables:

    • R(i)R(i): Facts found after step ii.

    • ΔR(i)=R(i)−R(i−1)\Delta R(i) = R(i) - R(i-1).

  • Rule Expansion (Example R(x,y):−R(x,z),R(z,y)R(x, y) :- R(x, z), R(z, y)):

    • Let the rule be a function f(R,R)f(R, R).

    • R(i+1)=f(R(i−1)∪ΔR(i),R(i−1)∪ΔR(i))R(i+1) = f(R(i-1) \cup \Delta R(i), R(i-1) \cup \Delta R(i))

    • Expanded: f(R(i−1),R(i−1))∪f(R(i−1),ΔR(i))∪f(ΔR(i),R(i−1))∪f(ΔR(i),ΔR(i))f(R(i-1), R(i-1)) \cup f(R(i-1), \Delta R(i)) \cup f(\Delta R(i), R(i-1)) \cup f(\Delta R(i), \Delta R(i))

    • ΔR(i+1)=f(R(i−1),ΔR(i))∪f(ΔR(i),R(i−1))∪f(ΔR(i),ΔR(i))\Delta R(i+1) = f(R(i-1), \Delta R(i)) \cup f(\Delta R(i), R(i-1)) \cup f(\Delta R(i), \Delta R(i))

  • Procedure:

    1. Maintain two copies for each IDB relation RR: the relation itself and its delta ΔR\Delta R.

    2. Apply the immediate consequence operator using the split formula to isolate new derivations.

    3. Update relations and deltas after each application until the delta is empty.

Linear Datalog Programs

  • Definition: A rule is linear if it contains at most one IDB relation name in its body.

  • Linear Program: A program consisting exclusively of linear rules.

  • Efficiency: Linear programs are significantly more efficient to compute than non-linear programs.

  • Linear Delta Derivation:

    • ΔR(i+1)=f(ΔR(i),E(0))\Delta R(i+1) = f(\Delta R(i), E(0))

    • This removes the need to cross-join multiple IDB relations or deltas from different steps.

Undecidability and Satisfiability

  • Undecidable Properties:

    • Determining if there is a bound on the number of applications of the immediate consequence operator needed for a fixpoint.

    • Determining query containment (q⊆q′q \subseteq q') between two Datalog queries.

  • Satisfiability Question: The transcript asks if every Datalog query (P,R)(P, R) is satisfiable.

Questions & Discussion

  • Discussion on Minimal Models:

    • Question: Which databases are minimal models for PP and DD in the reachability example?

    • Answer Logic: The unique minimal model is the one generated by the fixpoint iteration, containing only nodes with a path from 's'.

  • Discussion on Immediate Consequences:

    • Question: What are the immediate consequences of the reachability program on a graph with nodes (s→a)(s \rightarrow a), (a→b)(a \rightarrow b), (a→c)(a \rightarrow c)?

    • Answer: In the first step, we derive Reachable(a)Reachable(a). In subsequent steps, we derive Reachable(b)Reachable(b) and Reachable(c)Reachable(c).

  • Discussion on Performance:

    • Question: Why use Semi-Naive evaluation?

    • Answer: To avoid the overhead of re-deriving facts that were already established in previous rounds.