A Direct-Style Effect Notation for Sequential and Parallel Programs
David Richter, Timon Böhler, Pascal Weisenburger, Mira Mezini
TL;DR
This work introduces a direct-style mixed applicative/monadic notation that preserves parallelism inherent in code structure while allowing sequential effects where necessary. It provides a one-pass translation from direct-style to a language of effect combinators ($Pure$, $Bind$, $Ap$) and proves that this translation preserves typability, semantics, and parallelism, with a Coq mechanisation based on PHOAS and intrinsically typed terms. The authors also implement the approach in Scala using macros, enabling a practical, type-preserving compilation path from mixed direct-style code to mixed monadic/applicative programs. By treating parallelism as a property derived from program structure (span and work), the method offers a principled, verifiable route to optimizing effectful code without sacrificing correctness. The work lays groundwork for richer language features and broader effect systems, while demonstrating concrete gains in parallelism preservation and reasoning about correctness.
Abstract
Modeling sequential and parallel composition of effectful computations has been investigated in a variety of languages for a long time. In particular, the popular do-notation provides a lightweight effect embedding for any instance of a monad. Idiom bracket notation, on the other hand, provides an embedding for applicatives. First, while monads force effects to be executed sequentially, ignoring potential for parallelism, applicatives do not support sequential effects. Composing sequential with parallel effects remains an open problem. This is even more of an issue as real programs consist of a combination of both sequential and parallel segments. Second, common notations do not support invoking effects in direct-style, instead forcing a rigid structure upon the code. In this paper, we propose a mixed applicative/monadic notation that retains parallelism where possible, but allows sequentiality where necessary. We leverage a direct-style notation where sequentiality or parallelism is derived from the structure of the code. We provide a mechanisation of our effectful language in Coq and prove that our compilation approach retains the parallelism of the source program.
