%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Predicati di base per operare su insiemi
% (insiemi rappresentati come liste in cui eventuali
% ripetizioni e l'ordine degli elementi non contano.
% Ad esempio: le liste [1,2,3] e [2,1,3,1] rappresentano
% lo stesso insieme {1,2,3})
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  member(X,[X|_R]).
  member(X,[_Y|R]) :- 
      member(X,R).

  subset([],_).
  subset([X|R],S) :- 
      member(X,S),
      subset(R,S).

  eqset(S1,S2) :-
      subset(S1,S2),
      subset(S2,S1).

  union([],S,S).
  union([X|R1],S2,S3) :-
      member(X,S2),
      union(R1,S2,S3).
  union([X|R1],S2,[X|R3]) :-
      \+ member(X,S2),
      union(R1,S2,R3).

  inters([],_S,[]).
  inters([X|R1],S2,[X|R3]) :-
      member(X,S2),
      inters(R1,S2,R3).
  inters([X|R1],S2,S3) :-
      \+ member(X,S2),
      inters(R1,S2,S3).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Goal d'esempio
%
% ?- eqset([1,2,3],[2,1,3,1]).
% yes
