lights_on(Len, W, Sol) :- length(Sol, Len), fd_domain_bool(Sol), make_constraints(0, Len, W, Sol), fd_labeling(Sol). print_sol(W, Sol) :- length(L, W), ( append(L, T, Sol) -> write(L), nl, print_sol(W, T) ; write(Sol), nl ). make_constraints(Ix, Len, W, Sol) :- ( Ix == Len -> true ; make_constraint(Ix, W, Sol), Ix1 is Ix + 1, make_constraints(Ix1, Len, W, Sol)). nth0(Ix, L, Var) :- Ix1 is Ix + 1, nth(Ix1, L, Var). up(Ix, W, Sol, Var) :- Ix1 is Ix - W, ( nth0(Ix1, Sol, Var) -> true ; Var = 0 ). down(Ix, W, Sol, Var) :- Ix1 is Ix + W, ( nth0(Ix1, Sol, Var) -> true ; Var = 0 ). left(Ix, W, Sol, Var) :- Ix1 is Ix - 1, ( Ix1 // W =:= Ix // W, nth0(Ix1, Sol, Var) -> true ; Var = 0). right(Ix, W, Sol, Var) :- Ix1 is Ix + 1, ( Ix1 // W =:= Ix // W, nth0(Ix1, Sol, Var) -> true ; Var = 0). make_constraint(Ix, W, Sol) :- nth0(Ix, Sol, This), up(Ix, W, Sol, Up), down(Ix, W, Sol, Down), right(Ix, W, Sol, Right), left(Ix, W, Sol, Left), This ## Up ## Down ## Right ## Left. test :- W = 14, H = 14, Missing = 7, Len is W * H - Missing, lights_on(Len, W, Sol), print_sol(W, Sol).