# Basic CHR question

**URL:** <https://swi-prolog.discourse.group/t/basic-chr-question/9653>\
**Category:** Help!\
**Created:** [June 8, 2026, 8:59am UTC](https://swi-prolog.discourse.group/t/basic-chr-question/9653 "2026-06-08T08:59:46Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Ian](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ian/32/1026_2.png) [@Ian](https://swi-prolog.discourse.group/u/Ian)\
**Post date:** [June 11, 2026, 12:20pm UTC](https://swi-prolog.discourse.group/t/basic-chr-question/9653/2 "2026-06-11T12:20:16Z")

</div>

I asked claude, and the reason is a subtle bug. It is because we are showing the contents of the store at the end of the query. This inadvertenlty duplicates constraints in the store.

You can test this by adding the following to your code:

```prolog
:- set_prolog_flag(chr_toplevel_show_store,false).

```

and it will work as expected.

I asked claude about a fix and it suggests replacing duplicate\_term with copy\_term in code called by $enumerate\_constraints:

```prolog
 Update(~/git/swipl-devel/packages/chr/chr_swi.pl)
  ⎿ Added 1 line, removed 1 line
      408 State = state(List2),
      409 ( call(Goal),
      410 arg(1, State, L),
      411 - duplicate_term(Templ, New),                                                                                                            
      411 + copy_term(Templ, New, _),                                                                                                              
      412 New = Templ,
      413 Cons = [New|_],
      414 nb_linkarg(2, L, Cons),

```

Claude verified the patch fixes the issue and reran the tests which passed successfully.

This also explains [Tricky CHR behaviour - #3 by AZH](https://swi-prolog.discourse.group/t/tricky-chr-behaviour/9642/3)

---

_[View the full topic](https://swi-prolog.discourse.group/t/basic-chr-question/9653)._
