# Set Difference of Two Compatible Lists in Prolog

I'm currently trying to implement an automated theorem prover in prolog and have stumbled across a problem.

If I have a list of lists such as: [[1,2],[-1,3],[4,5,7],[-2,4]]

How would I get the "set difference" of two compatible list items: What I mean by compatible is, if the negation of a certain number exists in another list, then replace those two lists with the set difference, ie: [1,2] and [-1,3] are compatible because -1 is present in the second clause and thus it should return the set difference of [2,3] and the new list should be [[2,3],[4,5,7],[-2,4]].

Currently I have this following step:

``````memberlist(X,[[X|_]|_]).
memberlist(X,[[_|T1]|T2]) :-
memberlist(X,[T1|T2]).
memberlist(X,[[]|T2]) :-
memberlist(X,T2).

step([]).
step([_|T]) :-
memberlist(neg X,T),
write(X),
nl,
step(T).
step([_|T]) :-
step(T).
``````

So it simply checks each list and checks if the negation of a variable exists and if it does simply write it out. I've already added code which deals with negative number - so neg X will match to -X (x being any integer).

I'm quite stuck at this point, and any help would be greatly appreciated.