Prolog Parent relation using only brother and sister rules

允我心安 提交于 2020-01-03 04:34:08

问题


this is my first time using Prolog, and I was wondering if anyone could give me some advice on my logic:

male(jerry).
male(stuart).
male(warren).
male(peter).
female(kather).
female(maryalice).
female(ann).
brother(jerry,stuart).
brother(jerry,kather).
brother(peter, warren).
sister(ann, maryalice).
sister(kather,jerry).
parent_of(warren,jerry).
parent_of(maryalice,jerry).

This is part of a homework assignment, and we are only allowed to use the above facts. In order to know that warren and mary alice are also parents of stuart and kather, some rules need to be implemented. What I've done is:

parent_of(X,Y) :- brother(Z,Y), parent_of(X,Z).
parent_of(X,Y) :- brother(Y,Z), parent_of(X,Z).
parent_of(X,Y) :- sister(Z,Y), parent_of(X,Z).
parent_of(X,Y) :- sister(Y,Z), parent_of(X,Z).

Querying parent_of(X,Y) in prolog using the above rules and facts have set me on an infinite loop with recursive values of X=warren, Y=stuart and X=maryalice, Y=stuart.

Any advice would be greatly appreciated. Thank You!


回答1:


If you tried your query from the interactive prompt, you will find it works. I would have probably rewritten it as:

?- parent_of(P,C),
   ( ( sister(C,OC) ; sister(OC,C) )
   ; ( brother(C,OC) ; brother(OC,C) )
   ).

but it really doesn't matter.

The easiest solution to defining a predicate with the exact definition as this query is not calling it parent_of, as you are introducing a recursive call that you don't terminate in any way. So call it maybe also_parent_of?

Furthermore, you have a redundant fact in your database: brother(jerry,kather) and sister(kather,jerry). I don't know if this is on purpose, but using this query, you will get the answer P=warren, C=jerry, OC=kather twice.



来源:https://stackoverflow.com/questions/15714834/prolog-parent-relation-using-only-brother-and-sister-rules

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!