HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulefree-5.2Haskell2010

Control.Comonad.Cofree

Cofree comonads

  • 1 type
  • 1 class
  • 12 values
  • Packagefree-5.2
  • Exports14
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceCofree.hs
datadata Cofree (f :: Type -> Type) a
#

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

Instances35ComonadTrans, ComonadHoist, Generic1, ComonadEnv, ComonadStore, ComonadTraced, …
classclass (Functor f, Comonad w) => ComonadCofree (f :: Type -> Type) (w :: Type -> Type) | w -> f where
#

Allows you to peel a layer off a cofree comonad.

Methods

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

    Remove a layer.

Instances10ComonadCofree, …
valueunfold :: Functor f => (b -> (a, f b)) -> b -> Cofree f a
#

Unfold a cofree comonad from a seed.

Lenses into cofree comonads

6 declarations
valuetelescoped
  1. :: Functor f
  2. => [(Cofree g a -> f (Cofree g a)) -> g (Cofree g a) -> f (g (Cofree g a))]
  3. -> a -> f a
  4. -> Cofree g a
  5. -> f (Cofree g a)
#

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
valuetelescoped_
  1. :: Functor f
  2. => [(Cofree g a -> f (Cofree g a)) -> g (Cofree g a) -> f (g (Cofree g a))]
  3. -> Cofree g a -> f (Cofree g a)
  4. -> Cofree g a
  5. -> f (Cofree g a)
#

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)
valueshoots
  1. :: (Applicative f, Traversable g)
  2. => a -> f a
  3. -> Cofree g a
  4. -> f (Cofree g a)
#

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.

valueleaves
  1. :: (Applicative f, Traversable g)
  2. => a -> f a
  3. -> Cofree g a
  4. -> f (Cofree g a)
#

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.