Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog: Clauses are not together in source-file

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!

like image 922
intelis Avatar asked May 04 '13 11:05

intelis


People also ask

How do you write a clause in Prolog?

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.

What are Singleton variables in Prolog?

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.

What are Prolog clauses?

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

What are the three kinds of Prolog clauses?

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.


2 Answers

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.

like image 101
Sergey Kalinichenko Avatar answered Sep 20 '22 14:09

Sergey Kalinichenko


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. 
like image 23
mndrix Avatar answered Sep 19 '22 14:09

mndrix