Mathematical Foundations for a Compositional Account of the Bayesian Brain