killajoule
killajoule

Reputation: 3832

Prolog ~ Resolution involving the negation of some other statement?

I have the following code, where I define is_max as the negation of is_below.

rank(mary,1).
rank(juan,2).
rank(john,3).
rank(jane,4).

is_below(X)   :- rank(X,A), rank(Y,B), A<B .
is_max(X)     :- not(is_below(X)) .

is_below is true if X is below someone in terms of rank. Therefore the person with the highest/max rank is someone for whom is_below is false (accordingly, is_max is defined).

When I query

is_max(jane) .

This is true (the above is false for mary, juan, and john)

However, when I query

is_max(X) .

This is false. I was expecting it to return jane. My logic seems to be fine, so I am not sure why I don't get jane. Any insight would be really appreciated!

Upvotes: 1

Views: 75

Answers (1)

Sergii Dymchenko
Sergii Dymchenko

Reputation: 7209

Prolog has negation as failure. Think of it as 'not provable'.

is_max(X) would be true if is_below(X) would always be false. is_below(X) is true for X=mary, so is_below(X) is provable, and is_max(X) is false.

Because of this it's better to use notation \+ instead of not, to remind yourself that it's not true not.

A possible way to fix your code is to explicitly instantiate X:

is_max(X) :- rank(X,_), \+ is_below(X).

Upvotes: 2

Related Questions