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

**URL:** <https://swi-prolog.discourse.group/t/why-cant-i-add-chr-constraints-in-declaratives-when-reading-a-file/2544>\
**Category:** Help!\
**Created:** [June 20, 2020, 3:49pm UTC](https://swi-prolog.discourse.group/t/why-cant-i-add-chr-constraints-in-declaratives-when-reading-a-file/2544 "2020-06-20T15:49:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![meditans](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/meditans/32/1768_2.png) [@meditans](https://swi-prolog.discourse.group/u/meditans)\
**Post date:** [June 20, 2020, 3:49pm UTC](https://swi-prolog.discourse.group/t/why-cant-i-add-chr-constraints-in-declaratives-when-reading-a-file/2544/1 "2020-06-20T15:49:48Z")

</div>

Consider this simple program:

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

```

When I try loading it, I get:

```prolog
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?

---

<div class="post-metadata">

**Author:** ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)\
**Post date:** [June 20, 2020, 4:34pm UTC](https://swi-prolog.discourse.group/t/why-cant-i-add-chr-constraints-in-declaratives-when-reading-a-file/2544/2 "2020-06-20T16:34:39Z")

</div>

Well, you should follow CHR syntax. Are you aware of [Annie’ tutorial](http://www.pathwayslms.com/swipltuts/chr/index.html) ?  
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

```prolog
foo(a) <=> true.

```

---

<div class="post-metadata">

**Author:** ![meditans](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/meditans/32/1768_2.png) [@meditans](https://swi-prolog.discourse.group/u/meditans)\
**Post date:** [June 20, 2020, 4:39pm UTC](https://swi-prolog.discourse.group/t/why-cant-i-add-chr-constraints-in-declaratives-when-reading-a-file/2544/3 "2020-06-20T16:39:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)\
**Post date:** [June 20, 2020, 4:43pm UTC](https://swi-prolog.discourse.group/t/why-cant-i-add-chr-constraints-in-declaratives-when-reading-a-file/2544/4 "2020-06-20T16:43:41Z")

</div>

yes, my example is wrong… should be simply

```prolog
foo(a).

```

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

---

<div class="post-metadata">

**Author:** ![meditans](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/meditans/32/1768_2.png) [@meditans](https://swi-prolog.discourse.group/u/meditans)\
**Post date:** [June 20, 2020, 4:53pm UTC](https://swi-prolog.discourse.group/t/why-cant-i-add-chr-constraints-in-declaratives-when-reading-a-file/2544/5 "2020-06-20T16:53:27Z")

</div>

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

```prolog
:- 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! 🙂
