



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




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:
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
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?
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?
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.
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
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