Lucas Kreutz
Lucas Kreutz

Reputation: 469

Prolog Cut operator in the first predicate

I have the following base in Prolog:

holiday(friday,may1).
weather(friday,fair).
weather(saturday,fair).
weather(sunday,fair).
weekend(saturday).
weekend(sunday).

picnic(Day) :- !,weather(Day,fair), weekend(Day).
picnic(Day) :- holiday(Day,may1).

When I run picnic(When). I get the following trace:

[trace]  ?- picnic(When).
   Call: (6) picnic(_G716) ? creep
   Call: (7) weather(_G716, fair) ? creep
   Exit: (7) weather(friday, fair) ? creep
   Call: (7) weekend(friday) ? creep
   Fail: (7) weekend(friday) ? creep
   Redo: (7) weather(_G716, fair) ? creep
   Exit: (7) weather(saturday, fair) ? creep
   Call: (7) weekend(saturday) ? creep
   Exit: (7) weekend(saturday) ? creep
   Exit: (6) picnic(saturday) ? creep
When = saturday ;
   Redo: (7) weather(_G716, fair) ? creep
   Exit: (7) weather(sunday, fair) ? creep
   Call: (7) weekend(sunday) ? creep
   Exit: (7) weekend(sunday) ? creep
   Exit: (6) picnic(sunday) ? creep
When = sunday.

My doubt is: the cut operator, as I know, should stop to search alternatives when the predicates to the left of the ! signal are true. What's the meaning of the signal in the first position? Why does the interpreter keep searching for another values that can turn the other predicates true?

Upvotes: 2

Views: 406

Answers (2)

Profiterole
Profiterole

Reputation: 187

Art of Prolog, part 11, cuts and negation "Operationally, the cut is handled as follows. The goal succeeds and commits Prolog to all the choices made since the parent goal was unified with the head of the clause the cut occurs in." So once your top goal gets unified with the head of your first picnic/1, it can't be alternatively unified with the head of your second picnic/1.

Upvotes: 0

CapelliC
CapelliC

Reputation: 60024

The effect of that cut is that your second picnic/1 rule will be totally ignored. It will never have a chance to fire during your program lifetime.

But backtracking is still at work among available alternatives (goals to the right of the cut), and you can clearly observe them in your trace.

Upvotes: 3

Related Questions