Javatpoint Logo
Javatpoint Logo

Backtracking with Failure

While backtracking or 'standard' evaluation left-to-right, the fail predicate always fails, as the name implies. We can take advantage of this by combining it with Prolog's automatic backtracking to find all the clauses in the database with a specified property.

Searching the database of Prolog

Suppose, the clauses contained in the database as follows:

Using the predicate alldogs, we can process each clause of the dog in turn as follows:

When we call the alldogs, due to this dog(A) will be matched with the clauses of dog in the database. In the starting, variable A will be bound to atom pug and produced the output as 'pug is a dog'. In the first clause of predicate alldogs, the final goal will cause evaluation to fail. Then, Prolog will backtrack over two goals write and nl, that goals are unsatisfiable until Prolog reaches to dog(A). Then the second time this goal succeeds, and due to this, variable A will be bound to boxer.

This process will continue until pug, boxer, and rottweiler have all been output, and when evaluation again fails. In the database, there is no further dog, this time call to dog(A) will also fail. Due to this, the first clause of alldogs will fail. Now the second clause of alldogs is examined by Prolog, and this will succeed. After that, the evaluation will stop.

Due to this effect, Prolog will generate a loop through the database. To satisfy the goal dog(A), Prolog will find all possible values of A.

For the predicate alldogs, the second clause is very important. It is used to ensure that the goal succeeds after the database has been searched. It will succeed with only the first line, any call to predicate alldogs will eventually fail.

The next program is used to search the database, which contains the clauses. The clauses contain the name, occupation, residence, and age of the number of people.

Suppose these five clauses contained in the database as follows:

Using the allteachers predicate, we can find the name of all the teachers.

In this case, the effect of using backtracking with failure is to find all teachers in the database.

Both the teachers are found, and if we omit the second clause of allteachers, the evaluation of allteachers will end with failure. If the goal is entered at the system prompt, this has no importance. In the body of a rule, if allteachers were used as a goal, it would ensure that it always succeeded.

To search the database, it is not always necessary to use 'backtracking with failure'. For example, we will find all the people in the database using the somepeople/0 predicate. For this, we will down to jolie, using only standard backtracking.

If the variable Lastname is bound to jolie, the goal Lastname= jolie will succeed. If it is not, it will fail. The effect is to search the database down to and include the clause of person with second argument jolie.

Next TopicThe Cut Predicate

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA