So, you are here to understand the exact properties of the completion of a particular program. The prologue here is completely different from other programming languages, because it has a relatively complex execution mechanism. In particular, backtracking is completely intertwined with “normal resolution” and then merged with aggregation.
The easiest way to understand the problem with your source program is to consider the following failure fragment (see link for other examples):
plus (0, B, B): - false , nat (B) .
plus (s (A), B, C): - plus (A, s (B), C), false .
If this tiny program is not completed, then your original program will not be completed. Therefore, we can consider when this program will not be completed, first.
Consider the third argument: there is simply a C variable that is passed together. No one is interested in its value (in this fragment). Thus, the third argument will have no effect on termination at all!
Worse, the second argument is just the free variable B in the head, and again, the argument will never affect whether this fragment ends.
And thus: If you want this fragment to end, the first argument must be known. Otherwise, the program will loop. You can also use the termination terminator to see this , which gives a termination condition:
plus(A,B,C)terminates_if b(A),b(B);b(A),b(C).
It is best to reformulate your program. Providing the following completion conditions :
plus(A,B,C)terminates_if b(A),b(B);b(C).
In Prolog, we usually like to generalize programs even more, making some unexpected decisions, while improving the termination condition to an even better condition.
plus(A,B,C)terminates_if b(A);b(C).
However, this latest version now allows solutions that are not always acceptable. For instance. plus(0, nonnumber, nonnumnber) now succeeds, while you may want it to work.
Of course, you can also do some abbreviation experiments, but be careful, using a cut is extremely error prone. At the very least, you should combine it with related tests, which often lead to "performance improvements."