I'm using Prolog's integer constraint libraries (clpfd) and I'm having some difficulty getting it to reliably answer queries using range information. As an example program:
puzzle(X, Y) :-
X in 1..5,
X #>= Y.
The following query can then be answered in constant time: puzzle(2, -10000000000000000000000)
. This query is constant regardless of the size of Y.
I'm trying to get this same behavior, but in a recursive setup (which is closer to the problem I am actually trying to solve). However, clpfd
doesn't seem able to deduce the range information in this case:
recursive(0).
recursive(X) :-
X1 #= X - 1,
recursive(X1).
With this setup, the query recursive(10000000000000)
takes a large amount of time (seemingly searching linearly down to 0).
Is there a way I can get constant time evaluation with the second example?
It seems like clpfd is only smart enough to make this constant time step when the variables span a single predicate. But it seems like there should be some way to hint to prolog about the induction happening in the recursive setup, or is that too much to ask?