There’s a whole subfield called generalized distributive law https://en.wikipedia.org/wiki/Generalized_distributive_law