La presentazione è in caricamento. Aspetta per favore

La presentazione è in caricamento. Aspetta per favore

DatalogDatalog S. Costantini. S. Costantini / Logical Query Languages 2 Logic As a Query Language If-then logical rules have been used in many systems.

Presentazioni simili


Presentazione sul tema: "DatalogDatalog S. Costantini. S. Costantini / Logical Query Languages 2 Logic As a Query Language If-then logical rules have been used in many systems."— Transcript della presentazione:

1 DatalogDatalog S. Costantini

2 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.

3 S. Costantini / Logical Query Languages 3 Datalog A language for Deductive Databases –Extensional part: tables –Intensional part: rules

4 S. Costantini / Logical Query Languages 4 Logic: Intuition

5 S. Costantini / Logical Query Languages 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)

6 S. Costantini / Logical Query Languages 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).

7 S. Costantini / Logical Query Languages 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 “  ”

8 S. Costantini / Logical Query Languages 8 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.

9 S. Costantini / Logical Query Languages 9 Alternative notation Explicitly list attributes E.g., facts: in(Object:alan,Place:r123) E.g., rules: in(Object:X,Place:Y):- part_of(Place: Z, Place:Y), in(Object:X, Place:Z).

10 S. Costantini / Logical Query Languages 10 Datalog semantics Least Herbrand Model –The head of a rule is in the Least Herbrand Model only if the body is in the Least Herbrand Model.

11 S. Costantini / Logical Query Languages 11 Least Herbrand Model: How to find it p  g,h. g  r,s. m  p,q. r  f. s. f. h. Step 1: facts M 0 = {s,f,h} Step 2: M 1 = M 0 plus what I can derive from facts M 1 = {s,f,h,r} Step 3: M 2 = M 1 plus what I can derive from M 1 M 2 = {s,f,h,r,g} Step 4: M 3 = M 2 plus what I can derive from M 2 M 3 = {s,f,h,r,g,p} If you try to go on, no more added conclusions: fixpoint

12 S. Costantini / Logical Query Languages 12 Least Herbrand Model: Intuition

13 S. Costantini / Logical Query Languages 13 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)}

14 S. Costantini / Logical Query Languages 14 Datalog and Transitivity Rule in(X,Y):- part_of(X,Z),in(Z,Y) defines in as the transitive closure of part_of

15 S. Costantini / Logical Query Languages 15 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.

16 S. Costantini / Logical Query Languages 16 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) AND Sells(bar2,beer,p2) AND p1 < 2.00 AND p2 bar2

17 S. Costantini / Logical Query Languages 17 Negated Subgoals We may put NOT in front of an atom, to negate its meaning. Example: at_home(alan):- not at_work(alan)

18 S. Costantini / Logical Query Languages 18 Negated Subgoals: Intuition Given atom A, not A holds if A cannot be proved Negation as finite failure

19 S. Costantini / Logical Query Languages 19 Safe Rules A rule is safe if: 1.Each variable in the head 2.Each variable in an arithmetic subgoal, 3.Each variable in a negated subgoal, also appears in a nonnegated, subgoal in the body. We allow only safe rules.

20 S. Costantini / Logical Query Languages 20 Example: Unsafe Rules Each of the following is unsafe and not allowed: 1.S(x) <- R(y) 2.S(x) <- R(y) AND NOT R(x) 3.S(x) <- R(y) AND x < y In each case, an infinity of x ’s can satisfy the rule, even if R is a finite relation.

21 S. Costantini / Logical Query Languages 21 Datalog Programs A Datalog program is a collection of rules. In a program, predicates can be either 1.EDB = Extensional Database = stored table. 2.IDB = Intensional Database = relation defined by rules.

22 S. Costantini / Logical Query Languages 22 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.

23 S. Costantini / Logical Query Languages 23 Datalog semantics With negation: several proposals, in deductive databases answer set semantics.

24 S. Costantini / Logical Query Languages 24 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.

25 S. Costantini / Logical Query Languages 25 Non-Monotonicità Aggiungere nuove conoscenze può portare a ritrarre precedenti conclusioni. Vi possono essere conclusioni alternative tramite cicli sulla negazione.

26 S. Costantini / Logical Query Languages 26 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à

27 S. Costantini / Logical Query Languages 27 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)

28 S. Costantini / Logical Query Languages 28 DATALOG + Negazione Semantica degli Answer Sets Paradigma dell’Answer Set Programming (ASP) Negazione, insiemi-risposta alternativi Numerosi solver

29 S. Costantini / Logical Query Languages 29 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

30 S. Costantini / Logical Query Languages 30 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.

31 S. Costantini / Logical Query Languages 31 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

32 S. Costantini / Logical Query Languages 32 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)

33 S. Costantini / Logical Query Languages 33 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)


Scaricare ppt "DatalogDatalog S. Costantini. S. Costantini / Logical Query Languages 2 Logic As a Query Language If-then logical rules have been used in many systems."

Presentazioni simili


Annunci Google