English 中文(简体)
Prolog - Examples of Cuts
  • 时间:2024-09-08

Prolog - Examples of Cuts


Previous Page Next Page  

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.

    If X > Y, then Max := X

    if X <= Y, then Max := Y

Now from these two pnes, 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] ?
compipng D:/TP Prolog/Sample_Codes/cut_example.pl for byte code...
D:/TP Prolog/Sample_Codes/cut_example.pl compiled, 3 pnes 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 pst. In this program we will try to insert an element into a pst, if it is not present in the pst before. And if the pst 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.


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

pst_append(A,T,T) :- pst_member(A,T),!.
pst_append(A,T,[A|T]).

Output


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

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

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

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

(16 ms) yes
{trace}
| ?-
Advertisements