简体   繁体   English

Functor用于(a - > b) - >(fa - > fb),什么是(类别c)=> cab - > c(fa)(fb)?

[英]Functor is for (a -> b) -> (f a -> f b), what is for (Category c) => c a b -> c (f a) (f b)?

I would like to have a function for either mapping a pure function to a container or sequencing applicative/monadic action through it. 我想有一个函数,用于将纯函数映射到容器或通过它对应用/ monadic动作进行排序。 For pure mapping we have 对于纯映射,我们有

fmap :: Functor f => (a -> b) -> (f a -> f b)

For monadic sequencing we have (from Data.Taversable) 对于monadic测序,我们有(来自Data.Taversable)

mapM :: (Traversable f, Monad m) => (a -> m b) -> (f a -> m (f b))

Which is similar to 这是类似的

mapKleisli :: (Traversable f, Monad m) => Kleisli m a b -> Kleisli m (f a) (f b)
mapKleisli = Kleisli . mapM . runKleisli

We know both (->) and (Kleisli m) are categories (and arrows). 我们知道( - >)和(Kleisli m)都是类别(和箭头)。 So it's naturally to make a generalization: 所以自然要做出概括:

mapCategory :: (X f, Category c) => c a b -> c (f a) (f b)

Do you know such a class X with similar method? 你知道类似方法的X类吗? Maybe, somewhere on hackage? 也许,在某个地方有hackage? I tried to hoogle/hayoo but haven't found anything appropriate. 我试图hoogle / hayoo但没有找到任何合适的东西。

Update: 更新:

Now I know better what I need. 现在我知道我需要什么。 Both Kleisli arrows and (->) are instances of ArrowApply which is as powerful as Monad. Kleisli箭头和( - >)都是ArrowApply的实例,它和Monad一样强大。 I came up with this arrow-based version of Travesable: 我想出了这个基于箭头的Travesable版本:

{-# LANGUAGE TypeOperators #-}

import Prelude hiding (id, (.), mapM)
import Control.Arrow
import Control.Category

class Traversable f where
  traverse :: ArrowApply (~>) => f a -> (a ~> b) ~> f b

mapArrow :: (ArrowApply (~>), Traversable f) => a ~> b -> f a ~> f b
mapArrow a = arr (\x -> (traverse x, a)) >>> app

instance Traversable Maybe where
  traverse Nothing = arr (const Nothing)
  traverse (Just x) = arr (\a -> (a, x)) >>> app >>> arr Just

instance Traversable [] where
  traverse [] = arr (const [])
  traverse (x : xs) = undefined -- this is hard!

I could use just usual Applicative-based Traversable, with Identity for pure functions, but I'm not sure it is good. 我可以使用通常的基于Applicative的Traversable,具有纯函数的Identity,但我不确定它是好的。 Considering pure functions as special case of monadic actions is weird. 将纯函数视为monadic动作的特例是很奇怪的。 Interpreting both pure functions and monadic actions as instances of some action class (Category/Arrow/ArrowApply) looks more straightforward to me. 将纯函数和monadic动作解释为某个动作类(Category / Arrow / ArrowApply)的实例对我来说更直接。

Questions: would you like to finish instance for [] ? 问题:你想完成[]实例吗? Has my opinion about ArrowApply vs Monad any sense? 我对ArrowApply vs Monad有什么看法吗?

You're asking for "some class X", but it should be pretty clear that the most (or perhaps, only) correct name for this class would be "Functor". 你要的是“某些类X”,但应该很清楚,这个类的最多(或许是唯一的)正确名称是“Functor”。 What you want is simply a functor class defined for an arbitrary Category instance, rather than limited to (->) . 你想要的只是为任意Category实例定义的仿函数类,而不是限于(->)

Of course, your definition is still limited to (endo)functors from a category to a subcategory defined by the type constructor giving the instance. 当然,您的定义仍限于(endo)仿函数,从类别到由给定实例的类型构造函数定义的子类别。 If you generalize a bit further, there's no reason for the two categories to be the same, giving you a type class something like this one : 如果你进一步概括,那么两个类别没有理由相同,给你一个类似这样的类型

class (Category r, Category t) => Functor f r t | f r -> t, f t -> r where
    fmap :: r a b -> t (f a) (f b)

This is still awfully limited vs. the full concept of a functor in category theory, but oh well. 与类别理论中的仿函数的完整概念相比,这仍然非常有限,但是哦。

It's also interesting to observe that this still has a (->) type constructor in it--that's because, even though we're modeling the source and target categories with arbitrary instances, the whole thing (and in particular, the functor itself) still exists in some sense in Hask , ie, the category associated with (->) . 有趣的是,它仍然有一个(->)类型的构造函数 - 这是因为,即使我们使用任意实例建模源和目标类别,整个事物(特别是仿函数本身)在某种意义上仍然存在于Hask中 ,即与(->)相关联的类别。 The other half of the functor (the part mapping objects) is, roughly speaking, the (->) in the kind * -> * for the type constructor f . 对于类型构造函数f ,函数的另一半(部分映射对象)粗略地说是类型* -> *(->)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么类型f(fbc)(f(fab)(fac))不匹配(。)? - Why doesn't the type f (f b c) (f (f a b) (f a c)) match (.)? Haskell:是否有这样的运算符:(&lt;$$&gt;):: Functor f =&gt; fa-&gt;(a-&gt; b)-&gt; fb - Haskell: Is there an operator like this: (<$$>) :: Functor f => f a -> (a -> b) -> f b (&lt;*&gt;):: f(a-&gt; b)-&gt; fa-&gt; fb在Functor类中到底做了什么 - what does (<*>) :: f (a -> b) -> f a -> f b exactly do in the Functor class ((fa)b)与Haskell中的(fab)相同吗? - Is ((f a) b) the same as (f a b) in Haskell? bi fab的通用变体=(fa,fb) - Generic variant of bi f a b = (f a, f b) 为什么在 Applicative 上 pure 的类型是 a -> fa,而不是 (a -> b) -> f (a -> b)? - Why type of pure is a -> f a, not (a -> b) -> f (a -> b) on Applicative? 如果f是a-&gt; b类型的函数,则(fmap f)与(f。)相同吗? - Is (fmap f) the same as (f .) if f is a function of type a->b? 涉及函数组合的常见模式(\ a b - > f(g a)(g b)) - A common pattern involving composition of functions (\a b -> f (g a) (g b)) f, g, h :: Kleisli ((-&gt;) e) ab &lt;=&gt; f &gt;&gt;&gt; (g &amp;&amp;&amp; h) = (f &gt;&gt;&gt; g) &amp;&amp;&amp; (f &gt;&gt;&gt; h)? - f, g, h :: Kleisli ((->) e) a b <=> f >>> (g &&& h) = (f >>> g) &&& (f >>> h)? Haskell 中的“f (a -&gt; b)”类型签名是什么意思? - What does “f (a -> b)” type signature mean in Haskell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM