Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, collecting solutions, assert/retract.

Slides:



Advertisements
Similar presentations
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Advertisements

Higher Order Predicates Higher order predicates in PROLOG. Higher order declarative predicates : – findall – bagof – setof – =.. Higher order non-declarative.
Logic Programming Lecture 1: Course orientation; Getting started with Prolog.
Logic Programming Lecture 9: Constraint logic programming.
Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines
Prolog Nonmonotonic logic.
1 Chapter 7: Controlling Search. 2 Controlling Search u Estimating Efficiency of a CLP Program u Rule Ordering u Literal Ordering u Adding Redundant Constraints.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Logic Programming (cont’d): Lists Lists in Prolog are represented by a functor (similar to cons in Scheme): 1.The empty list is represented by the constant.
”assert” and ”retract”
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
Logic Programming Lecture 3: Recursion, lists, and data structures.
Answer Set Programming Overview Dr. Rogelio Dávila Pérez Profesor-Investigador División de Posgrado Universidad Autónoma de Guadalajara
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 11: Database Manipulation and Collecting Solutions Theory –Discuss database manipulation in.
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, collecting solutions, assert/retract.
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,
Side effects in Prolog Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
For Monday Take home exam due Exam 1. For Wednesday Read chapter 10, sections 1-2 Prolog Handout 4.
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
Logic Programming Lecture 3: Recursion, lists, and data structures.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
”assert” and ”retract”  Modifying Programs Dynamically  Prolog’s Execution Tree.
Inference and Resolution for Problem Solving
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
4.3 Systems of Equations - Elimination Objective: The student will be able to: Solve systems of equations using elimination with addition and subtraction.
CSE 311 Foundations of Computing I Lecture 6 Predicate Logic, Logical Inference Spring
Logic Programming Lecture 6: Parsing, Difference Lists and Definite Clause Grammars.
Formal Models of Computation Part II The Logic Model
Logic Programming Lecture 7: Search Strategies: Problem representations Depth-first, breadth-first, and AND/OR search.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
For Wednesday Read chapter 10 Prolog Handout 4. Exam 1 Monday Take home due at the exam.
Logic Programming Programming Lecture 1: Getting started with Prolog.
Warm up Add the following polynomials x + 2y = 10 5y – x = 7 + 4x – 3y = 1 + 9y + 4x = -1.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
Lecture 9: Constraint logic programming
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
Control Algorithms 1 Chapter 6 Control Algorithms 1 Chapter 6 Pattern Search.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, assert/retract, collecting solutions.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Cs774 (Prasad)L6Backtracking1 Controlling Backtracking : Cuts
Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate.
Solving Systems of Equations By Elimination. Warm – up!! *As you walk in, please pick up your calculator!!* Use substitution to solve the following systems.
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
For Friday No reading Prolog Handout 2. Homework.
1 Knowledge Based Systems (CM0377) Lecture 9 (Last modified 11th April 2002)
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Answer Extraction To use resolution to answer questions, for example a query of the form  X C(X), we must keep track of the substitutions made during.
1 TP #2: How does Prolog answer questions? n Miscellaneous info; n Last TP exercises solved; n How does Prolog answer questions? n Recursive Prolog programs;
Logic Programming Lecture 7: Search Strategies:
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
Logic Programming Lecture 2: Unification and proof search.
Objective I can solve systems of equations using elimination with addition and subtraction.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Predicate logic CSC 333.
Solving Systems – Choose Your Method
Negations of quantifiers
Solving Systems of Equations
Solving Systems of Equations
Presentation transcript:

Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, collecting solutions, assert/retract

James CheneyLogic ProgrammingOctober 9, 2014 Outline for today Nonlogical features continued Negation-as-failure Collecting solutions (findall, setof, bagof) Assert/retract

James CheneyLogic ProgrammingOctober 9, 2014 Negation-as-failure We can use cut to define negation-as- failure recall Tutorial #1 not(G) :- G, !, fail ; true. This tries to solve G If successful, fail Otherwise, succeed

