data Cofree f a Source #

The Cofree Comonad of a functor f .

Formally

A Comonad v is a cofree Comonad for f if every comonad homomorphism from another comonad w to v is equivalent to a natural transformation from w to f .

A cofree functor is right adjoint to a forgetful functor.

Cofree is a functor from the category of functors to the category of comonads that is right adjoint to the forgetful functor from the category of comonads to the category of functors that forgets how to extract and duplicate , leaving you with only a Functor .

In practice, cofree comonads are quite useful for annotating syntax trees, or talking about streams.

A number of common comonads arise directly as cofree comonads.

For instance,

Cofree Maybe forms the a comonad for a non-empty list.

forms the a comonad for a non-empty list. Cofree ( Const b) is a product.

is a product. Cofree Identity forms an infinite stream.

forms an infinite stream. Cofree ((->) b)' describes a Moore machine with states labeled with values of type a, and transitions on edges of type b.

Furthermore, if the functor f forms a monoid (for example, by being an instance of Alternative ), the resulting Comonad is also a Monad . See Monadic Augment and Generalised Shortcut Fusion by Neil Ghani et al., Section 4.3 for more details.