Assume we have the following predicates (This is an example from Programming in Prolog):
[F0] isInteger(0).
[F1] isInteger(X):- isInteger(Y), X is Y+1.
The first result for query isInteger(R), the marker is placed at F0, and will return R=0
If user presses ; , the marker is placed at F1, we move to subgoal(isInteger(Y), which is satisfied with F0) and R=1.
I understand the above. Now here are my questions:
I am looking for any tutorials that explain backtracking in presence of recursion, hopefully with images of stack contents that helps me understand.
Thank you in advance Suzanne
Backtracking can be thought of as a selective tree/graph traversal method. The tree is a way of representing some initial starting position (the parent node) and a final goal state (one of the leaves).
Difference between Recursion and Backtracking: In recursion, the function calls itself until it reaches a base case. In backtracking, we use recursion to explore all the possibilities until we get the best result for the problem.
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.
Backtracking is a technique based on algorithm to solve problem. It uses recursive calling to find the solution by building a solution step by step increasing values with time. It removes the solutions that doesn't give rise to the solution of the problem based on the constraints given to solve the problem.
Understanding backtracking and recursion by using images works for very tiny examples, but it does not scale to larger programs. Also, by stepping through a program you easily miss the most interesting properties. Fortunately, there are better notions than that. Let's take your example isInteger/1
.
Your primary interest is to ensure that you are describing the right thing. Here, the second rule is most interesting. Read it in the direction of the arrow :-
. That is, right-to-left: Provided Y
is an integer, and X is Y+1
then also X
is an integer.
Then, you can estimate the set of solutions which is infinite in this case.
The next question concerns the termination properties of the predicate. Note, that it cannot – in fact must not – terminate, if it has to produce infinitely many answers. On the other hand, ground queries like isInteger(1)
have either one or no solution. So it is desirable that the predicate terminates for such cases. However, your definition does not terminate here!
To better understand this, I will use a failure-slice. That is, I will insert goals false
into your program. If the resulting program fragment does not terminate, then the original doesn't.
?- isInteger(1), falseisInteger(0) :- false. isInteger(X) :- isInteger(Y), false,X is Y+1.
Only a very small part is responsible for non-termination! The remaining part does not even look at the value of X
at all. Therefore your program terminates never. No matter how you call it.
See failure-slice for more examples.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With