(Let me sneak into a wave of intermediate questions.)
The general definition of the sum of two natural numbers is nat_nat_sum/3
:
nat_nat_sum(0, N, N). nat_nat_sum(s(M), N, s(O)) :- nat_nat_sum(M, N, O).
Strictly speaking, this definition is too general, since now we are successful for
?- nat_nat_sum(A, B, unnatural_number).
Similarly, we get the following substitution of answers:
?- nat_nat_sum(0, A, B). A = B.
We interpret this replacement of the answer as including all natural numbers and do not care about other terms.
Given this, now consider its completion property. In fact, it is enough to consider the following fragment of failure . That is, not only nat_nat_sum/3
does not end if this slice does not end. This time they are exactly the same! Therefore, we can say that iff.
nat_nat_sum (0, N, N): - false .
nat_nat_sum (s (M), N, s (O)): -
nat_nat_sum (M, N, O), false .
This failure fragment now displays the symmetry between the first and third arguments: they both affect non-ending in exactly the same way! Therefore, when they describe completely different things - one term, the other - the sum, they have exactly the same effect on the termination. And the poor second argument has no effect.
To be sure that not only the failure fragment is identical in its general termination condition ( use cTI ), which reads
nat_nat_sum(A,B,C)terminates_if b(A);b(C).
It also ends in exactly the same way for cases that are not covered by this condition, for example
?- nat_nat_sum(f(X),Y,Z).
Now my question is:
Is there an alternative definition for nat_nat_sum/3
that has a termination condition:
nat_nat_sum2(A,B,C) terminates_if b(A);b(B);b(C).
(If yes, show it. If not, explain why)
In other words, the new definition of nat_nat_sum2/3
should end if only one of its arguments is finite and justified.
Great print . Consider only clean, monotonous Prolog programs. That is, no built-in functions except (=)/2
and dif/2
(I reward 200 bounties for this)