
- 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 - Backtracking
In this chapter, we will discuss the backtracking in Prolog. Backtracking is a procedure, in which prolog searches the truth value of different predicates by checking whether they are correct or not. The backtracking term is quite common in algorithm designing, and in different programming environments. In Prolog, until it reaches proper destination, it tries to backtrack. When the destination is found, it stops.
Let us see how backtracking takes place using one tree like structure −

Suppose A to G are some rules and facts. We start from A and want to reach G. The proper path will be A-C-G, but at first, it will go from A to B, then B to D. When it finds that D is not the destination, it backtracks to B, then go to E, and backtracks again to B, as there is no other child of B, then it backtracks to A, thus it searches for G, and finally found G in the path A-C-G. (Dashed lines are indicating the backtracking.) So when it finds G, it stops.
How Backtracking works?
Now we know, what is the backtracking in Prolog. Let us see one example,
Note − While we are running some prolog code, during backtracking there may be multiple answers, we can press semicolon (;) to get next answers one by one, that helps to backtrack. Otherwise when we get one result, it will stop.
Now, consider a situation, where two people X and Y can pay each other, but the condition is that a boy can pay to a girl, so X will be a boy, and Y will be a girl. So for these we have defined some facts and rules −
Knowledge Base (backtrack.pl)
boy(tom). boy(bob). girl(alice). girl(lili). pay(X,Y) :- boy(X), girl(Y).
Following is the illustration of the above scenario −

As X will be a boy, so there are two choices, and for each boy there are two choices alice and lili. Now let us see the output, how backtracking is working.
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, 4 lines read - 703 bytes written, 8 ms yes | ?- pay(X,Y). X = tom Y = alice ? yes | ?- trace. The debugger will first creep -- showing everything (trace) yes {trace} | ?- pay(X,Y). 1 1 Call: pay(_23,_24) ? 2 2 Call: boy(_23) ? 2 2 Exit: boy(tom) ? 3 2 Call: girl(_24) ? 3 2 Exit: girl(alice) ? 1 1 Exit: pay(tom,alice) ? X = tom Y = alice ? (47 ms) yes {trace} | ?-
Negation as Failure
Here we will perform failure when condition does not satisfy. Suppose we have a statement, Mary likes all animals but snakes, we will express this in Prolog.
It would be very easy and straight forward, if the statement is Mary likes all animals. In that case we can write Mary likes X if X is an animal. And in prolog we can write this statement as, likes(mary, X) := animal(X).
Our actual statement can be expressed as −
If X is snake, then Mary likes X is not true
Otherwise if X is an animal, then Mary likes X.
In prolog we can write this as −
likes(mary,X) :- snake(X), !, fail. likes(mary, X) :- animal(X).
The fail statement causes the failure. Now let us see how it works in Prolog.
Program(negate_fail.pl)
animal(dog). animal(cat). animal(elephant). animal(tiger). animal(cobra). animal(python). snake(cobra). snake(python). likes(mary, X) :- snake(X), !, fail. likes(mary, X) :- animal(X).
Output
| ?- consult('D:/TP Prolog/Sample Codes/negate_fail.pl'). compiling D:/TP Prolog/Sample Codes/negate_fail.pl for byte code... D:/TP Prolog/Sample Codes/negate_fail.pl compiled, 9 lines read - 1117 bytes written, 8 ms (16 ms) yes | ?- likes(mary,elephant). yes | ?- likes(mary,tiger). yes | ?- likes(mary,python). no | ?- likes(mary,cobra). no | ?- trace. The debugger will first creep -- showing everything (trace) yes {trace} | ?- likes(mary,dog). 1 1 Call: likes(mary,dog) ? 2 2 Call: snake(dog) ? 2 2 Fail: snake(dog) ? 2 2 Call: animal(dog) ? 2 2 Exit: animal(dog) ? 1 1 Exit: likes(mary,dog) ? yes {trace} | ?- likes(mary,python). 1 1 Call: likes(mary,python) ? 2 2 Call: snake(python) ? 2 2 Exit: snake(python) ? 3 2 Call: fail ? 3 2 Fail: fail ? 1 1 Fail: likes(mary,python) ? no {trace} | ?-