Extending monads with pattern matching

  • Authors:
  • Tomas Petricek;Alan Mycroft;Don Syme

  • Affiliations:
  • University of Cambridge, Cambridge, United Kingdom;University of Cambridge, Cambridge, United Kingdom;Microsoft Research Cambridge, Cambridge, United Kingdom

  • Venue:
  • Proceedings of the 4th ACM symposium on Haskell
  • Year:
  • 2011

Quantified Score

Hi-index 0.00

Visualization

Abstract

Sequencing of effectful computations can be neatly captured using monads and elegantly written using do notation. In practice such monads often allow additional ways of composing computations, which have to be written explicitly using combinators. We identify joinads, an abstract notion of computation that is stronger than monads and captures many such ad-hoc extensions. In particular, joinads are monads with three additional operations: one of type m a - m b - m (a, b) captures various forms of parallel composition, one of type m a - m a - m a that is inspired by choice and one of type m a - m (m a) that captures aliasing of computations. Algebraically, the first two operations form a near-semiring with commutative multiplication. We introduce docase notation that can be viewed as a monadic version of case. Joinad laws imply various syntactic equivalences of programs written using docase that are analogous to equivalences about case. Examples of joinads that benefit from the notation include speculative parallelism, waiting for a combination of user interface events, but also encoding of validation rules using the intersection of parsers.