%% generacya %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

n(0,0,0,0).
n(0,1,1,0).
n(0,2,2,0).
n(1,0,1,0).
n(1,1,1,0).
n(1,2,0,1). %% tol'ko odi raz v konjunkcii
n(2,0,2,0).
n(2,1,0,1). %% tol'ko odi raz v konjunkcii
n(2,2,2,0).

c([], [], [], 0, 1).
c([X|A], [Y|B], [Z|C], N, K) :- N>0, n(X,Y,Z,I), Ki is K+I, Ki<2, Ni is N-1, c(A,B,C,Ni,Ki).

gen(A, B, C) :- length(A,N), c(A,B,C,N,0).

soch([X|A], K, [X|B]) :- length([X|A],N), N>K, K1 is K-1, soch(A,K1,B).
soch([X|A], K, B) :- length([X|A],N), N>K, soch(A,K,B).
soch(A, K, A) :- length(A,N), K is N.

res(L, C) :- soch(L,2,[A,B]), gen(A,B,C).
list(L, R) :- setof(Res, res(L, Res), R).

%% main %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

otv(L, O) :- list(L,R), union(L,R,U), ( L=U, O=U, ! ; not(L=U), otv(U,O) ).

podrobno([A|L]) :- otv([A|L],R), write('Результат = '),write(R),nl, genzero(A,E),
  (
    member(E,R), write('Тождественно равна единице'),nl ;
    not(member(E,R)), write('Не всегда равна единице'),nl
  ), nl.

genzero([], []).
genzero([_|L], [0|R]) :- genzero(L,R).

%% test %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

test :-
  podrobno([[1,2,0],[1,1,0],[2,2,1]]),
  %% результат = [[1, 2, 0], [1, 1, 0], [2, 2, 1], [1, 0, 0], [0, 2, 1], [1, 0, 1]].

  podrobno([[1,2,0],[1,1,0],[2,0,0]]).
  %% результат = [[1, 2, 0], [1, 1, 0], [2, 0, 0], [1, 0, 0], [0, 2, 0], [0, 1, 0], [0, 0, 0]].

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
