## Composing monads using coproducts

### Cached

### Download Links

- [www.cs.nott.ac.uk]
- [www.informatik.uni-bremen.de]
- [www.informatik.uni-bremen.de]
- DBLP

### Other Repositories/Bibliography

Venue: | ACM SIGPLAN Notices |

Citations: | 24 - 2 self |

### BibTeX

@ARTICLE{Lüth_composingmonads,

author = {Christoph Lüth and Neil Ghani},

title = {Composing monads using coproducts},

journal = {ACM SIGPLAN Notices},

year = {},

pages = {2002}

}

### Years of Citing Articles

### OpenURL

### Abstract

Monads are a useful abstraction of computation, as they model diverse computational effects such as stateful computations, exceptions and I/O in a uniform manner. Their potential to provide both a modular semantics and a modular programming style was soon recognised. However, in general, monads proved difficult to compose and so research focused on special mechanisms for their composition such as distributive monads and monad transformers. We present a new approach to this problem which is general in that nearly all monads compose, mathematically elegant in using the standard categorical tools underpinning monads and computationally expressive in supporting a canonical recursion operator. In a nutshell, we propose that two monads should be composed by taking their coproduct. Although abstractly this is a simple idea, the actual construction of the coproduct of two monads is non-trivial. We outline this construction, show how to implement the coproduct within Haskell and demonstrate its usage with a few examples. We also discuss its relationship with other ways of combining monads, in particular distributive laws for monads and monad transformers.

### Citations

466 | Comprehending monads
- WADLER
- 1992
(Show Context)
Citation Context ...y: A first guess would be to consider if the functorial composite T.R is a monad. This would require a multiplication of the form T.R.T .R ⇒ T .R but there is no reason one should exist. One solution =-=[6, 10]-=- is to restrict attention to those monads for which there is a natural transformation λ : R.T ⇒ T .R and then, given some coherence laws, the multiplication can be constructed as TλR µT µR T .R.T .R =... |

455 | Computational lambda-calculus and monads
- Moggi
- 1989
(Show Context)
Citation Context ...of software could be achieved by reasoning about smaller (and hence more tractable) subcomponents and then lifting these results to the original problem. One exciting possibility was Moggi’s proposal =-=[17]-=- to use monads to structure denotational semantics. Moggi pointed out that a number of computational features (including all of those mentioned above) could be modelled by monads. Wadler took Moggi’s ... |

391 | Basic concepts of enriched category theory
- Kelly
- 2005
(Show Context)
Citation Context ... change of the base category, but imposing an order structure on the morphisms as well (see e.g. [23]; crucially the set of functions between two cpo’s forms in turn a cpo.) This is called enrichment =-=[8]-=-. We note that all of the theory in the following can be enriched [9], but prefer to keep the presentation simple. The definition of a monad given above is unlike what readers may have seen in a funct... |

