datalogdatalog s. costantini. s. costantini / datalog e asp2 datalog base: notazione, esempi,...
TRANSCRIPT
![Page 1: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/1.jpg)
DatalogDatalogDatalogDatalog
S. CostantiniS. Costantini
![Page 2: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/2.jpg)
S. Costantini / Datalog e ASP 2
Datalog base: notazione, esempi, semantica
![Page 3: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/3.jpg)
S. Costantini / Datalog e ASP 3
Datalog• A language for Deductive Databases
– Extensional part: tables– Intensional part: rules
![Page 4: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/4.jpg)
S. Costantini / Datalog e ASP 4
Logic: Intuition
![Page 5: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/5.jpg)
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)
![Page 6: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/6.jpg)
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).
![Page 7: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/7.jpg)
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 “”
![Page 8: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/8.jpg)
S. Costantini / Datalog e ASP 8
Datalog Terminology: Rules
• Rules without body: E.g., in(alan,r123).
![Page 9: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/9.jpg)
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
![Page 10: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/10.jpg)
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.
![Page 11: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/11.jpg)
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.
![Page 12: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/12.jpg)
S. Costantini / Datalog e ASP 12
DatalogTerminology: 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 unit rule, or a fact.
This kind of rules ale also called Horn Clauses.
![Page 13: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/13.jpg)
S. Costantini / Datalog e ASP 13
DatalogTerminology:
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.
![Page 14: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/14.jpg)
S. Costantini / Datalog e ASP 14
DatalogTerminology:
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).
![Page 15: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/15.jpg)
S. Costantini / Datalog e ASP 15
Datalog Programs• A Datalog theory, or “program”, is a
collection of rules.• In a program, predicates can be
either1. EDB = Extensional Database = facts.2. IDB = Intensional Database = relation
defined by rules.
![Page 16: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/16.jpg)
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.
![Page 17: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/17.jpg)
S. Costantini / Datalog e ASP 17
Examplep(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.
![Page 18: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/18.jpg)
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).
![Page 19: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/19.jpg)
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 rulesr(a) s(a).r(b) s(b).
![Page 20: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/20.jpg)
S. Costantini / Datalog e ASP 20
DatalogGrounding of the programp(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 ModelM = {s(a),s(b),q(b),r(a),r(b),p(b)}
![Page 21: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/21.jpg)
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}
![Page 22: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/22.jpg)
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.
![Page 23: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/23.jpg)
S. Costantini / Datalog e ASP 23
Least Herbrand Model: Intuition
![Page 24: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/24.jpg)
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)}
![Page 25: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/25.jpg)
S. Costantini / Datalog e ASP 25
Least Herbrand Model: How to find itp 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
![Page 26: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/26.jpg)
S. Costantini / Datalog e ASP 26
Datalog con Negazione
![Page 27: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/27.jpg)
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).
![Page 28: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/28.jpg)
S. Costantini / Datalog e ASP 28
Negated Subgoals: Intuition
• Given atom A, not A holds if A cannot be proved
• Negation as finite failure
![Page 29: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/29.jpg)
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.
![Page 30: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/30.jpg)
S. Costantini / Datalog e ASP 30
Cos’è un modello minimo: è un insieme di atomi
Data una regolaA:- 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
![Page 31: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/31.jpg)
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
![Page 32: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/32.jpg)
S. Costantini / Datalog e ASP 32
Safe Rules• A rule is safe if:
1. Each variable in the head2. 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.
![Page 33: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/33.jpg)
S. Costantini / Datalog e ASP 33
Example: Unsafe Rules
• Each of the following is unsafe and not allowed:
1. s(X) :- r(Y).
2. s(X) :- r(Y), not r(X).
3. 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.
![Page 34: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/34.jpg)
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.
![Page 35: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/35.jpg)
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à
![Page 36: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/36.jpg)
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)
![Page 37: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/37.jpg)
S. Costantini / Datalog e ASP 37
DATALOG + Negazione• Semantica degli Answer Sets• Paradigma dell’Answer Set
Programming (ASP)• Negazione, insiemi-risposta
alternativi• Numerosi solver
![Page 38: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/38.jpg)
S. Costantini / Datalog e ASP 38
Esempio NMRpersona(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
![Page 39: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/39.jpg)
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.
![Page 40: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/40.jpg)
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
![Page 41: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/41.jpg)
S. Costantini / Datalog e ASP 41
Risultato di smodels (inference engine per NMR)
• Answer: 1malato(giorgio) a_casa(giorgio) a_casa(carlo)
in_ufficio(anna)
• Answer: 2malato(giorgio) a_casa(giorgio)
in_ufficio(carlo) in_ufficio(anna)
![Page 42: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/42.jpg)
S. Costantini / Datalog e ASP 42
Risultato di smodels (inference engine per NMR)
• Answer: 3malato(giorgio) a_casa(giorgio)
in_ufficio(carlo) a_casa(anna) • Answer: 4malato(giorgio) a_casa(giorgio) a_casa(carlo)
a_casa(anna)
![Page 43: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/43.jpg)
S. Costantini / Datalog e ASP 43
Datalog con negazione: nuovo Paradigma
di Programmazione Logica Answer Set Programming
(ASP)basato su Answer Set
Semantics
![Page 44: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/44.jpg)
S. Costantini / Datalog e ASP 44
Uno sguardo sull’Answer Set Programming (ASP)
![Page 45: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/45.jpg)
S. Costantini / Datalog e ASP 45
Example: 3-coloring in ASPAssigning 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).
![Page 46: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/46.jpg)
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
![Page 47: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/47.jpg)
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”
…
![Page 48: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/48.jpg)
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)
…
…
![Page 49: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/49.jpg)
S. Costantini / Datalog e ASP 49
Cosa si ottiene nell’esempio?
Answer1color(0,red), color(1,blue), color(green), color(3,red)
Answer2color(0,red), color(1,green), color(blue), color(3,red)…% e tutte le altre colorazioni, ognuna come % “Answer Set”
…
![Page 50: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/50.jpg)
S. Costantini / Datalog e ASP 50
Answer Set Semantics: su cosa si basano gli Answer Set e come si
ottengono
![Page 51: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/51.jpg)
S. Costantini / Datalog e ASP 51
Cos’è un modello minimo: è un insieme di atomi
Data una regolaA:- 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
![Page 52: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/52.jpg)
S. Costantini / Datalog e ASP 52
Modelli minimi Datalog• I fatti fanno parte di tutti i modelli
minimi• Tutto ciò che si deriva in modo
aciclico dai fatti fa parte di tutti i modelli minimi
• not A vale solo se non vale A
![Page 53: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/53.jpg)
S. Costantini / Datalog e ASP 53
Esempioa:- not b.
b:- e, not c.
c.
e.
M = {c, e, a}
![Page 54: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/54.jpg)
S. Costantini / Datalog e ASP 54
Esempioa:- not b.
b:- not a.
d:- c.
c
M1 = {c, d, a} M2 = {c, d, b}
Cicli: vari modelli (diversi Answer Sets)
![Page 55: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/55.jpg)
S. Costantini / Datalog e ASP 55
Cos’è un Answer Set• E’ un modello minimo dove però dati
due qualunque atomi che ne fanno parte, nessuno di essi può dipendere (direttamente o indirettamente) dalla negazione dell’altro
• (negazione coerente)
![Page 56: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/56.jpg)
S. Costantini / Datalog e ASP 56
Esempio
a:- not b.
b:- not a.
p:- not p, not a.
{b,p} modello minimo, NO answer set
{a} modello minimo, answer set
![Page 57: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/57.jpg)
S. Costantini / Datalog e ASP 57
Come si trovano gli Answer Set
• Cicli pari (dipendenze cicliche che coinvolgono un numero pari di atomi)
a:- not b.
b:- not a.
M1 = {a} M2 = {b}
![Page 58: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/58.jpg)
S. Costantini / Datalog e ASP 58
Come si trovano gli Answer Set
• Cicli pari a:- not b.
b:- not c.
c:- not d.
d:- not a.
M1 = {a,c} M2 = {b,d}
![Page 59: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/59.jpg)
S. Costantini / Datalog e ASP 59
Come si trovano gli Answer Set
• Ogni ciclo pari ha due Answer Set, uno costituito dagli atomi “pari” del ciclo, l’altro da quelli “dispari”.
• Se vi sono diversi cicli pari, gli answer set si combinano
![Page 60: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/60.jpg)
S. Costantini / Datalog e ASP 60
Come si trovano gli Answer Set
a:- not b. e:- not f.
b:- not c. f:- not e.
c:- not d.
d:- not a.
M11 = {a,c,e} M12 = {a,c,f}
M21 = {b,d,e} M22 = {b,d,f}
![Page 61: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/61.jpg)
S. Costantini / Datalog e ASP 61
Come si trovano gli Answer Set
• Cicli dispari (dipendenze cicliche che coinvolgono un numero dispari di atomi)
p:- not p.
No Answer Set: contraddizione sulla negazione {p}
![Page 62: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/62.jpg)
S. Costantini / Datalog e ASP 62
Come si trovano gli Answer Set
• Cicli dispari (dipendenze cicliche che coinvolgono un numero dispari di atomi)
a:- not b.b:- not c.c:- not a.No Answer Set: contraddizione sulla negazione
Es. {a,c} {a,b} {b,c}
![Page 63: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/63.jpg)
S. Costantini / Datalog e ASP 63
Come si trovano gli Answer Set
• Programmi con cicli dispari: possono avere Answer Set solo se:– vi sono anche cicli pari– vi sono connessioni fra cicli pari e dispari
che “rimuovono” le contraddizioni
• Chiamiamo queste speciali connessioni “Handle”
![Page 64: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/64.jpg)
S. Costantini / Datalog e ASP 64
Come si trovano gli Answer Set: OR Handle
• OR Handle: legata a regola alternativa per uno degli atomi del ciclo dispari
a:- not b. p:- not p.
b:- not a. p:- a. OR handle
M = {a,p}
![Page 65: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/65.jpg)
S. Costantini / Datalog e ASP 65
Come si trovano gli Answer Set
• OR Handle: legata a regola alternativa per uno degli atomi del ciclo dispari
a:- not b. p:- not q.b:- not a. q:- not r.
r:- not p.q:- not b. OR handle
M = {a,q,r}
![Page 66: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/66.jpg)
S. Costantini / Datalog e ASP 66
Come si trovano gli Answer Set
• OR handle: regola alternativa per un qualche atomo che fa parte del ciclo dispari. Il corpo deve essere VERO per “supportare” l’atomo
![Page 67: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/67.jpg)
S. Costantini / Datalog e ASP 67
Come si trovano gli Answer Set
• AND Handle: letterale aggiuntivo in una delle regole del ciclo dispari:
a:- not b. p:- not p, not a. AND
b:- not a. handle
M = {a}
![Page 68: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/68.jpg)
S. Costantini / Datalog e ASP 68
Come si trovano gli Answer Set
• Cicli dispari: Answer set se Handle da cicli pari
a:- not b. p:- not p, a. AND
b:- not a. handle
M = {b}
![Page 69: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/69.jpg)
S. Costantini / Datalog e ASP 69
Come si trovano gli Answer Set
• Cicli dispari: Answer set se Handle da cicli pari
• AND handle: letterale L (A oppure not A) nel corpo di una regola del ciclo dispari, dove A fa parte di un ciclo pari. L deve essere FALSO per “aprire” il ciclo dispari rendendo falso uno dei suoi atomi
![Page 70: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/70.jpg)
S. Costantini / Datalog e ASP 70
Come si trovano gli Answer Set
1. Si cercano gli Answer Set della parte aciclica “di base” del programma
2. Si cercano gli Answer Set dei cicli pari3. Si combinano le due componenti 4. Si estendono alla parte aciclica “intermedia” (se
c’è)5. Fra gli Answer Set risultanti si selezionano quelli
(se vi sono) che forniscono handle ai cicli dispari6. Si riconsidera la parte aciclica “top” rimanente
(se c’è)
![Page 71: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/71.jpg)
S. Costantini / Datalog e ASP 71
Come si trovano gli Answer Set
c. p:- not p.
d:- c. p:- e.
a:- not b. g:- p.
b:- not a.
e:- b.
h:- b,p.
![Page 72: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/72.jpg)
S. Costantini / Datalog e ASP 72
Come si trovano gli Answer Set: parte aciclica di base
c.
d:- c.
M1 = {c,d}
![Page 73: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/73.jpg)
S. Costantini / Datalog e ASP 73
Come si trovano gli Answer Set: ciclo pari e parte aciclica
intermedia
a:- not b.
b:- not a.
e:- b.
M1 = {c,d}
M11 = {a,c,d} M12 = {b,c,d,e}
![Page 74: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/74.jpg)
S. Costantini / Datalog e ASP 74
Come si trovano gli Answer Set: ciclo dispari e parte aciclica”top”
p:- not p.
p:- e.
g:- p.
• Si seleziona M12 per dare la OR handle all’atomo p.
M = {b,c,d,e,p,g}
![Page 75: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/75.jpg)
S. Costantini / Datalog e ASP 75
Caso problematicop:- not p, not a.
q:- not q, not b.
a:- not b.
b:- not a.
Per ciclo pari M1 = {a} M2 = {b}
ma non abbiamo le due AND handle per i cicli dispari (richiesta contraddittoria): no Answer Set
![Page 76: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/76.jpg)
S. Costantini / Datalog e ASP 76
Caso problematico Risoltop:- not p, not a.p:- e.q:- not q, not b.a:- not b. e:- not f.b:- not a. f:- not e.Per cicli pari M11 = {a,e} M12 = {a,f}M21 = {b,e} M12 = {b,f}Con M21 otteniamo M1 = {b,p,e}
![Page 77: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/77.jpg)
S. Costantini / Datalog e ASP 77
ASP più in dettaglio
![Page 78: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/78.jpg)
S. Costantini / Datalog e ASP 78
Answer Set Programming
Constraints
:- v,w,z.
rephrased as
p :- not p, v,w,z.
![Page 79: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/79.jpg)
S. Costantini / Datalog e ASP 79
Answer Set Programming
Disjunction
v | w |z.
rephrased as
v :- not w, not z.
w :- not v, not z.
z :- not v, not w.
![Page 80: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/80.jpg)
S. Costantini / Datalog e ASP 80
Answer Set Programming
Choice (XOR)
v+w+z.
rephrased as
v | w | z.
:- w, z.
:- v, z.
:- v, w.
![Page 81: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/81.jpg)
S. Costantini / Datalog e ASP 81
Answer Set Programming
Classical Negation ¬p
- p :- q,r.
rephrased as
p' :- q,r.
:- p,p‘
![Page 82: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/82.jpg)
S. Costantini / Datalog e ASP 82
Answer Set Programming
A party: guests that hate each other cannot seat together,
guests that love each other should sit together
table(1..3).guest(1..5).
hates(1,3). hates(2,4).hates(1,4). hates(2,3). hates(4,5). hates(1,5).likes(2,5).
![Page 83: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/83.jpg)
S. Costantini / Datalog e ASP 83
Answer Set Programming
% Choice rules
1{at_table(P,T) : table(T)}1 :- guest(P).
0{at_table(P,T) : guest(P)}3 :- table(T).
![Page 84: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/84.jpg)
S. Costantini / Datalog e ASP 84
Answer Set Programming
% Choice rules
n { p(X,Y) : d1(X) } m :- d2(Y).
Meaning: forall Y which is a d2 we admit only answer sets with at least n atoms and at most m atoms
of the form p(X,Y), where X is a d1
![Page 85: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/85.jpg)
S. Costantini / Datalog e ASP 85
Answer Set Programming
% hard constraint
:- hates(P1,P2),at_table(P1,T),at_table(P2,T), guest(P1),guest(P2),table(T).
![Page 86: DatalogDatalog S. Costantini. S. Costantini / Datalog e ASP2 Datalog base: notazione, esempi, semantica](https://reader037.vdocument.in/reader037/viewer/2022103113/5542eb49497959361e8b4b03/html5/thumbnails/86.jpg)
S. Costantini / Datalog e ASP 86
Answer Set Programming
% should be a soft constraint!
:- likes(P1,P2), at_table(P1,T1), at_table(P2,T2),
T1 != T2, guest(P1),
guest(P2),table(T1),table(T2).