What about arrows?

前端 未结 2 1625
时光说笑
时光说笑 2021-02-20 06:14

Reading through various tutorials about Haskell\'s various category-themed classes, we find things like Monoid, Functor, Monad and so on -

相关标签:
2条回答
  • 2021-02-20 06:28

    HXT, a library which is used for parsing XML, is a very good example for the usage of arrows (have a look how often the word Arrow occurs in the module names of this package!). You shall have a look on the great tutorial: http://adit.io/posts/2012-04-14-working_with_HTML_in_haskell.html

    But it is also good to have the arrow concept for functions. For example the following code

    ((+1) &&& (*2)) 3 -- result is (4,6)
    

    just works, because (->) is an instance of the arrow class (The operator &&& is defined in Control.Arrow).

    Thanks to the arrow syntax you have also a great tool to write complex computations in Haskell (it works as well for functions, monads and XML filters in HXT).

    0 讨论(0)
  • 2021-02-20 06:48

    I like to think of Arrows as composable directed acyclic graphs. For example, an arrow of type:

    SomeArrow (a, b, c) (d, e, f)
    

    ... you can think of as a graph that has three incoming edges of type a, b, and c and three outgoing edges of type d, e, and f.

    Using this interpretation, the category composition operations for Arrows are like horizontal concatenation for graphs, connecting their edges together:

    (.) :: SomeArrow b c -> SomeArrow a b -> Some Arrow a c
    

    ... where a, b, and c may be themselves tuples. Similarly, id is just the identity graph that forwards all incoming edges to outgoing edges:

    id :: SomeArrow a a
    

    The other key operation is (***) which is like vertical concatenation of graphs:

    (***) :: Arrow a b -> Arrow c d -> Arrow (a, c) (b, d)
    

    You can think of that as putting two graphs side-by-side, combining their input edges and output edges.

    So Arrow commonly arise when working with typed directed acyclic graphs. However, the reason you usually don't see them that often is because most people mentally associate graphs with untyped and high-performance data structures.

    0 讨论(0)
提交回复
热议问题