I need to create a predicate:
applyConstraints(L)
That applies constraints to the variables in L such that no two contiguous elements in L are both odd or even how can I do that? With a fixed size L it's simple but what about a variable size L? I need that to be done using sicstus-prolog clpfd library.
Just consider pair of elements
test (using SWI-Prolog library(clpfd), maybe you will need to substitute ins/2 with the counterpart from Sicstus)
Inspired by @MatsCarlsson's version, I tried to minimize the number of constrained variables involved:
Edit: This version has one flaw for the goal
applyConstraints([])
which is not readily visible. In fact, one needs to switch tofull_answer
mode in SICStus like so:So we have this useless constraint hanging around which might eat up resources. To overcome this deficiency, some special casing is needed:
Note 1 — in
SWI orYAP there is no direct way to switch full answer mode on. The only way to get hold of the problem is to wrap the query aroundcall_residue_vars/2
like so:Note 2 — Thanks to @mat, there is similar functionality since SWI 7.3 (remember that SWI 7 needs
--traditional
for compatibility):(It is not quite clear what "detached" should mean in this context, after all, the residual goals have to be true, to make the answer true. So there is no detachement involved.)