Prolog Termination of Generated List

92 Views Asked by At
N #>= 0, N #< 3, length(Ls, N), false.

The expression above does not terminate when posted on the swi prolog terminal.

I have tried exchanging the order of goals.

length(Ls, N), N #>= 0, N #< 3,  false.

and

length(Ls, N), N >= 0, N < 3, false.

I am using SWI-Prolog version 8.4.3 for x86_64-linux

2

There are 2 best solutions below

3
brebs On

N is still var at that point. Use instead (if you really want to use clpfd - I'm not seeing a reason):

?- N #>= 0, N #< 3, label([N]), length(Ls, N), false.
false.
1
TessellatingHeckler On

length(Ls, N), N >= 0, N < 3, false.

This will count up N trying different values until all the conditions are true, but false is never true.

So this is "count until false is true", which is "count forever", an infinite loop.