Prolog - Simple Negative Request

My database:

eat(magi,limo).
eat(nona,banana).

How can I ask: "Who does not eat a limousine?" It:

eat(X,not(limo)).

Does not work.: (

+3
source share
2 answers

First of all, it limois a symbol, and you cannot deny symbols. What you want to do is nullify the predicate, i.e. not(eat(X, limo)).

However, this still does not give you nona. Why not? Well, there are infinitely many values โ€‹โ€‹of X for which it eat(X, limo)will be false. The system needs more information than X doesn't eat a limo to find out which one you want. Instead, we need to ask X"X is eating something, but X is not eating a limo." This brings us to the following query:

eat(X,Y), not(eat(X, limo)).

Which gives us nonaas a solution for X.

+8
source

eat(X, Y), Y \= limo, writeln(X), false.

0
source

Source: https://habr.com/ru/post/1785973/


All Articles