# Running CHR in sandbox

**URL:** <https://swi-prolog.discourse.group/t/running-chr-in-sandbox/6536>\
**Category:** General\
**Created:** [May 9, 2023, 6:28pm UTC](https://swi-prolog.discourse.group/t/running-chr-in-sandbox/6536 "2023-05-09T18:28:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![esad](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/esad/32/884_2.png) [@esad](https://swi-prolog.discourse.group/u/esad)\
**Post date:** [May 9, 2023, 6:28pm UTC](https://swi-prolog.discourse.group/t/running-chr-in-sandbox/6536/1 "2023-05-09T18:28:10Z")

</div>

Hi! Is there a way to run chr in sandbox, or just using a new (empty) constraint store? I am using CHR in a library and the problem is that a second call to the library predicate, reuses same CHR module and constraints get reused.

---

<div class="post-metadata">

**Author:** ![abaljeu](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/abaljeu/32/136_2.png) [@abaljeu](https://swi-prolog.discourse.group/u/abaljeu)\
**Post date:** [May 9, 2023, 7:58pm UTC](https://swi-prolog.discourse.group/t/running-chr-in-sandbox/6536/2 "2023-05-09T19:58:46Z")

</div>

CHR constraints are module specific. So rules you define in your module won’t directly interact with rules defined by a library. But if you want to use the same module twice, that won’t help. (Is there a prolog facility to load a module twice, but into two separate namespaces?)

The other way to separate invocations of a constraint is to add a groupid parameter.

x(Groupid, …)  
y(Groupid, …)

If that’s there then x(3, …), y(3, …) won’t interfere with x(2, …), y(2, …).

But if your library of CHR rules doesn’t provide either of those, I’m afraid you’re stuck.

I notice with the toy examples the writers provide, this grouping isn’t used. They will define for example incremental sort predicate whose only parameters are the things to be sorted. Which is a limitation if you have 2 sets of things to be sorted.

There’s one other option: You can run a separate thread, as each thread has its own store.

---

<div class="post-metadata">

**Author:** ![esad](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/esad/32/884_2.png) [@esad](https://swi-prolog.discourse.group/u/esad)\
**Post date:** [May 9, 2023, 8:43pm UTC](https://swi-prolog.discourse.group/t/running-chr-in-sandbox/6536/3 "2023-05-09T20:43:30Z")

</div>

Thanks for the thread hint, this might do it as a workaround. As a temporary solution, I added a new constraint `cleanup/0` and bunch of simpagation rules with all other constraints to removes them after I computed the answer and bound it to variable.

To clarify my initial question, I am writing a library myself that uses CHR under the hood, but the problem is that if user of the library calls the predicate multiple times, then the constraints are not cleared between runs. I was hoping I can just use something like `chr_destroy_store(my_library_module)` after each run.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [May 10, 2023, 9:11am UTC](https://swi-prolog.discourse.group/t/running-chr-in-sandbox/6536/4 "2023-05-10T09:11:32Z")

</div>

> [@esad](#):
>
> As a temporary solution, I added a new constraint `cleanup/0` and bunch of simpagation rules with all other constraints to removes them after I computed the answer and bound it to variable.

I think that is how it is intended to be used. It shouldn’t be too hard to add a chr\_cleanup/0 though. The constraints are stored in a number of global variables, so you have to find them and delete them using nb\_delete/1. AFAIK they are recreated lazily when needed. If not, instead of deleting set them back to their initial value. Maybe create a PR?

Threads (or engines) are indeed another work-around.

---

<div class="post-metadata">

**Author:** ![abaljeu](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/abaljeu/32/136_2.png) [@abaljeu](https://swi-prolog.discourse.group/u/abaljeu)\
**Post date:** [May 18, 2023, 2:18pm UTC](https://swi-prolog.discourse.group/t/running-chr-in-sandbox/6536/5 "2023-05-18T14:18:10Z")

</div>

If it’s your own library, simply add a parameter for the call id and you can have multiple parallel queries.
