Prolog规则与查询

Prolog规则与查询,prolog,Prolog,我需要一些帮助来查找规则和/或在Prolog中查询知识库,以及超市中的客户信息 例如,我有: Customer(Name,Age,Sex,Wage). customer(John,30,M,2000). customer(Mary,35,F,2500). customer(Mark,40,M,2500). invoice(Number, CostumerName, Product, Price). invoice(001, John, Potatoes, 20). invoice(002,

我需要一些帮助来查找规则和/或在Prolog中查询知识库,以及超市中的客户信息

例如,我有:

Customer(Name,Age,Sex,Wage).

customer(John,30,M,2000).
customer(Mary,35,F,2500).
customer(Mark,40,M,2500).

invoice(Number, CostumerName, Product, Price).

invoice(001, John, Potatoes, 20).
invoice(002, John, Tomatoes, 10).
invoice(003, Mary, Soap, 50).
invoice(004, Mark, Potatoes, 20).
invoice(005, Mary, Detergent, 15).

Food(Potatoes).
Food(Tomatoes).
CleanProd(Soap).
CleanProd(Detergent).

例如,如果我想找到一种趋势,了解女性购买的清洁产品比男性多……我应该使用哪种类型或什么规则和查询?

您可以使用
findall
然后是
sort
来收集查询的唯一结果(您不希望列表中出现重复的发票),和
length
检查收集结果的长度

例如:

findall(X, (invoice(X, C, P, _), customer(C, _, f, _), cleanProd(P)), Xs),
sort(Xs, InvoicesOfWomenBuyingCleanProducts),
length(InvoicesOfMenBuyingCleanProducts, N).
另外,请注意,变量以大写字母开头,原子以小写字母开头(也适用于谓词名称)

如果你真的想要一个以大写字母开头的原子,你必须用单引号括起来

变量:
M

原子:
'M'

原子:
m

例如,您可以通过以下方式重写知识库:

%   this is not needed, however is useful as
%   a documentation of what your predicate means:
% customer(Name,Age,Sex,Wage).

customer('John',30,m,2000).
customer('Mary',35,f,2500).
customer('Mark',40,m,2500).

% invoice(Number, CostumerName, Product, Price).

invoice(001, 'John', potatoes, 20).
invoice(002, 'John', tomatoes, 10).
invoice(003, 'Mary', soap, 50).
invoice(004, 'Mark', potatoes, 20).
invoice(005, 'Mary', detergent, 15).

food(potatoes).
food(tomatoes).
cleanProd(soap).
cleanProd(detergent).

谢谢你的帮助。但是我正在尝试使用setof(),它工作不正常…它给了我语法错误…对不起,
setof
可能有点混乱。我编辑了我的答案,使用
findall
sort
来做同样的事情,以获得帮助。但是我正在尝试使用setof()但它不能正常工作…它给了我语法错误。。。如何使用setof并引入argumenst的“角色”?我的意思是…想象一下,我只是想知道客户表中的女性名单。。。给定的结果应该是这样的:Women=[mary]我怎么能这样做呢?
findall(C,customer(C,f,f,Women)