简体   繁体   English

箭头对函数的优势

[英]Advantage of Arrows over Functions

What is the advantage of arrows over regular functions in haskell. 箭头比haskell中的常规函数​​有什么优势。 What can they do the functions can't. 他们可以做什么功能不能。 Functions can map over structures using fmap. 函数可以使用fmap映射结构。

On more of a broad picture, arrows get you out of Hask and into other categories there are to explore. 从更广泛的角度来看 ,箭头可以让你走出Hask并进入其他类别。 The Kleisli category is probably the best-acquainted to Haskellers, followed by Cokleisli . Kleisli类别可能是最熟悉的Haskellers,其次是Cokleisli Those are the natural "extensions" of Hask : add an endofunctor around either the result or argument, then you get a category again if 这些是Hask的自然“扩展”:在结果或参数周围添加一个endofunctor,然后再次获得一个类别

  • Kleisli : the functor is a monad, so id ≅ return :: a -> ma KleisliKleisli函数是monad,所以Kleisli id ≅ return :: a -> ma

     (.) ≅ (<=<) :: (b->mc) -> (a->mb) -> a->mc 
  • CoKleisli : the functor is a comonad, so id ≅ coreturn :: ma -> a and CoKleisliCoKleisli函数是一个comonad,所以id ≅ coreturn :: ma -> a

     (.) :: (m b->c) -> (m a->b) -> m a->c 

(For that you don't need Arrow yet, only Category . But general categories aren't very interesting, you normally want monoidal or even cartesian closed categories, which is what Arrow is roughly aiming at.) (为此你不需要Arrow ,只有Category 。但是一般类别不是很有趣,你通常想要幺半群甚至是笛卡尔闭合类别,这正是Arrow大致瞄准的目标。)

But there are sure are lots of other categories . 但肯定有很多其他类别 Most don't have much to do with Hask and can't be expressed with the standard Arrow class, mainly because the objects have special properties that not every Haskell type fulfills. 大多数与Hask没有多大关系,也不能用标准的Arrow类表达,主要是因为对象具有不是每个Haskell类型都满足的特殊属性。 Actually, if you add the ability to constrain the object types, the possibilities immediately become much wider . 实际上,如果添加约束对象类型的能力, 则可能会立即变得更宽 But even if you stay with the standard classes, perhaps even simply in -> , the point-free composition-style that is natural with arrows often comes out very nice, concise, and opens up new ways to think about transformations. 但是,即使你继续使用标准类,也许只是简单地使用-> ,带箭头的自由组合风格通常会非常精致,简洁,并开辟了思考转换的新方法。

Functions are only an instance of arrows, it's like asking "Why use monads instead of just Maybe ". 函数只是箭头的一个实例,就像问“为什么使用monads而不仅仅是Maybe ”。

Anything you can do with arrows can of course be done with functions since the Arrow (->) instance can only talk about one small part of functions, namely what's in the Arrow type class. 你可以用箭头做任何事情当然可以用函数来完成,因为Arrow (->)实例只能讨论函数的一小部分,即Arrow类型中的内容。 However, arrows has more instances than just plain functions, so we can use the ssame functions to operate on more complex types. 但是,箭头具有的实例多于普通函数,因此我们可以使用ssame函数对更复杂的类型进行操作。

Arrows are nice since they can have a lot more structure than just a function, when traversing with just fmap , we have no way to accumulate effects, are more expressive than monads! 箭头很好,因为它们可以拥有比仅仅函数更多的结构,当只使用fmap遍历时,我们无法累积效果,比monad更具表现力! Consider the Kleisli arrow, 考虑Kleisli箭头,

newtype Kleisli m a b = Kleisli {runKleisli :: a -> m b}

This forms an arrow when m is a monad. m是monad时,这形成一个箭头。 So every Monad forms an arrow and thus we can build up monadic computations by seamlessly composing a -> mb 's and do all sorts of useful things like this. 因此,每个Monad形成一个箭头,因此我们可以通过无缝地组合a -> mb来构建monadic计算,并做各种有用的事情。 Some XML libraries use arrows to abstract over functions from an element to it's subelements and use this to traverse over the document. 一些XML库使用箭头来抽象从元素到其子元素的函数,并使用它来遍历文档。 Other parsers use arrows (their original purpose) though nowadays this seems to be falling out of favor for Applicative . 其他解析器使用箭头(它们的最初目的)虽然现在这似乎不再适用于Applicative

The point that you've hopefully noticed is that arrows are more generic, when we just talk about arrows, we avoid duplicating all the code that we would need to write to do something with our parsers, xml scrapers, and monadic functions! 你希望注意到的一点是箭头更通用,当我们只谈论箭头时,我们避免复制我们需要编写的所有代码来使用我们的解析器,xml scraper和monadic函数做一些事情!

It's just the same as opting for Monad over Maybe , we lose some power since we're no longer able to make specific statements, but we get more generic code in return. 这与选择Monad over Maybe ,我们失去了一些力量,因为我们不再能够做出具体的陈述,但我们得到了更多的通用代码作为回报。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM