Why can't I add chr constraints in declaratives when reading a file?

Consider this simple program:

:- use_module(library(chr)).
:- chr_constraint foo/1.
:- foo(a).

When I try loading it, I get:

ERROR: /home/carlo/code/prolog/question.pl:4:
ERROR:    catch/3: Unknown procedure: foo/1
Warning: /home/carlo/code/prolog/question.pl:4:
Warning:    Goal (directive) failed: user:foo(a)

could you explain why? I was convinced that all the CHR constraints went in an unique per-module store. What’s the mental model I should use instead?

Well, you should follow CHR syntax. Are you aware of Annie’ tutorial ?
What you did is an invocation of an unknown predicate foo/1, by means of a ‘directive’ (sorry I can’t find the relevant section in docs…)
Maybe you should write

foo(a) <=> true.
1 Like

Thanks @CapelliC, yes I read Annie’s tutorial, but my intent here is not discharging the rule, like you’re doing in your example, but just load that constraint in the constraint store.
I’ll define the rules to manipulate those constraints later, I just wanted to start filling the store when I read the file (I’m exploring mechanisms to add typechecking).

Why do you say that the predicate is unknown? Shouldn’t it be known at that point since I defined it at line 2?

yes, my example is wrong… should be simply

foo(a).

I cannot see much value in CHR (my bad for sure), so… sorry for the noise…

It’s not noise at all @CapelliC, in fact you’re right, I can just write:

:- use_module(library(chr)).
:- chr_constraint foo/1.
foo(a).

without errors. I wonder if this expresses what I mean; I’ll play with this some more, thanks for chiming in! :slight_smile: