Haskell中的索引函子究竟是什么?它的用法是什么? [英] What is exactly an indexed functor in Haskell and what are its usages?

查看:117
本文介绍了Haskell中的索引函子究竟是什么?它的用法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Haskell中学习函子时,我想出了 Functor。索引类函子。这个函数定义了一个名为 imap 的操作。我不明白它的定义,并且 imap 签名: imap ::(a - > b) - > f j k a - > f j k b 。我试图找到它的正式定义,只发现这一点: http://ncatlab.org/nlab/show/indexed+functor 。但它确实对我毫无帮助。那么有人可以用更简单的话来澄清这种函子吗?我应该在什么情况下使用它?感谢。

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.

推荐答案

索引函数是使用 spacesuitburritoesque 措辞,一个容器也包含映射。即一个值 f j k a 将包含某种态射 j - > k (不一定是函数,可以是更一般的箭头),也可以是 a 类型的值。

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 type a.

对于那些 a 值,容器显然是一个仿函数。事实上, IxFunctor 类本身很无聊 -

For those a values, the container is a functor in the obvious way. In fact the IxFunctor class on its own is pretty boring – an

instance IxFunctor f

基本上与

is basically the same as

instance Functor (f j k)

现在,有趣的是当你考虑更具体的函子类。这个monad实际上并不在 Indexed 模块中,但我认为它最清楚地表明了这一点:

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:

class IxPointed f => IxMonad f where
  ijoin :: m j k (m k l a) -> m j l a

并排比较:

compare this side-by-side:

(>>>) ::  (j->k) -> (k->l)   ->   j->l
ijoin :: m j  k   (m k  l a) -> m j  l a
join  :: m        (m      a) -> m      a

因此,我们在加入容器层的同时,撰写态射。

So what we do is, while joining the "container-layers", we compose the morphisms.

显而易见的例子是 IxState 。回想一下标准状态monad

The obvious example is IxState. Recall the standard state monad

newtype State s a = State { runState :: s -> (a, s) }

当它用作monad时,它简单地组成 s - >

This, when used as a monad, simply composes the s -> s aspect of the function:

  join (State f) = State $ \s -> let (State f', s') = f s in f' s'

首先通过 f ,然后通过 f'。那么,我们真的没有理由需要所有的州有相同的类型,对吧?毕竟,中间状态只是传递给下一个动作。这里是索引状态monad,

so you thread the state first through f, then through f'. 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,

newtype IxState i j a = IxState { runIxState :: i -> (a, j) }

它就是这样:

It does just that:

  ijoin (IxState f) = IxState $ \s -> let (IxState f', s') = f s in f' s'

这篇关于Haskell中的索引函子究竟是什么?它的用法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