我在Prolog中为一个家庭的数据库编写了一个程序。我定义了一些谓词,例如mother_of
,father_of
,son_of
,grandparent(G,X)
。我应该定义上述规则。我是Prolog的新手。如果我的代码是正确的,或者我是在正确的道路上,有人可以告诉我吗?
man(dan).
man(joe).
man(paul).
woman(marry).
woman(susie).
parent(joe,susie). % means adam is parent of peter
parent(joe,dan) :- child(dan,joe).
parent(marry,susie).
parent(giuseppe,margaret).
parent(giuseppe,evelyn).
parent(giuseppe,robert).
parent(giuseppe,grace).
parent(giuseppe,fred).
parent(giuseppe,mary).
parent(giuseppe,estelle).
parent(albina,margaret).
parent(albina,evelyn).
parent(albina,robert).
parent(albina,grace).
parent(albina,fred).
parent(albina,mary).
parent(albina,estelle).
parent(ida,catherine).
parent(ida,juanita).
parent(ida,edythe).
parent(ida,george).
parent(johnMiddleton,catherine).
parent(georgeAlexandar,juanita).
parent(georgeAlexandar,edythe).
parent(georgeAlexandar,george).
parent(estelle,steve).
parent(estelle,judy).
parent(estelle,david).
parent(estelle,marilyn).
parent(estelle,john).
parent(george,steve).
parent(george,judy).
parent(george,david).
parent(george,marilyn).
parent(george,john).
parent(marry,dan).
father_of(F,C):-man(F),parent(F,C).
mother_of(M,C):-woman(M),parent(M,C).
son_of(C,F) :-man(C),child(C,F).
brother(B,P) :- man(B),
mother(M,B),
mother(M,P),
father(F,B),
father(F,P).
sister(S,P) :- woman(S),
mother(M,S),
mother(M,P),
father(F,S),
father(F,P).
grandparent(G,X) :- parent(G,P),
parent(P,X).
grandparent(G,john).
答案 0 :(得分:0)
我添加了
child(C, P) :-
parent(P, C).
并注释掉
% parent(joe,dan) :- child(dan,joe).
和
% grandparent(G, john).
现在您可以尝试
?- child(evelyn, giuseppe).
true .
符合预期。
所有代码供您参考
man(dan).
man(joe).
man(paul).
woman(marry).
woman(susie).
parent(joe, susie).
% Do not use this as it cause stack overflow due to child/2 definition later on
parent(joe, dan).
% :- child(dan, joe).
parent(marry, susie).
parent(giuseppe, margaret).
parent(giuseppe, evelyn).
parent(giuseppe, robert).
parent(giuseppe, grace).
parent(giuseppe, fred).
parent(giuseppe, mary).
parent(giuseppe, estelle).
parent(albina, margaret).
parent(albina, evelyn).
parent(albina, robert).
parent(albina, grace).
parent(albina, fred).
parent(albina, mary).
parent(albina, estelle).
parent(ida, catherine).
parent(ida, juanita).
parent(ida, edythe).
parent(ida, george).
parent(johnMiddleton, catherine).
parent(georgeAlexandar, juanita).
parent(georgeAlexandar, edythe).
parent(georgeAlexandar, george).
parent(estelle, steve).
parent(estelle, judy).
parent(estelle, david).
parent(estelle, marilyn).
parent(estelle, john).
parent(george, steve).
parent(george, judy).
parent(george, david).
parent(george, marilyn).
parent(george, john).
parent(marry, dan).
father_of(F, C) :-
man(F),
parent(F, C).
mother_of(M, C) :-
woman(M),
parent(M, C).
son_of(C, F) :-
man(C),
child(C, F).
brother(B, P) :-
man(B),
mother(M, B),
mother(M, P),
father(F, B),
father(F, P).
sister(S, P) :-
woman(S),
mother(M, S),
mother(M, P),
father(F, S),
father(F, P).
grandparent(G, X) :-
parent(G, P),
parent(P, X).
% ???
% grandparent(G, john).
child(A, B) :-
parent(B, A).