Prolog Family 关系,意外失败 [英] Prolog Family Relation, unexpected failure
问题描述
谁能告诉我为什么我的阿姨关系不起作用?每当我尝试调用它时,它都会返回 false.
Could anyone tell me why my aunt relation isn't working? It just returns false whenever I try to call it.
我在它下面写的叔叔关系似乎很完美.我无法弄清楚有什么区别.我也尝试过 (not(mother(X,Y)). 在最后,但这并没有改变任何东西.
The Uncle relation I wrote under it seems to work perfectly. I can't figure out what the difference is. I tried (not(mother(X,Y)). at the end also but that doesn't change anything.
/* FACTS */
parents(david, george, noreen).
parents(jennifer, george, noreen).
parents(georgejr, george, noreen).
parents(scott, george, noreen).
parents(joanne, george, noreen).
parents(jessica, david, edel).
parents(clara, david, edel).
parents(michael, david, edel).
parents(laura, georgejr, susan).
parents(anna, scott, siobhan).
/* Relationships */
father(X, Y) :- parents(Y, X, _).
male(X) :- father(X, _).
mother(X, Y) :- parents(Y, _, X).
female(X) :- mother(X, _).
grandfather(X, Y) :- father(X, Z), father(Z, Y).
grandfather(X, Y) :- father(X, Z), mother(Z, Y).
grandmother(X, Y) :- mother(X, Z), mother(Z, Y).
grandmother(X, Y) :- mother(X, Z), father(Z, Y).
brother(X, Y) :- male(X), father(Z, X), father(Z, Y).
sister(X, Y) :- female(X), father(Z, X), father(Z, Y).
aunt(X,Y) :- sister(X,Z), parents(Y, Z, _).
aunt(X,Y) :- sister(X,Z), parents(Y, _, Z).
uncle(X, Y) :- brother(X, Z), parents(Y, Z, _), not(father(X,Y)).
uncle(X, Y) :- brother(X, Z), parents(Y, _, Z), not(father(X,Y)).
推荐答案
你引入了很多冗余和至少奇怪的检查机制.
You are introducing a lot of redundancy and at least strange checking mechanisms.
father
和 mother
关系意味着您将 parents/3
关系指定为 parents(child,father,morther)代码>.我不明白你为什么定义两个查询.
The father
and mother
relationship imply that you specify the parents/3
relationship as parents(child,father,morther)
. I don't see why you define two queries.
出错的是兄妹关系会在brother(X,X)
上成功.可以用 X \= X
避免这种情况,这基本上是你在 aunt 子句中解决的.
What goes wrong is that the brother and sister relationship will succeed on brother(X,X)
. One can avoid this with X \= X
, this is basically what you resolve in the aunt clause.
此外,您还需要提供其他信息.male
和 female
关系仅在此人 (X
) 有孩子时才会解决.然而,当你没有自己的孩子时,你也可以成为阿姨或叔叔.
Furthermore you will need to provide additional information. The male
and female
relationship are only resolved when the person (X
) has children. It is however possible to be an aunt or uncle when you have no children on your own.
这应该有效:
/* FACTS */
parents(david, george, noreen).
parents(jennifer, george, noreen).
parents(georgejr, george, noreen).
parents(scott, george, noreen).
parents(joanne, george, noreen).
parents(jessica, david, edel).
parents(clara, david, edel).
parents(michael, david, edel).
parents(laura, georgejr, susan).
parents(anna, scott, siobhan).
/* Relationships */
parent(X,Y) :- parents(Y,X,_).
parent(X,Y) :- parents(Y,_,X).
father(X, Y) :- parents(Y, X, _).
male(michael).
male(X) :- father(X, _).
mother(X, Y) :- parents(Y, _, X).
female(joanne).
female(jessica).
female(jennifer).
female(clara).
female(laura).
female(anna).
female(X) :- mother(X, _).
grandfather(X, Y) :- father(X, Z), father(Z, Y).
grandfather(X, Y) :- father(X, Z), mother(Z, Y).
grandmother(X, Y) :- mother(X, Z), mother(Z, Y).
grandmother(X, Y) :- mother(X, Z), father(Z, Y).
brother(X, Y) :- male(X), father(Z, X), father(Z, Y), X \= Y.
sister(X, Y) :- female(X), father(Z, X), father(Z, Y), X \= Y.
aunt(X,Y) :- sister(X,Z), parent(Z,Y).
uncle(X, Y) :- brother(X, Z), parent(Z,Y).
这篇关于Prolog Family 关系,意外失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!