skip to main content
article

Composing monads using coproducts

Published:17 September 2002Publication History
Skip Abstract Section

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.

References

  1. J. Adamek and J. Rosický. Locally Presentable and Accessible Categories. LMS Lecture Notes 183. Cambridge University Press, 1994.Google ScholarGoogle Scholar
  2. M. Barr and C. Wells. Toposes, Triples and Theories. Grundlehren der mathematischen Wissenschaften 278. Springer Verlag, 1985.Google ScholarGoogle Scholar
  3. R. Bird and O. de Moor. Algebra of Programming. Prentice-Hall, 1997. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. M. Fiore, G. Plotkin, and D. Turi. Abstract syntax and variable binding. In Proc. LICS'99, pages 193--202. IEEE Computer Society Press, 1999. Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. Martin Hyland, Gordon Plotkin, and John Power. Combining computational effects: Commutativity and sum. In TCS 2002, 2nd IFIP International Conference on Computer Science, Montreal, 2002. Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. M. Jones and L. Duponcheel. Composing monads. Technical Report YALEU/DCS/RR-1004, Yale University, Dept. Comp. Sci, Dec 1993.Google ScholarGoogle Scholar
  7. G. M. Kelly. A unified treatment of transfinite constructions for free algebras, free monoids, colimits, associated sheaves and so on. Bulletins of the Australian Mathematical Society, 22:1--83, 1980.Google ScholarGoogle ScholarCross RefCross Ref
  8. G. M. Kelly. Basic Concepts of Enriched Category Theory, LMS Lecture Notes~64. Cambridge University Press, 1982.Google ScholarGoogle Scholar
  9. G. M. Kelly and A. J. Power. Adjunctions whose counits are coequalizers, and presentations of finitary monads. Journal for Pure and Applied Algebra, 89:163--179, 1993.Google ScholarGoogle ScholarCross RefCross Ref
  10. David King and Philip Wadler. Combining monads. In J. Launchbury and P.M. Samson, editors, Glasgow Workshop on Functional Programming, Workshops in Computing Series, Ayr, July 1992. Springer Verlag. Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. S. Liang, P. Hudak, and M. Jones. Monad transformers and modular interpreters. In Proceedings of the 22nd ACM Symposium on Principles of Programming Languages. ACM Press, Jan 1995. Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. C. Lüth. Categorical Term Rewriting: Monads and Modularity. PhD thesis, University of Edinburgh, 1998.Google ScholarGoogle Scholar
  13. Monads and modular term rewriting. In Category Theory in Computer Science CTCS'97, LNCS 1290, pages 69--86. Springer Verlag, September 1997. Google ScholarGoogle ScholarDigital LibraryDigital Library
  14. C. Lüth and N. Ghani. Monads and modularity. In Frontiers of Combining Systems FroCoS'02, LNAI 2309, pages 18--32. Springer Verlag, 2002. Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. S. Mac Lane. Categories for the Working Mathematician, Graduate Texts in Mathematics 5. Springer Verlag, 1971.Google ScholarGoogle Scholar
  16. Ernest G. Manes. Algebraic Theories, Graduate Texts in Mathematics 26. Springer Verlag, 1976.Google ScholarGoogle Scholar
  17. E. Moggi. Computational lambda-calculus and monads. In Fourth Annual Symposium on Logic in Computer Science. IEEE, Computer Society Press, June 1989. Google ScholarGoogle ScholarDigital LibraryDigital Library
  18. E. Moggi. An abstract view of programming languages. Technical Report ECS-LFCS-90-113, LFCS, 1990.Google ScholarGoogle Scholar
  19. Gordon Plotkin and John Power. Notions of computation determine monads. In M. Nielsen and U. Engeberg, editors, Proc. FOSSACS 2002, LNCS 2303, pages 342--356, 2002. Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. John Power. Enriched Lawvere theories. Theories and Applications of Categories 6:83--93, 2000.Google ScholarGoogle Scholar
  21. E. Robinson. Variations on algebra: monadicity and generalisations of equational theories. Technical Report 6/94, Sussex Computer Science, 1994.Google ScholarGoogle Scholar
  22. D. E. Rydeheard and J. G. Stell. Foundations of equational deduction: A categorical treatment of equational proofs and unification algorithms. In Category Theory and Computer Science, LNCS 283, pages 114--139. Springer Verlag, 1987. Google ScholarGoogle ScholarDigital LibraryDigital Library
  23. M. B. Smyth and G. D. Plotkin. The category-theoretic solution of recursive domain equations. SIAM Journal on Computing, 11(4):763--783, 1982.Google ScholarGoogle ScholarCross RefCross Ref

Index Terms

  1. Composing monads using coproducts

    Recommendations

    Comments

    Login options

    Check if you have access through your login credentials or your institution to get full access on this article.

    Sign in

    Full Access

    • Published in

      cover image ACM SIGPLAN Notices
      ACM SIGPLAN Notices  Volume 37, Issue 9
      September 2002
      283 pages
      ISSN:0362-1340
      EISSN:1558-1160
      DOI:10.1145/583852
      Issue’s Table of Contents
      • cover image ACM Conferences
        ICFP '02: Proceedings of the seventh ACM SIGPLAN international conference on Functional programming
        October 2002
        294 pages
        ISBN:1581134878
        DOI:10.1145/581478

      Copyright © 2002 ACM

      Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than ACM must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]

      Publisher

      Association for Computing Machinery

      New York, NY, United States

      Publication History

      • Published: 17 September 2002

      Check for updates

      Qualifiers

      • article

    PDF Format

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader