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

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 8 – Search and conclusions.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Some Prolog Prolog is a logic programming language
Higher Order Predicates Higher order predicates in PROLOG. Higher order declarative predicates : – findall – bagof – setof – =.. Higher order non-declarative.
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.
Standard Logical Equivalences
1 Logic Programming School of Informatics, University of Edinburgh Transformations Specification-Program An introduction to moving between Prolog and First.
”assert” and ”retract”
A Third Look At Prolog Chapter Twenty-TwoModern Programming Languages, 2nd ed.1.
Logic Programming Lecture 3: Recursion, lists, and data structures.
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
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.
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 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.
Warm Up 1) Is (-3, 4) a solution to the system? 2)Solve the system by graphing y = -2x + 5 2y = x - 2.
Logic Programming Lecture 6: Parsing, Difference Lists and Definite Clause Grammars.
Logic Programming Lecture 7: Search Strategies: Problem representations Depth-first, breadth-first, and AND/OR search.
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.
First Order Logic Chapter 7. PL is a Weak Representational Language §Propositional Logic (PL) is not a very expressive language because: §Hard to identify.
1 Chapter 8 Inference and Resolution for Problem Solving.
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.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
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.
Complex Numbers Definitions Graphing 33 Absolute Values.
Lecture course: Heuristics & Prolog for Artificial Intelligence Applications Notes on Prolog from Professor Bruce Batchelor, DSc.
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:
COP4020 Programming Languages Logic Programming Prof. Xin Yuan.
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.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
To solve absolute value equations and inequalities in one variable
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

Logic Programming Outline for today Nonlogical features continued Negation-as-failure Collecting solutions (findall, setof, bagof) Assert/retract

Logic Programming Negation-as-failure We can use cut to define negation-as- failure not(G) :- G, !, fail ; true. This tries to solve G If successful, fail Otherwise, succeed

Logic Programming 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

Logic Programming true How it works member(5,[2,1]), !, fail. member(5,[1]), !, fail. not(member(5,[2,1]) done member(5,[]), !, fail.

Logic Programming Negation-as-failure Built-in syntax: not(G) Example: people that are not teachers q(X) :- person(X), not(teaches(X,Y)).

Logic Programming Behavior person(a). person(b). teaches(a,b). q(X) done person(X), not(teaches(X,Y)) X = aX = b not(teaches(a,Y)) teaches(a,Y) done not(teaches(b,Y)) teaches(b,Y') q(X) :- person(X), not(teaches(X,Y)). Y= b

Logic Programming X = a Y = b Behavior person(a). person(b). teaches(a,b). q(X) done not(teaches(X,Y)),person(X) person(X) q(X) :- not(teaches(X,Y)), person(X). teaches(X,Y)

Logic Programming 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).

Logic Programming 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) :- not(member(X,P)), edge(X,Y), find2(Y,Z,[X|P]). Note: Needs mode (+,?,+).

Logic Programming Safe use of negation- as-failure As with cut, negation-as-failure can have non-logical behavior Goal order matters not(X = Y), X = a, Y = b fails X = a, Y = b, \+(X = Y) succeeds

Logic Programming Safe use of negation as failure (2) Can read not(G) as "not G" only if G is ground when we start solving it Any free variables "existentially quantified“ not(p(X,a)) == not ∃ X. p(X,a) General heuristic: delay negation after other goals to make negated goals ground

Logic Programming 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

Logic Programming 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)|...]

Logic Programming 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"...)

Logic Programming bagof/3 - list of solutions ?- bagof(Y,ancestor(Y,bart),L). L = [homer,marge,abe,jacqueline] bagof/3

Logic Programming Assert and retract So far we have statically defined facts and rules usually in separate file We can also dynamically add and remove clauses

Logic Programming assert/1 ?- assert(p). yes ?- p. yes ?- assert(q(1)). yes ?- q(X). X = 1.

Logic Programming Searching a graph using assert/1 find3(X,X). find3(X,Z) :- not(v(X)), assert(v(X)), edge(X,Y), find3(Y,Z). Mode (+,?). Problem: Need to clean up afterwards.

Logic Programming 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 24, 2011 Fibonacci, memoized 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)).

Logic Programming 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

Logic Programming retract/1 ?- retract(p). yes ?- p. no ?- retract(q(1)). yes ?- q(X). no

Logic Programming Collecting solutions using assert, retract Here's a way to calculate list of all ancestors usign assert/retract: 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).

Logic Programming Next time: Definite Clause Grammars Further reading: LPN, ch. 10 & 11