Abstract
Stan is a probabilistic programming language that has been increasingly used for real-world scalable projects. However, to make practical inference possible, the language sacrifices some of its usability by adopting a block syntax, which lacks compositionality and flexible user-defined functions. Moreover, the semantics of the language has been mainly given in terms of intuition about implementation, and has not been formalised. This paper provides a formal treatment of the Stan language, and introduces the probabilistic programming language SlicStan — a compositional, self-optimising version of Stan. Our main contributions are: (1) the formalisation of a core subset of Stan through an operational density-based semantics; (2) the design and semantics of the Stan-like language SlicStan, which facilities better code reuse and abstraction through its compositional syntax, more flexible functions, and information-flow type system; and (3) a formal, semantic-preserving procedure for translating SlicStan to Stan.
Introduction
Stan has established itself as the "gold standard" for scalable Bayesian inference, primarily due to its efficient implementation of Hamiltonian Monte Carlo (HMC). However, its rigid block-based syntax—requiring separate sections for data, parameters, and model—presents a significant barrier to code reuse and modularity. In standard Stan, related logic is often fragmented across these blocks, making complex models difficult to read, debug, and refactor. SlicStan addresses these limitations by providing a compositional, blockless interface that remains compatible with Stan’s high-performance backend.
Formalizing the Core Language
A primary contribution of this work is the first formal operational density-based semantics for a core subset of Stan. The authors argue that a Stan program is essentially a deterministic imperative program that calculates a "score" (the log-posterior density) at a specific point in the parameter space. This insight allows for the application of standard procedural abstraction techniques to probabilistic programming.
The syntax defined in Figure 1 allows for standard imperative constructs like assignments, sequences, and loops, which are then evaluated to compute the target density.
Operational Semantics
The evaluation of these constructs is formalized through a set of big-step operational semantics. These rules define how expressions and statements transform the state $s$ to produce values $V$ or updated states $s'$.
As shown in Figure 4, the evaluation of a variable depends on its presence in the domain of the state $s$, and array evaluation proceeds recursively over its elements. This formal framework ensures that the behavior of the language is well-defined and predictable, moving beyond the implementation-based definitions of the past.
SlicStan: Compositionality and Information Flow
SlicStan moves away from Stan's block-based constraints by introducing an information-flow type system. This system uses a lattice-based approach (data < model < genquant) to track how information moves through the program:
- Data: Known quantities processed once (pre-processing).
- Model: Unknown parameters and log-density calculations required at every HMC step.
- Genquant: Quantities derived after sampling for post-processing.
The SlicStan compiler uses this information to perform "automatic shredding" — a semantic-preserving procedure that partitions a blockless program into the appropriate Stan blocks. This allows users to write code that follows the "generative story" of their model, keeping parameter declarations, priors, and likelihoods in one place. Furthermore, SlicStan supports first-order functions that can declare their own local parameters, a feature notably absent in standard Stan.
Evaluation and Results
The effectiveness of SlicStan was evaluated using classic benchmarks such as Neal’s Funnel, Eight Schools, and the Cockroaches Poisson regression model. The results demonstrate that SlicStan significantly improves code compactness and locality. For instance, in the Eight Schools model, SlicStan eliminates the need for manual reparameterization across multiple blocks, leading to more maintainable code.
Crucially, the authors provide a formal proof of semantic preservation, ensuring that the translation from SlicStan to Stan does not alter the underlying statistical meaning of the model.
Conclusion and Future Work
SlicStan represents a significant step forward in making probabilistic programming more accessible and modular without sacrificing performance. By leveraging information-flow analysis for optimization, it provides a modern imperative feel to a high-performance PPL. Future work includes extending the type system to handle recursion and more complex control flows, as well as exploring ways to automatically choose the most efficient sampling methods for different model components.
Summary updated