这东西和KleisliFunctor有什么相似? [英] What is this thing similar to KleisliFunctor?

查看:91
本文介绍了这东西和KleisliFunctor有什么相似?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我们定义 KleisliFunctor 的方法:

Here is how we can define KleisliFunctor:

class (Monad m, Functor f) => KleisliFunctor m f where
  kmap :: (a -> m b) -> f a -> f b
  kmap f = kjoin . fmap f

  kjoin :: f (m a) -> f a
  kjoin = kmap id

此类型类别

class (Functor f, Monad m) => Absorb f m where
  (>>~) :: f a -> (a -> m b) -> m b
  a >>~ f = ajoin $ fmap f a

  ajoin :: f (m a) -> m a
  ajoin a = a >>~ id

适合类别理论的地方吗?有什么法律?他们是

fit somewhere into category theory? What are the laws? Are they

a >>~ g . f     === fmap f a >>~ g
a >>~ (f >=> g) === a >>~ f >>= g

?

推荐答案

这是一个推测性答案.谨慎行事.

让我们首先考虑KleisliFunctor,重点是类似绑定的箭头映射:

Let's first consider KleisliFunctor, focusing on the bind-like arrow mapping:

class (Monad m, Functor f) => KleisliFunctor m f where
  kmap :: (a -> m b) -> f a -> f b

要使它实际上是从m的Kleisli类别到 Hask 的函子,kmap必须遵循相关的函子定律:

For this to actually be a functor from the Kleisli category of m to Hask, kmap has to follow the relevant functor laws:

-- Mapping the identity gives identity (in the other category).
kmap return = id
-- Mapping a composed arrow gives a composed arrow (in the other category).
kmap (g <=< f) = kmap g . kmap f

涉及两个Functor的事实使事情有些不寻常,但并非不合理-例如,法律确实适用mapMaybe,这是KleisliFunctor帖子暗示的第一个具体示例.

The fact that there are two Functors involved makes things a little unusual, but not unreasonable -- for instance, the laws do hold for mapMaybe, which is the first concrete example the KleisliFunctor post alludes to.

对于Absorb,为了清楚起见,我将翻转类似bind的方法:

As for Absorb, I will flip the bind-like method for the sake of clarity:

class (Functor f, Monad m) => Absorb f m where
  (~<<) :: (a -> m b) -> f a -> m b

如果我们正在寻找类似于KleisliFunctor的东西,那么马上就会出现一个问题,即哪个类别将具有f a -> m b类型的功能,如箭头所示.它肯定不能为 Hask ,因为其标识(类型为f a -> m a)不能为id.我们不仅要弄清身份,还要弄清楚组成.对于与Monad ...

If we are looking for something analogous to KleisliFunctor, a question that immediately arises is which category would have functions of type f a -> m b as arrows. It certainly cannot be Hask, as its identity (of type f a -> m a) cannot be id. We would have to figure out not only identity but also composition. For something that is not entirely unlike Monad...

idAbsorb :: f a -> m a
compAbsorb :: (f b -> m c) -> (f a -> m b) -> (f a -> m c)

...我现在唯一想到的似乎是将单子态变为idAbsorb,并在相反的方向(即从mf)使用第二个单子态.可以通过应用第一个功能,然后回到f并最终应用第二个功能来实现compAbsorb.我们需要解决这个问题,以便确定我的假设是否适当,这种方法是否可行以及是否可以为您的目的带来帮助.

... the only plausible thing I can think of right now is having a monad morphism as idAbsorb and using a second monad morphism in the opposite direction (that is, from m to f) so that compAbsorb can be implemented by applying the first function, then going back to f and finally applying the second function. We would need to work that out in order to see if my assumptions are appropriate, if this approach works, and if it leads to something useful for your purposes.

这篇关于这东西和KleisliFunctor有什么相似?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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