Search code examples
prologsudokuclpfd

Restricting Variable Domain without CLPFD Library


I have a list of variables in my program, say A, B, C, D, ..., J, and I need to restrict the domain of each of the variables to the same set of integers, say 1, 2, ... 10. I know of a few different ways to do this, but all of them use at least one method from CLPFD, such as X #> 0, X ins [domain], or fd_domain(args). I haven't been able to find a way of doing this without any built-in libraries (all of which are banned for this project).

I would think that writing in the rules like this would be sufficient:

A > 0, A < 11, B > 0, B < 11, ..., J > 0, J < 11

But apparently the variables are not sufficiently instantiated. I know it's a trivially simple question, but I've been searching on my own for a while and really have no other resources for help. Thanks very much.


Solution

  • There's the between function in SWI-Prolog:

    ?- between(1, 9, A).
    A = 1 ;
    A = 2 ;
    A = 3 ;
    A = 4 ;
    A = 5 ;
    A = 6 ;
    A = 7 ;
    A = 8 ;
    A = 9.
    

    If that's banned as well (it's non-standard), then you use member/2:

    ?- member(A, [1, 2, 3, 4, 5, 6, 7, 8, 9]).
    A = 1 ;
    A = 2 ;
    A = 3 ;
    A = 4 ;
    A = 5 ;
    A = 6 ;
    A = 7 ;
    A = 8 ;
    A = 9.
    

    Without CLP(fd), you'll be trapped in the generate-and-test paradigm, though, and perhaps it's better to find some workaround where you check variable values in an ad hoc way to get decent performance.