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.
翻译:概率编程的推理算法是复杂的命令式程序,包含众多动态组成部分。高效推理通常需要针对特定概率模型或问题定制算法,这有时被称为推理编程。大多数推理框架采用缺乏对副作用进行规范处理的编程语言实现,导致算法结构模糊、推理编程困难且实现过于庞大。基于类型化效应的函数式编程为可编程推理提供了更结构化、模块化的基础,其中单子变换器是迄今为止被主要探索的构造机制。本文提出了一种基于代数效应的替代性推理编程方法。通过利用效果签名来指定算法的关键操作,并借助效果处理器对特定变体的操作进行模块化解释,我们开发了两种抽象算法(即推理模式),分别代表两类重要的推理方法:梅特罗波利斯-黑斯廷斯算法和粒子滤波。我们展示了该方法如何揭示算法的高级结构,并使得轻松定制和重组其组件以生成新变体成为可能。我们以Haskell库实现了这两种推理模式,并讨论了代数效应与单子变换器作为模块化命令式算法设计构造机制的优劣比较。