Linter

Expanding my previous hints for using the Logtalk linter to check Prolog modules code.

Since yesterday, I have been doing just that on the SWI-Prolog libraries and packages plus on some non-trivial open source Prolog applications. This work resulted in opening issues on code repos and mailing authors for other codebases I checked. Some of the issues found have already been fixed.

A recipe that you can apply is:

  1. Install Logtalk if not already done. For this use case, you can use the pack:
?- pack_install(logtalk).
  1. Change directory to where your Prolog modules files reside and load Logtalk:
$ cd .....
$ swipl
?- use_module(library(logtalk).
  1. Load a required library and set linter flags (we turn a number of them off to minimize noise):
?- {os(loader)}.
...
true

?- set_logtalk_flag(duplicated_clauses,warning), set_logtalk_flag(unknown_entities,silent), set_logtalk_flag(unknown_predicates,silent), set_logtalk_flag(undefined_predicates,silent), set_logtalk_flag(unknown_entities,silent), set_logtalk_flag(missing_directives,silent).
true.
  1. Try to Prolog compile the code as Logtalk in order to use the linter:
?- os::directory_files('.', Files, [type(regular),extensions(['.pl']),paths(relative)]), forall(member(File,Files), ignore(logtalk_compile(File))).

Ignore the errors (Logtalk cannot cope with everything proprietary found in all Prolog systems and the code and its loading order would need some changes to avoid some other errors) and instead look for the warnings. A few reported issues will be false positives. Several will likely be programming style issues. Some will be performance issues (e.g. using =../2 when not necessary). Any duplicated clauses/grammar rules are most likely bugs. Feel free to ask if some of the reported issues are not clear.

3 Likes