%%% 6.04.2013 %%%

%% Task 1
% is_member(X, L) :- X is element of L
is_member(X, [X|_]).
is_member(X, [_|T]) :- is_member(X, T).

p(L) :- not((
			 	is_member(X, L), is_member(Y, L), X \= Y,
				 	not((
				 	  is_member(E, X),
				 	  is_member(E, Y),
				 	  is_member(Z, L),
				 	  not(is_member(E, Z))
				 		 ))
		  		 )).

%% Task 2
% append_lists(A, B, C)
append_lists([], B, B).
append_lists([X|T], B, [X|S]) :- append_lists(T, B, S).

% out(L, X, M) :- removes an element X from L, M is the rest of L
out(L, X, M) :- append_lists(A, [X|B], L), append_lists(A, B, M).

% major(X, Y) :- X is a subset of Y
major(X, Y) :- not(is_member(A, X), not(is_member(A, Y))).

permutate([], []).
permutate(L, [H|T]) :- out(L, H, M), permutate(M, T).

sorted_list(M) :- not(append_lists(_, [X|B], M), is_member(B, Y), major(Y, X)).

t(L, M) :- permutate(L, M), sorted_list(M).