A Datalog program consists of facts, which are statements that are held to be true, and rules, which say how to deduce new facts from known facts. For example, here are two facts that mean xerces is a parent of brooke and brooke is a parent of damocles:
parent(xerces,brooke).parent(brooke,damocles).
The names are written in lowercase because strings beginning with an uppercase letter stand for variables. Here are two rules:
The :- symbol is read as "if", and the comma is read "and", so these rules mean:
X is an ancestor of Y if X is a parent of Y.
X is an ancestor of Y if X is a parent of some Z, and Z is an ancestor of Y.
The meaning of a program is defined to be the set of all of the facts that can be deduced using the initial facts and the rules. This program's meaning is given by the following facts:
Some Datalog implementations don't deduce all possible facts, but instead answer queries:
?-ancestor(xerces,X).
This query asks: Who are all the X that xerces is an ancestor of? For this example, it would return brooke and damocles.
Comparison to relational databases
The non-recursive subset of Datalog is closely related to query languages for relational databases, such as SQL. The following table maps between Datalog, relational algebra, and SQL concepts:
A Datalog program consists of a list of rules (Horn clauses).[1] If constant and variable are two countable sets of constants and variables respectively and relation is a countable set of predicate symbols, then the following BNF grammar expresses the structure of a Datalog program:
Atoms are also referred to as literals. The atom to the left of the :- symbol is called the head of the rule; the atoms to the right are the body. Every Datalog program must satisfy the condition that every variable that appears in the head of a rule also appears in the body (this condition is sometimes called the range restriction).[1][2]
There are two common conventions for variable names: capitalizing variables, or prefixing them with a question mark ?.[3]
Note that under this definition, Datalog does not include negation nor aggregates; see § Extensions for more information about those constructs.
Rules with empty bodies are called facts. For example, the following rule is a fact:
r(x):-.
The set of facts is called the extensional database or EDB of the Datalog program. The set of tuples computed by evaluating the Datalog program is called the intensional database or IDB.
Syntactic sugar
Many implementations of logic programming extend the above grammar to allow writing facts without the :-, like so:
r(x).
Some also allow writing 0-ary relations without parentheses, like so:
p:-q.
These are merely abbreviations (syntactic sugar); they have no impact on the semantics of the program.
There are three widely-used approaches to the semantics of Datalog programs: model-theoretic, fixed-point, and proof-theoretic. These three approaches can be proven equivalent.[4]
An atom is called ground if none of its subterms are variables. Intuitively, each of the semantics define the meaning of a program to be the set of all ground atoms that can be deduced from the rules of the program, starting from the facts.
Model theoretic
A rule is called ground if all of its atoms (head and body) are ground. A ground rule R1 is a ground instance of another rule R2 if R1 is the result of a substitution of constants for all the variables in R2. The Herbrand base of a Datalog program is the set of all ground atoms that can be made with the constants appearing in the program. The Herbrand model of a Datalog program is the smallest subset of the Herbrand base such that, for each ground instance of each rule in the program, if the atoms in the body of the rule are in the set, then so is the head.[5] The model-theoretic semantics define the minimal Herbrand model to be the meaning of the program.
Fixed-point
Let I be the power set of the Herbrand base of a program P. The immediate consequence operator for P is a map T from I to I that adds all of the new ground atoms that can be derived from the rules of the program in a single step. The least-fixed-point semantics define the least fixed point of T to be the meaning of the program; this coincides with the minimal Herbrand model.[6]
The fixpoint semantics suggest an algorithm for computing the minimal model: Start with the set of ground facts in the program, then repeatedly add consequences of the rules until a fixpoint is reached. This algorithm is called naïve evaluation.
Proof-theoretic
The proof-theoretic semantics defines the meaning of a Datalog program to be the set of facts with corresponding proof trees. Intuitively, a proof tree shows how to derive a fact from the facts and rules of a program.
One might be interested in knowing whether or not a particular ground atom appears in the minimal Herbrand model of a Datalog program, perhaps without caring much about the rest of the model. A top-down reading of the proof trees described above suggests an algorithm for computing the results of such queries. This reading informs the SLD resolution algorithm, which forms the basis for the evaluation of Prolog.
Evaluation
There are many different ways to evaluate a Datalog program, with different performance characteristics.
Bottom-up evaluation strategies
Bottom-up evaluation strategies start with the facts in the program and repeatedly apply the rules until either some goal or query is established, or until the complete minimal model of the program is produced.
Naïve evaluation
Naïve evaluation mirrors the fixpoint semantics for Datalog programs. Naïve evaluation uses a set of "known facts", which is initialized to the facts in the program. It proceeds by repeatedly enumerating all ground instances of each rule in the program. If each atom in the body of the ground instance is in the set of known facts, then the head atom is added to the set of known facts. This process is repeated until a fixed point is reached, and no more facts may be deduced. Naïve evaluation produces the entire minimal model of the program.[7]
Semi-naïve evaluation
This section needs expansion. You can help by adding to it. (February 2023)
Semi-naïve evaluation is a bottom-up evaluation strategy that can be asymptotically faster than naïve evaluation.[8]
Performance considerations
Naïve and semi-naïve evaluation both evaluate recursive Datalog rules by repeatedly applying them to a set of known facts until a fixed point is reached. In each iteration, rules are only run for "one step", i.e., non-recursively. As mentioned above, each non-recursive Datalog rule corresponds precisely to a conjunctive query. Therefore, many of the techniques from database theory used to speed up conjunctive queries are applicable to bottom-up evaluation of Datalog, such as
Datalog engines using OpenMP[19] are instances of the MIMD paradigm.
In the shared-nothing setting, Datalog engines execute on a cluster of nodes. Such engines generally operate by splitting relations into disjoint subsets based on a hash function, performing computations (joins) on each node, and then exchanging newly-generated tuples over the network.[20] Examples include Datalog engines based on MPI,[9]Hadoop,[21] and Spark.[22]
Top-down evaluation strategies
This section needs expansion. You can help by adding to it. (March 2023)
SLD resolution is sound and complete for Datalog programs.
Magic sets
Top-down evaluation strategies begin with a query or goal. Bottom-up evaluation strategies can answer queries by computing the entire minimal model and matching the query against it, but this can be inefficient if the answer only depends on a small subset of the entire model. The magic sets algorithm takes a Datalog program and a query, and produces a more efficient program that computes the same answer to the query while still using bottom-up evaluation.[23] A variant of the magic sets algorithm has been shown to produce programs that, when evaluated using semi-naïve evaluation, are as efficient as top-down evaluation.[24]
Complexity
The decision problem formulation of Datalog evaluation is as follows: Given a Datalog program P split into a set of facts (EDB) E and a set of rules R, and a ground atom A, is A in the minimal model of P? In this formulation, there are three variations of the computational complexity of evaluating Datalog programs:[25]
The data complexity is the complexity of the decision problem when A and E are inputs and R is fixed.
The program complexity is the complexity of the decision problem when A and R are inputs and E is fixed.
The combined complexity is the complexity of the decision problem when A, E, and R are inputs.
With respect to data complexity, the decision problem for Datalog is P-complete. With respect to program complexity, the decision problem is EXPTIME-complete. In particular, evaluating Datalog programs always terminates; Datalog is not Turing-complete.
Some extensions to Datalog do not preserve these complexity bounds. Extensions implemented in some Datalog engines, such as algebraic data types, can even make the resulting language Turing-complete.
Extensions
Several extensions have been made to Datalog, e.g., to support negation, aggregate functions, inequalities, to allow object-oriented programming, or to allow disjunctions as heads of clauses. These extensions have significant impacts on the language's semantics and on the implementation of a corresponding interpreter.
Adding negation to Datalog complicates its semantics, leading to whole new languages and strategies for evaluation. For example, the language that results from adding negation with the stable model semantics is exactly answer set programming.
Stratified negation can be added to Datalog while retaining its model-theoretic and fixed-point semantics. Notable Datalog engines that implement stratified negation include:
Unlike in Prolog, statements of a Datalog program can be stated in any order. Datalog does not have Prolog's cut operator. This makes Datalog a fully declarative language.
In contrast to Prolog, Datalog
disallows complex terms as arguments of predicates, e.g., p(x, y) is admissible but not p(f(x), y),
disallows negation,
requires that every variable that appears in the head of a clause also appear in a literal in the body of the clause.
also disallows complex terms as arguments of predicates,
requires that every variable that appears in the head of a clause also appear in a positive (i.e., not negated) atom in the body of the clause,
requires that every variable appearing in a negative literal in the body of a clause also appear in some positive literal in the body of the clause.[30][unreliable source?]
When we consider ordered databases, i.e., databases with an order relation on their active domain, then the Immerman–Vardi theorem implies that the expressive power of Datalog is precisely that of the class PTIME: a property can be expressed in Datalog if and only if it is computable in polynomial time.[31]
The boundedness problem for Datalog asks, given a Datalog program, whether it is bounded, i.e., the maximal recursion depth reached when evaluating the program on an input database can be bounded by some constant. In other words, this question asks whether the Datalog program could be rewritten as a nonrecursive Datalog program, or, equivalently, as a union of conjunctive queries. Solving the boundedness problem on arbitrary Datalog programs is undecidable,[32] but it can be made decidable by restricting to some fragments of Datalog.
A logic programming and deductive database system based on Prolog with tabling giving Datalog-like termination and efficiency, including incremental evaluation[36]
FoundationDB provides a free-of-charge database binding for pyDatalog, with a tutorial on its use.[37]
Leapsight Semantic Dataspace (LSD) is a distributed deductive database that offers high availability, fault tolerance, operational simplicity, and scalability. LSD uses Leaplog (a Datalog implementation) for querying and reasoning and was create by Leapsight.[38]
LogicBlox, a commercial implementation of Datalog used for web-based retail planning and insurance applications.
Profium Sense is a native RDF compliant graph database written in Java. It provides Datalog evaluation support of user defined rules.
.QL, a commercial object-oriented variant of Datalog created by Semmle for analyzing source code to detect security vulnerabilities.[39]
Stardog is a graph database, implemented in Java. It provides support for RDF and all OWL 2 profiles providing extensive reasoning capabilities, including datalog evaluation.
StrixDB: a commercial RDF graph store, SPARQL compliant with Lua API and Datalog inference capabilities. Could be used as httpd (Apache HTTP Server) module or standalone (although beta versions are under the Perl Artistic License 2.0).
Uses and influence
Datalog is quite limited in its expressivity. It is not Turing-complete, and doesn't include basic data types such as integers or strings. This parsimony is appealing from a theoretical standpoint, but it means Datalog per se is rarely used as a programming language or knowledge representation language.[41] Most Datalog engines implement substantial extensions of Datalog. However, Datalog has a strong influence on such implementations, and many authors don't bother to distinguish them from Datalog as presented in this article. Accordingly, the applications discussed in this section include applications of realistic implementations of Datalog-based languages.
Some widely used database systems include ideas and algorithms developed for Datalog. For example, the SQL:1999 standard includes recursive queries, and the Magic Sets algorithm (initially developed for the faster evaluation of Datalog queries) is implemented in IBM's DB2.[50]
History
The origins of Datalog date back to the beginning of logic programming, but it became prominent as a separate area around 1977 when Hervé Gallaire and Jack Minker organized a workshop on logic and databases.[51]David Maier is credited with coining the term Datalog.[52]
^Antoniadis, Tony; Triantafyllou, Konstantinos; Smaragdakis, Yannis (2017-06-18). "Porting doop to Soufflé". Proceedings of the 6th ACM SIGPLAN International Workshop on State of the Art in Program Analysis. SOAP 2017. New York, NY, USA: Association for Computing Machinery. pp. 25–30. doi:10.1145/3088515.3088522. ISBN978-1-4503-5072-3. S2CID3074689. "The LogicBlox engine performs full query optimization."
^Wu, Jiacheng; Wang, Jin; Zaniolo, Carlo (2022-06-11). "Optimizing Parallel Recursive Datalog Evaluation on Multicore Machines". Proceedings of the 2022 International Conference on Management of Data. SIGMOD '22. New York, NY, USA: Association for Computing Machinery. pp. 1433–1446. doi:10.1145/3514221.3517853. ISBN978-1-4503-9249-5. S2CID249578825. "These approaches implement the idea of parallel bottom-up evaluation by splitting the tables into disjoint partitions via discriminating functions, such as hashing, where each partition is then mapped to one of the parallel workers. After each iteration, workers coordinate with each other to exchange newly generated tuples where necessary.
^"6.4. Negation - LogicBlox 3.10 Reference Manual". developer.logicblox.com. Retrieved 2023-03-04. "Additionally, negation is only allowed when the platform can determine a way to stratify all rules and constraints that use negation."
^Michael Lam; Dr. Sin Min Lee. "Datalog". Course CS 157A. SAN JOSÉ STATE UNIVERSITY, department of Computer Science. Archived from the original on 2017-03-25.
^Lifschitz, Vladimir. "Foundations of logic programming." Principles of knowledge representation 3 (1996): 69-127. "The expressive possibilities of [Datalog] are much too limited for meaningful applications to knowledge representation."
^Huang, Green, and Loo, "Datalog and Emerging applications", SIGMOD 2011(PDF), UC Davis{{citation}}: CS1 maint: multiple names: authors list (link).
^Mei, Hongyuan; Qin, Guanghui; Xu, Minjie; Eisner, Jason (2020). "Neural Datalog Through Time: Informed Temporal Modeling via Logical Specification". Proceedings of ICML 2020. arXiv:2006.16723.
^Chin, Brian; Dincklage, Daniel von; Ercegovac, Vuk; Hawkins, Peter; Miller, Mark S.; Och, Franz; Olston, Christopher; Pereira, Fernando (2015). Ball, Thomas; Bodik, Rastislav; Krishnamurthi, Shriram; Lerner, Benjamin S.; Morrisett, Greg (eds.). Yedalog: Exploring Knowledge at Scale. 1st Summit on Advances in Programming Languages (SNAPL 2015). Leibniz International Proceedings in Informatics (LIPIcs). Vol. 32. Dagstuhl, Germany: Schloss Dagstuhl–Leibniz-Zentrum fuer Informatik. pp. 63–78. doi:10.4230/LIPIcs.SNAPL.2015.63. ISBN978-3-939897-80-4.
^Antoniadis, Tony; Triantafyllou, Konstantinos; Smaragdakis, Yannis (2017-06-18). "Porting doop to Soufflé". Proceedings of the 6th ACM SIGPLAN International Workshop on State of the Art in Program Analysis. SOAP 2017. New York, NY, USA: Association for Computing Machinery. pp. 25–30. doi:10.1145/3088515.3088522. ISBN978-1-4503-5072-3. S2CID3074689.
^Gallaire, Hervé; Minker, John 'Jack', eds. (1978), "Logic and Data Bases, Symposium on Logic and Data Bases, Centre d'études et de recherches de Toulouse, 1977", Advances in Data Base Theory, New York: Plenum Press, ISBN978-0-306-40060-5.