Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

Prolog - Preventing Backtracking



So far we have seen some concepts of backtracking. Now let us see some drawbacks of backtracking. Sometimes we write the same predicates more than once when our program demands, for example to write recursive rules or to make some decision making systems. In such cases uncontrolled backtracking may cause inefficiency in a program. To resolve this, we will use the Cut in Prolog.

Suppose we have some rules as follows −

Double step function

  • Rule 1 − if X < 3 then Y = 0

  • Rule 2 − if 3 <= X and X < 6 then Y = 2

  • Rule 3 − if 6 <= X then Y = 4

In Prolog syntax we can write,

f(X,0) :- X < 3.             % Rule 1
f(X,2) :- 3 =< X, X < 6.  % Rule 2
f(X,4) :- 6 =< X.            % Rule 3

Now if we ask for a question as f (1,Y), 2

The first goal f(1,Y) instantiated Y to 0. The second goal becomes 2 < 0 which fails. Prolog tries through backtracking two unfruitful alternatives (Rule 2 and Rule 3). If we see closer, we can observe that −

  • The three rules are mutually exclusive and one of them at most will succeed.

  • As soon as one of them succeeds there is no point in trying to use the others as they are bound to fail.

So we can use cut to resolve this. The cut can be expressed using Exclamation symbol. The prolog syntax is as follows −

f(X,0) :- X < 3, !.            % Rule 1
f(X,2) :- 3 =< X, X < 6, !. % Rule 2
f(X,4) :- 6 =< X.              % Rule 3

Now if we use the same question, ?- f (1,Y), 2 < Y. Prolog choose rule 1 since 1 < 3 and fails the goal 2 < Y fails. Prolog will try to backtrack, but not beyond the point marked ! In the program, rule 2 and rule 3 will not be generated.

Let us see this in below execution −

Program(backtrack.pl)

f(X,0) :- X < 3, !.            % Rule 1
f(X,2) :- 3 =< X, X < 6, !. % Rule 2
f(X,4) :- 6 =< X.              % Rule 3

Output

| ?- consult('D:/TP Prolog/Sample Codes/backtrack.pl').
compiling D:/TP Prolog/Sample Codes/backtrack.pl for byte code...
D:/TP Prolog/Sample Codes/backtrack.pl compiled, 2 lines read - 805 bytes written, 3 ms

yes
| ?- f(1,Y), 2<Y.

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

yes
{trace}
| ?- f(1,Y), 2<Y.
      1    1  Call: f(1,_23) ? 
      2    2  Call: 1<3 ? 
      2    2  Exit: 1<3 ? 
      1    1  Exit: f(1,0) ? 
      3    1  Call: 2<0 ? 
      3    1  Fail: 2<0 ? 

(31 ms) no
{trace}
| ?- 

Let us see the same without using cut.

Program(backtrack.pl)

f(X,0) :- X < 3               % Rule 1
f(X,2) :- 3 =< X, X < 6.   % Rule 2
f(X,4) :- 6 =< X.             % Rule 3

Output

| ?- consult('D:/TP Prolog/Sample Codes/backtrack.pl').
compiling D:/TP Prolog/Sample Codes/backtrack.pl for byte code...
D:/TP Prolog/Sample Codes/backtrack.pl compiled, 2 lines read - 656 bytes written, 3 ms

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

yes
{trace}
| ?- f(1,Y), 2<Y.
      1    1  Call: f(1,_23) ? 
      2    2  Call: 1<3 ? 
      2    2  Exit: 1<3 ? 
      1    1  Exit: f(1,0) ? 
      3    1  Call: 2<0 ? 
      3    1  Fail: 2<0 ? 
      1    1  Redo: f(1,0) ? 
      2    2  Call: 3=<1 ? 
      2    2  Fail: 3=<1 ? 
      2    2  Call: 6=<1 ? 
      2    2  Fail: 6=<1 ? 
      1    1  Fail: f(1,_23) ? 

no
{trace}
| ?- 
Advertisements