How do you define a rule that requires everything in Prolog?

Knowledge base:

must_have(user, car, blue).
must_have(user, car, yellow).
must_have(user, bike, green).

      

How to determine:

is_a_collector(X):-must_have(X, car, blue),
                   must_have(X,car,yellow),
                   must_have(X,bike,green).

      

without explaining all the conditions?

I say this because the field of knowledge is large and I want to define a rule that captures many conditions.

I am using Swi-Prolog.

+3


source to share


1 answer


For SWI-Prolog, you can use foreach/2

from library(aggregate)

. Read the documentation on what exactly it does, but it actually creates the connection you need.

?- foreach(must_have(X, _, _), X = user).
true.

?- foreach(must_have(X, _, _), X = foo).
false.

      

Or, if you want to define a predicate,

is_a_collector(X) :- foreach(must_have(Y, _, _), Y = X).

      



However, @lurker is right that your question is a little unclear. The proposed solution also seems strange. At the very least, your database must contain other tables if you want to specify any queries that don't return truisms. At this point, the first request above asks for:

"Does every must_have/3

fact have user

as its first argument?"

You could use forall/2

directly for this purpose , because you don't need a connection.

And what are you going to do with the answer to this question?

+3


source







All Articles