interstar
interstar

Reputation: 27166

Prolog : querying if something doesn't satisfy a predicate

I'm sure this is a n00b question, but how do I ask for things which satisfy one predicate and not another?

Eg. in a database of people and ice-cream,

person(john).
person(jane).
person(nicholas).

likes(john,strawberry).
likes(john,mint).
likes(jane,choc-chip).

How do I ask for the people who don't like ice-cream, to get just nicholas?

Upvotes: 0

Views: 62

Answers (2)

RobertBaron
RobertBaron

Reputation: 2854

This query is true if X is a person, and there exists no _ where likes(X, _) is true.

person(X), \+ likes(X, _).

X will evaluate to Nicolas.

Upvotes: 1

interstar
interstar

Reputation: 27166

Ah. OK I found it.

no-ice(X) :- 
    person(X),
    not(likes(X,_)).

Upvotes: 0

Related Questions