Table of Contents
Fetching ...

Effect Handlers for Programmable Inference

Minh Nguyen, Roly Perera, Meng Wang, Steven Ramsay

TL;DR

This paper addresses the challenge of programmable inference for probabilistic programs by proposing algebraic effects and effect handlers as a typed, modular foundation. It introduces two abstract inference patterns—Metropolis-Hastings and Particle Filter—implemented as combinable components in a Haskell library, enabling pattern instantiation such as Independence MH, Single-Site MH, Multinomial PF, Resample-Move PF, and Particle MH. The approach separates model semantics from inference semantics via effect signatures and handlers, providing reusable, extensible building blocks for designing new variants and combining algorithms without reimplementing core logic. Empirical results indicate competitive performance relative to state-of-the-art systems like MonadBayes and Gen, demonstrating the practicality and scalability of modular algebraic-effect-based programmable inference; the work also discusses advantages over dynamically typed systems and outlines avenues for future work, including automatic differentiation integration and formal semantic properties, while exploring unembedding techniques for broader applicability.

Abstract

Inference algorithms for probabilistic programming are complex imperative programs with many moving parts. Efficient inference often requires customising an algorithm to a particular probabilistic model or problem, sometimes called inference programming. Most inference frameworks are implemented in languages that lack a disciplined approach to side effects, which can result in monolithic implementations where the structure of the algorithms is obscured and inference programming is hard. Functional programming with typed effects offers a more structured and modular foundation for programmable inference, with monad transformers being the primary structuring mechanism explored to date. This paper presents an alternative approach to inference programming based on algebraic effects. Using effect signatures to specify the key operations of the algorithms, and effect handlers to modularly interpret those operations for specific variants, we develop two abstract algorithms, or inference patterns, representing two important classes of inference: Metropolis-Hastings and particle filtering. We show how our approach reveals the algorithms' high-level structure, and makes it easy to tailor and recombine their parts into new variants. We implement the two inference patterns as a Haskell library, and discuss the pros and cons of algebraic effects vis-a-vis monad transformers as a structuring mechanism for modular imperative algorithm design.

Effect Handlers for Programmable Inference

TL;DR

This paper addresses the challenge of programmable inference for probabilistic programs by proposing algebraic effects and effect handlers as a typed, modular foundation. It introduces two abstract inference patterns—Metropolis-Hastings and Particle Filter—implemented as combinable components in a Haskell library, enabling pattern instantiation such as Independence MH, Single-Site MH, Multinomial PF, Resample-Move PF, and Particle MH. The approach separates model semantics from inference semantics via effect signatures and handlers, providing reusable, extensible building blocks for designing new variants and combining algorithms without reimplementing core logic. Empirical results indicate competitive performance relative to state-of-the-art systems like MonadBayes and Gen, demonstrating the practicality and scalability of modular algebraic-effect-based programmable inference; the work also discusses advantages over dynamically typed systems and outlines avenues for future work, including automatic differentiation integration and formal semantic properties, while exploring unembedding techniques for broader applicability.

Abstract

Inference algorithms for probabilistic programming are complex imperative programs with many moving parts. Efficient inference often requires customising an algorithm to a particular probabilistic model or problem, sometimes called inference programming. Most inference frameworks are implemented in languages that lack a disciplined approach to side effects, which can result in monolithic implementations where the structure of the algorithms is obscured and inference programming is hard. Functional programming with typed effects offers a more structured and modular foundation for programmable inference, with monad transformers being the primary structuring mechanism explored to date. This paper presents an alternative approach to inference programming based on algebraic effects. Using effect signatures to specify the key operations of the algorithms, and effect handlers to modularly interpret those operations for specific variants, we develop two abstract algorithms, or inference patterns, representing two important classes of inference: Metropolis-Hastings and particle filtering. We show how our approach reveals the algorithms' high-level structure, and makes it easy to tailor and recombine their parts into new variants. We implement the two inference patterns as a Haskell library, and discuss the pros and cons of algebraic effects vis-a-vis monad transformers as a structuring mechanism for modular imperative algorithm design.
Paper Structure (20 sections, 1 equation, 17 figures)

This paper contains 20 sections, 1 equation, 17 figures.

Figures (17)

  • Figure 1: Inference patterns presented in this paper
  • Figure 2: Extensible freer monad embedding
  • Figure 3: Effect handlers and handle/handleWith helpers
  • Figure 4: Models as computations that sample and observe
  • Figure 5: Effect handlers for model simulation
  • ...and 12 more figures