264 |
Categories for the Working Mathematician (Graduate Texts
- Lane
- 1998
(Show Context)
Citation Context ... term algebras. Such monads provide good intuitions as to how to construct the coproduct of two monads and so we begin with them. Since this is standard material, we refer the reader to general texts =-=[15]-=- for more details. 2.1 Monads and Term Algebras Term algebras are built from signatures which are defined as follows: Definition 1. A (single-sorted) signature consists of a function Σ : → Set. The se... |

246 | Monad transformers and modular interpreters
- Liang, Hudak, et al.
- 1995
(Show Context)
Citation Context ...ad T is sitting inside F(T). Given that the identity functor is a monad, we may thus regard F as adding to every monad T the monad F(1). For example, the definition of the exception monad transformer =-=[11]-=- is type ExnT m a = m (Exn a) As we have already seen, there is a distributivity law present and in this case, the action of the monad transformer takes any monad T to TExn. However, the definition of... |

219 |
Locally presentable and accessible categories
- Adámek, Rosický
- 1994
(Show Context)
Citation Context ...l not need this level of detail; however, some of our constructions will only be valid for finitary monads, so we introduce the terminology on an informal level here. For the precise definitions, see =-=[1]-=-. All known computational monads are finitary, except for the continuation monad [19]. Secondly, when reasoning about the semantics of functional languages, we usually pass from sets to order structur... |

175 |
The category-theoretic solution of recursive domain equations
- Smyth, Plotkin
(Show Context)
Citation Context ...m sets to order structures such as complete partial orders (cpos). Categorically, this means not only a change of the base category, but imposing an order structure on the morphisms as well (see e.g. =-=[23]-=-; crucially the set of functions between two cpo’s forms in turn a cpo.) This is called enrichment [8]. We note that all of the theory in the following can be enriched [9], but prefer to keep the pres... |

172 |
An abstract view of programming languages
- Moggi
- 1989
(Show Context)
Citation Context ...he coproduct monad. Monad Transformers: Monad transformers provide a partial answer to the question of composing monads when there is no distributivity law present. In a nutshell, a monad transformer =-=[18]-=- is a pointed endofunctor on the category Mon(C) of monads over a fixed base category. That is, a functor F : Mon(C) → Mon(C) mapping every monad T to a monad F(T), and for every monad T a natural fam... |

148 |
Abstract syntax and variable binding
- Fiore, Plotkin, et al.
- 1999
(Show Context)
Citation Context ...pse two layers of the same type into a single layer. Monads model a number of other interesting structures in computer science, such as (many-sorted) algebraic theories, calculi with variable binders =-=[4]-=-, term rewriting systems [12], and, via computational monads [17], state-based computations, exceptions, continuations etc. Several of these computational monads are used throughout this paper and the... |

72 | Composing Monads
- Jones, Duponcheel
- 1993
(Show Context)
Citation Context ...y: A first guess would be to consider if the functorial composite T.R is a monad. This would require a multiplication of the form T.R.T .R ⇒ T .R but there is no reason one should exist. One solution =-=[6, 10]-=- is to restrict attention to those monads for which there is a natural transformation λ : R.T ⇒ T .R and then, given some coherence laws, the multiplication can be constructed as TλR µT µR T .R.T .R =... |

63 |
Adjunctions whose counits are coequalizers, and presentations of finitary enriched monads
- Kelly, Power
- 1993
(Show Context)
Citation Context ...t, every monad on Set can be considered as an algebraic theory [16]), and possibly even enrichment. Even in this abstract setting, monads can be considered as a generalised form of algebraic theories =-=[9, 21]-=-. We do not pursue the theory in its full generality here, but should bear in mind that although we draw our intuitions from monads on Set, this is just a particular case which we do well not to restr... |

56 | Notions of computation determine monads
- Plotkin, Power
- 2002
(Show Context)
Citation Context ...id for finitary monads, so we introduce the terminology on an informal level here. For the precise definitions, see [1]. All known computational monads are finitary, except for the continuation monad =-=[19]-=-. Secondly, when reasoning about the semantics of functional languages, we usually pass from sets to order structures such as complete partial orders (cpos). Categorically, this means not only a chang... |

50 |
Toposes, Triples, and Theories, Grundlehren der math. Wissenschaften 278, Springer-Verlag
- Barr, Wells
- 1985
(Show Context)
Citation Context ... ⇒ T .R and then, given some coherence laws, the multiplication can be constructed as TλR µT µR T .R.T .R ===== ⇒ T .T .R.R ======= ⇒ T .R Such a natural transformation λ is called a distributive law =-=[2]-=- and ensures T.R is a monad. A practical example occurs with the exception monad which distributes over any other monad. That is, if E is a fixed object of exceptions and T is a monad, then the assign... |

49 |
A Unified Treatment of Transfinite Constructions for Free Algebras, Free Monoids
- Kelly
- 1980
(Show Context)
Citation Context ...his paper has introduced the coproduct of two monads as their canonical combination: the smallest non-interacting combination of their computational effects. From the general categorical construction =-=[7]-=-, we have derived an implementation in Haskell for a wide class of monads, the so-called layered monads. Computations in the coproduct monad can be thought of as sequences of steps in the two componen... |

