Skip to main content

A Notation for Comonads

  • Conference paper
  • First Online:
Implementation and Application of Functional Languages (IFL 2012)

Part of the book series: Lecture Notes in Computer Science ((LNTCS,volume 8241))

Included in the following conference series:

Abstract

The category-theoretic concept of a monad occurs widely as a design pattern for functional programming with effects. The utility and ubiquity of monads is such that some languages provide syntactic sugar for this pattern, further encouraging its use. We argue that comonads, the dual of monads, similarly provide a useful design pattern, capturing notions of context dependence. However, comonads remain relatively under-used compared to monads—due to a lack of knowledge of the design pattern along with the lack of accompanying simplifying syntax.

We propose a lightweight syntax for comonads in Haskell, analogous to the do-notation for monads, and provide examples of its use. Via our notation, we also provide a tutorial on programming with comonads.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Subscribe and save

Springer+ Basic
$34.99 /Month
  • Get 10 units per month
  • Download Article/Chapter or eBook
  • 1 Unit = 1 Article or 1 Chapter
  • Cancel anytime
Subscribe now

Buy Now

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Similar content being viewed by others

Notes

  1. 1.

    Available via Edward Kmett’s Control.Comonad package.

  2. 2.

    http://hackage.haskell.org/package/codo-notation

  3. 3.

    There are many alternative methods for abstracting boundary checking and values; our choice here is for simplicity of presentation rather than performance or accuracy.

  4. 4.

    Morphisms generalise the notion of function. Readers unfamiliar with category theory may safely replace ‘morphism’ with ‘function’ here.

References

  1. Wadler, P.: The essence of functional programming. In: Proceedings of POPL ’92, pp. 1–14. ACM (1992)

    Google Scholar 

  2. Wadler, P.: Monads for functional programming. In: Jeuring, J., Meijer, E. (eds.) AFP 1995. LNCS, vol. 925, pp. 24–52. Springer, Heidelberg (1995)

    Google Scholar 

  3. Petricek, T., Syme, D.: Syntax Matters: writing abstract computations in F#. Pre-proceedings of TFP (Trends in Functional Programming), St. Andrews, Scotland (2012)

    Google Scholar 

  4. Uustalu, T., Vene, V.: The essence of dataflow programming. In: Horváth, Z. (ed.) CEFP 2005. LNCS, vol. 4164, pp. 135–167. Springer, Heidelberg (2006)

    Google Scholar 

  5. Uustalu, T., Vene, V.: Comonadic functional attribute evaluation. Trends Funct. Program. 6, 145–160 (2007)

    Google Scholar 

  6. Orchard, D., Bolingbroke, M., Mycroft, A.: Ypnos: declarative, parallel structured grid programming. In: DAMP ’10, pp. 15–24. ACM, NY (2010)

    Google Scholar 

  7. Kieburtz, R.B.: Codata and Comonads in Haskell (1999) (unpublished)

    Google Scholar 

  8. Uustalu, T., Vene, V.: Comonadic notions of computation. Electron. Notes Theor. Comput. Sci. 203, 263–284 (2008)

    Article  MathSciNet  Google Scholar 

  9. Huet, G.: The zipper. J. Funct. Program. 7, 549–554 (1997)

    Article  MathSciNet  MATH  Google Scholar 

  10. McBride, C.: The derivative of a regular type is its type of one-hole contexts. Unpublished manuscript (2001)

    Google Scholar 

  11. Ahman, D., Chapman, J., Uustalu, T.: When is a container a comonad? In: Birkedal, L. (ed.) FOSSACS 2012. LNCS, vol. 7213, pp. 74–88. Springer, Heidelberg (2012)

    Google Scholar 

  12. Orchard, D.: Programming contextual computations (2013) Forthcoming PhD dissertation. http://www.cl.cam.ac.uk/techreports

  13. Lambek, J., Scott, P.: Introduction to higher-order categorical logic. Cambridge University Press, Cambridge (1988)

    MATH  Google Scholar 

  14. Moggi, E.: Computational lambda-calculus and monads. In: Logic in Computer Science, LICS’89, pp. 14–23. IEEE (1989)

    Google Scholar 

  15. Moggi, E.: Notions of computation and monads. Inf. Comput. 93, 55–92 (1991)

    Article  MathSciNet  MATH  Google Scholar 

  16. Hughes, J.: Programming with arrows. In: Vene, V., Uustalu, T. (eds.) AFP 2004. LNCS, vol. 3622, pp. 73–129. Springer, Heidelberg (2005)

    Google Scholar 

  17. Paterson, R.: A new notation for arrows. In: ACM SIGPLAN Notices, vol. 36, pp. 229–240. ACM (2001)

    Google Scholar 

  18. Abbott, M., Altenkirch, T., Ghani, N.: Containers: constructing strictly positive types. Theor. Comput. Sci. 342, 3–27 (2005)

    Article  MathSciNet  MATH  Google Scholar 

