Reputation: 3303
I keep running across this operator. Here is an example, a predicate that uses insertion sort to sort a list of integers.
inser(A,[],[A],P).
inser(A,[H|L],R,P):- P(A,H),append([A,H],L,R),!.
inser(A,[H|L],[H|R],P):- inser(A,L,R,P).
sortin([],[],P).
sortin([H|L],Re,P):- sortin(L,R,P),inser(H,R,Re,P).
I am new to Prolog.
Upvotes: 1
Views: 6671
Reputation: 60024
That's called cut. Citing from SWI-Prolog documentation page:
Cut. Discard all choice points created since entering the predicate in which the cut appears. In other words, commit to the clause in which the cut appears and discard choice points that have been created by goals to the left of the cut in the current clause
The name is descriptive WRT the abstract computation model of Prolog, that builds a proof tree attempting to satisfy all conditions in a well defined order. The cut prunes potential branchs that application of alternative rules could require.
In your inser/3 predicate, it means that the third clause will not be tried if P(A,H) succeeds and append([A,H],L,R) succeeds as well.
Note that P(A,H)
is not valid Prolog syntax. You should use call(P,A,H) to invoke P
comparison predicate.
Upvotes: 3