Prolog 给定值x和y如果为true,则返回规则名称
这是我的prolog文件Prolog 给定值x和y如果为true,则返回规则名称,prolog,Prolog,这是我的prolog文件 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,
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=betty
和y=john
母亲(贝蒂,约翰)。
可能很容易
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.
答案后的分号表示“给我下一个”
$ swipl
?- ['facts'].
?- setof( Functor,
Term^(member(Functor, [father, mother, husband, wife, son, daughter, sister, brother]),
Term =.. [Functor, betty, john],
once(Term)),
Answer).
Answer = [mother].
?-
如果您不想指定感兴趣的函子列表,可以使用
current\u谓词(F/2)
我不是Prolog专家,但我认为不可能做这种事情。当你从命令式或面向对象的世界中走出来时,Prolog非常特殊,很难理解。花点时间阅读更多关于Prolog及其工作原理的信息。“序言,现在!”是一本非常好的开始书。@EmrysMyrooin这里有。但是代码是相当阻碍的。至少对我来说。你也可以使用?-[facts]。
,让查询参数贝蒂、约翰
深深地埋藏在这样复杂的查询中听起来不是很有用。如果某个参数是变量,会发生什么?