Reputation: 1293
I'm trying to write a Prolog program with a rule good
which takes a list, where each term in the list is a Peano number. The rule should be true if and only if each element in the list is greater than the previous element.
For example, the following examples should be true:
good([]).
good([0]).
good([0,s(0)]).
good([0,s(s(0))]).
good([0,s(0),s(s(0))]).
And the following examples should be false:
good([s(0),0]).
good([0,s(0),0]).
Here is my code:
plus(X,0,X).
plus(X,s(Y),s(Z)) :- plus(X,Y,Z).
geq(X,Y) :- plus(K,Y,X).
ge(X,Y) :- geq(X,Y), not(X = Y).
good([]).
good([X]).
good([H|T]) :- good(H,T).
good(X,[H|T]) :- ge(H,X), good(T).
However, for the positive query good([0,s(0),s(s(0))])
, both true and false are solutions.
What is the error?
Upvotes: 1
Views: 510
Reputation: 4078
Please see this answer. First getting true
and then getting false
after backtracking, just means that it found an answer, then at your request tried a different path, and then couldn't find any other path to reach true
, so returned false
.
In other words, as that answerer says,
The
false
response can appear inconsistent to beginning Prolog programmers and "feel" like an error or warning, but it's actually a perfectly normal Prolog response.
Upvotes: 1