Prolog: Clauses are not together in source-file

2019-04-03 08:15发布

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!

2条回答
劳资没心,怎么记你
2楼-- · 2019-04-03 08:17

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.
查看更多
贪生不怕死
3楼-- · 2019-04-03 08:39

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.

查看更多
登录 后发表回答