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.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Similar content being viewed by others
Notes
- 1.
Available via Edward Kmett’s Control.Comonad package.
- 2.
- 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.
Morphisms generalise the notion of function. Readers unfamiliar with category theory may safely replace ‘morphism’ with ‘function’ here.
References
Wadler, P.: The essence of functional programming. In: Proceedings of POPL ’92, pp. 1–14. ACM (1992)
Wadler, P.: Monads for functional programming. In: Jeuring, J., Meijer, E. (eds.) AFP 1995. LNCS, vol. 925, pp. 24–52. Springer, Heidelberg (1995)
Petricek, T., Syme, D.: Syntax Matters: writing abstract computations in F#. Pre-proceedings of TFP (Trends in Functional Programming), St. Andrews, Scotland (2012)
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)
Uustalu, T., Vene, V.: Comonadic functional attribute evaluation. Trends Funct. Program. 6, 145–160 (2007)
Orchard, D., Bolingbroke, M., Mycroft, A.: Ypnos: declarative, parallel structured grid programming. In: DAMP ’10, pp. 15–24. ACM, NY (2010)
Kieburtz, R.B.: Codata and Comonads in Haskell (1999) (unpublished)
Uustalu, T., Vene, V.: Comonadic notions of computation. Electron. Notes Theor. Comput. Sci. 203, 263–284 (2008)
Huet, G.: The zipper. J. Funct. Program. 7, 549–554 (1997)
McBride, C.: The derivative of a regular type is its type of one-hole contexts. Unpublished manuscript (2001)
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)
Orchard, D.: Programming contextual computations (2013) Forthcoming PhD dissertation. http://www.cl.cam.ac.uk/techreports
Lambek, J., Scott, P.: Introduction to higher-order categorical logic. Cambridge University Press, Cambridge (1988)
Moggi, E.: Computational lambda-calculus and monads. In: Logic in Computer Science, LICS’89, pp. 14–23. IEEE (1989)
Moggi, E.: Notions of computation and monads. Inf. Comput. 93, 55–92 (1991)
Hughes, J.: Programming with arrows. In: Vene, V., Uustalu, T. (eds.) AFP 2004. LNCS, vol. 3622, pp. 73–129. Springer, Heidelberg (2005)
Paterson, R.: A new notation for arrows. In: ACM SIGPLAN Notices, vol. 36, pp. 229–240. ACM (2001)
Abbott, M., Altenkirch, T., Ghani, N.: Containers: constructing strictly positive types. Theor. Comput. Sci. 342, 3–27 (2005)
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
Corresponding author
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:
The proof of shape preservation (3) is then:
\(\Box \)
Rights 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)