free-4.12.1: Monads for free

Copyright(C) 2008-2013 Edward Kmett
LicenseBSD-style (see the file LICENSE)
MaintainerEdward Kmett <ekmett@gmail.com>
Stabilityprovisional
PortabilityMPTCs, fundeps
Safe HaskellSafe
LanguageHaskell2010

Control.Comonad.Cofree

Contents

Description

Cofree comonads

Synopsis

Documentation

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 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.
  • Cofree (Const b) is a product.
  • Cofree Identity 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.

In particular, if f a ≡ [a], the resulting data structure is a Rose tree. For a practical application, check Higher Dimensional Trees, Algebraically by Neil Ghani et al.

Constructors

a :< (f (Cofree f a)) infixr 5 

Instances

ComonadTrans Cofree Source

This is not a true Comonad transformer, but this instance is convenient.

ComonadTraced m w => ComonadTraced m (Cofree w) Source 
ComonadStore s w => ComonadStore s (Cofree w) Source 
ComonadEnv e w => ComonadEnv e (Cofree w) Source 
Functor f => ComonadCofree f (Cofree f) Source 
Alternative f => Monad (Cofree f) Source 
Functor f => Functor (Cofree f) Source 
Alternative f => Applicative (Cofree f) Source 
Foldable f => Foldable (Cofree f) Source 
Traversable f => Traversable (Cofree f) Source 
(Alternative f, MonadZip f) => MonadZip (Cofree f) Source 
Functor f => Comonad (Cofree f) Source 
ComonadApply f => ComonadApply (Cofree f) Source 
Distributive f => Distributive (Cofree f) Source 
(Functor f, Eq1 f) => Eq1 (Cofree f) Source 
(Functor f, Ord1 f) => Ord1 (Cofree f) Source 
(Functor f, Show1 f) => Show1 (Cofree f) Source 
(Functor f, Read1 f) => Read1 (Cofree f) Source 
Traversable1 f => Traversable1 (Cofree f) Source 
Apply f => Apply (Cofree f) Source 
Functor f => Extend (Cofree f) Source 
Foldable1 f => Foldable1 (Cofree f) Source 
(Eq (f (Cofree f a)), Eq a) => Eq (Cofree f a) Source 
(Ord (f (Cofree f a)), Ord a) => Ord (Cofree f a) Source 
(Read (f (Cofree f a)), Read a) => Read (Cofree f a) Source 
(Show (f (Cofree f a)), Show a) => Show (Cofree f a) Source 

class (Functor f, Comonad w) => ComonadCofree f w | w -> f where Source

Allows you to peel a layer off a cofree comonad.

Methods

unwrap :: w a -> f (w a) Source

Remove a layer.

section :: Comonad f => f a -> Cofree f a Source

coiter :: Functor f => (a -> f a) -> a -> Cofree f a Source

Use coiteration to generate a cofree comonad from a seed.

coiter f = unfold (id &&& f)

unfold :: Functor f => (b -> (a, f b)) -> b -> Cofree f a Source

Unfold a cofree comonad from a seed.

unfoldM :: (Traversable f, Monad m) => (b -> m (a, f b)) -> b -> m (Cofree f a) Source

Unfold a cofree comonad from a seed, monadically.

hoistCofree :: Functor f => (forall x. f x -> g x) -> Cofree f a -> Cofree g a Source

Lenses into cofree comonads

_extract :: Functor f => (a -> f a) -> Cofree g a -> f (Cofree g a) Source

This is a lens that can be used to read or write from the target of extract.

Using (^.) from the lens package:

foo ^. _extract == extract foo

For more on lenses see the lens package on hackage

_extract :: Lens' (Cofree g a) a

_unwrap :: Functor f => (g (Cofree g a) -> f (g (Cofree g a))) -> Cofree g a -> f (Cofree g a) Source

This is a lens that can be used to read or write to the tails of a Cofree Comonad.

Using (^.) from the lens package:

foo ^. _unwrap == unwrap foo

For more on lenses see the lens package on hackage

_unwrap :: Lens' (Cofree g a) (g (Cofree g a))

telescoped :: (Functor f, Functor g) => [(Cofree g a -> f (Cofree g a)) -> g (Cofree g a) -> f (g (Cofree g a))] -> (a -> f a) -> Cofree g a -> f (Cofree g a) Source

Construct a Lens into a Cofree f given a list of lenses into the base functor.

For more on lenses see the lens package on hackage.

telescoped :: Functor g => [Lens' (Cofree g a) (g (Cofree g a))] -> Lens' (Cofree g a) a