Modulecomonad-5.0.9Haskell2010
Control.Comonad.Trans.Store
The store comonad holds a constant value along with a modifiable accessor function, which maps the stored value to the focus.
This module defines the strict store (aka state-in-context/costate) comonad transformer.
stored value = (1, 5), accessor = fst, resulting focus = 1:
:{ let storeTuple :: Store (Int, Int) Int storeTuple = store fst (1, 5):}
Add something to the focus:
:{ let addToFocus :: Int -> Store (Int, Int) Int -> Int addToFocus x wa = x + extract wa:}
:{ let added3 :: Store (Int, Int) Int added3 = extend (addToFocus 3) storeTuple:}
The focus of added3 is now 1 + 3 = 4. However, this action changed only
the accessor function and therefore the focus but not the stored value:
pos added3(1,5)
extract added34
The strict store (state-in-context/costate) comonad transformer is subject to the laws:
x = seek (pos x) x
y = pos (seek y x)
seek y x = seek y (seek z x)Thanks go to Russell O'Connor and Daniel Peebles for their help formulating and proving the laws for this comonad transformer.
- 2 types
- 9 values
- Packagecomonad-5.0.9
- Exports11
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceStore.hs
The Store comonad
3 declarationsCreate a Store using an accessor function and a stored value
The Store comonad transformer
2 declarationsConstructors
StoreT (w (s -> a)) s
Instances9ComonadEnv, ComonadStore, ComonadTraced, ComonadTrans, ComonadHoist, Functor, …
ComonadEnv e w => ComonadEnv e (StoreT t w)Defined in comonad-5.0.9 · Control.Comonad.Env.ClassComonad w => ComonadStore s (StoreT s w)Defined in comonad-5.0.9 · Control.Comonad.Store.ClassComonadTraced m w => ComonadTraced m (StoreT s w)Defined in comonad-5.0.9 · Control.Comonad.Traced.ClassComonadTrans (StoreT s)Defined in comonad-5.0.9 · Control.Comonad.Trans.StoreComonadHoist (StoreT s)Defined in comonad-5.0.9 · Control.Comonad.Trans.StoreFunctor w => Functor (StoreT s w)Defined in comonad-5.0.9 · Control.Comonad.Trans.Store(Applicative w, Monoid s) => Applicative (StoreT s w)Defined in comonad-5.0.9 · Control.Comonad.Trans.StoreComonad w => Comonad (StoreT s w)Defined in comonad-5.0.9 · Control.Comonad.Trans.Store(ComonadApply w, Semigroup s) => ComonadApply (StoreT s w)Defined in comonad-5.0.9 · Control.Comonad.Trans.Store
Operations
6 declarationsRead the stored value
pos $ store fst (1,5)(1,5)
Set the stored value
pos . seek (3,7) $ store fst (1,5)(3,7)
Seek satisfies the law
seek s = peek s . duplicateModify the stored value
pos . seeks swap $ store fst (1,5)(5,1)
Seeks satisfies the law
seeks f = peeks f . duplicatePeek at what the current focus would be for a different stored value
Peek satisfies the law
peek x . extend (peek y) = peek yPeek at what the current focus would be if the stored value was modified by some function
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 Nothingexperiment f $ store (+1) 2Just 5experiment f $ store (+1) (-2)Nothing