Skip to search form Skip to main content You are currently offline. Some features of the site may not work correctly. DOI: Abstract Monads have become very popular for structuring functional programs since Wadler introduced their use in In particular, libraries of combinators are often based on a monadic type. Such libraries share in part a common interface, from which numerous benefits flow, such as the possibility to write generic code which works together with any library.
|Published (Last):||8 January 2018|
|PDF File Size:||16.63 Mb|
|ePub File Size:||2.75 Mb|
|Price:||Free* [*Free Regsitration Required]|
The most important thing to understand is that there are more things which are arrows than there are things which are monads. Conversely, monads are strictly more powerful than arrows the second paper above specifies precisely in which fashion. Lindley et al. Applicative functors have a wide variety of applications, particularly for things which are best thought of as operations on streams. One can in fact think of arrows as arising from generalizing the notion of a transformer on streams i.
In my experience, because monads blur the distinction between objects and morphisms i. So if something is not given the characteristics of a monad even though conceptually it forms one , then it is potentially open to greater inspection and optimization.
It is also potentially easier to serialize. Hence the use of applicatives and arrows in parsers and circuit modeling. The above attempted to be general and descriptive. Below are some of my opinionated rules of thumb. If you need to model something that looks like state, start with a monad. If you need to model something that looks like global control flow i.
If a requirement arises that conflicts with the power and generality of monads i. If you need to model streams, and transformations on streams, and particularly streams for which certain characteristics particularly unlimited views of the past and future should be opaque, then start with an applicative functor.
If you need stronger reasoning about properties of transformations on streams, then think about reaching for an arrow. Or, very crudely put, applicatives are for the actions of circuits, arrows are for the structures of circuits, and monads are for general-purpose computational effects.
There's of course much more to the story. For monads, see everywhere. And of course take note that just because something is a monad, that doesn't mean that applicative notation might not be clearer and more expressive. The short answer is that Arrows are more general than Monads and are also more cumbersome to use. Thus you should use Monads whenever you can, leaving the use of Arrows for the cases where Monads are not applicable.
These two articles are easy to read and provide the answer to your question. Even if some people do not understand all the details or the code in these two articles, they will certainly find lots of information and very helpful explanations about Monads and Arrows. When Monads were introduced, people thought that they were all-powerful. Indeed, Monads pack a lot of power.
But at some point, it was found that there are cases where Monads cannot be applied. These cases have to do with multiple inputs, especially when some of the inputs are static and some of the inputs are dynamic. So, John Hughes stepped up and introduced Arrows. Arrows are more general than Monads.
Arrows are a superset of Monads. They can do all that Monads do and more. But they are also more difficult to use. John Hughes recommends that you should use Monads whenever you can and that you should use Arrows when you cannot use Monads.
I agree with John Hughes. Of course, it all depends on the particular situation at hand. Let me explain. Let us suppose that you are learning Haskell. Then it would be a great task to do each program using a monadic approach and redo it using an arrow-based approach. When you learn, you should strive to explore all possibilities and implement all kinds of approaches.
This way, you obtain great insight and you are able to compare different solutions first-hand. Now let us suppose that you want to provide a library to the community.
Well, you owe it to the people that will read your code that you should use the approach that is easiest to understand and still gets the job done. You also owe it to the people that will use your code that your solution lacks unnecessary complexity. This way, your solution is more easily maintainable and less prone to errors and bugs. But what if you are in a borderline case? Let us suppose that you are not sure whether or not you will need the extra power of Arrows. Then what should you do?
Should you start with a monadic approach and later switch to an arrow-based one if the need arises? Or should you start with Arrows from the get-go, avoiding a costly switch halfway through the project? Again, my answer is to try the first approach: try to use Monads if you can. If you later find out that you cannot use Monads, you will have to endure a costly switch, where you will have to restart and redo the project in order to use Arrows.
This approach will certainly require more time and other resources from your part. But you will know that you did the correct thing, which was to try to provide the simplest, clearest, less complex solution possible. Avoiding unnecessary complexity is the most important thing. Believe it or not, this is the reason concepts such as function composition, Monads and Arrows from Category Theory were introduced to Computer Science. Learn more.
Monads vs. Arrows Ask Question. Asked 9 years, 9 months ago. Active 3 years, 1 month ago. Viewed 9k times. However, I'm still a bit confused about how to select which one to use in any given situation. When should I use monads and when should I use arrows? According to the Wikipedia link you posted, monads are a type of arrow. Active Oldest Votes.
Conal Thanks - very useful and great to hear your opinionated perspective! I downvoted because of information overflow. The answer generated more jargon than clear guidance, makig me even more lost and scared of arrows. The straight answer from Dimitrios is much more a clear guidance than this no offense! I will now highlight the main points from these articles that pertain to your question When Monads were introduced, people thought that they were all-powerful.
ErikR 49k 6 6 gold badges 62 62 silver badges bronze badges. Dimitrios Kalemis Dimitrios Kalemis 1 1 silver badge 3 3 bronze badges. The sentence seems to contradict the first two. I can see why you are confused. The problem is that I use the word "general" and the term "superset" rather loosely.
So, in order for me to be understood, I state: Functors can do less than Monads can do. Monads can do less than Arrows can do. As a result, Functors are the least powerful, more power comes with Monads and the most power comes with Arrows. And here, with the word "power", I mean "ability to do things, functionality". Thanks for the clarification Dimitrios. I agree with Matthias here.
Every monad is a functor, but not every Arrow is a Monad. You need ArrowApply to get a monad from an arrow. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.
Arrow (computer science)
Arrows: A General Interface to Computation
In computer science , arrows or bolts are a type class used in programming to describe computations in a pure and declarative fashion. First proposed by computer scientist John Hughes as a generalization of monads , arrows provide a referentially transparent way of expressing relationships between logical steps in a computation. As a result, they have found use in functional reactive programming , point-free programming , and parsers among other applications. While arrows were in use before being recognized as a distinct class, it wasn't until that John Hughes first published research focusing on them. Until then, monads had proven sufficient for most problems requiring the combination of program logic in pure code.