How to add constraints to elements of a list in other predicates?

Viewed 79

I'm trying to do a Sudoku solver in SICStus Prolog using the clpfd library, but it should be similar to other implementations like SWI-Prolog. The main predicate is something like:

solve(Board) :-
    length(Board, 81),
    domain(Board, 1, 9),
    restrict_board(Board),
    labeling([], Board).

The return of solve is all possible combinations, the first answer is [1, 1, 1, ..., 1], if I ask for another is [1, 1, 1, ..., 2] and so on.

So my problem is in the restriction, now my code looks like this (the predicate get_cells is working and returns a list with the positions of the cells of that row, column or square):

restrict(Name, Board) :-
    restrict(Name, Board, 9).
restrict(_, _, 0) :- !.
restrict(Name, Board, Actual) :-
    get_cells(Name, Actual, Cells),
    get_board_cells(Board, Cells, Elements),
    all_different(Elements),
    New is Actual - 1,
    restrict(Name, Board, New).

restrict_board(Board) :-
    restrict(row, Board),
    restrict(col, Board),
    restrict(square, Board).

I think the problem lies in the get_board_cells predicate

get_board_cells(Board, List, Cells) :-
    findall(Cell, (
        member(Pos, List),
        nth0(Pos, Board, Cell)
    ), Cells).

because the cells are copies of the original that does not have a single element instanciated, but prolog seems to not restrict the original board, only the copies of the elements, how could I get the original Board elements to be restricted?

0 Answers
Related