什么是“不同的方法"?Traversable 除了 Foldable 之外还有什么? [英] What would be the "distinct method" that Traversable has in addition to Foldable?

查看:20
本文介绍了什么是“不同的方法"?Traversable 除了 Foldable 之外还有什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可折叠Traversable,类似于 FunctorApplicativeMonad 的超类.

类似于Monad的情况,基本上可以将fmap实现为

liftM :: Monad m =>(a->b)->m a ->mb提升M f q = 返回.f =<<q

我们也可以将 foldMap 模拟为

foldLiftT :: (Traversable t, Monoid m) =>(a -> m) ->t a ->米foldLiftT f = fst .遍历 (f >>> x -> (x,x))-  或者: .序列A.fmap (f >>> x -> (x, x))

使用 Monoid m =>(,) m 单子.所以超类和方法的组合在这两种情况下都有一定的冗余.

在 monad 的情况下,可以说类型类的更好"定义是(我将跳过 applicative/monoidal)

class (Functor m) =>单子 m 哪里返回 :: a ->嘛加入 :: m (m a) ->嘛

至少那是范畴论中使用的.这个定义确实,不使用 Functor 超类,not 允许 liftM,所以它没有这种冗余.

Traversable 类是否可以进行类似的转换?

<小时>

澄清一下:我所追求的是重新定义,我们称之为,

class (Functor t, Foldable t) =>可遍历的 t where脱脂 :: ???

这样我们就可以将实际的 Traverse 方法设为顶级函数

sequenceA :: (Traversable t, Applicative f) =>t (f a) ->f (t a)

但它不可能通用

instance (Traversable t) =>可折叠foldMap = ... 略读 ...数据 T实例 Traversable T 其中脱脂 = ...

我不是在问,因为我需要这个特定的东西;这是一个概念问题,以便更好地理解 FoldableTraversable 之间的区别.再次很像 Monad vs Functor:而对于日常 Haskell,>>=join 方便得多编程(因为您通常需要 fmapjoin 的这种组合),后者更容易掌握 monad 的含义.

解决方案

Foldable is to Functor as Traversable is to Monad,即 FoldableFunctorMonadTraversable 的超类(取模所有的 applicative/monad建议噪音).

确实,这已经在代码中了

instance Foldable f =>可遍历的 f where...

所以,还不清楚还需要什么.Foldable 的特点是 toList :: Foldable f =>f a ->[a]Traversable 最终不仅取决于能够像 toList 那样将内容抽象为列表,还取决于能够提取形状

shape :: Functor f =>f a ->F ()形状 = fmap (const ())

然后重新组合它们

combine :: Traversable f =>f () ->[一]->也许(f a)combine f_ = evalStateT (traverse pop f_) wherepop :: StateT [a] 也许是pop = do x <- 得到案例 x 的[] = 空(a:as) =​​ 设置为 >>返回一个

这取决于遍历.

有关此属性的更多信息,请参阅 Russell O'Connor 的这篇博文.p>

Foldable is a superclass of Traversable, similarly to how Functor is a superclass of Applicative and Monad.

Similar to the case of Monad, where it is possible to basically implement fmap as

liftM :: Monad m => (a->b) -> m a -> m b
liftM f q = return . f =<< q

we could also emulate foldMap as

foldLiftT :: (Traversable t, Monoid m) => (a -> m) -> t a -> m
foldLiftT f = fst . traverse (f >>> x -> (x,x))
           -- or: . sequenceA . fmap (f >>> x -> (x, x))

using the Monoid m => (,) m monad. So the combination of superclass and methods bears in both cases a certain redundancy.

In case of monads, it can be argued that a "better" definition of the type class would be (I'll skip applicative / monoidal)

class (Functor m) => Monad m where
  return :: a -> m a
  join :: m (m a) -> m a

at least that's what's used in category theory. This definition does, without using the Functor superclass, not permit liftM, so it is without this redundancy.

Is a similar transformation possible for the Traversable class?


To clarify: what I'm after is a re-definition, let's call it,

class (Functor t, Foldable t) => Traversable t where
  skim :: ???

such that we could make the actual Traverse methods top-level functions

sequenceA :: (Traversable t, Applicative f) => t (f a) -> f (t a)

but it would not be possible to make generically

instance (Traversable t) => Foldable t where
  foldMap = ... skim ...

data T
instance Traversable T where
  skim = ...

I'm not asking because I need this for something particular; it's a conceptual question so as to better understand the difference between Foldable and Traversable. Again much like Monad vs Functor: while >>= is much more convenient than join for everyday Haskell programming (because you usually need precisely this combination of fmap and join), the latter makes it simpler to grasp what a monad is about.

解决方案

Foldable is to Functor as Traversable is to Monad, i.e. Foldable and Functor are superclasses of Monad and Traversable (modulo all the applicative/monad proposal noise).

Indeed, that's already in the code

instance Foldable f => Traversable f where
  ...

So, it's not clear what more there is to want. Foldable is characterized by toList :: Foldable f => f a -> [a] while Traversable depends ultimately on not only being able to abstract the content as a list like toList does, but also to be able to extract the shape

shape :: Functor f => f a -> f ()
shape = fmap (const ())

and then recombine them

combine :: Traversable f => f () -> [a] -> Maybe (f a)
combine f_ = evalStateT (traverse pop f_) where
  pop :: StateT [a] Maybe a
  pop = do x <- get
           case x of
             [] = empty
             (a:as) = set as >> return a

which depends on traverse.

For more information on this property see this blog post by Russell O'Connor.

这篇关于什么是“不同的方法"?Traversable 除了 Foldable 之外还有什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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