prologsuccessor-arithmetics

Prolog first integer exactly 1 greater than second integer


The program should return true if the first integer is exactly 1 more than the second integer.

Current code returns false every time.

Without the line abs(X-Y) == 1 it's working for checking if the second integer is more than the first, but it should check if the difference is exactly 1.

expected output:

greater_than(succ(succ(0)),succ(0)).
yes
greater_than(succ(succ(0)),succ(succ(succ(0))))) 
no

current code :
greater_than(succ(X),0).
greater_than(succ(X),succ(Y)) :-
   abs(X-Y) == 1,
   greater_than(X,Y).

Solution

  • A Peano number x is exactly one greater than a Peano number y, given x = succ(y), so we can here write this like:

    one_greater_than(succ(X), X).
    

    Given the first argument thus is of shape succ(X), Prolog will aim to unify the only argument of the succ/1 functor with the second predicate.