Here is a short snippet of code, which is an example of a situation I often find myself in, and which does not work as you would expect:
?- L in 1..3, length(Ls, L). L = 1, Ls = [_G2634] ; L = 2, Ls = [_G2634, _G2637] ; L = 3, Ls = [_G2634, _G2637, _G2640] ; <infinite loop>
We can see very easily that length/2
does not go well with constraints. When one would expect that length/2
would check that its second argument is in the right domain before trying to instantiate the list, it seems to do the opposite and check that its second argument respects the constraint after trying to instantiate the list.
How is this problem typically solved? What implementation of say clength/2
could we come up with so that the example above properly terminates?