Search code examples
haskellarrow-abstraction

What about arrows?


Reading through various tutorials about Haskell's various category-themed classes, we find things like Monoid, Functor, Monad and so on - all of which have dozens of instances. But for some reason, when we reach Arrow, there are only two instances: functions and monads. In both cases, using the Arrow instance is less powerful and more difficult than just using the underlying thing directly.

Does anybody have any interesting examples of arrows? I'm sure there must be some, but I've never come across any writing about them...


Solution

  • 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).