Prolog - Examples of Cuts


In this section, we will see some examples of cuts in prolog. Let us consider, we want to find the maximum of two elements. So we will check these two conditions.

Now from these two lines, we can understand that these two statements are mutually exclusive, so when one is true, another one must be false. In such cases we can use the cut. So let us see the program.

We can also define a predicate where we use the two cases using disjunction (OR logic). So when first one satisfies, it does not check for the second one, otherwise, it will check for the second statement.

Program

max(X,Y,X) :- X >= Y,!.
max(X,Y,Y) :- X < Y.

max_find(X,Y,Max) :- X >= Y,!, Max = X; Max = Y.

Output

| ?- [cut_example].
      1 1 Call: [cut_example] ?
compiling D:/TP Prolog/Sample_Codes/cut_example.pl for byte code...
D:/TP Prolog/Sample_Codes/cut_example.pl compiled, 3 lines read - 1195 bytes written, 43 ms
      1 1 Exit: [cut_example] ?
yes
{trace}
| ?- max(10,20,Max).
      1 1 Call: max(10,20,_23) ?
      2 2 Call: 10>=20 ?
      2 2 Fail: 10>=20 ?
      2 2 Call: 10<20 ?
      2 2 Exit: 10<20 ?
      1 1 Exit: max(10,20,20) ?
Max = 20

yes
{trace}
| ?- max_find(20,10,Max).
      1 1 Call: max_find(20,10,_23) ?
      2 2 Call: 20>=10 ?
      2 2 Exit: 20>=10 ?
      1 1 Exit: max_find(20,10,20) ?
Max = 20

yes
{trace}
| ?-

Program

Let us see another example, where we will use list. In this program we will try to insert an element into a list, if it is not present in the list before. And if the list has the element before we will simply cut it. For the membership checking also, if the item is at the head part, we should not check further, so cut it, otherwise check into the tail part.

list_member(X,[X|_]) :- !.
list_member(X,[_|TAIL]) :- list_member(X,TAIL).

list_append(A,T,T) :- list_member(A,T),!.
list_append(A,T,[A|T]).

Output

| ?- [cut_example].
compiling D:/TP Prolog/Sample_Codes/cut_example.pl for byte code...
D:/TP Prolog/Sample_Codes/cut_example.pl compiled, 9 lines read - 1954 bytes written, 15 ms

yes
| ?- trace.
The debugger will first creep -- showing everything (trace)

yes
{trace}
| ?- list_append(a,[a,b,c,d,e], L).
      1 1 Call: list_append(a,[a,b,c,d,e],_33) ?
      2 2 Call: list_member(a,[a,b,c,d,e]) ?
      2 2 Exit: list_member(a,[a,b,c,d,e]) ?
      1 1 Exit: list_append(a,[a,b,c,d,e],[a,b,c,d,e]) ?
      
L = [a,b,c,d,e]

yes
{trace}
| ?- list_append(k,[a,b,c,d,e], L).
      1 1 Call: list_append(k,[a,b,c,d,e],_33) ?
      2 2 Call: list_member(k,[a,b,c,d,e]) ?
      3 3 Call: list_member(k,[b,c,d,e]) ?
      4 4 Call: list_member(k,[c,d,e]) ?
      5 5 Call: list_member(k,[d,e]) ?
      6 6 Call: list_member(k,[e]) ?
      7 7 Call: list_member(k,[]) ?
      7 7 Fail: list_member(k,[]) ?
      6 6 Fail: list_member(k,[e]) ?
      5 5 Fail: list_member(k,[d,e]) ?
      4 4 Fail: list_member(k,[c,d,e]) ?
      3 3 Fail: list_member(k,[b,c,d,e]) ?
      2 2 Fail: list_member(k,[a,b,c,d,e]) ?
      1 1 Exit: list_append(k,[a,b,c,d,e],[k,a,b,c,d,e]) ?
      
L = [k,a,b,c,d,e]

(16 ms) yes
{trace}
| ?-