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

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
:- 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

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
:- 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 \/?

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.

?- 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.
1 Like