Scarica Basi di dati Datalog e più Dispense in PDF di Basi di Dati solo su Docsity!
DatalogDatalogDatalogDatalog
S. Costantini S. Costantini
S. Costantini / Logical Query Languages 2
Logic As a Query
Language
- (^) If-then logical rules have been used in many systems.
- (^) Recursive rules extend relational algebra --- have been used to add recursion to SQL-99.
For all programming languages and formalisms…
- (^) There is the problem to provide the programmer with syntactic means for expressing what (s)he has in mind
- (^) & to provide a semantic account for this syntax
- (^) & to device a corresponding “procedural semantics” to construct running systems
- (^) & to account for the gap between what the programmer has in mind and the actual semantics S. Costantini / Logical Query Languages 4
In Logic Programming:
- (^) The programmer has to formalize her/his idea of the domain (s)he intends to describe in terms of: - (^) Constants, that in her/his mind indicate (“denote”) objects of a certain domain (“world”) - (^) Relations, that express what objects are involved into (e.g., an object being in some place can be expressed by a relation “to be in” a place) - (^) Rules, that express how this “world” function S. Costantini / Logical Query Languages 5
S. Costantini / Logical Query Languages 7
Logic Programming:
Intuition
S. Costantini / Logical Query Languages 8
Datalog Terminology:
Atoms
- (^) An atom is a predicate , or relation name with variables or constants as arguments. E.g., in(alan,r123) part_of(r123,cs_building) p(X,a)
S. Costantini / Logical Query Languages 10
Datalog Terminology:
Rules
- (^) The head of a rule is an atom; the body of a rule is the AND of one or more atoms. E.g., in(X,Y):- part_of(Z,Y), in(X,Z). where “:-” stands for “”
S. Costantini / Logical Query Languages 11
Datalog Terminology:
Facts
- (^) Rules without body called “facts”: E.g., in(alan,r123).
- (^) Facts constitute the extensional part of a deductive database, i.e., define tables.
S. Costantini / Datalog e ASP 13
Datalog
Terminology: p q, not r Atoms which occur positively in the body are positive literals , and the negations are negative literals.
S. Costantini / Datalog e ASP 14 Datalog Negation not r Meaning: not r is true if r is false and vice versa. r is true if r can be computed in given database instance (seen as a a query, r is true if query r has non-empty answer)
S. Costantini / Logical Query Languages 16
Datalog Databases
- (^) A Datalog database is a collection of facts and rules.
- (^) In a program, predicates can be either
- EDB = Extensional Database = stored tables, i.e., Datalog facts.
- IDB = Intensional Database = relations (or part of them) defined by rules.
S. Costantini / Datalog e ASP 17 Datalog
Datalog databases are often called
- (^) Deductive databases or also
- (^) Knowledge bases or also
- (^) Datalog programs
S. Costantini / Datalog e ASP 19
Expressive Power of
Datalog
- (^) Without recursion, Datalog can express all and only the queries of core relational algebra.
- (^) But with recursion, Datalog can express more than these languages.
- (^) Yet it is not Turing-complete, i.e., it cannot express all computable functions.
S. Costantini / Datalog (^20)
Recursion: Example
parent(X,Y):- mother(X,Y). parent(X,Y):- father(X,Y). ancestor(X,Y):- parent(X,Y). ancestor(X,Y):- parent(X,Z),ancestor(Z,Y). mother(a,b). father(c,b). mother(b,d). … other facts …
- (^) A parent X of Y is either a mother or a father (disjunction as alternative rules);
- (^) An ancestor is either a parent, or the parent of an ancestor (the ancestor X of Y is the parent of some Z who in turn is an ancestor of Y).