free-5.1.10: Monads for free
Copyright (C) 2008-2013 Edward Kmett
License BSD-style (see the file LICENSE)
Maintainer Edward Kmett <ekmett@gmail.com>
Stability provisional
Portability MPTCs, fundeps
Safe Haskell Safe
Language Haskell2010

Control.Comonad.Cofree

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 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 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

Instances details
ComonadTrans Cofree Source #

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

Instance details

Defined in Control.Comonad.Cofree

ComonadHoist Cofree Source #
Instance details

Defined in Control.Comonad.Cofree

Methods

cohoist :: ( Comonad w, Comonad v) => ( forall x. w x -> v x) -> Cofree w a -> Cofree v a Source #

ComonadEnv e w => ComonadEnv e ( Cofree w) Source #
Instance details

Defined in Control.Comonad.Cofree

Methods

ask :: Cofree w a -> e Source #

ComonadStore s w => ComonadStore s ( Cofree w) Source #
Instance details

Defined in Control.Comonad.Cofree

ComonadTraced m w => ComonadTraced m ( Cofree w) Source #
Instance details

Defined in Control.Comonad.Cofree

Methods

trace :: m -> Cofree w a -> a Source #

Functor f => ComonadCofree f ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Alternative f => Monad ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Functor f => Functor ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Methods

fmap :: (a -> b) -> Cofree f a -> Cofree f b Source #

(<$) :: a -> Cofree f b -> Cofree f a Source #

Alternative f => Applicative ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Foldable f => Foldable ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Traversable f => Traversable ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Eq1 f => Eq1 ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Methods

liftEq :: (a -> b -> Bool ) -> Cofree f a -> Cofree f b -> Bool Source #

Ord1 f => Ord1 ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Read1 f => Read1 ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Show1 f => Show1 ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

( Alternative f, MonadZip f) => MonadZip ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Functor f => Comonad ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

ComonadApply f => ComonadApply ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Distributive f => Distributive ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Traversable1 f => Traversable1 ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Methods

traverse1 :: Apply f0 => (a -> f0 b) -> Cofree f a -> f0 ( Cofree f b) Source #

sequence1 :: Apply f0 => Cofree f (f0 b) -> f0 ( Cofree f b) Source #

Apply f => Apply ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Foldable1 f => Foldable1 ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Functor f => Extend ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Functor f => Generic1 ( Cofree f :: Type -> Type ) Source #
Instance details

Defined in Control.Comonad.Cofree

Associated Types

type Rep1 ( Cofree f) :: k -> Type Source #

Methods

from1 :: forall (a :: k). Cofree f a -> Rep1 ( Cofree f) a Source #

to1 :: forall (a :: k). Rep1 ( Cofree f) a -> Cofree f a Source #

FunctorWithIndex i f => FunctorWithIndex [i] ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Methods

imap :: ([i] -> a -> b) -> Cofree f a -> Cofree f b Source #

FoldableWithIndex i f => FoldableWithIndex [i] ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Methods

ifoldMap :: Monoid m => ([i] -> a -> m) -> Cofree f a -> m Source #

ifoldMap' :: Monoid m => ([i] -> a -> m) -> Cofree f a -> m Source #

ifoldr :: ([i] -> a -> b -> b) -> b -> Cofree f a -> b Source #

ifoldl :: ([i] -> b -> a -> b) -> b -> Cofree f a -> b Source #

ifoldr' :: ([i] -> a -> b -> b) -> b -> Cofree f a -> b Source #

ifoldl' :: ([i] -> b -> a -> b) -> b -> Cofree f a -> b Source #

TraversableWithIndex i f => TraversableWithIndex [i] ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Methods

itraverse :: Applicative f0 => ([i] -> a -> f0 b) -> Cofree f a -> f0 ( Cofree f b) Source #

( Eq1 f, Eq a) => Eq ( Cofree f a) Source #
Instance details

