prologsuccessor-arithmeticsnon-termination

Prompt does not come back


I try to do some exercise - to represent numbers in "s representation" which means '0' is zero, s(0) is 1, s(s(0)) is 2 and so on. I tried to write predicate for adding "s numbers": the predicate s2int convert "s number" to int.

s2int(0, 0).
s2int(s(X), Y) :-
   s2int(X, Y1),
   Y is 1 + Y1.

add(X, Y, Z) :-
   s2int(X, SX),
   s2int(Y, SY),
   s2int(Z, SZ),
   SZ is SX + SY.

When I query add it writes the correct answer but the prompt does not come back. What's the problem?


Solution

  • Your definition of add/3 works fine, and also terminates, if all three arguments are given. If you leave one of them as a variable, one of the goals s2int(XYZ, SXYZ) has then two uninstantiated variables as arguments. It describes thus an infinitely large set, whose complete enumeration takes infinitely long.

    Not sure what you are after, but probably you want to define add/3 for successor arithmetics. You can do this, without resorting to the 0,1,2 integers! Try it! Otherwise search of .