README.markdown
1comonad
2=======
3
4[![Build Status](https://github.com/ekmett/comonad/workflows/Haskell-CI/badge.svg)](https://github.com/ekmett/comonad/actions?query=workflow%3AHaskell-CI)
5
6This package provides comonads, the categorical dual of monads. The typeclass
7provides three methods: `extract`, `duplicate`, and `extend`.
8
9 class Functor w => Comonad w where
10 extract :: w a -> a
11 duplicate :: w a -> w (w a)
12 extend :: (w a -> b) -> w a -> w b
13
14There are two ways to define a comonad:
15
16I. Provide definitions for `extract` and `extend` satisfying these laws:
17
18 extend extract = id
19 extract . extend f = f
20 extend f . extend g = extend (f . extend g)
21
22In this case, you may simply set `fmap` = `liftW`.
23
24These laws are directly analogous to the [laws for
25monads](https://wiki.haskell.org/Monad_laws). The comonad laws can
26perhaps be made clearer by viewing them as stating that Cokleisli composition
27must be a) associative and b) have `extract` for a unit:
28
29 f =>= extract = f
30 extract =>= f = f
31 (f =>= g) =>= h = f =>= (g =>= h)
32
33II. Alternately, you may choose to provide definitions for `fmap`,
34`extract`, and `duplicate` satisfying these laws:
35
36 extract . duplicate = id
37 fmap extract . duplicate = id
38 duplicate . duplicate = fmap duplicate . duplicate
39
40In this case, you may not rely on the ability to define `fmap` in
41terms of `liftW`.
42
43You may, of course, choose to define both `duplicate` _and_ `extend`.
44In that case, you must also satisfy these laws:
45
46 extend f = fmap f . duplicate
47 duplicate = extend id
48 fmap f = extend (f . extract)
49
50These implementations are the default definitions of `extend` and`duplicate` and
51the definition of `liftW` respectively.
52
53Contact Information
54-------------------
55
56Contributions and bug reports are welcome!
57
58Please feel free to contact me through github or on the #haskell IRC channel on irc.freenode.net.
59
60-Edward Kmett
61