Download references

Acknowledgements

We thank Jeremy Gibbons, Ralf Hinze, Tomas Petricek, Tarmo Uustalu, and Varmo Vene for helpful discussions, and to the anonymous reviewers for their comments on this paper and an earlier draft. This research was supported by an EPSRC Doctoral Training Award.

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Dominic Orchard .

Editor information

Editors and Affiliations

A Proof of Shape Preservation

A Proof of Shape Preservation

To prove shape preservation we first prove the following intermediate lemma:

$$\begin{aligned} cmap \; g \mathbin {\circ }extend \; f \mathrel {=}extend \;( g \mathbin {\circ } f ) \end{aligned}$$
(7)
$$\begin{aligned} \begin{array}{rll} &{} cmap \; g \mathbin {\circ }extend \; f &{} \\ \equiv \;&{} extend \;( g \mathbin {\circ }extract )\mathbin {\circ }extend \; f &{} \quad \text {definition of cmap } \\ \equiv \;&{} extend \;( g \mathbin {\circ }extract \mathbin {\circ }extend \; f ) &{} \quad \text {[C3]} \\ \equiv \;&{} extend \;( g \mathbin {\circ } f ) \quad \Box &{} \quad \text {[C2]} \end{array} \end{aligned}$$

The proof of shape preservation (3) is then:

$$\begin{aligned} \begin{array}{rll} &{} shape \mathbin {\circ }(extend \; f ) &{} \\ \equiv \;&{} ( cmap \;( const \;())\mathbin {\circ }(extend \; f ) &{} \text {definition of shape } \\ \equiv \;&{} extend \;(( const \;())\mathbin {\circ } f ) &{} (7) \\ \equiv \;&{} extend \;(( const \;())\mathbin {\circ }extract ) &{} ( const \; x )\mathbin {\circ } f \equiv ( const \; x )\mathbin {\circ } g \\ \equiv \;&{} ( cmap \;( const \;()))\mathbin {\circ }(extend \;extract ) \quad \quad &{} (7) \\ \equiv \;&{} cmap \;( const \;()) &{} \text {[C1]} \\ \equiv \;&{} shape &{} \text {definition of shape } \end{array} \end{aligned}$$

\(\Box \)

Rights and permissions

Reprints and permissions

Copyright information

© 2013 Springer-Verlag Berlin Heidelberg

About this paper

Cite this paper

Orchard, D., Mycroft, A. (2013). A Notation for Comonads. In: Hinze, R. (eds) Implementation and Application of Functional Languages. IFL 2012. Lecture Notes in Computer Science(), vol 8241. Springer, Berlin, Heidelberg. https://doi.org/10.1007/978-3-642-41582-1_1

Download citation

  • DOI: https://doi.org/10.1007/978-3-642-41582-1_1

  • Published:

  • Publisher Name: Springer, Berlin, Heidelberg

  • Print ISBN: 978-3-642-41581-4

  • Online ISBN: 978-3-642-41582-1

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics