I am using SWI Prolog, the following code is used in my homework(the code is not the homework but needed to write the methods the course requires):
nat(0).
nat(s(X)) :-
nat(X).
plus(0,N,N) :-
nat(N).
plus(s(M),N,s(Z)) :-
plus(M,N,Z).
times(0,N,0) :-
nat(N).
times(s(M),N,Z) :-
times(M,N,W),
plus(W,N,Z).
exp(s(M),0,0) :-
nat(M).
exp(0,s(M),s(0)) :-
nat(M).
exp(s(N),X,Z) :-
exp(N,X,Y),
times(X,Y,Z).
exp(a,b,c)
means c=b^a
.
It is copied directly from the book: "The Art of Prolog: Advanced Programming Techniques".
When I run the following query:
exp(s(s(0)),L,s(s(s(s(0))))).
I get an answer:
L = s(s(0))
But when I ask for another answer by entering ;
L = s(s(0)) ;
I get an infinite loop(ending in an out of stack error), I was expecting to get false.
What is the problem in the code? is there a code that does the same(with the same representation of natural numbers) but behaves the way I described? and if so, a few pointers or suggestions would be of great help.
Thanks in advance.