Having some idea of what the Comonad typeclass is in Haskell, I've heard about the Store comonad. But looking at Control.Comonad.Store.Lazy, I don't really get it. What does it mean? What is it for? I've heard that Store = CoState, the dual of the State Monad. What does that mean?
相关问题
- Understanding do notation for simple Reader monad:
- Making Custom Instances of PersistBackend
- Haskell: What is the differrence between `Num [a]
- applying a list to an entered function to check fo
- Haskell split a list into two by a pivot value
相关文章
- Is it possible to write pattern-matched functions
- Haskell underscore vs. explicit variable
- Top-level expression evaluation at compile time
- Stuck in the State Monad
- foldr vs foldr1 usage in Haskell
- List of checkboxes with digestive-functors
- How does this list comprehension over the inits of
- Replacing => in place of -> in function type signa
It's much easier if you look at the definition of StoreT itself.
You can think of it as a "place" in a larger structure. For instance, a lens is just
a -> Store b a
; you get the value of the b field, and a functionb -> a
to put a new value back into the larger context.Considering it in its simplified, non-transformer form:
i.e.
duplicate
changes thes -> a
into ans -> Store s a
that just returns the "updated" place after replacing the value, andextract
restores the original a by placing the value back into the larger structure.As far as its relation to State goes, you could look at it like this:
Given the following definition of store,
I like to think of a
Store
as a big warehouse filled with values of typea
. Each value of typea
is slotted into a position labeled by an index value of types
. Finally there is a forklift parked at positionpos
. The forklift can be used toextract
a value of typea
from the store by pulling the value out from where it is parked. You can useseek
to move the forklift to a new absolute position or useseeks
to move the forklift to a new relative location. To update all values of the store usefmap
. Finallyextend f
is similar tofmap
except instead off :: a -> a'
we havef :: Store s a -> a'
which lets the update function not only have access to the value being updated but also gives access to the value's position and access to the values of everything else in the store. In other words,extend
uses the value plus its surrounding context to perform the update.A more computery analogy would be to think of a
Store
as a big platter of a hard disk with values stored at various positions, plus a head parked at a particular position.