Slide 14.26: Rules (cont.)
Slide 14.28: Search (cont.)
Home

Search


Introducing Backtracking
Suppose that we have the following database
     eats( fred, pears ).
     eats( fred, t_bone_steak ).
     eats( fred, apples ).
So far we have only been able to ask if fred eats specific things. Suppose that I wish to instead answer the question, “What are all the things that fred eats.” To answer this I can use variables again. Thus I can type in the query
     ?- eats( fred, FoodItem ).
As we have seen earlier, Prolog will answer with
     FoodItem = pears
This is because it has found the first clause in the database. At this point Prolog allows us to ask if there are other possible solutions. When we do so we get the following.
     FoodItem = t_bone_steak
if I ask for another solution, Prolog will then give us.
     FoodItem = apples
If we ask for further solutions, Prolog will answer no, since there are only three ways to prove fred eats something. The mechanism for finding multiple solution is called backtracking.