comonad-4.2.7.2: Comonads

Copyright | (C) 2008-2013 Edward Kmett
License | BSD-style (see the file LICENSE)
Maintainer | Edward Kmett <ekmett@gmail.com>
Stability | provisional
Portability | portable
Safe Haskell | Safe
Language | Haskell2010

Control.Comonad.Trans.Env

Contents

- The strict environment comonad
- The strict environment comonad transformer
- Combinators

Description

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
    0

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

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

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

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

Synopsis

- type Env e = EnvT e Identity
- env :: e -> a -> Env e a
- runEnv :: Env e a -> (e, a)
- data EnvT e w a = EnvT e (w a)
- runEnvT :: EnvT e w a -> (e, w a)
- lowerEnvT :: EnvT e w a -> w a
- ask :: EnvT e w a -> e
- asks :: (e -> f) -> EnvT e w a -> f
- local :: (e -> e') -> EnvT e w a -> EnvT e' w a

# The strict environment comonad

type Env e = EnvT e Identity Source

env :: e -> a -> Env e a Source

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

Constructors

EnvT e (w a) |

Instances

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

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

lowerEnvT :: EnvT e w a -> w a Source

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

# Combinators

ask :: EnvT e w a -> e Source

Retrieves the environment.

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

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

    asks = f . ask

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

Modifies the environment using the specified function.