给定值 x 和 y 如果为真则返回规则名称 [英] Given values x and y return rule name if it is true

查看:43
本文介绍了给定值 x 和 y 如果为真则返回规则名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的序言文件.

male(bob).
male(john).

female(betty).
female(dana).

father(bob, john).
father(bob, dana).
mother(betty, john).
mother(betty, dana).

husband(X, Y) :- male(X), mother(Y, Z), father(X, Z).
wife(X, Y) :- female(X), father(Y, Z), mother(X, Z).
son(X, Y) :- male(X), mother(Y, X);female(X), father(Y, X).
daughter(X, Y) :- female(X), mother(Y, X);female(X), father(Y, X).
sister(X, Y) :- female(X), mother(Z, X), mother(Z, Y), X \= Y.
brother(X, Y) :- male(X), mother(Z, X), mother(Z, Y), X \= Y.

我想要一个规则名称,如果它对任何值 x 或 y 返回 true.假设 x = bettyy = john.

I want a name of rule if it returns true for any value x or y. Let's say x = betty and y = john.

mother(betty, john). <- 这会满足所以我的规则应该返回'mother'.类似地,如果任何其他规则或事实对于某个值 x, y 满足 true,则应返回该规则名称.

mother(betty, john). <- this will meet so my rule should return 'mother'. Similarly if any other rule or fact meets true for some value x, y it should return that rule name.

我怎样才能实现这样的目标?

How can I achieve something like that?

推荐答案

可以很简单

query_family(P1, P2, P) :-
    % current_predicate(P/2),
    member(P, [father, mother, husband, wife, son, daughter, sister, brother]),
    call(P, P1, P2).

给予

?- query_family(betty, john, R).
R = mother ;
false.

?- query_family(betty, X, R).
X = john,
R = mother ;
X = dana,
R = mother ;
X = bob,
R = wife ;
X = bob,
R = wife ;
false.

答案后面的分号表示'gimme next'

the semicolon after the answer means 'gimme next'

这篇关于给定值 x 和 y 如果为真则返回规则名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