t(0).
t(1).

affect(0,[]).
affect(N,[X|Y]) :- N > 0, N1 is N-1,t(X),affect(N1,Y).

eval1(_,[]).
eval1(T,[X|Y]):- eval(T,X), !, eval1(T,Y).

eval(T,[X|Y]) :- X > 0, pos(X,T,R), R = 1.
eval(T,[X|Y]) :- X < 0, X1 is (-1)*X,pos(X1,T,R), R = 0.
eval(T,[_|Y]) :- eval(T,Y).

pos(X,T,R) :- pos1(1,X,T,R).

pos1(X,X,[Z|_],Z).
pos1(A,X,[Z|Y],R) :- A \== X, A1 is A + 1, pos1(A1,X,Y,R).

sat(N,[],[]).
sat(N,L,T) :-
      affect(N,T),
      eval1(T,L).
     
