Abstract
Stochastic memoization is a higher-order construct of probabilistic programming languages that is key in Bayesian nonparametrics, a modular approach that allows us to extend models beyond their parametric limitations and compose them in an elegant and principled manner. Stochastic memoization is simple and useful in practice, but semantically elusive, particularly regarding dataflow transformations. As the naive implementation resorts to the state monad, which is not commutative, it is not clear if stochastic memoization preserves the dataflow property -- i.e., whether we can reorder the lines of a program without changing its semantics, provided the dataflow graph is preserved. In this paper, we give an operational and categorical semantics to stochastic memoization and name generation in the context of a minimal probabilistic programming language, for a restricted class of functions. Our contribution is a first model of stochastic memoization of constant Bernoulli functions with a non-enumerable type, which validates data flow transformations, bridging the gap between traditional probability theory and higher-order probability models. Our model uses a presheaf category and a novel probability monad on it.
The Problem: Consistency in Random Functions
In standard programming, memoization is a performance optimization: caching function results to avoid redundant computation. In probabilistic programming, however, memoization is a semantic necessity. Consider a function f(x) that represents a random property of an individual x. In a Bayesian nonparametric model, calling f(Alice) twice must return the same result to maintain logical consistency, even though the result was originally sampled at random. This is stochastic memoization.
The challenge arises when the input domain is non-enumerable (e.g., real numbers). Traditional measure theory struggles to define a "white noise" distribution that supports independent random choices for every real number while remaining compatible with higher-order functions. Furthermore, naive implementations using state monads are typically non-commutative, meaning reordering lines of code (the dataflow property) could change program semantics, complicating formal reasoning and optimization.
Methods: Operational and Categorical Semantics
The authors address these challenges through a dual approach:
- Operational Semantics: They define a toy probabilistic language with small-step reduction rules. The "memo-table" is represented as a partial bigraph, where nodes represent function and atom labels, and edges track the results of previous calls.
- Denotational (Categorical) Semantics: Moving beyond measure theory, the authors utilize Presheaf Categories. They define a category of Total Bigraphs as "possible worlds" and introduce a novel Probabilistic Local State Monad ($T$). This monad uses a coend construct to glue together different possible extensions of the memo-table.
Key Insights
- Names as Probability: The paper observes that name generation (creating unique, fresh IDs) satisfies the same logical equations as sampling from a diffuse probability distribution. By treating random samples as "pure names," the authors sidestep measure-theoretic paradoxes associated with real numbers.
- The Dataflow Property: The authors prove their model is strong commutative and affine. This confirms that stochastic memoization preserves the ability to reorder independent probabilistic computations without altering results.
- Freshness Invariance: The model identifies "freshness-invariant" functions—those whose behavior is independent of name generation elsewhere—as the safe domain for this semantic framework.
Key Results and Soundness
- Theorem 5.5 (Dataflow): Formally proves that the probabilistic local state monad satisfies the dataflow property, bridging Bayesian modeling with categorical semantics.
- Theorem 5.10 (Soundness): Establishes that the abstract denotational semantics perfectly match the operational execution of the language.
- Implementation: The theory is validated via a Haskell implementation (using the
LazyPPLlibrary) that manages memoization bigraphs.
Limitations and Future Work
The current model is restricted to constant Bernoulli functions (memoized coin flips with fixed probabilities). The authors identify the "Grand Challenge" as extending this to all definable expressions and complex distributions. Future work may also integrate "synthetic measure theory" using quasi-Borel spaces to handle more complex mathematical manipulations of real numbers.
Summary updated