Sweeprolog and diagnostics on chr symbols

Hey @oskardrums, I have noticed that the sweeprolog analyzer shows spurious diagnostics in files that use the chr store. For example, I don’t see how I should import chr_constraint/1, '<=>'/2 and '==>'/2 so that I don’t get the unreferenced definition and unreferenced predicate errors.

Can I somehow convince sweeprolog to behave here?

Hi,

Good question, AFAICT this is not strictly a Sweep issue, but let’s tackle it regardless.
The reason that Sweep says that chr_constraint/1 and friends are not defined is because they aren’t. These terms are processed via term_expansion/2 rules.
(BTW I don’t see unreferenced warnings, only undefined. do you have some code I can look at where you get those?)
(EDIT: OK now I see the unreferenced warnings as well :slight_smile: The warning depends on whether or not you put a :- before e.g. chr_type/1.)
PceEmacs also seem to consider these predicates undefined, which is not surprising since Sweep reuses exactly the same code for this analysis.

The simplest solution that comes to mind is for library(chr) to provide dummy definitions for these “predicates”, or otherwise explicitly inform library(prolog_xref) about them. I think that’s a reasonable expectation from libraries that define such term expansions, since the cross reference analysis has little chance to spot these cases reliably.

A simple solution that you can employ in the meantime is to define chr_constraint/1, like so:

chr_constraint(_).
chr_type(_).

Still not great, for example now jumping to the definition of these things takes you to the wrong place. But it should at least pacify library(prolog_xref) so you no longer see these warnings.

1 Like

Very insightful reply as always! :slightly_smiling_face:

Is this kind of solution implemented in an analogue case so I can take a look at an example?

That works beautifully for chr_constraint/1, but doesn’t for (<=>)/2 and (==>)/2, maybe since they are expanded to other things? Or is the reason a different one?

Thanks, but scratch (most of) that.
After giving it a closer look, I see that @jan already took care of everything quite nicely. library(chr) defines goal_colours/2 and term_colours/2 rules that correctly analyze the relevant chr_* terms. The thing is that for Sweep to pick it up, you need to load library(chr).
What do you get if you say ?- [library(chr)]. in the Sweep top-level followed by C-c C-c in a buffer with CHR code?

EDIT: here’s how it should look in Sweep with library(chr) loaded: