List_to_fdset/2

Hi,

I wanted to naively translate a list of integers into a finite domain set but I got this error which I am unable to interpret.

L = [1,3,5], list_to_fdset(L, S), X in S.
Domain error: `clpfd_domain' expected, found `split(2,from_to(n(1),n(1)),split(4,from_to(n(3),n(3)),from_to(n(5),n(5))))'
In:
   [4] throw(error(domain_error(clpfd_domain,...),_1868))
   [2] clpfd:drep_to_domain(split(2,from_to(...,...),split(4,...,...)),_1922) at /usr/lib/swipl/library/clp/clpfd.pl:3704
   [1] clpfd:clpfd_in(_1988,split(2,from_to(...,...),split(4,...,...))) at /usr/lib/swipl/library/clp/clpfd.pl:1657

An fdset is not the same as a domain (it is the internal representation of a domain). The in operator works on domains.

If you want to use the fdset directly you can use in_set instead:


16 ?- L = [1,3,5], list_to_fdset(L, FD), X in_set FD.
L = [1, 3, 5],
FD = split(2, from_to(n(1), n(1)), split(4, from_to(n(3), n(3)), from_to(n(5), n(5)))),
X in 1\/3\/5.

If you want to use in you have to convert the fdset to a domain using fdset_to_range/2:

17 ?- L = [1,3,5], list_to_fdset(L, FD), 
      fdset_to_range(FD,Domain), 
      X in Domain.
L = [1, 3, 5],
FD = split(2, from_to(n(1), n(1)), split(4, from_to(n(3), n(3)), from_to(n(5), n(5)))),
Domain = 1\/3\/5,
X in 1\/3\/5.

Note the warning in the clpfd docs:

Note that the exact term representation of FD sets is unspecified and will vary across CLP(FD)
implementations or even different versions of the same implementation. FD set terms should be
manipulated only using the predicates in this section. The behavior of other operations on FD
set terms is undefined. In particular, you should not construct or deconstruct FD sets by
unification, and you cannot reliably compare FD sets using unification or generic term
equality/comparison predicates.

I didn’t know the difference between fdsets and domains. I got it: fdset are internal representation of domains.
Thank you