Style Warnings

In addition to checking for syntax errors, Quintus Prolog also has a style checker, which displays warning messages whenever certain stylistic conventions are violated in a program. Whereas syntax error messages indicate clauses that cannot be read into Prolog, style warnings simply indicate possible typing mistakes, or program construction that doesn't follow Quintus Prolog style conventions. The style conventions for Quintus Prolog are listed below. If you adhere to these conventions, you can use the style warnings to catch simple errors very easily.

  1. Define all clauses for a given procedure in one file. This is essential; the load predicates do not allow the definition of a procedure to be spread across more than one file unless the procedure is declared multifile -- see multifile/1 for more information on this. If a non-multifile procedure is defined in more than one file, and all the files in which the procedure is defined are compiled, each definition of the procedure in a new file will wipe out any clauses for the procedure that were defined in previous files.
  2. Make all clauses for a given procedure contiguous in the source file. This doesn't mean that you should avoid leaving blank space or putting comments between clauses, but simply that clauses for one procedure should not be interspersed with clauses for another procedure.
  3. If a variable appears only once in a clause, either write that variable as the single character _ (the void variable), or begin the variable name with the character _.

If any of these conditions are not met, you will be warned when the file containing the offending clauses is compiled.

If style convention 1 is violated, Prolog displays a message like the one shown below before it compiles each procedure that has been defined in another file that has already been loaded:

     * Procedure foo/2 is being redefined in a different file -
     *    Previous file: /ufs/george/file1
     *    New file:      /ufs/george/file2
     * Do you really want to redefine it? (y,n,p,s, or ?)
     

If you type y, the definition in the file currently being loaded replaces the existing procedure definition. If you type n, the existing definition remains intact, and the definition in the file currently being loaded is ignored. If you type p (for proceed), the definition in the file currently being loaded replaces the existing definition; furthermore, the remaining procedure definitions in the file /ufs/george/file2 will automatically replace any existing definitions made by the file /ufs/george/file1 without displaying any warning messages. If you type s (for suppress), the existing definition remains intact and the definition in the file currently being loaded is ignored; furthermore, the remaining procedure definitions in the file /ufs/george/file2 which attempt to replace definitions made by the file /ufs/george/file1 will be ignored without displaying any warning messages. (These options are particularly useful if you have changed the name or location of a file, since it suppresses the warnings you would otherwise get for every procedure in the file.) If you type ?, Prolog displays a message that briefly describes each of the options above, and then asks you again if you want to redefine the procedure.

If style convention 2 is violated, you will get a message of the form:

     * Clauses for foo/2 are not together in the source file
     

This indicates that between some pair of clauses defining procedure foo/2, there is a clause for some other procedure. If you followed the style conventions in writing your code, this message would indicate that some clause in your source file had either a mistyped name or the wrong arity, or that the predicate foo/2 was defined more than once in the file. One other possible cause for this message might be that a period was typed in place of a comma, as in

     foo(X, Y) :-
        goal1(X, Z),
        goal2(Z).
        goal3(X, Y).
     foo([], []).
     

Here the Prolog system will think that you are defining a clause for goal3/2 between the clauses for foo/2, and will issue a style warning.

If style convention 3 is violated, as in

     check_state(TheState):-
        old_state(TheStaye, X),
        write(TheState),
        write(X).
     

you will get a message of the form:

     * Singleton variables, clause 1 of check_state/1: TheStaye
     

indicating that in the first clause of procedure check_state/1, there is only one occurrence of the variable TheStaye. If that variable is a misspelling, you should correct the source text and recompile. If it was really meant to be a single variable occurrence, replace it with the anonymous variable _ or preface it with _ as in _TheStaye, and you will no longer get the style warning message.

It is good programming practice to respond immediately to these warnings by correcting the source text. By doing so, you will get the full benefit of the style warning facility in finding many errors painlessly.

By default, all the style warning facilities are turned on. You can turn off any or all of the style warning facilities by typing no_style_check(X). at the main Prolog prompt, where X represents one of the arguments listed below. To turn on style warning facilities, type style_check(X). at the main Prolog prompt, where X represents one of the arguments listed below.


Argument
Function
all
turns on (or off) all style checking
single_var
turns on (or off) checking for single variable occurrences
discontiguous
turns on (or off) checking for discontiguous clauses for procedures
multiple
turns on (or off) style checking for multiple definitions of same procedures (in different files)

For example, to turn off all the style warning facilities, you would type

     | ?- no_style_check(all).