generalized message passing implementation (to do SumProduct etc. with it)