# How are Prolog and CHR supposed to interact?

**URL:** <https://swi-prolog.discourse.group/t/how-are-prolog-and-chr-supposed-to-interact/7027>\
**Category:** General\
**Created:** [December 4, 2023, 3:58pm UTC](https://swi-prolog.discourse.group/t/how-are-prolog-and-chr-supposed-to-interact/7027 "2023-12-04T15:58:33Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![emiruz](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emiruz/32/4495_2.png) [@emiruz](https://swi-prolog.discourse.group/u/emiruz)\
**Post date:** [December 4, 2023, 5:06pm UTC](https://swi-prolog.discourse.group/t/how-are-prolog-and-chr-supposed-to-interact/7027/5 "2023-12-04T17:06:30Z")

</div>

@meditans in your solution here:

> [@Advent of code 2023](https://swi-prolog.discourse.group/t/advent-of-code-2023/7025/6):
>
> Hey @emiruz, I’m doing AoC2023 in prolog too! Caveat: I’m using prolog to explore the ways in which computations could be written, so my solutions are a bit idiosyncratic. That said, here’s my solution for day 3 (without imports unfortunately, as I’m using local packs for utilities): % Parsing dgt(dgt(N)) --\> digit(N). sym(sym(S)) --\> symbol\_chr(S), {S \= '.'}. dot(dot) --\> `.`. row(Xs) --\> list((dgt | sym | dot), Xs), nl. input(Xs) --\> list(row, Xs). position(M, I-J, X) :- nth0(I, M,…

I’m trying to understand how you get things into the constraint store in the first place. It seems to be related to the local\_chr predicate which is defined as follows:

```prolog
find_constraint(Goal, Cs) :-
    findall(Goal, find_chr_constraint(Goal), Cs).

local_chr(Facts, Result, Res) :-
    thread_create((maplist(call, Facts),
                   find_constraint(Result, Ns),
                   thread_exit(Ns)), Id),
    thread_join(Id, exited(Res)).

```

It is invoked here:

```prolog
parse(File, Mat-Ns) :-
    once(phrase_from_file(input(Mat), File)),
    findall(n(I-J, 1, N), position(Mat, I-J, dgt(N)), Constraints),
    local_chr(Constraints, n(_, _, _), Ns).

```

… and that seems to be what activates the constraints and adds to them, but I don’t understand what’s going on here. It looks like local\_chr calls all the facts in a new thread, is that what activates the constraints? It then uses find\_constraint to extract the result back into Ns. Is that right?

---

_[View the full topic](https://swi-prolog.discourse.group/t/how-are-prolog-and-chr-supposed-to-interact/7027)._
