如何在 Prolog 中修复这个循环谓词? [英] How can I fix this circular predicate in Prolog?

查看:36
本文介绍了如何在 Prolog 中修复这个循环谓词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在 Prolog 中定义已婚"不起作用?

Why doesn't this work to define "married" in Prolog?

married(X,Y):-married(Y,X).

这种循环谓词是不允许的吗?我该如何解决?

Are these kinds of circular predicates not allowed? How would I fix it?

谢谢

推荐答案

如果语法错误,请原谅我,我已经有一段时间没有使用 Prolog 了.

Forgive me if I get the syntax wrong, it's been a while since I played with Prolog.

典型的解决方案是在子句中引入另一个层次,如下所示:

A typical solution is to introduce another level to the clauses, like this:

married(X, Y) :- wife(X, Y).
married(X, Y) :- wife(Y, X).

然后使用wife子句指定关系:

and then specifying the relations using the wife clause instead:

wife(jane, bob).
wife(alice, john).

?- married(jane, X).
X = bob

更多信息可以在这里找到:CSc 8710,演绎数据库和逻辑编程,第 6 章 - 逻辑和数据库,在 6.5 - 特殊关系下.

More information can be found here: CSc 8710, Deductive Databases and Logic Programming, chapter 6 - Logic and databases, under 6.5 - Special Relations.

这篇关于如何在 Prolog 中修复这个循环谓词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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