Search code examples
matrixprologclpfd

How to set up a 12 x 12 matrix using clpfd library


I am new to constraint logic programming and wanted to know, how I can use clpfd to set up a 12 x 12 matrix in prolog. I am using the swi prolog ide.


Solution

  • To allocate a matrix of variables, we can do in plain Prolog:

    matrix(N,Rows) :- bagof(R,Y^(between(1,N,Y),length(R,N)),Rows).
    

    Then, to constraint each 'cell' to take values from a domain (let say 1..3), using library(yall):

    :- use_module(library(clpfd)).
    ?- matrix(12, Mat), maplist([R]>>(R ins 1..3), Mat).
    

    or, with builtins:

    ?- matrix(12, Mat), bagof(t, R^(member(R, Mat), R ins 1..3), _).