I have written a CSP program using CLP(FD) and SWI-Prolog.
Some constraints look like Xi mod N #= 0, so I have to call res(Xi,Li) (with res(X,L) :- setof(X, indomain(X), L)) for each variable Xi to do backtracking and get the list of effective solutions. If I don't do that, I get residual goals.
Now in the same call I want to convert this list of solutions for Xi into a list of continuous intervals. How can I do that ? Are there any existing predicates to do that ?
I tried using fd_dom(Li,Di), dom_intervals(Di,Ii) with no success as Li needs to be atomic, not a list.
dom_intervals is defined as follows, please see Getting intervals with CLP :
dom_intervals(Dom, Intervals) :-phrase(dom_intervals1(Dom), Intervals).
dom_intervals1(I) --> {integer(I)}, [I].
dom_intervals1(L..U) --> [L..U].
dom_intervals1(D1 \/ D2) --> dom_intervals1(D1), dom_intervals1(D2).
I also tried calling member(Xtmpi, Li) before fd_dom(Xtmpi,Di), dom_intervals(Di,Ii) but member enumerates results so I get a list of singletons.
Thanks in advance for your ideas.
It seems a bit wasteful to generate a full sequence and then compress it back down again, but I didn't find a way to resolve the fd_domain without doing that, so here's a numbers-to-consecutive-ranges grammar.
Usage:
Code: