comonad-4.0: Comonads

Safe HaskellTrustworthy

Control.Comonad.Trans.Store

Contents

Synopsis

The Store comonad

type Store s = StoreT s IdentitySource

store :: (s -> a) -> s -> Store s aSource

Create a Store using an accessor function and a stored value

runStore :: Store s a -> (s -> a, s)Source

The Store comonad transformer

data StoreT s w a Source

Constructors

StoreT (w (s -> a)) s 

runStoreT :: StoreT s w a -> (w (s -> a), s)Source

Operations

pos :: StoreT s w a -> sSource

Read the stored value

>>> pos $ store fst (1,5)
(1,5)

seek :: s -> StoreT s w a -> StoreT s w aSource

Set the stored value

 pos . seek (3,7) $ store fst (1,5)
 (3,7)

Seek satisfies the law

 seek s = peek s . duplicate

seeks :: (s -> s) -> StoreT s w a -> StoreT s w aSource

peek :: Comonad w => s -> StoreT s w a -> aSource

Peek at what the current focus would be for a different stored value

Peek satisfies the law

 peek x . extend (peek y) = peek y

peeks :: Comonad w => (s -> s) -> StoreT s w a -> aSource

Peek at what the current focus would be if the stored value was modified by some function

experiment :: (Comonad w, Functor f) => (s -> f s) -> StoreT s w a -> f aSource

Applies a functor-valued function to the stored value, and then uses the new accessor to read the resulting focus.

>>> let f x = if x > 0 then Just (x^2) else Nothing
>>> experiment f $ store (+1) 2
Just 5
>>> experiment f $ store (+1) (-2)
Nothing