Docsity

Prolog and the Cut Operator: Understanding Backtracking and Efficiency - Prof. Alan Mattox, Study notes of Programming Languages

Toggle sidebar
bg1
CS 440 — Prolog and the Cut Operator April 29, 2009
1 Introduction and Objectives
Prolog’s greatest strength is its ability to backtrack to find alternative solutions. If not controlled,
it can also be its greatest weakness. In this lecture we will go over the cut operator, which gives a
solution to this problem.
Your objectives:
•Know what the cut operator is and what it does.
•Know how to use the cut operator to assert failure.
•Know how to use the cut operator to stop recursion.
2 Backtracking
The cut operator is a single exclamation point. When activated, the cut operator stops Prolog’s
backtracking behavior. This won’t make any sense unless you understand backtracking first, so
consider this example.
1color(red).
2color(blue).
3car(honda).
4car(ford).
5car(toyota).
6
7?- color(A), car(B).
Come and see the backtracking inherent in the system! What will be the output of this example?
(See question 341 in the database for solutions.)
Mattox Beckman Page 1 Illinois Institute of Technology
pf3
pf4
pf5

Partial preview of the text

Download Prolog and the Cut Operator: Understanding Backtracking and Efficiency - Prof. Alan Mattox and more Study notes Programming Languages in PDF only on Docsity!

1 Introduction and Objectives

Prolog’s greatest strength is its ability to backtrack to find alternative solutions. If not controlled, it can also be its greatest weakness. In this lecture we will go over the cut operator, which gives a solution to this problem. Your objectives:

  • Know what the cut operator is and what it does.
  • Know how to use the cut operator to assert failure.
  • Know how to use the cut operator to stop recursion.

2 Backtracking

The cut operator is a single exclamation point. When activated, the cut operator stops Prolog’s backtracking behavior. This won’t make any sense unless you understand backtracking first, so consider this example.

1 color(red). 2 color(blue). 3 car(honda). 4 car(ford). 5 car(toyota). 6 7 ?- color(A), car(B).

Come and see the backtracking inherent in the system! What will be the output of this example? (See question 341 in the database for solutions.)

The Cut operator (!) stops backtracking. It is considered a goal that always succeeds. Let’s modify our query by adding a cut operator:

8 ?- color(A), !, car(B). 9 10 A = red 11 B = honda ; 12 A = red 13 B = ford ; 14 A = red 15 B = toyota ; 16 No

2.1 Commitment

We can put cut operators in our database as well. Once a cut is activated, the clause we are trying to satisfy is committed to that choice. Here’s an example where cut has been placed into the database.

17 color(red). 18 color(green) :- !. 19 color(blue). 20 21 ?- color(X). 22 X = red ; 23 X = green ; 24 No

Once X was set to green, the cut operator forces us to stay with green or else color should fail completely. Question: Can color(blue) ever be matched?

3 Cut and Recursion

Here’s the factorial function in Prolog, and a sample run.

25 fact(0,1). 26 fact(N,X) :- M is N-1, fact(M,Y), 27 X is Y * N. 28 29 ?- fact(5,N). 30 N = 120 ; 31 ERROR: Out of local stack

What happened here?

41 student(anna). 42 student(beth).

and so on. We have this rule for who can use the telescope.

43 telescope(X) :- (student(X); faculty(X); 44 club(X)), 45 trained(X).

Now consider what happens when we ask: “Can frank use the telescope?” It is true that frank is a faculty, and also a member of the club, but frank doesn’t have any training. What will telescope(frank). do?

Another example of inefficiency is when we ask “Who can use the telescope?”

46 ?- telescope(X). 47 X = anna ; 48 X = anna ; 49 X = harry ;

Since anna is a student and a member of the club, she gets listed twice. Before moving on to the next section: where do you think we could put a cut operator to fix these problems?

4.2 Cutting for Efficiency

Now let’s add a cut operator.

50 telescope(X) :- (student(X); faculty(X); 51 club(X)), 52 !, 53 trained(X).

This works. Mostly. If we ask about frank, we only check once. But there is a problem...

54 ?- telescope(X). 55 X = anna ; 56 No

Now we’ve dissed harry. Moral: cut will limit your choices to only one answer.

5 Inducing failure

We also have a predicate called fail, which, (surprise!) always fails. It can be a bit tricky to use, though. For example, suppose anna has her telescope privileges revoked.

57 telescope(anna) :- fail. 58 telescope(X) :- (student(X); faculty(X); club(X)), 59 trained(X). 60 61 ?- telescope(anna). 62 Yes.

This is less than what we hoped for. Why do you think that happened?

Let’s try again, this time using cut.

63 telescope(anna) :- !, fail. 64 telescope(X) :- (student(X); faculty(X); 65 club(X)), 66 trained(X). 67 68 ?- telescope(anna). 69 No 70 ?- telescope(harry). 71 Yes 72 ?- telescope(X). 73 No

6 Conclusions

  • Cut can stop searches that you already know will be useless.
  • Cut can make queries more efficient.
  • But, cut can make queries do strange things. Use with care.

Aside: you can define not (actually, it’s built in) this way:

74 not(X) :- call(X), !, fail. 75 not(X).

79 myflatten([H|T],X) :- is_list(H), 80 append(H,T,R), myflatten(R,X). 81 myflatten([H|T],[H|X]) :- myflatten(T,X). 82 myflatten([],[]). 83 84 ?- myflatten([[2,3],[3,4,[5,6],4],3],X). 85 X = [2, 3, 3, 4, 5, 6, 4, 3] ; 86 X = [2, 3, 3, 4, [5, 6], 4, 3] ; 87 X = [2, 3, [3, 4, [5, 6], 4], 3] ; 88 X = [[2, 3], 3, 4, 5, 6, 4, 3] ; 89 X = [[2, 3], 3, 4, [5, 6], 4, 3] ; 90 X = [[2, 3], [3, 4, [5, 6], 4], 3] ; 91 No