简体   繁体   English

如何在Prolog中定义要求所有的规则?

[英]How to define a rule to require all in Prolog?

Having the knowledge base: 拥有知识库:

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

How to define: 如何定义:

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

without expliciting all the conditions? 没有说明所有条件?

I say this because the knowledge domain is large and I want to define a rule that catches many conditions. 我这样说是因为知识领域很大,我想定义一个捕获许多条件的规则。

I'm using Swi-Prolog. 我正在使用Swi-Prolog。

For SWI-Prolog, you can use foreach/2 from library(aggregate) . 对于SWI-Prolog,您可以使用library(aggregate) foreach/2 library(aggregate) Read the documentation on what exactly it does, but in effect, it creates the conjunction that 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 bit unclear. 但是,@ lurker是对的,你的问题有点不清楚。 The proposed solution, too, feels somehow weird. 建议的解决方案也感觉有些奇怪。 At the very least, your database should contain other tables if you want to be able to state any queries that don't return truisms. 至少,如果您希望能够声明任何不返回真理的查询,那么您的数据库应该包含其他表。 At the moment, the first query above asks: 目前,上面的第一个查询询问:

"Does every must_have/3 fact have user as its first argument?" “每个must_have/3事实都有user作为它的第一个参数吗?”

For that purpose, you could have used forall/2 directly, because you don't need a conjunction. 为此,您可以直接使用forall/2 ,因为您不需要连接。

And what are you going to do with the answer to this? 那你对这个问题的答案是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM