1 / 86

Datalog

Datalog. S. Costantini. Datalog base: notazione, esempi, semantica. Datalog. A language for Deductive Databases Extensional part: tables Intensional part: rules. Logic: Intuition. Datalog Terminology: Atoms.

vera-turner
Download Presentation

Datalog

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Datalog S. Costantini

  2. Datalog base: notazione, esempi, semantica S. Costantini / Datalog e ASP

  3. Datalog • A language for Deductive Databases • Extensional part: tables • Intensional part: rules S. Costantini / Datalog e ASP

  4. Logic: Intuition S. Costantini / Datalog e ASP

  5. 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 / Datalog e ASP

  6. Datalog Terminology: Atoms • Conventions: • Databases: Predicates begin with a capital, variables begin with lower-case. Example: P(x,’a’) where ‘a’ is a constant, i.e., a data item. • Logic Programming: Variables begin with a capital, predicates and constants begin with lower-case. Example: p(X,a). S. Costantini / Datalog e ASP

  7. 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 / Datalog e ASP

  8. Datalog Terminology: Rules • Rules without body: E.g., in(alan,r123). S. Costantini / Datalog e ASP

  9. Datalog and Transitivity • Rule in(X,Y):- part_of(X,Z),in(Z,Y) defines in as the transitive closure of part_of S. Costantini / Datalog e ASP

  10. Arithmetic Subgoals • In addition to relations as predicates, a predicate of the body can be an arithmetic comparison. • We write such atoms in the usual way, e.g.: x < y. S. Costantini / Datalog e ASP

  11. Example: Arithmetic • A beer is “cheap” if there are at least two bars that sell it for under $2. cheap(Beer) :- sells(Bar1,Beer,P1), sells(Bar2,Beer,P2), P1 < 2.00, P2 < 2.00, Bar1 <> Bar2. S. Costantini / Datalog e ASP

  12. Datalog Terminology: p  q, not r. or p :- q, not r. is a rule, where p is the head, or conclusion, or consequent, and q, not r is the body, or the conditions, or the antecedent. p, q and r are atoms. A rule without body, indicated as p . or p. Is called a unitrule, or a fact. This kind of rules ale also called Horn Clauses. S. Costantini / Datalog e ASP

  13. Datalog Terminology: p  q, not r Atoms in the body can be called subgoals. Atoms which occur positively in the body are positive literals, and the negations are negative literals. S. Costantini / Datalog e ASP

  14. Datalog Terminology: p  q, not r We say that p depends on q and not r. The same atom can occur in a rule both as a positive literal, and inside a negative literal (e.g. rule p  not p). S. Costantini / Datalog e ASP

  15. Datalog Programs • A Datalog theory, or “program”, is a collection of rules. • In a program, predicates can be either • EDB = Extensional Database = facts. • IDB = Intensional Database = relation defined by rules. S. Costantini / Datalog e ASP

  16. 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 still not Turing-complete. S. Costantini / Datalog e ASP

  17. Example p(X) r(X),q(X). r(Z)  s(Z). s(a). s(b). q(b). • In every rule, each variable stands for the same value. • Thus, variables can be considered as “placeholders” for values. • Possible values are those that occur as constants in some rule/fact of the program itself. S. Costantini / Datalog e ASP

  18. Datalog Its grounding can be obtained by: • considering the constants, here a,b. • substituting variables with constants in any possible (coherent) way) • E. g., the atom r(Z) is transformed by grounding over constants a, b into the two ground atoms r(a), r(b). S. Costantini / Datalog e ASP

  19. Grounding • E. g., the atom r(Z) is transformed by grounding over constants a, b into the two ground atoms r(a), r(b). • Rule r(Z)  s(Z). is transformed into the two rules r(a)  s(a). r(b)  s(b). S. Costantini / Datalog e ASP

  20. Datalog Grounding of the program p(a)  r(a),q(a). p(b)  r(b),q(b). r(a)  s(a). r(b)  s(b). s(a). s(b). q(b). Semantics: Least Herbrand Model M = {s(a),s(b),q(b),r(a),r(b),p(b)} S. Costantini / Datalog e ASP

  21. Datalog A shortcut of the grounded program p1  r1,q1. p2  r2,q2. r1  s1. r2  s2. s1. s2. q2. M = {s1,s2,q2,r1,r2,p2} S. Costantini / Datalog e ASP

  22. Datalog semantics • Without negation (no negative literal in the body of rules): Least Herbrand Model • The head of a rule is in the Least Herbrand Model only if the body is in the Least Herbrand Model. S. Costantini / Datalog e ASP

  23. Least Herbrand Model: Intuition S. Costantini / Datalog e ASP

  24. Least Herbrand Model: Intuition In the example:Least Herbrand Model M0 = {in(alan,r123), part_of(r123,cs_building)} M1= {in(alan,r123), part_of(r123,cs_building), in(alan,cs_building)} S. Costantini / Datalog e ASP

  25. Least Herbrand Model: How to find it p g,h. g  r,s. m  p,q. r  f. s. f. h. Step 1: facts M0 = {s,f,h} Step 2: M1 = M0 plus what I can derive from facts M1 = {s,f,h,r} Step 3: M2 = M1 plus what I can derive from M1 M2 = {s,f,h,r,g} Step 4: M3 = M2 plus what I can derive from M2 M3 = {s,f,h,r,g,p} If you try to go on, no more added conclusions: fixpoint S. Costantini / Datalog e ASP

  26. Datalog con Negazione S. Costantini / Datalog e ASP

  27. Negated Subgoals • We may put NOT in front of an atom, to negate its meaning. • Example: at_home(alan):- not at_work(alan). S. Costantini / Datalog e ASP

  28. Negated Subgoals: Intuition • Given atom A, not A holds if A cannot be proved • Negation as finite failure S. Costantini / Datalog e ASP

  29. Minimal Models • When there is no negation, a Datalog program has a unique minimal model (one that does not contain any other model). • But with negation, there can be several minimal models. S. Costantini / Datalog e ASP

  30. Cos’è un modello minimo: è un insieme di atomi Data una regola A:- B1,...,Bn,not C1,...,not Cm • Un modello è un insieme di atomi che se NON contiene C1,...,Cm e contiene B1,...,Bn allora deve contenere A • Un modello minimo non è sovrainsieme di un altro modello S. Costantini / Datalog e ASP

  31. Datalog semantics • With negation: several proposals, in deductive databases Answer Set Semantics. • Based on Answer Set Semantics: Answer Set Programming, new logic programming paradigm S. Costantini / Datalog e ASP

  32. Safe Rules • A rule is safe if: • Each variable in the head • Each variable in an arithmetic subgoal, • Each variable in a negated subgoal, also appears in a nonnegated, subgoal in the body. • We allow only safe rules. S. Costantini / Datalog e ASP

  33. Example: Unsafe Rules • Each of the following is unsafe and not allowed: • s(X) :- r(Y). • s(X) :- r(Y), not r(X). • s(X) :- r(Y), X < Y. • In each case, an infinity of X ’s can satisfy the rule, even if R is a finite relation. S. Costantini / Datalog e ASP

  34. Non-Monotonicità • Aggiungere nuove conoscenze può portare a ritrarre precedenti conclusioni. • Vi possono essere conclusioni alternative tramite cicli sulla negazione. S. Costantini / Datalog e ASP

  35. Non-Monotonicità = Problema? • No, se gestita con opportuni strumenti semantici • Non-Monotonic Reasoning (NMR) = campo di ricerca in • Intelligenza Artificiale • Database deduttivi • si propone di sfruttare la non-monotonicità S. Costantini / Datalog e ASP

  36. DATALOG senza Negazione • Semantica del Least Herbrand Model • Paradigma procedurale della ricerca per tentativi ripetuti • Linguaggio Prolog (numerosi interpreti fra i quali SICSTUS-Prolog, SWI-Prolog) S. Costantini / Datalog e ASP

  37. DATALOG + Negazione • Semantica degli Answer Sets • Paradigma dell’Answer Set Programming (ASP) • Negazione, insiemi-risposta alternativi • Numerosi solver S. Costantini / Datalog e ASP

  38. Esempio NMR persona(anna). persona(carlo). persona(giorgio). malato(giorgio). a_casa(X):- persona(X), not in_ufficio(X). in_ufficio(X):- persona(X), not a_casa(X). :- in_ufficio(X),malato(X). % constraint S. Costantini / Datalog e ASP

  39. Risultato atteso • Anna e Carlo sono o a casa oppure in ufficio. • Giorgio non può essere in ufficio perchè è malato e quindi sarà a casa. S. Costantini / Datalog e ASP

  40. NMR in pratica • Vi sono vari linguaggi logici che consentono non-monotonicità. • Sono dotati di motori inferenziali (inference engines, o solvers) in grado di gestirla. • Possibili soluzioni inesistenti o altrernative. • Uno di essi: smodels S. Costantini / Datalog e ASP

  41. Risultato di smodels (inference engine per NMR) • Answer: 1 malato(giorgio) a_casa(giorgio) a_casa(carlo) in_ufficio(anna) • Answer: 2 malato(giorgio) a_casa(giorgio) in_ufficio(carlo) in_ufficio(anna) S. Costantini / Datalog e ASP

  42. Risultato di smodels (inference engine per NMR) • Answer: 3 malato(giorgio) a_casa(giorgio) in_ufficio(carlo) a_casa(anna) • Answer: 4 malato(giorgio) a_casa(giorgio) a_casa(carlo) a_casa(anna) S. Costantini / Datalog e ASP

  43. Datalog con negazione: nuovo Paradigma di Programmazione Logica Answer Set Programming(ASP)basato su Answer Set Semantics S. Costantini / Datalog e ASP

  44. Uno sguardo sull’Answer Set Programming (ASP) S. Costantini / Datalog e ASP

  45. Example: 3-coloring in ASP Assigning colors red/blue/green to vertices of a graph, so as no adjacent vertices have the same color. node(0..3). col(red). col(blue). col(green). edge(0,1). edge(1,2). edge(2,0). edge(1,3). edge(2,3). S. Costantini / Datalog e ASP

  46. 3-coloring in Answer Set Programming color(X,red) | color(X,blue) | color(X,green) :- node(X). :- edge(X,Y), col(C), color(X,C), color(Y,C). % I constraint (regole con conclusione vuota) % specificano cosa *non può* essere vero S. Costantini / Datalog e ASP

  47. Come “eseguire” il programma? • Si usa un “Motore Inferenziale”, ad esempio SMODELS (ma ce ne sono diversi) • Esso fornisce le soluzioni al problema dato come “insiemi risposta, appunto “Answer Set” … S. Costantini / Datalog e ASP

  48. Come “eseguire” il programma? • Sintassi: lparse < 3col.txt | smodels 0 • lparse effettua il parsing del programma dato e ne produce il grounding • smodels calcola le risposte (smodels 0 le chiede tutte) … … S. Costantini / Datalog e ASP

  49. Cosa si ottiene nell’esempio? Answer1 color(0,red), color(1,blue), color(green), color(3,red) Answer2 color(0,red), color(1,green), color(blue), color(3,red) … % e tutte le altre colorazioni, ognuna come % “Answer Set” … S. Costantini / Datalog e ASP

  50. Answer Set Semantics: su cosa si basano gli Answer Set e come si ottengono S. Costantini / Datalog e ASP

More Related