Modules 15
Control.Comonad.Trans.Store
provisionalThe 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 added3
4The 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.
The Store comonad
store :: (s -> a) -> s -> Store s a
Create a Store using an accessor function and a stored value
runStore :: Store s a -> (s -> a, s)
The Store comonad transformer
data StoreT s w a
StoreT :: (w (s -> a)) -> s -> StoreT s w a
runStoreT :: StoreT s w a -> (w (s -> a), s)
Operations
pos :: StoreT s w a -> s
Read the stored value
>>> pos $ store fst (1,5)
(1,5)seek :: s -> StoreT s w a -> StoreT s w a
Set the stored value
>>> pos . seek (3,7) $ store fst (1,5)
(3,7)Seek satisfies the law
seek s = peek s . duplicateseeks :: (s -> s) -> StoreT s w a -> StoreT s w a
Modify the stored value
>>> pos . seeks swap $ store fst (1,5)
(5,1)Seeks satisfies the law
seeks f = peeks f . duplicatepeek :: Comonad w => s -> StoreT s w a -> a
Peek at what the current focus would be for a different stored value
Peek satisfies the law
peek x . extend (peek y) = peek ypeeks :: Comonad w => (s -> s) -> StoreT s w a -> a
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 a
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)
NothingInstances
6
instance Functor w => Functor (StoreT s w)instance (ComonadApply w, Semigroup s) => ComonadApply (StoreT s w)instance (Applicative w, Monoid s) => Applicative (StoreT s w)instance Comonad w => Comonad (StoreT s w)instance ComonadTrans (StoreT s)instance ComonadHoist (StoreT s)
- Maintainer
- Edward Kmett <ekmett@gmail.com>
- Portability
- portable
- Copyright
- (C) 2008-2013 Edward Kmett
- License
- BSD-style (see the file LICENSE)
Build record and source files
Community contribution. The service has not verified this documentation.
Target: Linux on x86-64. Some inferred types and resolved links are not available.
Generator: aihc-haddock-aa6debbd-web-3
Metadata snapshot: 654821c0cfecd2668d9ddddd0bf4f45d1d796802db984d918e0d3e66c14a0555
Open the dependency plan · Download the documentation model
- aihc-base-4.21.2.0 · Core library · Cabal file
- aihc-internal-9.1204.0 · Core library · Cabal file
- aihc-prim-0.13.0 · Core library · Cabal file
- aihc-rts-1.0.2 · Core library · Cabal file
- aihc-template-haskell-2.23.0.0 · Core library · Cabal file
- array-0.5.8.0 · Cabal revision 2 · Cabal file · Source archive
- comonad-5.0.10 · Cabal revision 0 · Cabal file · Source archive
- containers-0.8.1 · Cabal revision 0 · Cabal file · Source archive
- deepseq-1.5.2.0 · Cabal revision 0 · Cabal file · Source archive
- distributive-0.6.3 · Cabal revision 0 · Cabal file · Source archive
- indexed-traversable-0.1.5 · Cabal revision 0 · Cabal file · Source archive
- tagged-0.9 · Cabal revision 0 · Cabal file · Source archive
- transformers-0.6.3.0 · Cabal revision 2 · Cabal file · Source archive