Abstract
It is natural for probabilistic programs to use conditionals to express alternative substructures in models, and loops (recursion) to express repeated substructures in models. Thus, probabilistic programs with conditionals and recursion motivate ongoing interest in efficient and general inference. A factor graph grammar (FGG) generates a set of factor graphs that do not all need to be enumerated in order to perform inference. We provide a semantics-preserving translation from first-order probabilistic programs with conditionals and recursion to FGGs.
Introduction
Probabilistic programming languages (PPLs) allow for the expression of complex models using high-level constructs like conditionals and recursion. However, standard inference techniques often rely on translating these programs into static graphical models, such as factor graphs. This translation is problematic for models with unbounded recursion (which would require infinite graphs) or complex branching (which often leads to "ghost" variables where all branches are instantiated regardless of the execution path).
This paper introduces a formal, semantics-preserving translation from a first-order recursive PPL to Factor Graph Grammars (FGGs). FGGs extend the concept of context-free grammars to graphs, allowing for the finite representation of infinite sets of factor graphs.
Methodology: Compiling to FGGs
The core contribution is a translation scheme that maps PPL constructs—such as let bindings, if/else conditionals, and recursive function calls—directly into FGG production rules.
An FGG consists of a set of rules that define how nonterminal hyperedges in a graph can be replaced by other subgraphs.
- Conditionals: Branching logic is handled by creating multiple production rules for a single nonterminal. For instance, an
ifstatement results in two rules: one for thetruebranch and one for thefalsebranch. This ensures that the generated graph only includes the active branch, bypassing the inefficiencies of static factor graphs. - Recursion: Function definitions are mapped to nonterminals. A recursive function call becomes a nonterminal edge that refers back to its own definition rule, allowing for unbounded depth without manual unrolling.
Key Insights
The use of FGGs as a compilation target offers several advantages over traditional factor graphs:
- Efficiency in Branching: Unlike prior methods that force the evaluation of all conditional branches, FGGs only generate the active path. This eliminates the need for "ghost" variables and masking factors.
- Handling Unbounded Structures: Because FGGs are grammars, they can represent infinite sets of graphs. Inference algorithms for FGGs, such as variable elimination, operate on the grammar rules themselves. This allows the system to sum over an infinite number of possible execution paths mathematically.
- Automatic Dynamic Programming: The authors demonstrate that compiling a recursive PPL implementation of a Probabilistic Context-Free Grammar (PCFG) into an FGG naturally recovers the $O(n^3)$ complexity of the classic CKY parsing algorithm.
Results and Future Work
As a theoretical work, the paper focuses on the formal proof of semantics preservation. The authors show that the probability distribution defined by the source PPL program is identical to the distribution represented by the generated FGG.
Qualitative results include the successful representation of PCFG string samplers and parsers. While the current implementation serves as a proof-of-concept (generating LaTeX diagrams), it lays the groundwork for a full software pipeline. Future work includes implementing machine-readable FGG outputs and exploring more complex FGG rule types that could support even more expressive programming constructs.
Summary updated