28

我有这段代码:

% Family tree
female(pen).
male(tom).
male(bob).
female(liz).
female(pat).
female(ann).
male(jim).

parent(pam, bob).
parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).
parent(pat, jim).

我收到此错误:

Warning: Clauses of female/1 are not together in source-file
Warning: Clauses of male/1 are not together in source-file

这个错误的目的是什么?
我的意思是,文件确实编译和运行得很好,我知道错误的含义。但为什么?
这只是执行最佳实践的通知吗?

我对逻辑编程很陌生。
谢谢!

4

2 回答 2

22

正确,这是强制执行最佳实践的警告,即将所有相关子句放在源文件中。除此之外,源文件中子句的接近程度无关紧要,只要它们的相对顺序不变。

于 2013-05-04T11:55:36.467 回答
10

该警告鼓励最佳实践并有助于发现拼写错误。这是一个错字示例:

small(ant).
small(fly).
small(molecule).

smell(sweet).
smell(pungent).
small(floral).

这个错误很难发现,但幸运的是编译器警告:

Warning: /tmp/test.pl:7:
Clauses of small/1 are not together in the source-file

通过警告和行错误,可以更快地找到并纠正错字。

ISO Prolog 提供discontiguous/1指令以使特定谓词的此警告静音。请参阅规范的第 7.4.2.3 节。它是这样使用的:

:- discontiguous small/1.
于 2013-07-19T19:14:13.670 回答