comonad-4.0: Comonads

MaintainerEdward Kmett <>
Safe HaskellTrustworthy




The environment comonad holds a value along with some retrievable context.

This module specifies the environment comonad transformer (aka coreader), which is left adjoint to the reader comonad.

The following sets up an experiment that retains its initial value in the background:

>>> let initial = env 0 0

Extract simply retrieves the value:

>>> extract initial

Play around with the value, in our case producing a negative value:

>>> let experiment = fmap (+ 10) initial
>>> extract experiment

Oh noes, something went wrong, 10 isn't very negative! Better restore the initial value using the default:

>>> let initialRestored = experiment =>> ask
>>> extract initialRestored


The strict environment comonad

type Env e = EnvT e IdentitySource

env :: e -> a -> Env e aSource

Create an Env using an environment and a value

runEnv :: Env e a -> (e, a)Source

The strict environment comonad transformer

data EnvT e w a Source


EnvT e (w a) 


Comonad w => ComonadEnv e (EnvT e w) 
ComonadStore s w => ComonadStore s (EnvT e w) 
ComonadTraced m w => ComonadTraced m (EnvT e w) 
ComonadTrans (EnvT e) 
ComonadHoist (EnvT e) 
Functor w => Functor (EnvT e w) 
(Typeable s, Typeable1 w) => Typeable1 (EnvT s w) 
Foldable w => Foldable (EnvT e w) 
Traversable w => Traversable (EnvT e w) 
(Semigroup e, ComonadApply w) => ComonadApply (EnvT e w) 
Comonad w => Comonad (EnvT e w) 
(Data e, Typeable1 w, Data (w a), Data a) => Data (EnvT e w a) 
(Typeable s, Typeable1 w, Typeable a) => Typeable (EnvT s w a) 

runEnvT :: EnvT e w a -> (e, w a)Source

lowerEnvT :: EnvT e w a -> w aSource

Gets rid of the environment. This differs from extract in that it will not continue extracting the value from the contained comonad.


ask :: EnvT e w a -> eSource

Retrieves the environment.

asks :: (e -> f) -> EnvT e w a -> fSource

Like ask, but modifies the resulting value with a function.

 asks = f . ask

local :: (e -> e') -> EnvT e w a -> EnvT e' w aSource

Modifies the environment using the specified function.