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!
In Prolog, the program contains a sequence of one or more clauses. The clauses can run over many lines. Using a dot character, a clause can be terminated. This dot character is followed by at least one 'white space' character.
A singleton variable is a variable that appears only one time in a clause. It can always be replaced by _ , the anonymous variable. In some cases, however, people prefer to give the variable a name.
A clause in Prolog is a unit of information in a Prolog program ending with a full stop (" . "). A clause may be a fact, like: likes(mary, pizza). food(pizza).
Prolog clauses are normally of three types: facts declare things that are always true. rules declare things that are true depending on a given condition. questions to find out if a particular goal is true.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With