Search code examples
prologclpfd

Misunderstanding chain/2?


If I have (with clpfd loaded):

test1(Ps):-
    permutation(Ps,[(a,1,1),(b,2,2),(c,3,1),(d,4,2)]), 
    Ps =[(L1,W1,X1),(L2,W2,X2),(L3,W3,Y1),(L4,W4,Y2)], 
    chain([W1,W2,W3,W4],#<).

And query:

?-test1(P).
P = [(a,1,1), (b,2,2), (c,3,1), (d,4,2)]
false

Which gives one answer as I expect, but with:

test2(Ps):-
    permutation(Ps,[(a,1,1),(b,2,2),(c,3,1),(d,4,2)]), 
    Ps =[(L1,W1,X1),(L2,W2,X2),(L3,W3,Y1),(L4,W4,Y2)], 
    chain([X1,X2],#<).

Query:

?-test2(P):-
P = [(a,1,1), (b,2,2), (c,3,1), (d,4,2)]
P = [(a,1,1), (b,2,2), (d,4,2), (c,3,1)]
P = [(a,1,1), (d,4,2), (b,2,2), (c,3,1)]
P = [(a,1,1), (d,4,2), (c,3,1), (b,2,2)]
P = [(c,3,1), (b,2,2), (a,1,1), (d,4,2)]
P = [(c,3,1), (d,4,2), (a,1,1), (b,2,2)]
P = [(c,3,1), (b,2,2), (d,4,2), (a,1,1)]
P = [(c,3,1), (d,4,2), (b,2,2), (a,1,1)]
false

I do not expect answers like:

P = [(c,3,1), (b,2,2), (d,4,2), (a,1,1)]
P = [(c,3,1), (d,4,2), (b,2,2), (a,1,1)]

Where the terms with as are after the terms with bs. What am I misunderstanding?


Solution

  • I can now see what I was doing wrong.

    As the answers:

    P = [(c,3,1), (b,2,2), (d,4,2), (a,1,1)]
    P = [(c,3,1), (d,4,2), (b,2,2), (a,1,1)]
    

    Clearly have 1 and 2 in the third argument of the first two elements and so are meeting the chain constraint.