-module(usableYunz).
-export([usable/2,compupred/3,getAndSendResult/3,garbagecollect/1]).




usable(Yunz,P) -> spawn(usableYunz,solver,[Yunz,P,self()]),
                  receive
                       X -> X
                  end.




solver([yor],P,PIDfather) -> PIDfather!false.

solver([frunz,N,M,Y1,Y2],P,PIDfather) -> 
                                      spawn(usableYunz,compupred,[P,N,self()]),
                                      spawn(usableYunz,compupred,[P,N,self()]),
                                      spawn(usableYunz,solver,[Y1,P]),
                                      spawn(usableYunz,solver,[Y2,P]),
                                      getAndSendResult(4,PIDfather).
                                      

compupred(P,N,PIDfather) ->  if 
                                 P(N) -> PIDfather!true;
                                 true -> PIDfather!false
                             end.


                           
getAndSendResult(0,PIDfather) -> PIDfather!false.      

getAndSendResult(N,PIDfather) -> receive
                                      true  -> PIDfather!true, garbagecollect(N-1),
                                      false -> getAndSendResult(N-1,PIDfather)
                                 end.

garbagecollect(0) -> true.

garbagecollect(N) -> receive
                          Any -> garbagecollect(N-1)
                     end.
