Prolog: Clauses are not together in source-file

风格不统一 提交于 2020-01-01 07:32:28

问题


I have this piece of code:

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

I get this error:

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

What is the purpose of this error?
I mean, file does compile and run just fine and I am aware of the meaning of the error. But why?
Is this just a notice to enforce best practice?

I am very new to logic programming.
Thanks!


回答1:


Correct, this is a warning to enforce best practices, which is to put all related clauses together in the source file. Other than that, the proximity of clauses to each other in the source file does not matter, as long as their relative order does not change.




回答2:


The warning encourages best practice and helps spot typos. Here's a typo example:

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

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

The mistake is hard to spot, but fortunately the compiler warns:

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

With the warning and a line error, one can find and correct the typo more quickly.

ISO Prolog provides the discontiguous/1 directive to silence this warning for specific predicates. See section 7.4.2.3 of the spec. It's used like this:

:- discontiguous small/1.


来源:https://stackoverflow.com/questions/16373927/prolog-clauses-are-not-together-in-source-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!