仅使用兄弟姐妹规则的 Prolog 父关系 [英] Prolog Parent relation using only brother and sister rules

查看:60
本文介绍了仅使用兄弟姐妹规则的 Prolog 父关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用 Prolog,我想知道是否有人可以就我的逻辑给我一些建议:

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).

使用上述规则和事实在 prolog 中查询 parent_of(X,Y) 使我陷入无限循环,递归值 X=warren, Y=stuart 和 X=maryalice, Y=stuart.

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) )
   ).

但这真的无所谓.

定义具有确切定义的谓词的最简单解决方案,因为此查询不调用它parent_of,因为您引入了一个不会以任何方式终止的递归调用.所以可以称之为also_parent_of?

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?

此外,您的数据库中有一个冗余事实:brother(jerry,kather)sister(kather,jerry).我不知道这是不是故意的,但是使用这个查询,你会得到两次 P=warren, C=jerry, OC=kather 的答案.

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.

这篇关于仅使用兄弟姐妹规则的 Prolog 父关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