Prolog: cut & fail http://www.learnprolognow.org © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Slides:



Advertisements
Similar presentations
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Advertisements

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
Prolog OR (disjunction) “;” is same as a logical OR “;” is same as a logical OR It is also equivalent to using separate clauses... It is also equivalent.
The factor theorem The Factor Theorem states that if f(a) = 0 for a polynomial then (x- a) is a factor of the polynomial f(x). Example f(x) = x 2 + x -
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3: Recursion Theory –Introduce recursive definitions in Prolog –Four examples –Show that there.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
1 Non-determinism and Cut  Non-determinism  Backtracking  Controlling Backtracking.
Inference and Resolution for Problem Solving
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 2 Theory –Unification –Unification in Prolog –Proof search Exercises –Correction exercises.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Equivalent Fractions Equivalent Fractions Using Cross-Multiplication.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Horn clauses A literal is an atomic formula or its negation A clause is a disjunction of literals.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
LING 388: Language and Computers Sandiway Fong Lecture 4.
CS 321 Programming Languages and Compilers Prolog part 2.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation, Search.
1 Chapter 8 Inference and Resolution for Problem Solving.
Standard Is the equation 3(2 x – 4) = −18 equivalent to 6 x − 12 = −18? a)Yes, the equations are equivalent by the Associative Property of Multiplication.
Agile Software Development Lab Dr. Günter Kniesel, Daniel Speicher, Tobias Rho Spring 2008 Prolog - Part 2 Patrick Rypalla Alexis Raptarchis
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.
Flow of Control Chapter 5. Flow of Control n What order computer uses to get answers –sub-goal ordering –clause ordering n Prolog flow-of-control –sequence.
CSE S. Tanimoto Horn Clauses and Unification 1 Horn Clauses and Unification Propositional Logic Clauses Resolution Predicate Logic Horn Clauses.
MA.912.A.4.2: Add, subtract, and multiply polynomials. Which of the following expressions is equivalent to (5x − 3) 2 ? A. 25x 2 − 30x + 9 B. 25x 2 −
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Standard What is the y-intercept of the graph of 4x + 2y = 12
Jigar Gaglani.  Programming paradigm is a fundamental style of computer programming  Paradigms differ in concepts and abstractions used to represent.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Cs774 (Prasad)L6Backtracking1 Controlling Backtracking : Cuts
CSE (c) S. Tanimoto, 2008 Predicate Calculus II 1 Predicate Calculus 2 Outline: Unification: definitions, algorithm Formal interpretations and satisfiability.
Algebra 1 Mini-Lessons 3x2y(6y + 12xy − 9x) 3(6x2y2 + 12x3y3 − 9x3y)
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Statements Containing Multiple Quantifiers Lecture 11 Section 2.3 Mon, Feb 5, 2007.
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.
Quadratic Functions A quadratic function is described by an equation of the following form: ax² + bx + c, where a ≠ 0 The graphs of quadratic functions.
Logic Programming Lecture 2: Unification and proof search.
Last Answer LETTER I h(x) = 3x 4 – 8x Last Answer LETTER R Without graphing, solve this polynomial: y = x 3 – 12x x.
1 TP #4: Control Facilities n Last TP exercises solved; n Don’t care variables « _ »; n Or « ; »; n Cut.
Lecture 1.2: Equivalences, and Predicate Logic
CSE 341, S. Tanimoto Logic Programming -
Prolog: Control, Negation and Cut
Horn Clauses and Unification
Tests, Backtracking, and Recursion
A road map for predicate logic translation.
Prolog Search Strategy
Clicker Question 1 Suppose a population of gerbils starts with 20 individuals and grows at an initial rate of 6% per month. If the maximum capacity is.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Horn Clauses and Unification
Horn Clauses and Unification
CSE (c) S. Tanimoto, 2004 Unification
CSE S. Tanimoto Unification
Chapter 12 :: Logic Languages
Programming Paradigms and Languages
Horn Clauses and Unification
CSE (c) S. Tanimoto, 2002 Unification
Chapter 2 Syntax and meaning of prolog programs
Horn Clauses and Unification
AS-Level Maths: Core 2 for Edexcel
Logic Programming Part 3: Control Flow
Lecture 7: Definite Clause Grammars
Controlling Backtracking : Cuts
Presentation transcript:

Prolog: cut & fail http://www.learnprolognow.org © Patrick Blackburn, Johan Bos & Kristina Striegnitz

The Cut Backtracking is a characteristic feature of Prolog But backtracking can lead to inefficiency: Prolog can waste time exploring possibilities that lead nowhere It would be nice to have some control The cut predicate !/0 offers a way to control backtracking

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X).

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- p(X).

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- p(X).

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- p(X).

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). X=1 ?- p(X). X=1

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X). X=1 ?- p(X). X=1;

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X). X=1 ?- p(X). X=1;

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X). X=1 X=1 ?- c(1),d(1),e(1). ?- p(X). X=1;

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X). X=1 X=1 ?- c(1),d(1),e(1). ?- d(1), e(1). ?- p(X). X=1;

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X). X=1 X=1 ?- c(1),d(1),e(1). ?- d(1), e(1). ?- p(X). X=1; †

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X). X=1 X=1 X=2 ?- c(1),d(1),e(1). ?- d(1), e(1). ?- p(X). X=1; †

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X). X=1 X=1 X=2 ?- c(1),d(1),e(1). ?- c(2),d(2),e(2). ?- d(1), e(1). ?- p(X). X=1; †

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X). X=1 X=1 X=2 ?- c(1),d(1),e(1). ?- c(2),d(2),e(2). ?- d(1), e(1). ?- d(2), e(2). ?- p(X). X=1; †

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X). X=1 X=1 X=2 ?- c(1),d(1),e(1). ?- c(2),d(2),e(2). ?- d(1), e(1). ?- d(2), e(2). ?- p(X). X=1; † ?- e(2).

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X). X=1 X=1 X=2 ?- c(1),d(1),e(1). ?- c(2),d(2),e(2). ?- d(1), e(1). ?- d(2), e(2). ?- p(X). X=1; X=2 † ?- e(2).

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X). ?- f(X). X=1 X=1 X=2 ?- c(1),d(1),e(1). ?- c(2),d(2),e(2). ?- d(1), e(1). ?- d(2), e(2). ?- p(X). X=1; X=2; † ?- e(2).

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X). ?- f(X). X=1 X=1 X=3 X=2 ?- c(1),d(1),e(1). ?- c(2),d(2),e(2). ?- d(1), e(1). ?- d(2), e(2). ?- p(X). X=1; X=2; X=3 † ?- e(2).

Example: cut-free code p(X):- a(X). p(X):- b(X), c(X), d(X), e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),d(X),e(X). ?- f(X). X=1 X=1 X=3 X=2 ?- c(1),d(1),e(1). ?- c(2),d(2),e(2). ?- d(1), e(1). ?- d(2), e(2). ?- p(X). X=1; X=2; X=3; no † ?- e(2).

Adding a cut Suppose we insert a cut in the second clause: If we now pose the same query we will get the following response: p(X):- b(X), c(X), !, d(X), e(X). ?- p(X). X=1; no

Example: cut p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X).

Example: cut p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). ?- p(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- p(X).

Example: cut p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). ?- p(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- p(X).

Example: cut p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). ?- p(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). X=1 ?- p(X). X=1

Example: cut p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),!,d(X),e(X). X=1 ?- p(X). X=1;

Example: cut p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),!,d(X),e(X). X=1 ?- p(X). X=1;

Example: cut p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),!,d(X),e(X). X=1 X=1 ?- c(1),!,d(1),e(1). ?- p(X). X=1;

Example: cut p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),!,d(X),e(X). X=1 X=1 ?- c(1), !, d(1), e(1). ?- p(X). X=1; ?- !, d(1), e(1).

Example: cut X X p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). a(1). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). X ?- a(X). ?- b(X),c(X),!,d(X),e(X). X=1 X=1 X ?- c(1), !, d(1), e(1). ?- p(X). X=1; ?- !, d(1), e(1). ?- d(1), e(1).

Example: cut † p(X):- a(X). p(X):- b(X),c(X),!,d(X),e(X). p(X):- f(X). b(1). b(2). c(1). c(2). d(2). e(2). f(3). ?- p(X). ?- a(X). ?- b(X),c(X),!,d(X),e(X). X=1 X=1 ?- c(1), !, d(1), e(1). ?- p(X). X=1; no ?- !, d(1), e(1). ?- d(1), e(1). †