Defined in Control.Comonad.Cofree

( Typeable f, Data (f ( Cofree f a)), Data a) => Data ( Cofree f a) Source #
Instance details

Defined in Control.Comonad.Cofree

Methods

gfoldl :: ( forall d b. Data d => c (d -> b) -> d -> c b) -> ( forall g. g -> c g) -> Cofree f a -> c ( Cofree f a) Source #

gunfold :: ( forall b r. Data b => c (b -> r) -> c r) -> ( forall r. r -> c r) -> Constr -> c ( Cofree f a) Source #

toConstr :: Cofree f a -> Constr Source #

dataTypeOf :: Cofree f a -> DataType Source #

dataCast1 :: Typeable t => ( forall d. Data d => c (t d)) -> Maybe (c ( Cofree f a)) Source #

dataCast2 :: Typeable t => ( forall d e. ( Data d, Data e) => c (t d e)) -> Maybe (c ( Cofree f a)) Source #

gmapT :: ( forall b. Data b => b -> b) -> Cofree f a -> Cofree f a Source #

gmapQl :: (r -> r' -> r) -> r -> ( forall d. Data d => d -> r') -> Cofree f a -> r Source #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> ( forall d. Data d => d -> r') -> Cofree f a -> r Source #

gmapQ :: ( forall d. Data d => d -> u) -> Cofree f a -> [u] Source #

gmapQi :: Int -> ( forall d. Data d => d -> u) -> Cofree f a -> u Source #

gmapM :: Monad m => ( forall d. Data d => d -> m d) -> Cofree f a -> m ( Cofree f a) Source #

gmapMp :: MonadPlus m => ( forall d. Data d => d -> m d) -> Cofree f a -> m ( Cofree f a) Source #

gmapMo :: MonadPlus m => ( forall d. Data d => d -> m d) -> Cofree f a -> m ( Cofree f a) Source #

( Ord1 f, Ord a) => Ord ( Cofree f a) Source #
Instance details

Defined in Control.Comonad.Cofree

( Read1 f, Read a) => Read ( Cofree f a) Source #
Instance details

Defined in Control.Comonad.Cofree

( Show1 f, Show a) => Show ( Cofree f a) Source #
Instance details

Defined in Control.Comonad.Cofree

Generic ( Cofree f a) Source #
Instance details

Defined in Control.Comonad.Cofree

Associated Types

type Rep ( Cofree f a) :: Type -> Type Source #

type Rep1 ( Cofree f :: Type -> Type ) Source #
Instance details

Defined in Control.Comonad.Cofree

type Rep ( Cofree f a) Source #
Instance details

Defined in Control.Comonad.Cofree

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.

Instances

Instances details
ComonadCofree [] Tree Source #
Instance details

Defined in Control.Comonad.Cofree.Class

ComonadCofree Maybe NonEmpty Source #
Instance details

Defined in Control.Comonad.Cofree.Class

Functor f => ComonadCofree f ( Cofree f) Source #
Instance details

Defined in Control.Comonad.Cofree

Comonad w => ComonadCofree Identity ( CoiterT w) Source #
Instance details

Defined in Control.Comonad.Trans.Coiter

( ComonadCofree f w, Monoid m) => ComonadCofree f ( TracedT m w) Source #
Instance details

Defined in Control.Comonad.Cofree.Class

Methods

unwrap :: TracedT m w a -> f ( TracedT m w a) Source #

ComonadCofree f w => ComonadCofree f ( StoreT s w) Source #
Instance details

Defined in Control.Comonad.Cofree.Class

Methods

unwrap :: StoreT s w a -> f ( StoreT s w a) Source #

ComonadCofree f w => ComonadCofree f ( EnvT e w) Source #
Instance details

Defined in Control.Comonad.Cofree.Class

Methods

unwrap :: EnvT e w a -> f ( EnvT e w a) Source #

