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,
Cofreeforms an infinite stream.IdentityCofree ((->) 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.
Instances35ComonadTrans, ComonadHoist, Generic1, ComonadEnv, ComonadStore, ComonadTraced, …
ComonadTrans CofreeDefined in free-5.2 · Control.Comonad.CofreeThis is not a true Comonad transformer, but this instance is convenient.
ComonadHoist CofreeDefined in free-5.2 · Control.Comonad.CofreeFunctor f => Generic1 (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeComonadEnv e w => ComonadEnv e (Cofree w)Defined in free-5.2 · Control.Comonad.CofreeComonadStore s w => ComonadStore s (Cofree w)Defined in free-5.2 · Control.Comonad.CofreeComonadTraced m w => ComonadTraced m (Cofree w)Defined in free-5.2 · Control.Comonad.CofreeFunctor f => ComonadCofree f (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeAlternative f => Monad (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeFunctor f => Functor (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeAlternative f => Applicative (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeFoldable f => Foldable (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeTraversable f => Traversable (Cofree f)Defined in free-5.2 · Control.Comonad.Cofree(Alternative f, MonadZip f) => MonadZip (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeFoldable1 f => Foldable1 (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeEq1 f => Eq1 (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeOrd1 f => Ord1 (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeRead1 f => Read1 (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeShow1 f => Show1 (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeDistributive f => Distributive (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeFunctor f => Comonad (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeComonadApply f => ComonadApply (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeApply f => Apply (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeFunctor f => Extend (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeTraversable1 f => Traversable1 (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeFoldableWithIndex i f => FoldableWithIndex [i] (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeFunctorWithIndex i f => FunctorWithIndex [i] (Cofree f)Defined in free-5.2 · Control.Comonad.CofreeTraversableWithIndex i f => TraversableWithIndex [i] (Cofree f)Defined in free-5.2 · Control.Comonad.Cofree(Eq1 f, Eq a) => Eq (Cofree f a)Defined in free-5.2 · Control.Comonad.Cofree(Typeable f, Data (f (Cofree f a)), Data a) => Data (Cofree f a)Defined in free-5.2 · Control.Comonad.Cofree(Ord1 f, Ord a) => Ord (Cofree f a)Defined in free-5.2 · Control.Comonad.Cofree(Read1 f, Read a) => Read (Cofree f a)Defined in free-5.2 · Control.Comonad.Cofree(Show1 f, Show a) => Show (Cofree f a)Defined in free-5.2 · Control.Comonad.CofreeGeneric (Cofree f a)Defined in free-5.2 · Control.Comonad.Cofreetype Rep (Cofree f a) = D1 ('MetaDataDefined in free-5.2 · Control.Comonad.Cofree"Cofree"
"Control.Comonad.Cofree"
"free-5.2-rp39PEumRD48nWoQbdqXD"
'False) (C1 ('MetaCons":<"
('InfixI 'RightAssociative5
) 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a) :*: S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (f (Cofree f a)))))type Rep1 (Cofree f) = D1 ('MetaDataDefined in free-5.2 · Control.Comonad.Cofree"Cofree"
"Control.Comonad.Cofree"
"free-5.2-rp39PEumRD48nWoQbdqXD"
'False) (C1 ('MetaCons":<"
('InfixI 'RightAssociative5
) 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1 :*: S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (f :.: Rec1 (Cofree f))))