26 | Variations on algebra: monadicity and generalisations of equational theories
- Robinson
- 2002
(Show Context)
Citation Context ...t, every monad on Set can be considered as an algebraic theory [16]), and possibly even enrichment. Even in this abstract setting, monads can be considered as a generalised form of algebraic theories =-=[9, 21]-=-. We do not pursue the theory in its full generality here, but should bear in mind that although we draw our intuitions from monads on Set, this is just a particular case which we do well not to restr... |

20 | Monads and modular term rewriting
- Lüth, Ghani
- 1997
(Show Context)
Citation Context ...is paper is about. Our interest in this topic arose from previous research of ours which used the coproduct of two monads as a framework for modularity [14] and, in particular, modular term rewriting =-=[12, 13]-=-. In modular term rewriting, terms built over two signatures are decomposed into terms built over each of the two signatures, called layers. Within these papers, we argued that the layer structure is ... |

20 |
Foundations of equational deduction: A categorical treatment of equational proofs and unification algorithms
- Rydeheard, Stell
- 1987
(Show Context)
Citation Context ...ategory can be considered the free syntactic category generated from the signature, and as such it has some attractive properties (for example, unification can be expressed elegantly as a coequaliser =-=[22]-=-). However, we prefer to work with Def. 3 since it is unclear how to form the coproduct of two Kleisli-categories directly. It is most certainly not the coproduct of the two categories, which would ju... |

19 | Combining computational effects: commutativity and sum
- Hyland, Plotkin, et al.
- 2002
(Show Context)
Citation Context ...tion to distribute over the other computation, so the second stateful computation can use the state left by the first stateful computation. This is related to recent work by Plotkin, Power and Hyland =-=[5]-=-, where they describe a commutative combination ⊗ which corresponds to an imposed distributivity law in our sense. Technically, their work uses so-called Lawvere theories but these are equivalent to m... |

18 |
Enriched Lawvere theories. Theory and Applications of Categories
- Power
- 1999
(Show Context)
Citation Context ...e they describe a commutative combination ⊗ which corresponds to an imposed distributivity law in our sense. Technically, their work uses so-called Lawvere theories but these are equivalent to monads =-=[20]-=-. In summary, the coproduct of monads is a simple, modular way of combining two monads. It is more general than distributivity and monad transformers, is applicable in nearly all cases, and based on s... |

17 |
Algebraic Theories, Graduate Texts
- Manes
- 1976
(Show Context)
Citation Context ...ut this paper and their definition is given in Appendix A. Importantly, these applications involve base categories other than Set (in fact, every monad on Set can be considered as an algebraic theory =-=[16]-=-), and possibly even enrichment. Even in this abstract setting, monads can be considered as a generalised form of algebraic theories [9, 21]. We do not pursue the theory in its full generality here, b... |

11 | Categorical Term Rewriting: Monads and Modularity
- Luth
- 1990
(Show Context)
Citation Context ...is paper is about. Our interest in this topic arose from previous research of ours which used the coproduct of two monads as a framework for modularity [14] and, in particular, modular term rewriting =-=[12, 13]-=-. In modular term rewriting, terms built over two signatures are decomposed into terms built over each of the two signatures, called layers. Within these papers, we argued that the layer structure is ... |

7 | Monads and modularity
- Lüth, Ghani
- 2002
(Show Context)
Citation Context ...her is take their coproduct, and this is what this paper is about. Our interest in this topic arose from previous research of ours which used the coproduct of two monads as a framework for modularity =-=[14]-=- and, in particular, modular term rewriting [12, 13]. In modular term rewriting, terms built over two signatures are decomposed into terms built over each of the two signatures, called layers. Within ... |

1 | Locally Presentable and Accessi-ble Categories. LMS Lecture Notes 183 - Adamek, Rosick'y - 1994 |

1 | Abstract syntax and variablebinding - Fiore, Plotkin, et al. - 1999 |

1 | Composing monads. TechnicalReport YALEU/DCS/RR-1004 - Jones, Duponcheel - 1993 |

1 | Monad transformersand modular interpreters - Liang, Hudak, et al. - 1995 |

1 | Algebraic Theories, Graduate Texts inMathematics 26 - Manes - 1976 |