Search code examples
prologclpfdmeta-predicate

How to set domain of pair variable in CSP in Sicstus Prolog


I'm using Sicstus Prolog, and I'm trying to solve the Domino Puzzle.

I have a list with triplets, which I need to set the domain of. I know how to set the domain of a single variable, or a list of single variables, but how can I do it if my list has triplets, and each of the elements of the triples has a different domain?

DV = [R1-C1-D1, R2-C2-D2, ... , R15-C15-D15]

What I want to do is this:

domain(R1, 1, 4)
domain(C1, 1, 4)
domain(D1, 0, 1)
domain(R2, 1, 4)
domain(C2, 1, 4)
domain(D2, 0, 1)
....
domain(R15, 1, 4)
domain(C15, 1, 4)
domain(D15, 0, 1)

How can I do this without having to explicitly state each of the variables?


Solution

  • Describe the domains for one element:

    eldoms(R-C-D) :-
       domain(R, 1, 4),
       domain(C, 1, 4),
       domain(D, 0, 1).
    

    And:

    ..., maplist(eldoms, RCDs), ...
    

    using library(maplist).

    Alternatively, using library(lambda):

    ..., maplist(
            \ (R-C-D) ^ (  domain(R, 1, 4),
                           domain(C, 1, 4),
                           domain(D, 0, 1) ), RCDs), ...