这是我的序言文件。

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
mother(betty, john). 类似地,如果任何其他规则或事实对于某个值 x, y 满足 true,则它应该返回该规则名称。

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

最佳答案

可能很容易

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.

答案后的分号表示“下一个给我”

关于prolog - 给定值 x 和 y 如果为真则返回规则名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32770849/

10-11 15:36