A model of stochastic memoization and name generation in probabilistic programming: categorical semantics via monads on presheaf categories

Younesse Kaddar, Sam Staton · DOI

TL;DR

Stochastic memoization is a powerful feature in probabilistic programming that caches random choices, but its mathematical foundations have long been shaky because standard implementations break the ability to reorder independent program lines. This paper provides the first formal mathematical model (categorical semantics) that successfully reconciles stochastic memoization over infinite domains with this "dataflow property." By using a novel probability monad on a presheaf category, the authors bridge the gap between practical probabilistic programming and rigorous probability theory.

What Problem Does This Paper Address?

In probabilistic programming languages (PPLs), developers write code to express complex statistical models. A key technique in Bayesian nonparametrics (models where the number of parameters can grow dynamically, such as infinite clustering models) is stochastic memoization.

While standard deterministic memoization caches the result of a function to save compute time, stochastic memoization caches a random choice. The first time a stochastic function is called with a specific argument, it samples a value from a distribution and remembers it; subsequent calls with that same argument simply return that cached sample.

The challenge is that a naive implementation relies on mutable state (a memo-table), which is inherently non-commutative. In practice, this means that changing the order of independent lines in a program could alter its underlying probability distribution. This violates the dataflow property—the intuitive rule that you can reorder or discard program lines as long as you don't break the graph of data dependencies. Without the dataflow property, compiler optimizations and formal program reasoning become incredibly difficult. Furthermore, traditional measure theory struggles to model stochastic memoization over continuous, uncountable domains (like the real numbers) while preserving standard function spaces.

Methods

To solve this semantic puzzle, the authors introduce a minimal toy probabilistic programming language that supports name generation, basic probabilistic effects (like coin flips), and a stochastic memoization operator ($\lambda_{\mathbb{M}}$).

The syntax and typing rules of this language are structured to separate pure values from monadic computations:

Grammar and typing rules of the language

The authors analyze this language through two lenses:

  1. Operational Semantics: A small-step execution model where the memo-table is partial and populated lazily (on-demand), mirroring how PPLs work in the real world. This is represented using partial bipartite graphs (bigraphs).
  2. Denotational Semantics: A mathematical model built on a presheaf category ([BiGrphemb, Set]), where the "possible worlds" are finite, fully populated memo-tables (total bigraphs).

The core innovation is a novel probabilistic local state monad ($T$) constructed using a category-theoretic tool called a coend. This monad tracks a "state of biases" (the probabilities of functions returning true on fresh inputs). The coend mathematically "glues together" different extensions of the memo-table and handles garbage collection of unused nodes, allowing the authors to prove that the monad is both strong commutative and affine.

Datasets

Not discussed in the paper. As a purely theoretical paper focused on programming language semantics and category theory, no empirical datasets or machine learning benchmarks were used.

Key Insights and Ideas

  • Side-stepping Measure Theory via Name Generation: Instead of wrestling with the complex measure-theoretic paradoxes of continuous real numbers, the authors realized that a diffuse probability distribution (where the chance of drawing the exact same value twice is zero) satisfies the exact same algebraic equations as nominal name generation (creating fresh, unique identifiers). By treating continuous samples as pure names, they avoided traditional measure-theoretic roadblocks.
  • The Lazy vs. Total Paradox: In a real program, random choices are made lazily as arguments appear. In the denotational math model, the table is treated as totally populated ahead of time. The "aha moment" of the paper is proving that these two perspectives are completely equivalent because the coend quotient automatically discards choices that the program never actually looks at.

Key Results

  • Validation of the Dataflow Property (Theorem 5.5): The authors formally proved that their novel monad $T$ is commutative and affine, mathematically guaranteeing that independent lines of code can be safely reordered or discarded.
  • Soundness (Theorem 5.10): They proved a soundness theorem establishing a rigorous bridge between the abstract denotational math model and the step-by-step operational execution of the language for a class of functions called freshness-invariant functions (which includes constant Bernoulli functions).
  • Haskell Implementation: The authors created a practical implementation of their framework in Haskell, validating their theoretical soundness results using property-based testing via QuickCheck.

Limitations and Future Work

The primary limitation of this work is its scope: the model is currently restricted to a "minimal working example." Specifically, it only supports the memoization of constant Bernoulli functions (functions that return booleans based on a coin flip) over a type of atomic names.

The authors note that the "grand challenge" in this sub-field is to generalize these insights to allow the stochastic memoization of arbitrary functions ($A \to B$) for general types $A$ and $B$, and for all definable expressions in a full-featured PPL.

Summary updated

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.
BibTeX
@misc{kaddar2023model,
  author = {Younesse Kaddar and Sam Staton},
  title = {{A model of stochastic memoization and name generation in probabilistic programming: categorical semantics via monads on presheaf categories}},
  year = {2023},
  eprint = {2309.09467},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2309.09467},
}
Figure