/* Global constraint lex2 in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Clex2.html """ lex2 (MATRIX) Purpose Given a matrix of domain variables, enforces that both adjacent rows, and adjacent columns are lexicographically ordered (adjacent rows and adjacent columns can be equal). Example ( < vec-<2, 2, 3>, vec-<2, 3, 1> > ) The lex2 constraint holds since: * The first row <2, 2, 3> is lexicographically less than or equal to the second row <2, 3, 1>. * The first column <2, 2> is lexicographically less than or equal to the second column <2, 3>. * The second column <2, 3> is lexicographically less than or equal to the third column <3, 1>. """ Note: Picat has built-in lex_lt/2 and lex_le/2 for comparing two lists, but not lex2/1, i.e. for a matrix. Here we implement lex2/1 as lex2eq. In lex_alldifferent.pi is the lex2/1 constraint. This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> R = 2, C = 3, X = new_array(R,C), X :: 1..4, % X = { % {2,2,3}, % {2,3,1} % }, lex2eq(X), solve(X), foreach(Row in X) println(Row) end, nl, fail, nl. go => true. lex2(X) => Len = X[1].length, foreach(I in 2..X.length) lex_lt(X[I-1], X[I]) end. % This use lex_lesseq/1 lex2eq(X) => Len = X[1].length, foreach(I in 2..X.length) lex_le(X[I-1], X[I]) end.