Prolog: Clauses are not together in source-file

2019-04-03 08:07发布

问题:

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.