Download PDF

International Conference on Mathematics of Program Construction, Date: 2015/06/29 - 2015/07/01, Location: Königswinter, Germany

Publication date: 2015-01-01
Volume: 9129 Pages: 302 - 322
ISSN: 978-3-319-19796-8
Publisher: Springer-Verlag

Mathematics of Program Construction - 12th International Conference, MPC 2015, Konigswinter, Germany, June 29 - July 1, 2015. Proceedings

Author:

Wu, Nicolas
Schrijvers, Tom

Keywords:

Haskell, algebraic effect handlers, side effects, fusion, monads, parametricity, Science & Technology, Technology, Physical Sciences, Computer Science, Theory & Methods, Mathematics, Applied, Computer Science, Mathematics

Abstract:

Algebraic effect handlers are a recently popular approach for modelling side-effects that separates the syntax and semantics of effectful operations. The shape of syntax is captured by functors, and free monads over these functors denote syntax trees. The semantics is captured by algebras, and effect handlers pass these over the syntax trees to interpret them into a semantic domain. This approach is inherently modular: different functors can be composed to make trees with richer structure. Such trees are interpreted by applying several handlers in sequence, each removing the syntactic constructs it recognizes. Unfortunately, the construction and traversal of intermediate trees is painfully inefficient and has hindered the adoption of the handler approach. This paper explains how a sequence of handlers can be fused into one, so that multiple tree traversals can be reduced to a single one and no intermediate trees need to be allocated. At the heart of this optimization is keeping the notion of a free monad abstract, thus enabling a change of representation that opens up the possibility of fusion. We demonstrate how the ensuing code can be inlined at compile time to produce efficient handlers.