What the cut does The cut only commits us to choices made since the parent goal was unified with the the left-hand side of the clause containing the cut For example, in a rule of the form q:- p1, … , pn, !, r1, … , rn. when we reach the cut it commits us: to this particular clause of q to the choices made by p1, … , pn NOT to choices made by r1, … , rn

Using Cut Consider the following predicate max/3 that succeeds if the third argument is the maximum of the first two max(X,Y,Y):- X =< Y. max(X,Y,X):- X>Y.

Using Cut Consider the following predicate max/3 that succeeds if the third argument is the maximum of the first two max(X,Y,Y):- X =< Y. max(X,Y,X):- X>Y. ?- max(2,3,3). yes ?- max(7,3,7).

Using Cut Consider the following predicate max/3 that succeeds if the third argument is the maximum of the first two max(X,Y,Y):- X =< Y. max(X,Y,X):- X>Y. ?- max(2,3,2). no ?- max(2,3,5).

The max/3 predicate What is the problem? There is a potential inefficiency Suppose it is called with ?- max(3,4,Y). It will correctly unify Y with 4 But when asked for more solutions, it will try to satisfy the second clause. This is completely pointless! max(X,Y,Y):- X =< Y. max(X,Y,X):- X>Y.

max/3 with cut With the help of cut this is easy to fix Note how this works: If the X =< Y succeeds, the cut commits us to this choice, and the second clause of max/3 is not considered If the X =< Y fails, Prolog goes on to the second clause max(X,Y,Y):- X =< Y, !. max(X,Y,X):- X>Y.

Green Cuts Cuts that do not change the meaning of a predicate are called green cuts The cut in max/3 is an example of a green cut: the new code gives exactly the same answers as the old version, but it is more efficient

Red Cuts Cuts that change the meaning of a predicate are called red cuts The cut in the revised max/3 is an example of a red cut: If we take out the cut, we don’t get an equivalent program Programs containing red cuts Are not fully declarative Can be hard to read Can lead to subtle programming mistakes

Red Cuts ≠ if_then_else(P, Q, R) :- P, !, Q. if-then-else(P, Q, R) :- R. ≠ if_then_else(P, Q, R) :- P, Q. if-then-else(P, Q, R) :- R.

Another build-in predicate: fail/0 As the name suggests, this is a goal that will immediately fail when Prolog tries to proof it That may not sound too useful But remember: when Prolog fails, it tries to backtrack

Vincent and burgers The cut fail combination allows to code exceptions enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). The cut fail combination allows to code exceptions

Vincent and burgers The cut fail combination allows to code exceptions enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). ?- enjoys(vincent,a). yes The cut fail combination allows to code exceptions

Vincent and burgers The cut fail combination allows to code exceptions enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). ?- enjoys(vincent,b). no The cut fail combination allows to code exceptions

Vincent and burgers The cut fail combination allows to code exceptions enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). ?- enjoys(vincent,c). yes The cut fail combination allows to code exceptions

Vincent and burgers The cut fail combination allows to code exceptions enjoys(vincent,X):- bigKahunaBurger(X), !, fail. enjoys(vincent,X):- burger(X). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). ?- enjoys(vincent,d). yes The cut fail combination allows to code exceptions

Negation as Failure The cut-fail combination seems to be offering us some form of negation It is called negation as failure, and defined as follows: neg(Goal):- Goal, !, fail. neg(Goal).

Vincent and burgers revisited enjoys(vincent,X):- burger(X), neg(bigKahunaBurger(X)). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d).

Vincent and burgers revisited enjoys(vincent,X):- burger(X), neg(bigKahunaBurger(X)). burger(X):- bigMac(X). burger(X):- bigKahunaBurger(X). burger(X):- whopper(X). bigMac(a). bigKahunaBurger(b). bigMac(c). whopper(d). ?- enjoys(vincent,X). X=a X=c X=d

Another build-in predicate: \+ Because negation as failure is so often used, there is no need to define it In standard Prolog the prefix operator \+ means negation as failure So we could define Vincent`s preferences as follows: ?- enjoys(vincent,X). X=a X=c X=d enjoys(vincent,X):- burger(X), \+ bigKahunaBurger(X).

Negation as failure and logic Negation as failure is not logical negation Changing the order of the goals in the vincent and burgers program gives a different behaviour: ?- enjoys(vincent,X). no enjoys(vincent,X):- \+ bigKahunaBurger(X), burger(X).

Summary of this lecture In this lecture we introduced cut & fail to control backtracking in Prolog.