ComonadCofree f w => ComonadCofree f ( IdentityT w) Source #
Instance details

Defined in Control.Comonad.Cofree.Class

( Functor f, Comonad w) => ComonadCofree f ( CofreeT f w) Source #
Instance details

Defined in Control.Comonad.Trans.Cofree

Methods

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

ComonadCofree ( Const b :: Type -> Type ) ( (,) b) Source #
Instance details

Defined in Control.Comonad.Cofree.Class

Methods

unwrap :: (b, a) -> Const b (b, 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)

coiterW :: ( Comonad w, Functor f) => (w a -> f (w a)) -> w a -> Cofree f a Source #

Like coiter for comonadic values.

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 => [( 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 an Lens into a Cofree g given a list of lenses into the base functor. When the input list is empty, this is equivalent to _extract . When the input list is non-empty, this composes the input lenses with _unwrap to walk through the Cofree g before using _extract to get the element at the final location.

For more on lenses see the lens package on hackage.

telescoped :: [Lens' (g (Cofree g a)) (Cofree g a)]      -> Lens' (Cofree g a) a
telescoped :: [Traversal' (g (Cofree g a)) (Cofree g a)] -> Traversal' (Cofree g a) a
telescoped :: [Getter (g (Cofree g a)) (Cofree g a)]     -> Getter (Cofree g a) a
telescoped :: [Fold (g (Cofree g a)) (Cofree g a)]       -> Fold (Cofree g a) a
telescoped :: [Setter' (g (Cofree g a)) (Cofree g a)]    -> Setter' (Cofree g a) a

telescoped_ :: Functor f => [( Cofree g a -> f ( Cofree g a)) -> g ( Cofree g a) -> f (g ( Cofree g a))] -> ( Cofree g a -> f ( Cofree g a)) -> Cofree g a -> f ( Cofree g a) Source #

Construct an Lens into a Cofree g given a list of lenses into the base functor. The only difference between this and telescoped is that telescoped focuses on a single value, but this focuses on the entire remaining subtree. When the input list is empty, this is equivalent to id . When the input list is non-empty, this composes the input lenses with _unwrap to walk through the Cofree g .

For more on lenses see the lens package on hackage.

telescoped :: [Lens' (g (Cofree g a)) (Cofree g a)]      -> Lens' (Cofree g a) (Cofree g a)
telescoped :: [Traversal' (g (Cofree g a)) (Cofree g a)] -> Traversal' (Cofree g a) (Cofree g a)
telescoped :: [Getter (g (Cofree g a)) (Cofree g a)]     -> Getter (Cofree g a) (Cofree g a)
telescoped :: [Fold (g (Cofree g a)) (Cofree g a)]       -> Fold (Cofree g a) (Cofree g a)
telescoped :: [Setter' (g (Cofree g a)) (Cofree g a)]    -> Setter' (Cofree g a) (Cofree g a)

shoots :: ( Applicative f, Traversable g) => (a -> f a) -> Cofree g a -> f ( Cofree g a) Source #

A Traversal' that gives access to all non-leaf a elements of a Cofree g a, where non-leaf is defined as x from (x :< xs) where null xs is False .

Because this doesn't give access to all values in the Cofree g , it cannot be used to change types.

shoots :: Traversable g => Traversal' (Cofree g a) a

N.B. On GHC < 7.9, this is slightly less flexible, as it has to use null (toList xs) instead.

leaves :: ( Applicative f, Traversable g) => (a -> f a) -> Cofree g a -> f ( Cofree g a) Source #

A Traversal' that gives access to all leaf a elements of a Cofree g a, where leaf is defined as x from (x :< xs) where null xs is True .

Because this doesn't give access to all values in the Cofree g , it cannot be used to change types.

shoots :: Traversable g => Traversal' (Cofree g a) a

N.B. On GHC < 7.9, this is slightly less flexible, as it has to use null (toList xs) instead.