在Prolog中如何设计一组编码族谱关系的谓词

时间:2017-07-31 03:27:50

标签: prolog

我是prolog的新手,我需要帮助,如何设计一组编码家谱关系的谓词。

• male(X) - X is male.
• female(X) - X is female.
• parent(X,Y) - X is the parent of Y.
• mother(X,Y) - X is the mother of Y.
• father(X,Y) - X is the father of Y.
• child(X,Y) - X is the child of Y.
• sibling/2 (reflexive)
• grandparent(X,Y) - X is the grandparent of Y.
• grandmother(X,Y) - X is the grandmother of Y.
• grandfather(X,Y) - X is the grandfather of Y.
• grandchild(X,Y) - X is the gradchild of Y.
• grandson(X,Y) - X is the grandson of Y.
• granddaughter(X,Y) - X is the granddaughter of Y.
• uncle(X,Y) - X is the uncle of Y.
• aunt(X,Y) - X is the aunt of Y.
• cousin/2 (reflexive)
• ancestor(X,Y) - X is an ancestor of Y.
• descendant(X,Y) - X is a descendant of Y.
  

注意:您的定义应避免无限递归并返回a   单个结果集。例如,兄弟姐妹(X,Y)应该查询   返回单个结果集,即不是X = bob,Y = joe; X =乔,Y =鲍勃。

     

注意:以下人员的知识库仅供参考。你是   只负责谓词规则的定义。知识   用于评分的基础会有所不同。

这里的例子我只对最后一个例子有问题

% Knowledge Base
male(adam).
male(bob).
male(brett).
male(charles).
male(chris).
male(clay).
female(ava).
female(barbara).
female(betty).
female(colette).
female(carrie).
parent(adam,bob).
parent(adam,barbara).
parent(ava,bob).
parent(ava,barbara).
parent(bob,clay).
parent(barbara,colette).

?- mother(ava,Kid)
Kid = bob;
Kid = barbara.

?- sibling(X,Y).
X = bob,
Y = barbara;

?- grandparent(GParent,colette).
GParent = adam;
GParent = ava.

这就是我所拥有的

male(adam).
male(bob).
male(brett).
male(charles).
male(chris).
male(clay).
female(ava).
female(barbara).
female(betty).
female(colette).
female(carrie).
parent(adam,bob).
parent(adam,barbara).
parent(ava,bob).
parent(ava,barbara).
parent(bob,clay).
parent(barbara,colette).

mother(X, Y) :-
        female(X),
        parent(X, Y).
father(X, Y) :-
        male(X),
        parent(X,Y).
child(X, Y) :-
        parent(Y, X).
daughter(X, Y) :-
        parent(Y, X),
        female(X).
son(X, Y) :-
        parent(Y,X),
        male(X).
sister(X, Y) :-
        female(X),
        parent(Q,X),
        parent(Q,Y).
brother(X, Y) :-
        male(X),
        parent(Q,X),
        parent(Q,Y).
sibling(X, Y) :-
        parent(Q,X),
        parent(Q,Y),
        X\=Y.
uncle(X, Y) :-
        parent(P,Y),
        brother(X,P).
aunt(X, Y) :-
        parent(P,Y),
        sister(X,P).

cousin(C, Cousin):-
        parent(Parent,C),
        sibling(Parent,AU),
        child(Cousin,AU).

        %Here is Relative

relative(An, Re):-
        parent(An, Re).
relative(An, Rela):-
        parent(An, C),
        relative(C, Rela).

0 个答案:

没有答案