Prolog约束逻辑编程-检查列表元素是否不同(list of lists)

Prolog constraint logic programming - Check if elements of list are different (list of lists)

我坚持这个练习有一段时间了。我想要完成的是:

给定一个列表列表,检查所有元素是否不同。例如:

Ex1 -

L=[[1,2,3],[3,2,1],[2,1,3]], check_diff(L).
%must return true

Ex2 -

L=[[1,2,3],[2,3,1],[2,1,3]], check_diff(L).
%must return true

Ex3 -

L=[[1,2,3],[1,2,3],[3,1,2]], check_diff(L).
%must return false

我的目标是,在知道这个问题的解决方案后,将其应用于Prolog约束逻辑编程,以限制任何列表L(列表的列表)的元素相同的可能性。

换句话说: 在 Sicstus Prolog 上应用 all_different 谓词,但这次是针对列表的列表。

这里是使用布尔变量的解决方案(我使用 ECLiPSe prolog 编写的,应该不难将其翻译成另一个 prolog...)

:-lib(fd).

allListsDifferent2([],[],[]).
allListsDifferent2([H1|T1],[H2|T2],[HB|TB]):-
    H1 #= H2 #<=> HB,
    allListsDifferent2(T1,T2,TB).

allListsDifferent1(_,[]).
allListsDifferent1(HA,[HB|T]):-
    length(HA,N),
    length(LB,N),
    LB::0..1,
    fd_global:sumlist(LB,S),
    S #< N,
    allListsDifferent2(HA,HB,LB),
    allListsDifferent1(HA,T).

allListsDifferent([_]).
allListsDifferent([HA,HB|T]):-
    allListsDifferent1(HA,[HB|T]),
    allListsDifferent([HB|T]).

?- allListsDifferent([[1, 2, 3], [3, 2, 1], [2, 1, 3]]).
Yes (0.02s cpu, solution 1, maybe more)
No (0.03s cpu)
?- allListsDifferent([[1, 2, 3], [3, 2, 1], [3, 2, 1]]).
No (0.00s cpu)