different/2 - does a pure, determinate definition exist?

后端 未结 8 1031
野的像风
野的像风 2020-12-07 00:24
different(Xs, Ys) :-
   member(X, Xs),
   non_member(X, Ys).
different(Xs, Ys) :-
   member(Y, Ys),
   non_member(Y, Xs).

While this definition usi

8条回答
  •  粉色の甜心
    2020-12-07 01:03

    First try!

    The following code is based on the meta-predicates tfilter/3 and tpartition/4, the monotone if-then-else control construct if_/3, the reified unary logical connective not_t/3, and the reified term equality predicate (=)/3:

    different([],[_|_]).
    different([X|Xs0],Ys0) :-
       tpartition(=(X),Ys0,Es,Ys1),
       if_(Es=[], true, (tfilter(not_t(=(X)),Xs0,Xs1),different(Xs1,Ys1))).
    

    Sample query:

    ?- different([A,B],[X,Y]).
                    A=Y ,           dif(B,Y),     X=Y
    ;     A=X           ,     B=X           , dif(X,Y)
    ;     A=X           , dif(B,X), dif(B,Y), dif(X,Y)
    ;               A=Y ,               B=Y , dif(X,Y)
    ;               A=Y , dif(B,X), dif(B,Y), dif(X,Y)
    ; dif(A,X), dif(A,Y).
    

    Let's observe determinism when working with ground data:

    ?- different([5,4],[1,2]).
    true.
    

    The above approach feels like a step in the right direction... But, as-is, I wouldn't call it perfect.

提交回复
热议问题