James CheneyLogic ProgrammingOctober 9, 2014 true How it works member(1,[2,1]), !, fail. member(1,[1]), !, fail. (X=1) (X = 1, L = [1]) not(member(1,[2,1]) ! done fail

James CheneyLogic ProgrammingOctober 9, 2014 true How it works member(5,[2,1]), !, fail. member(5,[1]), !, fail. not(member(5,[2,1]) done member(5,[]), !, fail.

James CheneyLogic ProgrammingOctober 9, 2014 Negation-as-failure Built-in syntax: \+(G) Example: people that are not teachers q(X) :- person(X), \+(teaches(X,Y)).

James CheneyLogic ProgrammingOctober 9, 2014 Behavior person(a). person(b). teaches(a,b). q(X) done person(X), \+(teaches(X,Y)) X = aX = b \+(teaches(a,Y)) teaches(a,Y) done \+(teaches(b,Y)) teaches(b,Y') q(X) :- person(X), \+(teaches(X,Y)). Y= b

James CheneyLogic ProgrammingOctober 9, 2014 X = a Y = b Behavior person(a). person(b). teaches(a,b). q(X) done \+(teaches(X,Y)),person(X) person(X) q(X) :- \+(teaches(X,Y)), person(X). teaches(X,Y)

James CheneyLogic ProgrammingOctober 9, 2014 Searching a graph find(X,X). find(X,Z) :- edge(X,Y), find(Y,Z). Problem: Loops easily if graph is cyclic: edge(a,b). edge(b,c). edge(c,a).

James CheneyLogic ProgrammingOctober 9, 2014 Searching a graph (2) To avoid looping: 1. Remember where we have been 2. Stop if we try to visit a node we've already seen. find2(X,X,_). find2(X,Z,P) :- \+(member(X,P)), edge(X,Y), find2(Y,Z,[X|P]). Note: Needs mode (+,?,+).

James CheneyLogic ProgrammingOctober 9, 2014 Safe use of negation- as-failure As with cut, negation-as-failure can have non-logical behavior Goal order matters \+(X = Y), X = a, Y = b fails X = a, Y = b, \+(X = Y) succeeds

James CheneyLogic ProgrammingOctober 9, 2014 Safe use of negation as failure (2) Can read \+(G) as "not G" only if G is ground when we start solving it Any free variables "existentially quantified" \+(1=2) == 1 ≠ 2 \+(X=Y) == ∃ X, Y. X ≠ Y General heuristic: delay negation after other goals to make negated goals ground

James CheneyLogic ProgrammingOctober 9, 2014 Collecting solutions We'd like to find all solutions collected as an explicit list alist(bart,X) = "X lists the ancestors of bart" Can't do this in pure Prolog cut doesn't help Technically possible (but painful) using assert/retract

James CheneyLogic ProgrammingOctober 9, 2014 Collecting solutions, declaratively Built-in predicate to do same thing: findall/3 - list of solutions ?- findall(Y,ancestor(Y,bart),L). L = [homer,marge,abe,jacqueline] ?- findall((X,Y),ancestor(X,Y),L). L = [(abe,homer),(homer,bart),(homer,lisa),(homer,ma ggie)|...]

James CheneyLogic ProgrammingOctober 9, 2014 findall/3 Usage: findall(?X, ?Goal, ?List) On success, List is list of all substitutions for X that make Goal succeed. Goal can have free variables! X treated as "bound" in G (X could also be a "pattern"...)

James CheneyLogic ProgrammingOctober 9, 2014 bagof/3 - list of solutions ?- bagof(Y,ancestor(Y,bart),L). L = [homer,marge,abe,jacqueline] different instantiations of free variables lead to different answers | ?- bagof(Y,ancestor(Y,X),L). L = [homer,marge,abe,jacqueline], X = bart ? ; L = [abe], X = homer ?... bagof/3

James CheneyLogic ProgrammingOctober 9, 2014 Quantification In goal part of a bagof/3, we can write: X^G(X) to "hide" (existentially quantify) X. | ?- bagof(Y,X^ancestor(X,Y),L). L = [homer,bart,lisa,maggie,rod, todd,ralph,bart,lisa,maggie|...] This also works for findall/3, but is redundant

James CheneyLogic ProgrammingOctober 9, 2014 setof/3 Similar to bagof/3, but sorts and eliminates duplicates | ?- bagof(Y,X^ancestor(X,Y),L). L = [homer,bart,lisa,maggie,rod, todd,ralph,bart,lisa,maggie|...] | ?- setof(Y,X^ancestor(X,Y),L). L = [bart,homer,lisa,maggie,marge, patty,ralph,rod,selma,todd]

James CheneyLogic ProgrammingOctober 9, 2014 Assert and retract So far we have statically defined facts and rules usually in separate file We can also dynamically add and remove clauses

James CheneyLogic ProgrammingOctober 9, 2014 assert/1 ?- assert(p). yes ?- p. yes ?- assert(q(1)). yes ?- q(X). X = 1.

James CheneyLogic ProgrammingOctober 9, 2014 Searching a graph using assert/1 :- dynamic v/1. find3(X,X). find3(X,Z) :- \+(v(X)), assert(v(X)), edge(X,Y), find3(Y,Z). Mode (+,?). Problem: Need to clean up afterwards.

James CheneyLogic ProgrammingOctober 9, 2014 Fibonacci fib(0,0). fib(1,1). fib(N,K) :- N >= 2, M is N-1, fib(M,F), P is M-1, fib(P,G), K is F+G.

James CheneyLogic ProgrammingOctober 9, 2014 Fibonacci, memoized :- dynamic memofib/2. fib(N,K) :- memofib(N,K), !.... fib(N,K) :- N >= 2, M is N-1, fib(M,F), P is M-1, fib(P,G), K is F+G, assert(memofib(N,K)).

James CheneyLogic ProgrammingOctober 9, 2014 asserta/1 and assertz/1 Provide limited control over clause order. asserta/1 adds to beginning of KB assertz/1 adds to end of KB

James CheneyLogic ProgrammingOctober 9, 2014 retract/1 ?- retract(p). yes ?- p. no ?- retract(q(1)). yes ?- q(X). no

James CheneyLogic ProgrammingOctober 9, 2014 Warning If you assert or retract an unused predicate interactively, Sicstus Prolog assumes it is dynamic But if you want to use assert/retract in programs, you should declare as dynamic in the program for example: :- dynamic memofig/2. Generally wise to avoid assert/retract without good reason

James CheneyLogic ProgrammingOctober 9, 2014 Collecting solutions using assert, retract Here's a way to calculate list of all ancestors using assert/retract: :- dynamic p/1. alist(X,L) :- assert(p([])), collect(X); p(L), retract(p(L)). collect(X) :- ancestor(Y,X), retract(p(L)), assert(p([Y|L])), fail. Kind of a hack! (also need to clean up afterwards).

James CheneyLogic ProgrammingOctober 9, 2014 Next time: Definite Clause Grammars Further reading: LPN, ch. 10 & 11