Prolog有没有办法限制列表中的元素是否在范围内?

时间:2017-10-09 04:33:10

标签: prolog clpfd

是否有任何方法来限制列表中的元素是否在范围内,例如1-6而不是使用between(1,6,X)

2 个答案:

答案 0 :(得分:6)

clpfd库有一个ins/2谓词,您可以在其中指定变量列表 - 元素的范围。

像:

all_between(Low, High, Vars) :-
    Vars ins Low..High.

致电all_between(1,6,[X1,X2,X3]).时,我们得到:

X1 in 1..6,
X2 in 1..6,
X3 in 1..6.

如果我们想要枚举元素,我们也可以使用label(L).来分配值:

all_between_enumerate(Low, High, Vars) :-
    Vars ins Low..High,
    label(Vars).

对于all_between_enumerate(1,2,[X1,X2,X3]).,这会生成:

?- all_between_enumerate(1,2,[X1,X2,X3]).
X1 = X2, X2 = X3, X3 = 1 ;
X1 = X2, X2 = 1,
X3 = 2 ;
X1 = X3, X3 = 1,
X2 = 2 ;
X1 = 1,
X2 = X3, X3 = 2 ;
X1 = 2,
X2 = X3, X3 = 1 ;
X1 = X3, X3 = 2,
X2 = 1 ;
X1 = X2, X2 = 2,
X3 = 1 ;
X1 = X2, X2 = X3, X3 = 2.

如果您已经为变量添加了约束,例如X1 #< X3,那么这些也会被考虑在内:

?- X1 #< X3, all_between_enumerate(1,2,[X1,X2,X3]).
X1 = X2, X2 = 1,
X3 = 2 ;
X1 = 1,
X3 = X2, X2 = 2.

答案 1 :(得分:4)

您可以使用库CLPFD

:- use_module(library(clpfd)).

constraint_list([]).
constraint_list([H|T]):-H in 1..6 ,label([H]),constraint_list(T).

示例:

?- constraint_list([X1,X2]).
X1 = X2, X2 = 2 ;
X1 = 2,
X2 = 3 ;
X1 = 2,
X2 = 4 ;
X1 = 2,
X2 = 5 ;
X1 = 3,
X2 = 2 ;
X1 = X2, X2 = 3 ;
X1 = 3,
X2 = 4 ;
X1 = 3,
X2 = 5 ;
X1 = 4,
X2 = 2 ;
X1 = 4,
X2 = 3 ;
X1 = X2, X2 = 4 ;
X1 = 4,
X2 = 5 ;
X1 = 5,
X2 = 2 ;
X1 = 5,
X2 = 3 ;
X1 = 5,
X2 = 4 ;
X1 = X2, X2 = 5.

?- L=[1,2,3] ,constraint_list(L).
false.

?- L=[2,2,3] ,constraint_list(L).
L = [2, 2, 3].

?- constraint_list(L).
L = [] ;
L = [2] ;
L = [2, 2] ;
L = [2, 2, 2] ;
L = [2, 2, 2, 2] ;
L = [2, 2, 2, 2, 2] ;
L = [2, 2, 2, 2, 2, 2] ;
L = [2, 2, 2, 2, 2, 2, 2] ;
L = [2, 2, 2, 2, 2, 2, 2, 2] ;
L = [2, 2, 2, 2, 2, 2, 2, 2, 2] ;
L = [2, 2, 2, 2, 2, 2, 2, 2, 2|...] ;
L = [2, 2, 2, 2, 2, 2, 2, 2, 2|...] ;
L = [2, 2, 2, 2, 2, 2, 2, 2, 2|...] ;
L = [2, 2, 2, 2, 2, 2, 2, 2, 2|...] ;
L = [2, 2, 2, 2, 2, 2, 2, 2, 2|...] .
    ... (and goes on)