35

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!

false
  • 10,264
  • 13
  • 101
  • 209
intelis
  • 7,829
  • 14
  • 58
  • 102

2 Answers2

24

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • is there a way to turn it off? Sometimes I just can't write code that way. – intelis May 04 '13 at 12:29
  • 1
    @intelis There is a way to tell Prolog that you intend to spread clauses around ([link to an answer explaining how to do it](http://stackoverflow.com/q/2482101/335858)). – Sergey Kalinichenko May 04 '13 at 12:34
  • thanks, but it is not just clauses. I get bunch of different warnings, like Singleton variables etc. – intelis May 04 '13 at 12:42
  • 2
    @intelis You should fix singleton variable warnings: they are far more important, because a small typo can make your rule incorrect. I once had to debug someone else's Prolog program where the author disregarded this warning. The problem was that he made a typo - he put a zero in place of an uppercase `O` in the middle of a variable name that was 23-characters long, and disregarded a warning. If you do need a singleton variable, simply replace it with underscore to silence the warning. – Sergey Kalinichenko May 04 '13 at 12:47
  • I am not sure what Singleton variables are :) ? – intelis May 04 '13 at 12:59
  • 2
    @intelis A singleton variable is a variable (i.e. an identifier starting in an uppercase letter) that is used only once in the entire body of a rule. Singleton variables unify with anything, but leave the results of unification unused. Sometimes this is legitimate, in which case you should rename the variable to `_`. However, very often it's a result of either capitalizing an atom improperly (i.e. using `female(Jane).` instead of `female(jane).`) or misspelling a variable name. – Sergey Kalinichenko May 04 '13 at 13:08
12

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.
mndrix
  • 3,131
  • 1
  • 30
  • 23