Search code examples
prologswi-prologclpfddeclarative-programming

Not sufficiently instantiated for maplist(all_distinct, list)


I'm not able to run this code, what exactly do I have to say about the list to allow maplist/2 to run all_distinct/1?

Solution = [A, B, C, D, E, F, G, H, I], 
Solution ins 1..9, 
maplist(all_distinct, Solution).

I get ERROR: Arguments are not sufficiently instantiated. I understand that I'm not telling it enough about the list of numbers, but I don't know what I need to tell it. I want a list of 9 different numbers 1 through 9.

Here's a trace when I try to execute:

   Call: (7) puzzle(_G548) ? creep
   Call: (8) _G548=[_G656, _G659, _G662, _G665, _G668, _G671, _G674, _G677|...] ? creep
   Exit: (8) [_G656, _G659, _G662, _G665, _G668, _G671, _G674, _G677|...]=[_G656, _G659, _G662, _G665, _G668, _G671, _G674, _G677|...] ? creep
   Call: (8) clpfd: ([_G656, _G659, _G662, _G665, _G668, _G671, _G674|...]ins 1..9) ? creep
   Call: (9) error:must_be(list, [_G656, _G659, _G662, _G665, _G668, _G671, _G674|...]) ? creep
   Exit: (9) error:must_be(list, [_G656, _G659, _G662, _G665, _G668, _G671, _G674|...]) ? creep
   Call: (9) clpfd:'__aux_maplist/2_fd_variable+0'([_G656, _G659, _G662, _G665, _G668, _G671, _G674|...]) ? creep
   Call: (10) clpfd:fd_variable(_G656) ? creep
   Call: (11) var(_G656) ? creep
   Exit: (11) var(_G656) ? creep
   Call: (11) true ? creep
   Exit: (11) true ? creep
   Exit: (10) clpfd:fd_variable(_G656) ? creep
   Call: (10) clpfd:'__aux_maplist/2_fd_variable+0'([_G659, _G662, _G665, _G668, _G671, _G674, _G677|...]) ? creep

It looks like ins/2 might be not working and then still passing off to maplist/2? I've got no idea what is happening.


Solution

  • What you are doing is that you are making a list of variables, Solutions, and then Solutions ins 1..9 makes each variable an integer between 1 and 9.

    all_distinct/1 expects a list, not an integer.

    So, if you want a list of 9 distinct integers:

    ?- Solutions = [A,B,C,D,E,F,G,H,I],
       Solutions ins 1..9,
       all_distinct(Solutions).
    L = [A, B, C, D, E, F, G, H, I],
    A in 1..9,
    all_distinct([A, B, C, D, E, F, G, H|...]),
    B in 1..9,
    C in 1..9,
    D in 1..9,
    E in 1..9,
    F in 1..9,
    G in 1..9,
    H in 1..9,
    I in 1..9.