When studying functors in Haskell I came up with Functor.Indexed type of functor. This functor defines an operation called imap
. I didn't understood its definition and imap
signature: imap :: (a -> b) -> f j k a -> f j k b
. I tried to find it s formal definition and found only this: http://ncatlab.org/nlab/show/indexed+functor . But it really didn't help me at all. So can someone clarify in more simple words this kind of functor and in what cases should I use it? Thanks.
相关问题
- 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
- Relation between Function1 and Reader Monad
相关文章
- Is it possible to write pattern-matched functions
- Haskell underscore vs. explicit variable
- Is there something like the threading macro from C
- Top-level expression evaluation at compile time
- Stuck in the State Monad
- Learning F#: What books using other programming la
- Creating a list of functions using a loop in R
- foldr vs foldr1 usage in Haskell
An indexed functor is, to use spacesuitburritoesque wording, “a container that also contains a mapping”. I.e. a value
f j k a
will “contain” some sort of morphism(s)j -> k
(not necessarily functions, can be more general arrows) and also values of typea
.For those
a
values, the container is a functor in the obvious way. In fact theIxFunctor
class on its own is pretty boring – anis basically the same as
Now, where it gets interesting is when you consider the more specific functor classes. This monad one isn't actually in the
Indexed
module, but I think it makes the point best clear:compare this side-by-side:
So what we do is, while joining the “container-layers”, we compose the morphisms.
The obvious example is
IxState
. Recall the standard state monadThis, when used as a monad, simply composes the
s -> s
aspect of the function:so you thread the state first through
f
, then throughf'
. Well, there's really no reason we need all the states to have the same type, right? After all, the intermediate state is merely passed on to the next action. Here's the indexed state monad,It does just that: