# Use constraints to build set union of finite sets of positive natural numbers

**URL:** <https://swi-prolog.discourse.group/t/use-constraints-to-build-set-union-of-finite-sets-of-positive-natural-numbers/6300>\
**Category:** Data Structure\
**Created:** [February 24, 2023, 3:50am UTC](https://swi-prolog.discourse.group/t/use-constraints-to-build-set-union-of-finite-sets-of-positive-natural-numbers/6300 "2023-02-24T03:50:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Losbarthos](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/losbarthos/32/4037_2.png) [@Losbarthos](https://swi-prolog.discourse.group/u/Losbarthos)\
**Post date:** [February 24, 2023, 3:50am UTC](https://swi-prolog.discourse.group/t/use-constraints-to-build-set-union-of-finite-sets-of-positive-natural-numbers/6300/1 "2023-02-24T03:50:01Z")

</div>

I want to use constraints to make some more general set union of sets of positive natural numbers. To realize this I just tried to write following code:

```prolog
%prolog
:- use_module(library(clpfd)).

union_constraint(Set1, Set2, Union) :-
   Set1 ins 1..sup,
   Set2 ins 1..sup,
   findall(X, (X ins Set1 #/\ X ins Set2, indomain(X)), Union).

```

The simple produces:\>

> ?- union\_constraint([1,2,3],[3,4],C).  
> ERROR: Domain error: `clpfd_reifiable_expression' expected, found `\_62678 ins [1,2,3]’  
> ERROR: In:  
> ERROR: [21] throw(error(domain\_error(clpfd\_reifiable\_expression,…),\_62748))  
> ERROR: [17] clpfd:(\_62790 ins [1|…]#/\_62802 ins [3|…]) at /usr/lib/swi-prolog/library/clp/clpfd.pl:3296  
> ERROR: [16] ‘’(user:(…,…))   
> ERROR: [15] findall\_loop(\_62880,user:(…,…),\_62884,) at /usr/lib/swi-prolog/boot/bags.pl:99  
> ERROR: [14] setup\_call\_catcher\_cleanup(‘$bags’:‘$new\_findall\_bag’,‘$bags’:findall\_loop(\_62944,…,\_62948,),\_62926,‘$bags’:‘$destroy\_findall\_bag’) at /usr/lib/swi-prolog/boot/init.pl:679  
> ERROR: [9] toplevel\_call(user:user: …) at /usr/lib/swi-prolog/boot/toplevel.pl:1173

What is my error of understanding?

On page:  
[https://www.complang.tuwien.ac.at/sicstus/sicstus\_32.html](https://www.complang.tuwien.ac.at/sicstus/sicstus_32.html)

I just read, that set union could simply be introduced by operator `\/` in the chapter Expressions.

But for some reason I cant use this operator in my standard clpfd library. So the predicate:

```prolog
%prolog
:- use_module(library(clpfd)).
union_constraint2(Set1, Set2, Union) :-
   Set1 ins 1..sup,
   Set2 ins 1..sup,
   Union ins Set1 \/ Set2.

```

leads to:

> ?- union\_constraint2([1,2,3],[3,4],C).  
> ERROR: Arguments are not sufficiently instantiated  
> ERROR: In:  
> ERROR: [17] throw(error(instantiation\_error,\_62280))  
> ERROR: [11] clpfd:(\_62310 ins [1|…]/[3|…]) at /usr/lib/swi-prolog/library/clp/clpfd.pl:1672  
> ERROR: [9] toplevel\_call(user:user: …) at /usr/lib/swi-prolog/boot/toplevel.pl:1173

Is their a way to solve this errors and why I cant use `\/`?

---

<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:** [February 24, 2023, 12:06pm UTC](https://swi-prolog.discourse.group/t/use-constraints-to-build-set-union-of-finite-sets-of-positive-natural-numbers/6300/2 "2023-02-24T12:06:52Z")

</div>

The ins/2 constraint doesn’t apply to a list variable, it applies the constraint to members of that list. Also, you are confusing domains with the variable they represent, you can use fd\_dom/2 to query a variables domain .

Without knowing exactly what you are trying to do, here is some sample code that may provide insight.

```prolog
?- X in 1..5, Y in 3..8, Z = [X,Y], Z = [H|T], fd_dom(H,FirstDom), foldl([I,C,O]>>(fd_dom(I,Range),O = C\/Range),T,FirstDom,Union),K in Union.
X = H,
Z = [H, Y],
T = [Y],
FirstDom = 1..5,
Union = 1..5\/3..8,
H in 1..5,
Y in 3..8,
K in 1..8.

```
