1

编译时收到两条错误消息:

"Clauses of (-)/1 are not together in source-file" and
"Singleton variables: [X]"

...这两个是我遇到的错误的例子...

我已经尝试删除源代码中的一些事实,这样所有事实都不会发生冲突,并且我尝试将人定义为原子而不是子句......它们都没有像我希望的那样起作用,有任何想法吗?

male(X).
female(X).
parent(X,Y).
father(X,Y):- male(X), parent(X,Y).
mother(X,Y):- female(X), parent(X,Y).
descendant(X,Y):- parent(Y,X).
sibling(X,Y):- descendant(X,Z), descendant(Y,Z).
brother(X,Y):- male(X), sibling(X,Y).
sister(X,Y):- female(X), sibling(X,Y).
grandparent(X,Y):- parent(X,Z), parent(Z,Y).
paternalgrandparent(X,Y):- father(X,Z), father(Z,Y).
ancestor(X,Y):- parent(X,Y).
ancestor(X,Y):- ancestor(X,Z), parent(Z,Y).
male(edward).
male(sean).
male(kevin).
female(vicky).
male(malcolm).
male(claude).
male(matthew).
female(stephania).
male(kurt).
male(david).
male(mark).
male(raymond).
female(therese).
female(nadine).
female(nathalie).
male(richard).
female(mary).
male(john).
female(lilian).
female(inez).
male(william).
female(rose).
male(richie).
female(alice).
brother(edward,vicky).
brother(kevin,sean).
brother(sean,vicky).
sister(vicky,edward).
brother(malcolm,claude).
brother(claude,malcolm).
brother(matthew,stephania).
brother(kurt,matthew).
sister(stephania,kurt).
brother(david,mark).
brother(mark,david).
sister(therese,nadine).
sister(nadine,therese).
sister(lilian,inez).
sister(inez,lilian).
father(david,edward).
mother(therese,edward).
father(mark,malcolm).
mother(nathalie,malcolm).
father(raymond,matthew).
mother(nadine,matthew).
father(richard,david).
mother(mary,david).
father(john,therese).
mother(lilian,therese).
paternalgrandparent(richard,edward).
grandparent(mary,edward).
grandparent(lilian,edward).
grandparent(john,edward).
paternalgrandparent(richard,malcolm).
grandparent(mary,malcolm).
grandparent(lilian,matthew).
grandparent(john,matthew).
father(william,richard).
mother(rose,richard).
paternalgrandparent(william,david).
grandparent(rose,david).
father(richie,william).
mother(alice,william).
paternalgrandparent(richie,richard).
grandparent(alice,richard).
ancestor(richie,william).
ancestor(alice,william).
ancestor(william,richard).
ancestor(rose,richard).
ancestor(richard,david).
ancestor(mary,david).
ancestor(david,edward).
ancestor(therese,edward).
ancestor(lilian,therese).
ancestor(john,therese).
ancestor(lilian,nadine).
ancestor(john,nadine).
ancestor(richard,mark).
ancestor(mary,mark).
4

2 回答 2

1

“子句不在一起”是最容易修复的错误:只需重新排列程序的事实,使所有具有相同名称的事实(例如male/1female/1等)一起出现在程序的源代码中。

“单例变量”错误来自以下几行:

male(X).
female(X).
parent(X,Y).

当一个变量在实体规则或事实中只被提及一次时,它是单例的。如果您确实需要这些变量,则应将它们替换为下划线。但是,在您的情况下,您不需要这些变量:否则,像这样的规则

male(_).

会断言任何人都是男性,而您不希望这种情况发生。您应该简单地删除这些规则以使单例错误消失。

但是,您的程序最大的问题似乎是您试图为已经通过规则定义的事物定义事实。您需要的事实是male/1female/1parent/2。其他一切都可以通过规则正确推导出来。

于 2013-04-23T14:52:47.833 回答
0

它们应该只是警告而不是错误。第一条消息是告诉您一起定义谓词。

所以例如

male(edward).
male(sean).
male(kevin).
female(vicky).
male(malcolm).

不好。这样做的原因是你传播了男性的定义,代码变得完全不可读。

第二个是告诉你,如果你在头部使用一个变量,你应该在正文中使用它。

所以例如

male(X).

不好。你可以写

male(_).

同样,单例变量经常会导致您尝试编写的内容出现错误,因此最好使用 _ 来明确使用它们。

于 2013-04-23T14:56:13.610 回答