Prolog家谱编译错误

时间:2013-04-23 14:40:48

标签: prolog

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

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

2 个答案:

答案 0 :(得分:1)

“条款不在一起”是最简单的修复错误:只需重新安排程序的事实,使所有具有相同名称的事实(例如male/1female/1等)一起出现在你的程序源中。

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

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

变量是单例,只在enture规则或事实中提及一次。如果您确实需要这些变量,则应使用下划线替换它们。但是,在您的情况下,您不需要这些变量:否则,这样的规则

male(_).

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

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

答案 1 :(得分:0)

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

所以例如

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

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

第二个是告诉你如果在头部使用变量,你应该在体内使用它。

所以例如

male(X).

不好。你可以写

male(_).

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