简体   繁体   中英

Prolog Parent relation using only brother and sister rules

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM