
- Prolog - Home
- Prolog - Introduction
- Prolog - Environment Setup
- Prolog - Hello World
- Prolog - Basics
- Prolog - Relations
- Prolog - Data Objects
- Loop & Decision Making
- Conjunctions & Disjunctions
Prolog Operators
- Prolog - Type of Operators
- Prolog - Arithmetic Comparison Operators
- Prolog - Unification Operators
- Prolog - Term Comparision Operators
- Prolog - Arithmetic Operators
- Prolog - Logical Operators
- Prolog - List Operators
- Prolog - Custom Operators
Prolog Lists
- Prolog - Lists
- Prolog - Member of List
- Prolog - Length of List
- Prolog - Concatenating Lists
- Prolog - Appending to a List
- Prolog - Deleting from a List
- Prolog - Inserting into a List
- Prolog - Permutation Operation
- Prolog - Combination Operation
- Prolog - Reverse Items of a List
- Prolog - Shift Items of a List
- Prolog - Check Order of a List
- Prolog - SubSet of a Set
- Prolog - Union of Sets
- Prolog - Intersection of Sets
- Prolog - Even and Odd Length Finding
- Prolog - Divide a List
- Prolog - Find Maximum of a List
- Prolog - Find Minimum of a List
- Prolog - Find Sum of a List
- Prolog - Sorting List using MergeSort
Built-In Predicates
- Prolog - Built-In Predicates
- Prolog - Identifying Terms
- Prolog - Decomposing Structures
- Prolog - Collecting All
- Prolog - Mathematical Predicates
- Prolog - Scientific Predicates
Miscellaneous
- Recursion and Structures
- Prolog - Backtracking
- Prolog - Preventing Backtracking
- Prolog - Different and Not
- Prolog - Inputs and Outputs
- Tree Data Structure (Case Study)
- Prolog - Examples
- Prolog - Basic Programs
- Prolog - Practical Arithmetic Examples
- Prolog - Examples of Cuts
- Towers of Hanoi Problem
- Prolog - Linked Lists
- Monkey and Banana Problem
- Prolog Useful Resources
- Prolog - Quick Guide
- Prolog - Useful Resources
- Prolog - Discussion
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} | ?-