Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An...
Transcript of Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An...
![Page 1: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/1.jpg)
Beyond Basic Logic Programming
![Page 2: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/2.jpg)
Basic Logic Programming
• Datasets
• Queries
• Updates
• View Definitions
• Operations
![Page 3: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/3.jpg)
Beyond Basic Logic Programming
• View definitions• No disjunctions in the head• Safe and stratified
• Efficiency of computation• Constraint logic programs• Existential rules
• Updates• Updates to the logic program• Constraint checking
![Page 4: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/4.jpg)
Beyond Basic Logic Programming
• View definitions• No disjunctions in the dataset (and rule heads)• Safe and stratified
• Efficiency of computation• Constraint logic programs• Existential rules
• Updates• Updates to the logic program• Constraint checking
![Page 5: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/5.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
![Page 6: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/6.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Base: {male(joe), female(joe)}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
![Page 7: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/7.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Base: {male(joe), female(joe)}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
![Page 8: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/8.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Base: {male(joe), female(joe)}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
![Page 9: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/9.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Base: {male(joe), female(joe)}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
![Page 10: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/10.jpg)
Semantics
• An interpretation satisfies a ground atom , if
• An interpretation satisfies a ground negation , if
![Page 11: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/11.jpg)
Semantics
• An interpretation satisfies a ground atom , if
• An interpretation satisfies a ground negation , if
Closed World Assumption
![Page 12: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/12.jpg)
Semantics
• An interpretation satisfies a ground atom , if
• An interpretation satisfies a ground negation , if
….
• An interpretation satisfies an arbitrary logic program if and only if satisfies every ground instance of every sentence in .
![Page 13: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/13.jpg)
Semantics
• An interpretation satisfies a ground atom , if
• An interpretation satisfies a ground negation , if
….
• An interpretation satisfies an arbitrary logic program if and only if satisfies every ground instance of every sentence in .
• A factoid is logically entailed by a closed logic program if and only if it is true in every model of the program, i.e., the set of conclusions is the intersection of all models of the program.
![Page 14: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/14.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
Models
![Page 15: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/15.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
Minimal Models
A factoid is logically entailed by a closed logic program if and only if it is true in every model of the program,
![Page 16: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/16.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
Minimal Models
Is male(joe) true? Is female(joe) true?
A factoid is logically entailed by a closed logic program if and only if it is true in every model of the program,
![Page 17: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/17.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
Minimal Models
Is male(joe) true? NoIs female(joe) true? No
A factoid is logically entailed by a closed logic program if and only if it is true in every model of the program,
![Page 18: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/18.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
Minimal Models
Is male(joe) true? NoIs female(joe) true? NoIs ~male(joe) true?Is ~female(joe) true?
A factoid is logically entailed by a closed logic program if and only if it is true in every model of the program,
![Page 19: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/19.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
Minimal Models
Is male(joe) true? NoIs female(joe) true? NoIs ~male(joe) true? YesIs ~female(joe) true? Yes
A factoid is logically entailed by a closed logic program if and only if it is true in every model of the program,
![Page 20: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/20.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
Minimal Models
Is male(joe) true? NoIs female(joe) true? NoIs ~male(joe) true? YesIs ~female(joe) true? YesInconsistent
A factoid is logically entailed by a closed logic program if and only if it is true in every model of the program,
![Page 21: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/21.jpg)
Semantics
• An interpretation satisfies a ground atom , if
• An interpretation satisfies a ground negation , if
![Page 22: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/22.jpg)
Generalized Closed World Assumptions
• H: Herbrand Base
• D: Definite facts are a union of• Set of all facts that are true in all the models
• Set of all facts that are false in all the models
• I : Indefinite facts are H-D
![Page 23: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/23.jpg)
Generalized Closed World Assumption
• An interpretation satisfies a ground atom , if
• An interpretation satisfies a ground negation , if
• An interpretation satisfies a ground disjunction 1,…, n, if satisfies at least one i.
• An interpretation satisfies an arbitrary logic program if and only if satisfies every ground instance of every sentence in .
• A factoid is logically entailed by a closed logic program if and only if it is true in every model of the program, i.e., the set of conclusions is the intersection of all models of the program.
A fact or its negation is true only if it appears in the definite set
![Page 24: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/24.jpg)
Generalized Closed World Assumption
• An interpretation satisfies a ground atom , if
• An interpretation satisfies a ground negation , if
• An interpretation satisfies a ground disjunction 1,…, n, if satisfies at least one i.
• An interpretation satisfies an arbitrary logic program if and only if satisfies every ground instance of every sentence in .
• A factoid is logically entailed by a closed logic program if and only if it is true in every model of the program, i.e., the set of conclusions is the intersection of all models of the program.
A fact or its negation is true only if it appears in the definite set
![Page 25: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/25.jpg)
Generalized Closed World Assumption
• An interpretation satisfies a ground atom , if
• An interpretation satisfies a ground negation , if
• An interpretation satisfies a ground disjunction 1,…, n, if satisfies at least one i.
• An interpretation satisfies an arbitrary logic program if and only if satisfies every ground instance of every sentence in .
• A factoid is logically entailed by a closed logic program if and only if it is true in every model of the program, i.e., the set of conclusions is the intersection of all models of the program.
only if it appears in the definite set
![Page 26: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/26.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
Definite facts: Facts that are true or false in all the minimal modelsIndefinite facts: remaining facts
![Page 27: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/27.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
Definite facts: {}Indefinite facts: {male(joe), female(joe)}
![Page 28: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/28.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
Definite facts: {}Indefinite facts: {male(joe), female(joe)}
Is male(joe) true? Is female(joe) true?
![Page 29: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/29.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
Definite facts: {}Indefinite facts: {male(joe), female(joe)}
Is male(joe) true? NoIs female(joe) true? No
![Page 30: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/30.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
Definite facts: {}Indefinite facts: {male(joe), female(joe)}
Is male(joe) true? NoIs female(joe) true? NoIs ~male(joe) true?Is ~female(joe) true?
![Page 31: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/31.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
Definite facts: {}Indefinite facts: {male(joe), female(joe)}
Is male(joe) true? NoIs female(joe) true? NoIs ~male(joe) true? NoIs ~female(joe) true?No
![Page 32: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/32.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
Definite facts: {}Indefinite facts: {male(joe), female(joe)}
Is male(joe) true? NoIs female(joe) true? NoIs ~male(joe) true? NoIs ~female(joe) true?NoIs male(joe) | female(joe)true?
![Page 33: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/33.jpg)
Disjunctive Logic Programs
male(joe) | female (joe)
Herbrand Universe: {joe}
Herbrand Interpretations:
{male(joe)}
{female(joe)}
{male(joe),female(joe)}
{}
Definite facts: {}Indefinite facts: {male(joe), female(joe)}
Is male(joe) true? NoIs female(joe) true? NoIs ~male(joe) true? NoIs ~female(joe) true?NoIs male(joe) | female(joe)true? Yes
![Page 34: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/34.jpg)
Disjunctive Logic Programs
• Model intersection property breaks down• ie, intersection of all the minimal models is not a model
• Generalized Closed World Assumption is a possible solution• Explicitly keep record of definite and indefinite facts
![Page 35: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/35.jpg)
Beyond Basic Logic Programming
• Limitations on view definitions• No disjunctions in the dataset • Safe and stratified
• Efficiency of computation• Constraint logic programs• Existential rules
• Updates• Updates to the logic program• Constraint checking
![Page 36: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/36.jpg)
Constraint Logic Programs
• Consider Peano Arithmetic (Section 10.2 of the textbook)
number(0)number(s(X)) :- number(X)
add(0,Y,Y) :- number(Y)add(s(X),Y,s(Z)) :- add(X,Y,Z)
![Page 37: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/37.jpg)
Constraint Logic Programs
• Consider Peano Arithmetic
number(0)number(s(X)) :- number(X)
add(0,Y,Y) :- number(Y)add(s(X),Y,s(Z)) :- add(X,Y,Z)
number(L) & number(M) & add(L,M,N) & add(L,M,s(N))
![Page 38: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/38.jpg)
Constraint Logic Programs
• Consider Peano Arithmetic
number(0)number(s(X)) :- number(X)
add(0,Y,Y) :- number(Y)add(s(X),Y,s(Z)) :- add(X,Y,Z)
number(L) & number(M) & add(L,M,N) & add(L,M,s(N))
Runs forever in the standard LP evaluation algorithm
![Page 39: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/39.jpg)
Constraint Logic Programs
• Consider Peano Arithmetic
number(0)number(s(X)) :- number(X)
add(0,Y,Y) :- number(Y)add(s(X),Y,s(Z)) :- add(X,Y,Z)
number(L) & number(M) & add(L,M,N) & add(L,M,s(N))
Runs forever in the standard LP evaluation algorithmSolution: Check satisfaction of constraints at each step
![Page 40: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/40.jpg)
Constraint Logic Programs
• Direct expression of constraints
sumto(0,0) 0
sumto(1,1) 0+1
sumto(2,3) 0+1+2
sumto(3,6) 0+1+2+3
![Page 41: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/41.jpg)
Constraint Logic Programs
• Direct expression of constraints
sumto(0,0)sumto(N,S) :- N ≥ 1 & N ≤ S & sumto(N-1,S-1)
![Page 42: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/42.jpg)
Constraint Logic Programs
• Direct expression of constraints
sumto(0,0)sumto(N,S) :- N ≥ 1 & N ≤ S & sumto(N-1,S-1)
Prove: S <= 1
N = N1 & S = S1 & N1 ≥ 1 & N1 ≤ S1 & sumto(N1-1,S1-1)
![Page 43: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/43.jpg)
Constraint Logic Programs
• Many problems can be naturally expressed as constraints• Map coloring
• SEND MORE MONEY
• Constraints with floating point numbers
• Distributed constraints
• Constraint optimization (Assignment 4.3)
![Page 44: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/44.jpg)
Beyond Basic Logic Programming
• Limitations on view definitions• No disjunctions in the dataset • Safe and stratified
• Efficiency of computation• Constraint logic programs• Existential rules
• Updates• Updates to the logic program• Constraint checking
![Page 45: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/45.jpg)
![Page 46: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/46.jpg)
![Page 47: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/47.jpg)
![Page 48: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/48.jpg)
Answer Set Semantics
• Defining semantics for programs that *may not* be stratified
![Page 49: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/49.jpg)
Answer Set Semantics
• Defining semantics for programs that *may not* be stratified• To check if a set S of atoms is an answer set of a program, compute the reduct
of the grounded program as follows:• For any rule that contains negative atoms in the body that do not appear in S, we drop
those atoms from the rule, and retain only its positive atoms
• We drop rest of the rules
• We compute the extension of the rules
• If the extension is the same as S, then S is the answer set of the program
![Page 50: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/50.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
Is p(a,b) an answer set?p(b,a)
![Page 51: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/51.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
Is p(a,b) an answer set?p(b,a)
For any rule that contains negative atoms in the body that do not appear in S, we drop those atoms from the rule, and retain only its positive atoms
![Page 52: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/52.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
Is p(a,b) an answer set?p(b,a)
For any rule that contains negative atoms in the body that do not appear in S, we drop those atoms from the rule, and retain only its positive atoms
![Page 53: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/53.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
Is p(a,b) an answer set?p(b,a)
For any rule that contains negative atoms in the body that do not appear in S, we drop those atoms from the rule, and retain only its positive atoms
We drop the rest of the rules
We compute the extension of the rules
![Page 54: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/54.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
Is p(a,b) an answer set?p(b,a)
For any rule that contains negative atoms in the body that do not appear in S, we drop those atoms from the rule, and retain only its positive atoms
We drop the rest of the rules
We compute the extension of the rules
If the extension is the same as S, then S is the answer set of the program
p(a,b)p(b,a)r(a)r(b)
Extension =
![Page 55: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/55.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
Is p(a,b) an answer set?p(b,a)
For any rule that contains negative atoms in the body that do not appear in S, we drop those atoms from the rule, and retain only its positive atoms
We drop the rest of the rules
We compute the extension of the rules
If the extension is the same as S, then S is the answer set of the program
p(a,b)p(b,a)r(a)r(b)
Extension = No
![Page 56: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/56.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
Is p(a,b) an answer set?p(b,a)r(a)
![Page 57: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/57.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
Is p(a,b) an answer set?p(b,a)r(a)
For any rule that contains negative atoms in the body that do not appear in S, we drop those atoms from the rule, and retain only its positive atoms
![Page 58: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/58.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
For any rule that contains negative atoms in the body that do not appear in S, we drop those atoms from the rule, and retain only its positive atoms
Is p(a,b) an answer set?p(b,a)r(a)
![Page 59: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/59.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
For any rule that contains negative atoms in the body that do not appear in S, we drop those atoms from the rule, and retain only its positive atoms
We drop the rest of the rules
We compute the extension of the rules
Is p(a,b) an answer set?p(b,a)r(a)
![Page 60: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/60.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
For any rule that contains negative atoms in the body that do not appear in S, we drop those atoms from the rule, and retain only its positive atoms
We drop the rest of the rules
We compute the extension of the rules
If the extension is the same as S, then S is the answer set of the program
p(a,b)p(b,a)r(a)Extension =
Is p(a,b) an answer set?p(b,a)r(a)
![Page 61: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/61.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
For any rule that contains negative atoms in the body that do not appear in S, we drop those atoms from the rule, and retain only its positive atoms
We drop the rest of the rules
We compute the extension of the rules
If the extension is the same as S, then S is the answer set of the program
p(a,b)p(b,a)r(a)Extension =
Is p(a,b) an answer set?p(b,a)r(a)
Yes
![Page 62: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/62.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
Is p(a,b) an answer set?p(b,a)r(a), r(b)
![Page 63: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/63.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
Is p(a,b) an answer set?p(b,a)r(a), r(b)
For any rule that contains negative atoms in the body that do not appear in S, we drop those atoms from the rule, and retain only its positive atoms
![Page 64: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/64.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
For any rule that contains negative atoms in the body that do not appear in S, we drop those atoms from the rule, and retain only its positive atoms
Is p(a,b) an answer set?p(b,a)r(a), r(b)
![Page 65: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/65.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
For any rule that contains negative atoms in the body that do not appear in S, we drop those atoms from the rule, and retain only its positive atoms
We drop the rest of the rules
We compute the extension of the rules
Is p(a,b) an answer set?p(b,a)r(a), r(b)
![Page 66: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/66.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
For any rule that contains negative atoms in the body that do not appear in S, we drop those atoms from the rule, and retain only its positive atoms
We drop the rest of the rules
We compute the extension of the rules
If the extension is the same as S, then S is the answer set of the program
p(a,b)p(b,a)Extension =
Is p(a,b) an answer set?p(b,a)r(a), r(b)
![Page 67: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/67.jpg)
Example
Data Set
p(a,b) p(b,a)
Rules
r(X) :- p(X,Y) & ~r(Y)
Grounded program:
r(a) :- p(a,b) & ~r(b)
r(b) :- p(b,a) & ~r(a)
For any rule that contains negative atoms in the body that do not appear in S, we drop those atoms from the rule, and retain only its positive atoms
We drop the rest of the rules
We compute the extension of the rules
If the extension is the same as S, then S is the answer set of the program
p(a,b)p(b,a)Extension =
Is p(a,b) an answer set?p(b,a)r(a), r(b)
No
![Page 68: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/68.jpg)
![Page 69: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/69.jpg)
Implementing an Answer Set Solver
• Start with an empty answer set
• Add one atom at a time to the answer set
• Compute all the atoms that can be derived • If a contradiction is obtained abandon that answer set
• Repeat
![Page 70: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/70.jpg)
Implementing an Answer Set Solver
• For a rule r• head(r): atom in the head of the rule r
• positive(r): set of positive atoms in the body of the rule r
• negative(r): set of the negative atoms in the body of the rule r
![Page 71: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/71.jpg)
Implementing an Answer Set Solver
• For a rule r• head(r): atom in the head of the rule r
• positive(r): set of positive atoms in the body of the rule r
• negative(r): set of the negative atoms in the body of the rule r
If an atom does not appear in the head of any rule, it cannot appear in any answer set
![Page 72: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/72.jpg)
Implementing an Answer Set Solver
• For a rule r• head(r): atom in the head of the rule r
• positive(r): set of positive atoms in the body of the rule r
• negative(r): set of the negative atoms in the body of the rule r
If an atom does not appear in the head of any rule, it cannot appear in any answer set
If an atom appears in the answer set S, then there must exist a rule r such thatpositive(r) ⊆ Snegative(r) ⊈ S
![Page 73: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/73.jpg)
Implementing an Answer Set Solver
compute_answer_sets(P)
return solve(P, ∅, ∅)
solve(P, CS, CN)
if expand(P, CS, CN) = false then return ∅
⟨CS,CN⟩ ← expand(P,CS,CN)
Select an atom a ∉ CS ∪ CN
return solve(P,CS∪{a},CN) ∪ solve(P,CS,CN∪{a})
![Page 74: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/74.jpg)
Implementing an Answer Set Solver
expand(P, CS, CN)
repeat
change ← false
find all rules r such that
positive(r) ⊆ CS and negative(r) ⊆ CN
add head(r) to CS
change ← true
if all rules r with same head satisfy that
positive(r) ∩ CN ≠ ∅ or negative(r) ∩ CS ≠ ∅
add head(r) to CN
change ← true
until change is false
if CS ∩ CN = ∅ return ⟨CS,CN⟩ else return false
![Page 75: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/75.jpg)
Implementing an Answer Set Solver
p(a,b)p(b,a)r(a) :- p(a,b) & ~r(b)r(b) :- p(b,a) & ~r(a)
CS = ∅ CN = ∅expand(P, CS, CN)
repeat
change ← false
find all rules r such that
positive(r) ⊆ CS and negative(r) ⊆ CN
add head(r) to CS
change ← true
if all rules r with same head satisfy that
positive(r) ∩ CN ≠ ∅ or negative(r) ∩ CS ≠ ∅
add head(r) to CN
change ← true
until change is false
if CS ∩ CN = ∅ return ⟨CS,CN⟩ else return false
![Page 76: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/76.jpg)
Implementing an Answer Set Solver
p(a,b)p(b,a)r(a) :- p(a,b) & ~r(b)r(b) :- p(b,a) & ~r(a)
CS = ∅ CN = ∅
CS={p(a,b),p(b,a)}
expand(P, CS, CN)
repeat
change ← false
find all rules r such that
positive(r) ⊆ CS and negative(r) ⊆ CN
add head(r) to CS
change ← true
if all rules r with same head satisfy that
positive(r) ∩ CN ≠ ∅ or negative(r) ∩ CS ≠ ∅
add head(r) to CN
change ← true
until change is false
if CS ∩ CN = ∅ return ⟨CS,CN⟩ else return false
![Page 77: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/77.jpg)
Implementing an Answer Set Solver
p(a,b)p(b,a)r(a) :- p(a,b) & ~r(b)r(b) :- p(b,a) & ~r(a)
CS = ∅ CN = ∅
CS={p(a,b),p(b,a)}
expand(P, CS, CN)
repeat
change ← false
find all rules r such that
positive(r) ⊆ CS and negative(r) ⊆ CN
add head(r) to CS
change ← true
if all rules r with same head satisfy that
positive(r) ∩ CN ≠ ∅ or negative(r) ∩ CS ≠ ∅
add head(r) to CN
change ← true
until change is false
if CS ∩ CN = ∅ return ⟨CS,CN⟩ else return false CS={p(a,b),p(b,a)} CN = ∅
![Page 78: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/78.jpg)
Implementing an Answer Set Solver
p(a,b)p(b,a)r(a) :- p(a,b) & ~r(b)r(b) :- p(b,a) & ~r(a)
CN = ∅CS={p(a,b),p(b,a),r(a)}expand(P, CS, CN)
repeat
change ← false
find all rules r such that
positive(r) ⊆ CS and negative(r) ⊆ CN
add head(r) to CS
change ← true
if all rules r with same head satisfy that
positive(r) ∩ CN ≠ ∅ or negative(r) ∩ CS ≠ ∅
add head(r) to CN
change ← true
until change is false
if CS ∩ CN = ∅ return ⟨CS,CN⟩ else return false
CS={p(a,b),p(b,a),r(a)} CN = {r(b)}
CS={p(a,b),p(b,a),r(a)} CN = {r(b)}
![Page 79: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/79.jpg)
Implementing an Answer Set Solver
p(a,b)p(b,a)r(a) :- p(a,b) & ~r(b)r(b) :- p(b,a) & ~r(a)
CN = r(a)CS={p(a,b),p(b,a)}
CS={p(a,b),p(b,a), r(b)} CN = {r(a)}
CS={p(a,b),p(b,a), r(b)} CN = {r(a)}
expand(P, CS, CN)
repeat
change ← false
find all rules r such that
positive(r) ⊆ CS and negative(r) ⊆ CN
add head(r) to CS
change ← true
if all rules r with same head satisfy that
positive(r) ∩ CN ≠ ∅ or negative(r) ∩ CS ≠ ∅
add head(r) to CN
change ← true
until change is false
if CS ∩ CN = ∅ return ⟨CS,CN⟩ else return false
![Page 80: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/80.jpg)
Available Answer Set Solvers
CLINGO DLV
![Page 81: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/81.jpg)
Extensions to ASP
• Choice rule• Disjunctions
• Constraints
• Classical negation
![Page 82: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/82.jpg)
Choice Rule
• Enclose a set of atoms in curly braces• Choose in all possible ways which atoms will be included in the answer set
{ p(1), p(2) }
Possible answer sets are ∅,{p(1)}, {p(2)}, {p(1), p(2)}
![Page 83: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/83.jpg)
Choice Rule
• Enclose a set of atoms in curly braces• Choose in all possible ways which atoms will be included in the answer set
• Can also indicate bounds on the number of atoms to be included
{ p(1), p(2) }
Possible answer sets are ∅,{p(1)}, {p(2)}, {p(1), p(2)}
1 { p(1), p(2) } 1
Possible answer sets are {p(1)}, {p(2)}
![Page 84: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/84.jpg)
Constraint
• A rule with an empty head
{ p(1), p(2) }Possible answer sets are ∅,{p(1)}, {p(2)}, {p(1), p(2)}
:- p(1), ~p(2)Possible answer sets are ∅, {p(2)}, {p(1), p(2)}
![Page 85: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/85.jpg)
Constraint
• A rule with an empty head• A constraint is an unstratified rule
• Stratification is defined only for rules with a head• Therefore, we have to convert a constraint to a rule with a head
:- pq :- p, ~q
![Page 86: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/86.jpg)
Classical Negation
• The predicates can have a classical negation symbol in front of them• -p(a) indicates that we know for sure that p(a) is false
• ~p(a) indicates that p(a) could be true or false
• Two negation operators can be related• -p :- ~p
![Page 87: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/87.jpg)
Beyond Basic Logic Programming
• Limitations on view definitions• No disjunctions in the dataset (and rule heads)• Safe and stratified
• Efficiency of computation• Constraint logic programs• Existential rules
• Updates• Updates to the logic program• Constraint checking
![Page 88: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/88.jpg)
Existential Rules
• A rule that has a functional term in its head
owns(X,house(X)) :- instance_of(X,person)
has_parent(X,f(X)) :- instance_of(X,person)
has_parent(X,g(X)) :- instance_of(X,male)
![Page 89: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/89.jpg)
Existential Rules
• In the context of database systems
Also known as:
Tuple generating dependencies (in relational databases)
has parent
john peter
sue peter
peter ??
… …
![Page 90: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/90.jpg)
Existential Rules
• In the context of description logic systems
Person ⊓ (has_parent.Person)
Also known as:
Existential rules
![Page 91: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/91.jpg)
Problems with Existential Rules
• Termination
has_parent(X,f(X)) :- instance_of(X,person)
Unrestricted application of this rule leads to infinite recursion
![Page 92: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/92.jpg)
Problems with Existential Rules
• Under-specification when used with a class hierarchy
has_parent(X,f(X)) :- instance_of(X,person)
subclass_of(male,person)
has_parent(X,g(X)) :- instance_of(X,male)
What is the relationship between f(X) and g(X)?
![Page 93: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/93.jpg)
Solutions for Existential Rules
• Ensure termination by design
• Limit depth of reasoning
• Rule strengthening
![Page 94: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/94.jpg)
Beyond Basic Logic Programming
• Limitations on view definitions• No disjunctions in the dataset (and rule heads)• Safe and stratified
• Efficiency of computation• Constraint logic programs• Existential rules
• Updates• Updates to the logic program• Constraint checking
![Page 95: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/95.jpg)
Updates
• What if the view definitions themselves need to be updated?• Naturally happens during rule authoring
• Dropping a relation used in multiple rules
• What if an update to the dataset violates some constraint?• For example, asserting two fathers of a person using a dynamic rule
![Page 96: Beyond Basic Logic Programminglogicprogramming.stanford.edu/lectures/extensions.pdf · •An interpretation satisfies an arbitrary logic program if and only if satisfies every ground](https://reader034.fdocuments.in/reader034/viewer/2022050512/5f9ce8ee27d46f06f25dc01a/html5/thumbnails/96.jpg)
Beyond Basic Logic Programming
• Limitations on view definitions• No disjunctions in the dataset (and rule heads)• Safe and stratified
• Efficiency of computation• Constraint logic programs• Existential rules
• Updates• Updates to the logic program• Constraint checking