p(X,TV0):-
	q(X,_TV1),
	r(X,_TV2),
	s(X,_TV3),
	agr_aver2(_TV2,_TV3,_TV4),
	and_godel(_TV1,_TV4,_TV5),
	and_prod(info(0.9,'RULE1.'),_TV5,TV0).


q(a,info(0.8,'RULE2.')).

r(X,info(0.7,'RULE3.')).

s(X,info(0.5,'RULE4.')).



member(info(X,_)):-number(X),0=<X,X=<1.

bot(info(0,_)).    top(info(1,_)).    

leq(info(X1,_),info(X2,_)):- X1 =< X2.


and_luka(info(X1,X2),info(Y1,Y2),info(Z1,Z2)) :- pri_add(X1,Y1,U1,DatADD),
pri_sub(U1,1,U2,DatSUB), pri_max(U2,0,Z1,DatMAX), pri_app(X2,Y2,Dat1),
pri_app(Dat1,'&LUKA.',Dat2), pri_app(Dat2,DatADD,Dat3), pri_app(Dat3,DatSUB,Dat4),
pri_app(Dat4,DatMAX,Z2).

and_prod(info(X1,X2),info(Y1,Y2),info(Z1,Z2)):-pri_prod(X1,Y1,Z1,DatPROD),
pri_app(X2,Y2,Dat1),pri_app(Dat1,'&PROD.',Dat2),pri_app(Dat2,DatPROD,Z2).

and_godel(info(X1,X2),info(Y1,Y2),info(Z1,Z2)):-pri_min(X1,Y1,Z1,DatMIN),
pri_app(X2,Y2,Dat1),pri_app(Dat1,'&GODEL.',Dat2),pri_app(Dat2,DatMIN,Z2).

agr_aver(info(X1,X2),info(Y1,Y2),info(Z1,Z2)):-pri_add(X1,Y1,Aux,DatADD),
pri_div(Aux,2,Z1,DatDIV),pri_app(X2,Y2,Dat1),pri_app(Dat1,'@AVER.',Dat2),
pri_app(Dat2,DatADD,Dat3),pri_app(Dat3,DatDIV,Z2).

% agr_aver2(X,Y,info(Za,Zb)):-or_godel(X,Y,Z1),or_luka(X,Y,Z2),
% agr_aver(Z1,Z2,info(Za,Zc)), pri_app(Zc,'@AVER2.',Zb).

agr_aver2(info(X1,X2),info(Y1,Y2),info(Z1,Z2)):-or_godel(info(X1,''),info(Y1,''),Za),
or_luka(info(X1,''),info(Y1,''),Zb),agr_aver(Za,Zb,info(Z1,Dat3)),pri_app(X2,Y2,Dat1),
pri_app(Dat1,'@AVER2.',Dat2),pri_app(Dat2,Dat3,Z2).


or_godel(info(X1,X2),info(Y1,Y2),info(Z1,Z2)):-pri_max(X1,Y1,Z1,DatMAX),
pri_app(X2,Y2,Dat1),pri_app(Dat1,'|GODEL.',Dat2),pri_app(Dat2,DatMAX,Z2).

or_luka(info(X1,X2),info(Y1,Y2),info(Z1,Z2)):-pri_add(X1,Y1,U1,DatADD),
pri_min(U1,1,Z1,DatMIN), pri_app(X2,Y2,Dat1),pri_app(Dat1,'|LUKA.',Dat2),
pri_app(Dat2,DatADD,Dat3),pri_app(Dat3,DatMIN,Z2).

pri_add(X,Y,Z, '#ADD.')  :- Z is X+Y.
pri_sub(X,Y,Z, '#SUB.')  :- Z is X-Y.
pri_prod(X,Y,Z,'#PROD.') :- Z is X * Y.
pri_div(X,Y,Z, '#DIV.')  :- Z is X/Y.
pri_min(X,Y,Z, '#MIN.')  :- (X=<Y,Z=X;X>Y,Z=Y).
pri_max(X,Y,Z, '#MAX.')  :- (X=<Y,Z=Y;X>Y,Z=X).
pri_app(X,Y,Z) :- name(X,L1),name(Y,L2),append(L1,L2,L3),name(Z,L3).


append([],X,X).
append([X|Xs],Y,[X|Zs]):-append(Xs,Y,Zs).